forked from CosmWasm/wasmd
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR CosmWasm#215: Update simulation tests
- Loading branch information
1 parent
10493a8
commit ebfbd95
Showing
7 changed files
with
251 additions
and
351 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
"github.com/cosmos/cosmos-sdk/x/simulation" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
) | ||
|
||
// 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) { | ||
config, db, dir, logger, _, err := simapp.SetupSimulation("goleveldb-app-sim", "Simulation") | ||
if err != nil { | ||
b.Fatalf("simulation setup failed: %s", err.Error()) | ||
} | ||
|
||
defer func() { | ||
db.Close() | ||
err = os.RemoveAll(dir) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
}() | ||
|
||
app := NewGaiaApp(logger, db, nil, true, simapp.FlagPeriodValue, interBlockCacheOpt()) | ||
|
||
// run randomized simulation | ||
_, simParams, simErr := simulation.SimulateFromSeed( | ||
b, os.Stdout, app.BaseApp, simapp.AppStateFn(app.Codec(), app.SimulationManager()), | ||
simapp.SimulationOperations(app, app.Codec(), config), | ||
app.ModuleAccountAddrs(), config, | ||
) | ||
|
||
// export state and simParams before the simulation error is checked | ||
if err = simapp.CheckExportSimulation(app, config, simParams); err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
if simErr != nil { | ||
b.Fatal(simErr) | ||
} | ||
|
||
if config.Commit { | ||
simapp.PrintStats(db) | ||
} | ||
} | ||
|
||
func BenchmarkInvariants(b *testing.B) { | ||
config, db, dir, logger, _, err := simapp.SetupSimulation("leveldb-app-invariant-bench", "Simulation") | ||
if err != nil { | ||
b.Fatalf("simulation setup failed: %s", err.Error()) | ||
} | ||
|
||
config.AllInvariants = false | ||
|
||
defer func() { | ||
db.Close() | ||
err = os.RemoveAll(dir) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
}() | ||
|
||
app := NewGaiaApp(logger, db, nil, true, simapp.FlagPeriodValue, interBlockCacheOpt()) | ||
|
||
// run randomized simulation | ||
_, simParams, simErr := simulation.SimulateFromSeed( | ||
b, os.Stdout, app.BaseApp, simapp.AppStateFn(app.Codec(), app.SimulationManager()), | ||
simapp.SimulationOperations(app, app.Codec(), config), | ||
app.ModuleAccountAddrs(), config, | ||
) | ||
|
||
// export state and simParams before the simulation error is checked | ||
if err = simapp.CheckExportSimulation(app, config, simParams); err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
if simErr != nil { | ||
b.Fatal(simErr) | ||
} | ||
|
||
if config.Commit { | ||
simapp.PrintStats(db) | ||
} | ||
|
||
ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight() + 1}) | ||
|
||
// 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() { | ||
cr := cr | ||
b.Run(fmt.Sprintf("%s/%s", cr.ModuleName, cr.Route), func(b *testing.B) { | ||
if res, stop := cr.Invar(ctx); stop { | ||
b.Fatalf( | ||
"broken invariant at block %d of %d\n%s", | ||
ctx.BlockHeight()-1, config.NumBlocks, res, | ||
) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.