github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/keeper/bench_test.go (about) 1 package keeper 2 3 import ( 4 "io/ioutil" 5 "testing" 6 7 dbm "github.com/fibonacci-chain/fbc/libs/tm-db" 8 "github.com/stretchr/testify/require" 9 10 "github.com/fibonacci-chain/fbc/libs/tendermint/crypto/secp256k1" 11 "github.com/fibonacci-chain/fbc/x/wasm/types" 12 ) 13 14 // BenchmarkVerification benchmarks secp256k1 verification which is 1000 gas based on cpu time. 15 // 16 // Just this function is copied from 17 // https://github.com/fibonacci-chain/fbc/libs/cosmos-sdk/blob/90e9370bd80d9a3d41f7203ddb71166865561569/crypto/keys/internal/benchmarking/bench.go#L48-L62 18 // And thus under the GO license (BSD style) 19 func BenchmarkGasNormalization(b *testing.B) { 20 priv := secp256k1.GenPrivKey() 21 pub := priv.PubKey() 22 23 // use a short message, so this time doesn't get dominated by hashing. 24 message := []byte("Hello, world!") 25 signature, err := priv.Sign(message) 26 if err != nil { 27 b.Fatal(err) 28 } 29 b.ResetTimer() 30 for i := 0; i < b.N; i++ { 31 pub.VerifyBytes(message, signature) 32 } 33 } 34 35 // By comparing the timing for queries on pinned vs unpinned, the difference gives us the overhead of 36 // instantiating an unpinned contract. That value can be used to determine a reasonable gas price 37 // for the InstantiationCost 38 func BenchmarkInstantiationOverhead(b *testing.B) { 39 specs := map[string]struct { 40 pinned bool 41 db func() dbm.DB 42 }{ 43 "unpinned, memory db": { 44 db: func() dbm.DB { return dbm.NewMemDB() }, 45 }, 46 "pinned, memory db": { 47 db: func() dbm.DB { return dbm.NewMemDB() }, 48 pinned: true, 49 }, 50 } 51 for name, spec := range specs { 52 b.Run(name, func(b *testing.B) { 53 wasmConfig := types.WasmConfig{MemoryCacheSize: 0} 54 ctx, keepers := createTestInput(b, false, SupportedFeatures, wasmConfig, spec.db()) 55 example := InstantiateHackatomExampleContract(b, ctx, keepers) 56 if spec.pinned { 57 require.NoError(b, keepers.ContractKeeper.PinCode(ctx, example.CodeID)) 58 } 59 b.ResetTimer() 60 for i := 0; i < b.N; i++ { 61 _, err := keepers.WasmKeeper.QuerySmart(ctx, example.Contract, []byte(`{"verifier":{}}`)) 62 require.NoError(b, err) 63 } 64 }) 65 } 66 } 67 68 // Calculate the time it takes to compile some wasm code the first time. 69 // This will help us adjust pricing for UploadCode 70 func BenchmarkCompilation(b *testing.B) { 71 specs := map[string]struct { 72 wasmFile string 73 }{ 74 "hackatom": { 75 wasmFile: "./testdata/hackatom.wasm", 76 }, 77 "burner": { 78 wasmFile: "./testdata/burner.wasm", 79 }, 80 "ibc_reflect": { 81 wasmFile: "./testdata/ibc_reflect.wasm", 82 }, 83 } 84 85 for name, spec := range specs { 86 b.Run(name, func(b *testing.B) { 87 wasmConfig := types.WasmConfig{MemoryCacheSize: 0} 88 db := dbm.NewMemDB() 89 ctx, keepers := createTestInput(b, false, SupportedFeatures, wasmConfig, db) 90 91 // print out code size for comparisons 92 code, err := ioutil.ReadFile(spec.wasmFile) 93 require.NoError(b, err) 94 b.Logf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b(size: %d) ", len(code)) 95 96 b.ResetTimer() 97 for i := 0; i < b.N; i++ { 98 _ = StoreExampleContract(b, ctx, keepers, spec.wasmFile) 99 } 100 }) 101 } 102 }