github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/integration/bench_db_flush_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/unicornultrafoundation/go-helios/consensus"
     9  	"github.com/unicornultrafoundation/go-helios/hash"
    10  	"github.com/unicornultrafoundation/go-helios/native/idx"
    11  	"github.com/unicornultrafoundation/go-helios/utils/cachescale"
    12  	"github.com/unicornultrafoundation/go-u2u/common"
    13  
    14  	"github.com/unicornultrafoundation/go-u2u/gossip"
    15  	"github.com/unicornultrafoundation/go-u2u/integration/makefakegenesis"
    16  	"github.com/unicornultrafoundation/go-u2u/native"
    17  	"github.com/unicornultrafoundation/go-u2u/utils"
    18  	"github.com/unicornultrafoundation/go-u2u/vecmt"
    19  )
    20  
    21  func BenchmarkFlushDBs(b *testing.B) {
    22  	dir := tmpDir("flush_bench")
    23  	defer os.RemoveAll(dir)
    24  	genStore := makefakegenesis.FakeGenesisStore(1, utils.ToU2U(1), utils.ToU2U(1))
    25  	g := genStore.Genesis()
    26  	_, _, store, s2, _, closeDBs := MakeEngine(dir, &g, Configs{
    27  		U2U:            gossip.DefaultConfig(cachescale.Identity),
    28  		U2UStore:       gossip.DefaultStoreConfig(cachescale.Identity),
    29  		Hashgraph:      consensus.DefaultConfig(),
    30  		HashgraphStore: consensus.DefaultStoreConfig(cachescale.Identity),
    31  		VectorClock:    vecmt.DefaultConfig(cachescale.Identity),
    32  		DBs:            DefaultDBsConfig(cachescale.Identity.U64, 512),
    33  	})
    34  	defer closeDBs()
    35  	defer store.Close()
    36  	defer s2.Close()
    37  	b.ResetTimer()
    38  	for i := 0; i < b.N; i++ {
    39  		b.StopTimer()
    40  		n := idx.Block(0)
    41  		randUint32s := func() []uint32 {
    42  			arr := make([]uint32, 128)
    43  			for i := 0; i < len(arr); i++ {
    44  				arr[i] = uint32(i) ^ (uint32(n) << 16) ^ 0xd0ad884e
    45  			}
    46  			return []uint32{uint32(n), uint32(n) + 1, uint32(n) + 2}
    47  		}
    48  		for !store.IsCommitNeeded() {
    49  			store.SetBlock(n, &native.Block{
    50  				Time:        native.Timestamp(n << 32),
    51  				Atropos:     hash.Event{},
    52  				Events:      hash.Events{},
    53  				Txs:         []common.Hash{},
    54  				InternalTxs: []common.Hash{},
    55  				SkippedTxs:  randUint32s(),
    56  				GasUsed:     uint64(n) << 24,
    57  				Root:        hash.Hash{},
    58  			})
    59  			n++
    60  		}
    61  		b.StartTimer()
    62  		err := store.Commit()
    63  		if err != nil {
    64  			b.Fatal(err)
    65  		}
    66  	}
    67  }
    68  
    69  func tmpDir(name string) string {
    70  	dir, err := ioutil.TempDir("", name)
    71  	if err != nil {
    72  		panic(err)
    73  	}
    74  	return dir
    75  }