2019-09-26 09:07:15 -07:00
|
|
|
package simapp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-01-16 13:46:51 -08:00
|
|
|
|
2020-08-24 03:55:42 -07:00
|
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
2020-01-16 13:46:51 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/simulation"
|
2019-09-26 09:07:15 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Profile with:
|
|
|
|
// /usr/local/go/bin/go test -benchmem -run=^$ github.com/cosmos/cosmos-sdk/simapp -bench ^BenchmarkFullAppSimulation$ -Commit=true -cpuprofile cpu.out
|
|
|
|
func BenchmarkFullAppSimulation(b *testing.B) {
|
2021-01-27 23:52:08 -08:00
|
|
|
b.ReportAllocs()
|
2021-03-03 03:54:14 -08:00
|
|
|
config, db, dir, logger, skip, err := SetupSimulation("goleveldb-app-sim", "Simulation")
|
2019-12-17 10:28:52 -08:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("simulation setup failed: %s", err.Error())
|
|
|
|
}
|
2019-09-26 09:07:15 -07:00
|
|
|
|
2021-03-03 03:54:14 -08:00
|
|
|
if skip {
|
|
|
|
b.Skip("skipping benchmark application simulation")
|
|
|
|
}
|
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
defer func() {
|
|
|
|
db.Close()
|
2019-12-17 10:28:52 -08:00
|
|
|
err = os.RemoveAll(dir)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2019-09-26 09:07:15 -07:00
|
|
|
}()
|
|
|
|
|
2020-11-02 11:10:14 -08:00
|
|
|
app := NewSimApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, FlagPeriodValue, MakeTestEncodingConfig(), EmptyAppOptions{}, interBlockCacheOpt())
|
2019-09-26 09:07:15 -07:00
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
// run randomized simulation
|
2019-09-26 09:07:15 -07:00
|
|
|
_, simParams, simErr := simulation.SimulateFromSeed(
|
2020-08-24 03:55:42 -07:00
|
|
|
b,
|
|
|
|
os.Stdout,
|
|
|
|
app.BaseApp,
|
|
|
|
AppStateFn(app.AppCodec(), app.SimulationManager()),
|
|
|
|
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
|
2020-08-13 06:20:02 -07:00
|
|
|
SimulationOperations(app, app.AppCodec(), config),
|
2020-08-24 03:55:42 -07:00
|
|
|
app.ModuleAccountAddrs(),
|
|
|
|
config,
|
2020-10-23 05:07:52 -07:00
|
|
|
app.AppCodec(),
|
2019-09-26 09:07:15 -07:00
|
|
|
)
|
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
// export state and simParams before the simulation error is checked
|
|
|
|
if err = CheckExportSimulation(app, config, simParams); err != nil {
|
|
|
|
b.Fatal(err)
|
2019-09-26 09:07:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if simErr != nil {
|
2019-12-17 10:28:52 -08:00
|
|
|
b.Fatal(simErr)
|
2019-09-26 09:07:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Commit {
|
2019-12-17 10:28:52 -08:00
|
|
|
PrintStats(db)
|
2019-09-26 09:07:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkInvariants(b *testing.B) {
|
2021-01-27 23:52:08 -08:00
|
|
|
b.ReportAllocs()
|
2021-03-03 03:54:14 -08:00
|
|
|
config, db, dir, logger, skip, err := SetupSimulation("leveldb-app-invariant-bench", "Simulation")
|
2019-12-17 10:28:52 -08:00
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("simulation setup failed: %s", err.Error())
|
|
|
|
}
|
2019-09-26 09:07:15 -07:00
|
|
|
|
2021-03-03 03:54:14 -08:00
|
|
|
if skip {
|
|
|
|
b.Skip("skipping benchmark application simulation")
|
|
|
|
}
|
|
|
|
|
2019-09-26 09:07:15 -07:00
|
|
|
config.AllInvariants = false
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
db.Close()
|
2019-12-17 10:28:52 -08:00
|
|
|
err = os.RemoveAll(dir)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2019-09-26 09:07:15 -07:00
|
|
|
}()
|
|
|
|
|
2020-11-02 11:10:14 -08:00
|
|
|
app := NewSimApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, FlagPeriodValue, MakeTestEncodingConfig(), EmptyAppOptions{}, interBlockCacheOpt())
|
2019-09-26 09:07:15 -07:00
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
// run randomized simulation
|
2019-09-26 09:07:15 -07:00
|
|
|
_, simParams, simErr := simulation.SimulateFromSeed(
|
2020-08-24 03:55:42 -07:00
|
|
|
b,
|
|
|
|
os.Stdout,
|
|
|
|
app.BaseApp,
|
|
|
|
AppStateFn(app.AppCodec(), app.SimulationManager()),
|
|
|
|
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
|
2020-08-13 06:20:02 -07:00
|
|
|
SimulationOperations(app, app.AppCodec(), config),
|
2020-08-24 03:55:42 -07:00
|
|
|
app.ModuleAccountAddrs(),
|
|
|
|
config,
|
2020-10-23 05:07:52 -07:00
|
|
|
app.AppCodec(),
|
2019-09-26 09:07:15 -07:00
|
|
|
)
|
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
// export state and simParams before the simulation error is checked
|
|
|
|
if err = CheckExportSimulation(app, config, simParams); err != nil {
|
|
|
|
b.Fatal(err)
|
2019-09-26 09:07:15 -07:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
if simErr != nil {
|
|
|
|
b.Fatal(simErr)
|
2019-09-26 09:07:15 -07:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:28:52 -08:00
|
|
|
if config.Commit {
|
|
|
|
PrintStats(db)
|
2019-09-26 09:07:15 -07:00
|
|
|
}
|
|
|
|
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight() + 1})
|
2019-09-26 09:07:15 -07:00
|
|
|
|
|
|
|
// 3. Benchmark each invariant separately
|
|
|
|
//
|
|
|
|
// NOTE: We use the crisis keeper as it has all the invariants registered with
|
|
|
|
// their respective metadata which makes it useful for testing/benchmarking.
|
|
|
|
for _, cr := range app.CrisisKeeper.Routes() {
|
2019-10-17 06:47:35 -07:00
|
|
|
cr := cr
|
2019-09-26 09:07:15 -07:00
|
|
|
b.Run(fmt.Sprintf("%s/%s", cr.ModuleName, cr.Route), func(b *testing.B) {
|
|
|
|
if res, stop := cr.Invar(ctx); stop {
|
2019-12-17 10:28:52 -08:00
|
|
|
b.Fatalf(
|
|
|
|
"broken invariant at block %d of %d\n%s",
|
|
|
|
ctx.BlockHeight()-1, config.NumBlocks, res,
|
|
|
|
)
|
2019-09-26 09:07:15 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|