github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/testing/simapp/sim_bench_test.go (about) 1 package simapp 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 tmproto "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 9 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation" 11 ) 12 13 // Profile with: 14 // /usr/local/go/bin/go test -benchmem -run=^$ github.com/fibonacci-chain/fbc/libs/ibc-go/modules/testing/simapp -bench ^BenchmarkFullAppSimulation$ -Commit=true -cpuprofile cpu.out 15 func BenchmarkFullAppSimulation(b *testing.B) { 16 b.ReportAllocs() 17 config, db, dir, logger, _, err := SetupSimulation("goleveldb-app-sim", "Simulation") 18 if err != nil { 19 b.Fatalf("simulation setup failed: %s", err.Error()) 20 } 21 22 defer func() { 23 db.Close() 24 err = os.RemoveAll(dir) 25 if err != nil { 26 b.Fatal(err) 27 } 28 }() 29 30 app := NewSimApp(logger, db, nil, true, map[int64]bool{}, FlagPeriodValue, interBlockCacheOpt()) 31 32 // run randomized simulation 33 _, simParams, simErr := simulation.SimulateFromSeed( 34 b, 35 os.Stdout, 36 app.BaseApp, 37 AppStateFn(*app.AppCodec(), app.SimulationManager()), 38 // simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 39 SimulationOperations(app, *app.AppCodec().GetCdc(), config), 40 app.ModuleAccountAddrs(), 41 config, 42 ) 43 44 // export state and simParams before the simulation error is checked 45 if err = CheckExportSimulation(app, config, simParams); err != nil { 46 b.Fatal(err) 47 } 48 49 if simErr != nil { 50 b.Fatal(simErr) 51 } 52 53 if config.Commit { 54 PrintStats(db) 55 } 56 } 57 58 func BenchmarkInvariants(b *testing.B) { 59 b.ReportAllocs() 60 config, db, dir, logger, _, err := SetupSimulation("leveldb-app-invariant-bench", "Simulation") 61 if err != nil { 62 b.Fatalf("simulation setup failed: %s", err.Error()) 63 } 64 65 config.AllInvariants = false 66 67 defer func() { 68 db.Close() 69 err = os.RemoveAll(dir) 70 if err != nil { 71 b.Fatal(err) 72 } 73 }() 74 75 app := NewSimApp(logger, db, nil, true, map[int64]bool{}, FlagPeriodValue, interBlockCacheOpt()) 76 77 // run randomized simulation 78 _, simParams, simErr := simulation.SimulateFromSeed( 79 b, 80 os.Stdout, 81 app.BaseApp, 82 AppStateFn(*app.AppCodec(), app.SimulationManager()), 83 // simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1 84 SimulationOperations(app, *app.AppCodec().GetCdc(), config), 85 app.ModuleAccountAddrs(), 86 config, 87 ) 88 89 // export state and simParams before the simulation error is checked 90 if err = CheckExportSimulation(app, config, simParams); err != nil { 91 b.Fatal(err) 92 } 93 94 if simErr != nil { 95 b.Fatal(simErr) 96 } 97 98 if config.Commit { 99 PrintStats(db) 100 } 101 102 ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight() + 1}) 103 104 // 3. Benchmark each invariant separately 105 // 106 // NOTE: We use the crisis keeper as it has all the invariants registered with 107 // their respective metadata which makes it useful for testing/benchmarking. 108 for _, cr := range app.CrisisKeeper.Routes() { 109 cr := cr 110 b.Run(fmt.Sprintf("%s/%s", cr.ModuleName, cr.Route), func(b *testing.B) { 111 if res, stop := cr.Invar(ctx); stop { 112 b.Fatalf( 113 "broken invariant at block %d of %d\n%s", 114 ctx.BlockHeight()-1, config.NumBlocks, res, 115 ) 116 } 117 }) 118 } 119 }