github.com/Finschia/finschia-sdk@v0.48.1/simapp/sim_bench_test.go (about)

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