github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/istorage/bbolt/bench_test.go (about)

     1  /*
     2  * Copyright (c) 2022-present unTill Pro, Ltd.
     3  * @author Maxim Geraskin
     4   */
     5  
     6  package bbolt
     7  
     8  import (
     9  	"encoding/binary"
    10  	"math/rand"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/voedger/voedger/pkg/istorage"
    16  	istorageimpl "github.com/voedger/voedger/pkg/istorage/provider"
    17  	"github.com/voedger/voedger/pkg/istructs"
    18  )
    19  
    20  func Benchmark_Put_One_SameBucket_ST(b *testing.B) {
    21  	require := require.New(b)
    22  
    23  	params := prepareTestData()
    24  	defer cleanupTestData(params)
    25  
    26  	factory := Provide(params)
    27  	storageProvider := istorageimpl.Provide(factory)
    28  
    29  	appStorage, err := storageProvider.AppStorage(istructs.AppQName_test1_app1)
    30  	require.NoError(err)
    31  
    32  	var cCols = make([]byte, 8)
    33  
    34  	for i := 0; i < b.N; i++ {
    35  		binary.BigEndian.PutUint64(cCols, rand.Uint64())
    36  		err = appStorage.Put([]byte("persons"), cCols, []byte("Nikitin Nikolay Valeryevich"))
    37  		if err != nil {
    38  			panic(err)
    39  		}
    40  	}
    41  }
    42  
    43  func Benchmark_Put_50_DifferentBuckets_ST(b *testing.B) {
    44  
    45  	const NumOfBatchItems = 50
    46  
    47  	require := require.New(b)
    48  
    49  	params := prepareTestData()
    50  	defer cleanupTestData(params)
    51  
    52  	factory := Provide(params)
    53  	storageProvider := istorageimpl.Provide(factory)
    54  
    55  	appStorage, err := storageProvider.AppStorage(istructs.AppQName_test1_app1)
    56  	require.NoError(err)
    57  
    58  	var pKey = make([]byte, 8)
    59  	var cCols = make([]byte, 8)
    60  	var batchItems = make([]istorage.BatchItem, NumOfBatchItems)
    61  
    62  	for i := 0; i < b.N; i++ {
    63  		binary.BigEndian.PutUint64(pKey, rand.Uint64())
    64  
    65  		for j := 0; j < NumOfBatchItems; j++ {
    66  			binary.BigEndian.PutUint64(cCols, rand.Uint64())
    67  			batchItems[j] = istorage.BatchItem{PKey: pKey, CCols: cCols, Value: []byte("Nikitin Nikolay Valeryevich")}
    68  		}
    69  		err = appStorage.PutBatch(batchItems)
    70  		if err != nil {
    71  			panic(err)
    72  		}
    73  	}
    74  }
    75  
    76  func Benchmark_Put_One_DifferentBuckets_ST(b *testing.B) {
    77  	require := require.New(b)
    78  
    79  	params := prepareTestData()
    80  	defer cleanupTestData(params)
    81  
    82  	factory := Provide(params)
    83  	storageProvider := istorageimpl.Provide(factory)
    84  
    85  	appStorage, err := storageProvider.AppStorage(istructs.AppQName_test1_app1)
    86  	require.NoError(err)
    87  
    88  	var pKey = make([]byte, 8)
    89  	var cCols = make([]byte, 8)
    90  
    91  	for i := 0; i < b.N; i++ {
    92  		binary.BigEndian.PutUint64(pKey, rand.Uint64())
    93  		binary.BigEndian.PutUint64(cCols, rand.Uint64())
    94  		err = appStorage.Put(pKey, cCols, []byte("Nikitin Nikolay Valeryevich"))
    95  		if err != nil {
    96  			panic(err)
    97  		}
    98  	}
    99  }
   100  
   101  func Benchmark_Put_One_SameBucket_Parallel(b *testing.B) {
   102  
   103  	require := require.New(b)
   104  
   105  	params := prepareTestData()
   106  	defer cleanupTestData(params)
   107  
   108  	factory := Provide(params)
   109  	storageProvider := istorageimpl.Provide(factory)
   110  
   111  	appStorage, err := storageProvider.AppStorage(istructs.AppQName_test1_app1)
   112  	require.NoError(err)
   113  
   114  	b.RunParallel(func(pb *testing.PB) {
   115  		for pb.Next() {
   116  			err := appStorage.Put([]byte("persons"), []byte("NNV"), []byte("Nikitin Nikolay Valeryevich"))
   117  			if err != nil {
   118  				panic(err)
   119  			}
   120  		}
   121  	})
   122  }