-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command line tool to dump chain state size
- Loading branch information
Showing
6 changed files
with
106 additions
and
20 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
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,5 @@ | ||
package operations | ||
|
||
var AllModules = []string{ | ||
"evm", "dex", "wasm", "aclaccesscontrol", "oracle", "epoch", "mint", "acc", "bank", "crisis", "feegrant", "staking", "distribution", "slashing", "gov", "params", "ibc", "upgrade", "evidence", "transfer", "tokenfactory", | ||
} |
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,84 @@ | ||
package operations | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sei-protocol/sei-db/common/logger" | ||
"github.com/sei-protocol/sei-db/sc/memiavl" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func StateSizeCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "state-size", | ||
Short: "Print analytical results for state size", | ||
Run: executeStateSize, | ||
} | ||
|
||
cmd.PersistentFlags().StringP("db-dir", "d", "", "Database Directory") | ||
cmd.PersistentFlags().Int64("height", 0, "Block Height") | ||
cmd.PersistentFlags().StringP("module", "m", "", "Module to export. Default to export all") | ||
return cmd | ||
} | ||
|
||
func executeStateSize(cmd *cobra.Command, _ []string) { | ||
module, _ := cmd.Flags().GetString("module") | ||
dbDir, _ := cmd.Flags().GetString("db-dir") | ||
height, _ := cmd.Flags().GetInt64("height") | ||
if dbDir == "" { | ||
panic("Must provide database dir") | ||
} | ||
|
||
opts := memiavl.Options{ | ||
Dir: dbDir, | ||
ZeroCopy: true, | ||
CreateIfMissing: false, | ||
} | ||
db, err := memiavl.OpenDB(logger.NewNopLogger(), height, opts) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer db.Close() | ||
err = PrintStateSize(module, db) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// PrintStateSize print the raw keys and values for given module at given height for memIAVL tree | ||
func PrintStateSize(module string, db *memiavl.DB) error { | ||
modules := []string{} | ||
if module == "" { | ||
modules = AllModules | ||
} else { | ||
modules = append(modules, module) | ||
} | ||
|
||
for _, moduleName := range modules { | ||
tree := db.TreeByName(moduleName) | ||
totalNumKeys := 0 | ||
totalKeySize := 0 | ||
totalValueSize := 0 | ||
totalSize := 0 | ||
if tree == nil { | ||
fmt.Printf("Tree does not exist for module %s \n", moduleName) | ||
} else { | ||
fmt.Printf("Calculating for module: %s \n", moduleName) | ||
sizeByPrefix := map[string]int{} | ||
tree.ScanPostOrder(func(node memiavl.Node) bool { | ||
if node.IsLeaf() { | ||
totalNumKeys++ | ||
totalKeySize += len(node.Key()) | ||
totalValueSize += len(node.Value()) | ||
totalSize += len(node.Key()) + len(node.Value()) | ||
prefix := string(node.Key()[:2]) | ||
sizeByPrefix[prefix] += len(node.Key()) + len(node.Value()) | ||
} | ||
return true | ||
}) | ||
fmt.Printf("Module %s total numKeys:%d, total keySize:%d, total valueSize:%d, totalSize: %d \n", moduleName, totalNumKeys, totalKeySize, totalValueSize, totalSize) | ||
fmt.Printf("Module %s prefix breakdown: %v \n", moduleName, sizeByPrefix) | ||
} | ||
} | ||
return nil | ||
} |