github.com/klaytn/klaytn@v1.12.1/storage/database/leveldb_bench_compaction_table_test.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The klaytn library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package database
    18  
    19  import (
    20  	"strconv"
    21  	"testing"
    22  
    23  	"github.com/syndtr/goleveldb/leveldb/opt"
    24  )
    25  
    26  type LDBOptions struct {
    27  	Name          string
    28  	ValueLength   int
    29  	NumInsertions int
    30  	NumGets       int
    31  	Opts          *opt.Options
    32  }
    33  
    34  const (
    35  	valueLength   = 250              // value size
    36  	numInsertions = 20 * 1000 * 1000 // number of insertions
    37  	numGets       = 100000           // number of get operations
    38  )
    39  
    40  // getLDBOptionsForTableSize gets default options except for `CompactionTableSize` which equals to `tableSize` * `unit`.
    41  func getLDBOptionsForTableSize(tableSize, unit int) *opt.Options {
    42  	opts := getKlayLDBOptions()
    43  	opts.CompactionTableSize = tableSize * unit
    44  
    45  	return opts
    46  }
    47  
    48  // getLDBOptionsForTableSizeWithMultiplier gets default options except for `CompactionTableSize` and `CompactionTableSizeMultiplier`
    49  // for each set to `tableSize` * `unit` and 2 respectively.
    50  func getLDBOptionsForTableSizeWithMultiplier(tableSize, unit int) *opt.Options {
    51  	opts := getLDBOptionsForTableSize(tableSize, unit)
    52  	opts.CompactionTableSizeMultiplier = 2
    53  
    54  	return opts
    55  }
    56  
    57  // getTestLDBOptions returns `LDBOptions` object with predefined constants with `name` and `options`.
    58  func getTestLDBOptions(name string, options *opt.Options) LDBOptions {
    59  	return LDBOptions{
    60  		name,
    61  		valueLength,
    62  		numInsertions,
    63  		numGets,
    64  		options,
    65  	}
    66  }
    67  
    68  // getTestLDBOptionsList returns an array of `LDBOptions` from 64KB to 32MB table size as growing 2 times larger for performing benchmarks.
    69  func getTestLDBOptionsList(getOption func(int, int) *opt.Options) []LDBOptions {
    70  	var options []LDBOptions
    71  	for i := 64; i <= 32*1024; i *= 2 {
    72  		if i < 1024 {
    73  			name := strconv.Itoa(i) + "KB"
    74  			options = append(options, getTestLDBOptions(name, getOption(i, opt.KiB)))
    75  		} else {
    76  			name := strconv.Itoa(i/1024) + "MB"
    77  			options = append(options, getTestLDBOptions(name, getOption(i/1024, opt.MiB)))
    78  		}
    79  	}
    80  	return options
    81  }
    82  
    83  // BenchmarkOptionsLDBTableSizeGet inserts the random bytes to the generated database in the different size of compaction tables.
    84  // To run:
    85  // $ go test -bench=BenchmarkOptionsLDBTableSizeGet
    86  func BenchmarkOptionsLDBTableSizeGet(b *testing.B) {
    87  	list := getTestLDBOptionsList(getLDBOptionsForTableSize)
    88  	for _, bm := range list {
    89  		b.Run(bm.Name, func(b *testing.B) {
    90  			benchmarkKlayOptionsGet(b, bm.Opts, bm.ValueLength, bm.NumInsertions, bm.NumGets, randomRead)
    91  		})
    92  	}
    93  }
    94  
    95  // BenchmarkOptionsLDBTableSizeMultiplierGet inserts the random bytes to the generated database in the different size of compaction tables
    96  // and the compaction table size multiplier set to 2
    97  // To run:
    98  // $ go test -bench=BenchmarkOptionsLDBTableSizeMultiplierGet
    99  func BenchmarkOptionsLDBTableSizeMultiplierGet(b *testing.B) {
   100  	list := getTestLDBOptionsList(getLDBOptionsForTableSizeWithMultiplier)
   101  	for _, bm := range list {
   102  		b.Run(bm.Name, func(b *testing.B) {
   103  			benchmarkKlayOptionsGet(b, bm.Opts, bm.ValueLength, bm.NumInsertions, bm.NumGets, randomRead)
   104  		})
   105  	}
   106  }
   107  
   108  // BenchmarkOptionsLDBTableSizePut do the benchmark test by inserting the randomly generated bytes with the length `valueLength`
   109  // at the 32 bytes key randomly generated.
   110  // To run:
   111  // $ go test -bench=BenchmarkOptionsLDBTableSizePut
   112  func BenchmarkOptionsLDBTableSizePut(b *testing.B) {
   113  	list := getTestLDBOptionsList(getLDBOptionsForTableSize)
   114  	for _, bm := range list {
   115  		b.Run(bm.Name, func(b *testing.B) {
   116  			benchmarkKlayOptionsPut(b, bm.Opts, bm.ValueLength, bm.NumInsertions)
   117  		})
   118  	}
   119  }
   120  
   121  // BenchmarkOptionsLDBTableSizeMultiplierPut do the benchmark test by inserting the randomly generated bytes with the length `valueLength`
   122  // at the 32 bytes key randomly generated and the compaction table size multiplier set to 2
   123  // To run:
   124  // $ go test -bench=BenchmarkOptionsLDBTableSizeMultiplierPut
   125  func BenchmarkOptionsLDBTableSizeMultiplierPut(b *testing.B) {
   126  	list := getTestLDBOptionsList(getLDBOptionsForTableSizeWithMultiplier)
   127  	for _, bm := range list {
   128  		b.Run(bm.Name, func(b *testing.B) {
   129  			benchmarkKlayOptionsPut(b, bm.Opts, bm.ValueLength, bm.NumInsertions)
   130  		})
   131  	}
   132  }