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