github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/allegrosql/memdb_bench_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package ekv
    15  
    16  import (
    17  	"context"
    18  	"encoding/binary"
    19  	"math"
    20  	"math/rand"
    21  	"testing"
    22  )
    23  
    24  const (
    25  	keySize   = 16
    26  	valueSize = 128
    27  )
    28  
    29  func newMemDBForBench() *memdb {
    30  	EDB := newMemDB()
    31  	EDB.bufferSizeLimit = math.MaxUint64
    32  	EDB.entrySizeLimit = math.MaxUint64
    33  	return EDB
    34  }
    35  
    36  func BenchmarkLargeIndex(b *testing.B) {
    37  	buf := make([][valueSize]byte, 10000000)
    38  	for i := range buf {
    39  		binary.LittleEndian.PutUint32(buf[i][:], uint32(i))
    40  	}
    41  	EDB := newMemDBForBench()
    42  	b.ResetTimer()
    43  
    44  	for i := range buf {
    45  		EDB.Set(buf[i][:keySize], buf[i][:])
    46  	}
    47  }
    48  
    49  func BenchmarkPut(b *testing.B) {
    50  	buf := make([][valueSize]byte, b.N)
    51  	for i := range buf {
    52  		binary.BigEndian.PutUint32(buf[i][:], uint32(i))
    53  	}
    54  
    55  	p := newMemDBForBench()
    56  	b.ResetTimer()
    57  
    58  	for i := range buf {
    59  		p.Set(buf[i][:keySize], buf[i][:])
    60  	}
    61  }
    62  
    63  func BenchmarkPutRandom(b *testing.B) {
    64  	buf := make([][valueSize]byte, b.N)
    65  	for i := range buf {
    66  		binary.LittleEndian.PutUint32(buf[i][:], uint32(rand.Int()))
    67  	}
    68  
    69  	p := newMemDBForBench()
    70  	b.ResetTimer()
    71  
    72  	for i := range buf {
    73  		p.Set(buf[i][:keySize], buf[i][:])
    74  	}
    75  }
    76  
    77  func BenchmarkGet(b *testing.B) {
    78  	buf := make([][valueSize]byte, b.N)
    79  	for i := range buf {
    80  		binary.BigEndian.PutUint32(buf[i][:], uint32(i))
    81  	}
    82  
    83  	p := newMemDBForBench()
    84  	for i := range buf {
    85  		p.Set(buf[i][:keySize], buf[i][:])
    86  	}
    87  
    88  	b.ResetTimer()
    89  	for i := range buf {
    90  		p.Get(context.TODO(), buf[i][:keySize])
    91  	}
    92  }
    93  
    94  func BenchmarkGetRandom(b *testing.B) {
    95  	buf := make([][valueSize]byte, b.N)
    96  	for i := range buf {
    97  		binary.LittleEndian.PutUint32(buf[i][:], uint32(rand.Int()))
    98  	}
    99  
   100  	p := newMemDBForBench()
   101  	for i := range buf {
   102  		p.Set(buf[i][:keySize], buf[i][:])
   103  	}
   104  
   105  	b.ResetTimer()
   106  	for i := 0; i < b.N; i++ {
   107  		p.Get(context.TODO(), buf[i][:keySize])
   108  	}
   109  }
   110  
   111  var opCnt = 100000
   112  
   113  func BenchmarkMemDbBufferSequential(b *testing.B) {
   114  	data := make([][]byte, opCnt)
   115  	for i := 0; i < opCnt; i++ {
   116  		data[i] = encodeInt(i)
   117  	}
   118  	buffer := newMemDB()
   119  	benchmarkSetGet(b, buffer, data)
   120  	b.ReportAllocs()
   121  }
   122  
   123  func BenchmarkMemDbBufferRandom(b *testing.B) {
   124  	data := make([][]byte, opCnt)
   125  	for i := 0; i < opCnt; i++ {
   126  		data[i] = encodeInt(i)
   127  	}
   128  	shuffle(data)
   129  	buffer := newMemDB()
   130  	benchmarkSetGet(b, buffer, data)
   131  	b.ReportAllocs()
   132  }
   133  
   134  func BenchmarkMemDbIter(b *testing.B) {
   135  	buffer := newMemDB()
   136  	benchIterator(b, buffer)
   137  	b.ReportAllocs()
   138  }
   139  
   140  func BenchmarkMemDbCreation(b *testing.B) {
   141  	for i := 0; i < b.N; i++ {
   142  		newMemDB()
   143  	}
   144  	b.ReportAllocs()
   145  }
   146  
   147  func shuffle(slc [][]byte) {
   148  	N := len(slc)
   149  	for i := 0; i < N; i++ {
   150  		// choose index uniformly in [i, N-1]
   151  		r := i + rand.Intn(N-i)
   152  		slc[r], slc[i] = slc[i], slc[r]
   153  	}
   154  }
   155  func benchmarkSetGet(b *testing.B, buffer MemBuffer, data [][]byte) {
   156  	b.ResetTimer()
   157  	for i := 0; i < b.N; i++ {
   158  		for _, k := range data {
   159  			buffer.Set(k, k)
   160  		}
   161  		for _, k := range data {
   162  			buffer.Get(context.TODO(), k)
   163  		}
   164  	}
   165  }
   166  
   167  func benchIterator(b *testing.B, buffer MemBuffer) {
   168  	for k := 0; k < opCnt; k++ {
   169  		buffer.Set(encodeInt(k), encodeInt(k))
   170  	}
   171  	b.ResetTimer()
   172  	for i := 0; i < b.N; i++ {
   173  		iter, err := buffer.Iter(nil, nil)
   174  		if err != nil {
   175  			b.Error(err)
   176  		}
   177  		for iter.Valid() {
   178  			iter.Next()
   179  		}
   180  		iter.Close()
   181  	}
   182  }