mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 08:15:19 +00:00
614d4e40fe
* Update cometbft, cosmos, ethermint, and ibc-go * Replace github.com/tendermint/tendermint by github.com/cometbft/cometbft * Replace github.com/tendermint/tm-db by github.com/cometbft/cometbft-db * Replace gogo/protobuf with cosmos/gogoproto & simapp replacement * Replace cosmos-sdk/simapp/helpers with cosmos-sdk/testutil/sims * Remove no longer used simulations * Replace ibchost with ibcexported See https://github.com/cosmos/ibc-go/blob/v7.2.2/docs/migrations/v6-to-v7.md#ibc-module-constants * Add new consensus params keeper * Add consensus keeper to blockers * Fix keeper and module issues in app.go * Add IsSendEnabledCoins and update SetParams interface changes * Fix protobuf build for cosmos 47 (#1800) * fix cp errors by using -f; fix lint by only linting our proto dir; and use proofs.proto directly from ics23 for ibc-go v7 * run proto-all; commit updated third party deps and swagger changes * regenerate proto files * use correct gocosmos build plugin for buf * re-gen all protobuf files to update paths for new gocosmos plugin * update protoc and buf to latest versions * fix staking keeper issues in app.go * update tally handler for gov changes * chain id fix and flag fixes * update deps for cometbft 47.7 upgrade * remove all module legacy queriers * update stakingKeeper to pointer * Replace ModuleCdc from govv1beta1 to govcodec * remove simulations * abci.LastCommitInfo → abci.CommitInfo * Remove unused code in keys.go * simapp.MakeTestEncodingConfig -> moduletestutil.MakeTestEncodingConfi * Fix chain id issues in tests * Fix remaining unit test issues * Update changelog for upgrade * Fix e2e tests using updated kvtool * Update protonet to v47 compatible genesis * Bump cometbft-db to v0.9.1-kava.1 * Update kvtool * Remove extra changelog * Fix merged rocksdb issues * go mod cleanup * Bump cometbft-db to v9 and go to 1.21 * Bump rocksdb version to v8.10.0 * Update kvtool to latest version * Update gin to v1.9.0 * Use ibctm.ModuleName in app_test * Fallback to genesis chain id instead of client toml * Remove all simulations * Fix cdp migrations issue with v47 * Update dependencies to correct tags --------- Co-authored-by: Nick DeLuca <nickdeluca08@gmail.com>
148 lines
4.3 KiB
Go
148 lines
4.3 KiB
Go
package keeper_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
sdkmath "cosmossdk.io/math"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
|
tmtime "github.com/cometbft/cometbft/types/time"
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
"github.com/kava-labs/kava/x/cdp/keeper"
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
)
|
|
|
|
// saving the result to a module level variable ensures the compiler doesn't optimize the test away
|
|
var coinsResult sdk.Coins
|
|
var coinResult sdk.Coin
|
|
|
|
// Note - the iteration benchmarks take a long time to stabilize, to get stable results use:
|
|
// go test -benchmem -bench ^(BenchmarkAccountIteration)$ -benchtime 60s -timeout 2h
|
|
// go test -benchmem -bench ^(BenchmarkCdpIteration)$ -benchtime 60s -timeout 2h
|
|
|
|
func BenchmarkAccountIteration(b *testing.B) {
|
|
benchmarks := []struct {
|
|
name string
|
|
numberAccounts int
|
|
coins bool
|
|
}{
|
|
{name: "10000 Accounts, No Coins", numberAccounts: 10000, coins: false},
|
|
{name: "100000 Accounts, No Coins", numberAccounts: 100000, coins: false},
|
|
{name: "1000000 Accounts, No Coins", numberAccounts: 1000000, coins: false},
|
|
{name: "10000 Accounts, With Coins", numberAccounts: 10000, coins: true},
|
|
{name: "100000 Accounts, With Coins", numberAccounts: 100000, coins: true},
|
|
{name: "1000000 Accounts, With Coins", numberAccounts: 1000000, coins: true},
|
|
}
|
|
coins := sdk.Coins{
|
|
sdk.NewCoin("xrp", sdkmath.NewInt(1000000000)),
|
|
sdk.NewCoin("usdx", sdkmath.NewInt(1000000000)),
|
|
}
|
|
|
|
for _, bm := range benchmarks {
|
|
b.Run(bm.name, func(b *testing.B) {
|
|
tApp := app.NewTestApp()
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
|
|
ak := tApp.GetAccountKeeper()
|
|
bk := tApp.GetBankKeeper()
|
|
|
|
tApp.InitializeFromGenesisStates()
|
|
for i := 0; i < bm.numberAccounts; i++ {
|
|
arr := []byte{byte((i & 0xFF0000) >> 16), byte((i & 0xFF00) >> 8), byte(i & 0xFF)}
|
|
addr := sdk.AccAddress(arr)
|
|
acc := ak.NewAccountWithAddress(ctx, addr)
|
|
if bm.coins {
|
|
if err := tApp.FundAccount(ctx, acc.GetAddress(), coins); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
ak.SetAccount(ctx, acc)
|
|
}
|
|
// reset timer ensures we don't count setup time
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
ak.IterateAccounts(ctx,
|
|
func(acc authtypes.AccountI) (stop bool) {
|
|
coins := bk.GetAllBalances(ctx, acc.GetAddress())
|
|
coinsResult = coins
|
|
return false
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func createCdps(n int) (app.TestApp, sdk.Context, keeper.Keeper) {
|
|
tApp := app.NewTestApp()
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
|
|
cdc := tApp.AppCodec()
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(n)
|
|
coins := cs(c("btc", 100000000))
|
|
authGS := app.NewFundedGenStateWithSameCoins(tApp.AppCodec(), coins, addrs)
|
|
tApp.InitializeFromGenesisStates(
|
|
authGS,
|
|
NewPricefeedGenStateMulti(cdc),
|
|
NewCDPGenStateMulti(cdc),
|
|
)
|
|
cdpKeeper := tApp.GetCDPKeeper()
|
|
for i := 0; i < n; i++ {
|
|
err := cdpKeeper.AddCdp(ctx, addrs[i], coins[0], c("usdx", 100000000), "btc-a")
|
|
if err != nil {
|
|
panic("failed to create cdp")
|
|
}
|
|
}
|
|
return tApp, ctx, cdpKeeper
|
|
}
|
|
|
|
func BenchmarkCdpIteration(b *testing.B) {
|
|
benchmarks := []struct {
|
|
name string
|
|
numberCdps int
|
|
}{
|
|
{"1000 Cdps", 1000},
|
|
{"10000 Cdps", 10000},
|
|
{"100000 Cdps", 100000},
|
|
}
|
|
for _, bm := range benchmarks {
|
|
b.Run(bm.name, func(b *testing.B) {
|
|
_, ctx, cdpKeeper := createCdps(bm.numberCdps)
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
cdpKeeper.IterateAllCdps(ctx, func(c types.CDP) (stop bool) {
|
|
coinResult = c.Principal
|
|
return false
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
var errResult error
|
|
|
|
func BenchmarkCdpCreation(b *testing.B) {
|
|
tApp := app.NewTestApp()
|
|
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
|
|
cdc := tApp.AppCodec()
|
|
|
|
_, addrs := app.GeneratePrivKeyAddressPairs(b.N)
|
|
coins := cs(c("btc", 100000000))
|
|
authGS := app.NewFundedGenStateWithSameCoins(tApp.AppCodec(), coins, addrs)
|
|
tApp.InitializeFromGenesisStates(
|
|
authGS,
|
|
NewPricefeedGenStateMulti(cdc),
|
|
NewCDPGenStateMulti(cdc),
|
|
)
|
|
cdpKeeper := tApp.GetCDPKeeper()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
err := cdpKeeper.AddCdp(ctx, addrs[i], coins[0], c("usdx", 100000000), "btc-a")
|
|
if err != nil {
|
|
b.Error("unexpected error")
|
|
}
|
|
errResult = err
|
|
}
|
|
}
|