From c6f9dfe5649ad5e623cc4b0a2fd9aba242f888bb Mon Sep 17 00:00:00 2001 From: Minh Vu Date: Mon, 10 Oct 2022 19:22:41 -0400 Subject: [PATCH] add support for multiple modes (#5) * add support for multiple modes * update loadtest help * compile -> must compile * fix bug and add to avail * update readme --- README.org | 7 +++++-- cmd/loadtest.go | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.org b/README.org index 0737b972..493f5023 100644 --- a/README.org +++ b/README.org @@ -213,12 +213,14 @@ Flags: -h, --help help for loadtest -i, --iterations uint If we're making contract calls, this controls how many times the contract will execute the instruction in a loop (default 100) --lt-address string A pre-deployed load test contract address - -m, --mode string t - sending transactions + -m, --mode string The testing mode to use. It can be multiple like: "tcdf" + t - sending transactions d - deploy contract c - call random contract functions f - call specific contract function s - store mode - l - long running mode (default "t") + l - long running mode + r - random modes (default "t") --pretty-logs Should we log in pretty format or JSON (default true) --private-key string The hex encoded private key that we'll use to sending transactions (default "42b6e34dc21598a807dc19d7784c71b2a7a01f6480dc6f58258f78e539f1a1fa") --rate-limit float An overall limit to the number of requests per second. Give a number less than zero to remove this limit all together (default 4) @@ -235,6 +237,7 @@ Flags: 400 INFO 500 Debug 600 Trace (default 200) + Global Flags: --config string config file (default is $HOME/.polygon-cli.yaml) #+end_example diff --git a/cmd/loadtest.go b/cmd/loadtest.go index 0aa61d51..d6e031d3 100644 --- a/cmd/loadtest.go +++ b/cmd/loadtest.go @@ -27,6 +27,7 @@ import ( "net/url" "os" "os/signal" + "regexp" "strconv" "strings" "sync" @@ -153,7 +154,8 @@ var loadtestCmd = &cobra.Command{ return fmt.Errorf("The scheme %s is not supported", url.Scheme) } inputLoadTestParams.URL = url - if !contains(validLoadTestModes, *inputLoadTestParams.Mode) { + r := regexp.MustCompile(fmt.Sprintf("^[%s]+$", strings.Join(validLoadTestModes, ""))) + if !r.MatchString(*inputLoadTestParams.Mode) { return fmt.Errorf("The mode %s is not recognized", *inputLoadTestParams.Mode) } return nil @@ -263,7 +265,14 @@ func init() { ltp.ToRandom = loadtestCmd.PersistentFlags().Bool("to-random", true, "When doing a transfer test, should we send to random addresses rather than DEADBEEFx5") ltp.HexSendAmount = loadtestCmd.PersistentFlags().String("send-amount", "0x38D7EA4C68000", "The amount of wei that we'll send every transaction") ltp.RateLimit = loadtestCmd.PersistentFlags().Float64("rate-limit", 4, "An overall limit to the number of requests per second. Give a number less than zero to remove this limit all together") - ltp.Mode = loadtestCmd.PersistentFlags().StringP("mode", "m", "t", "t - sending transactions\nd - deploy contract\nc - call random contract functions\nf - call specific contract function\ns - store mode\nl - long running mode") + ltp.Mode = loadtestCmd.PersistentFlags().StringP("mode", "m", "t", `The testing mode to use. It can be multiple like: "tcdf" +t - sending transactions +d - deploy contract +c - call random contract functions +f - call specific contract function +s - store mode +l - long running mode +r - random modes`) ltp.Function = loadtestCmd.PersistentFlags().Uint64P("function", "f", 1, "A specific function to be called if running with `--mode f` ") ltp.Iterations = loadtestCmd.PersistentFlags().Uint64P("iterations", "i", 100, "If we're making contract calls, this controls how many times the contract will execute the instruction in a loop") ltp.ByteCount = loadtestCmd.PersistentFlags().Uint64P("byte-count", "b", 1024, "If we're in store mode, this controls how many bytes we'll try to store in our contract") @@ -624,6 +633,10 @@ func mainLoop(ctx context.Context, c *ethclient.Client) error { currentNonceMutex.Unlock() localMode := mode + // if there are multiple modes, iterate through them, 'r' mode is supported here + if len(mode) > 1 { + localMode = string(mode[int(i+j)%(len(mode))]) + } // if we're doing random, we'll just pick one based on the current index if localMode == loadTestModeRandom { localMode = validLoadTestModes[int(i+j)%(len(validLoadTestModes)-1)] @@ -971,6 +984,10 @@ func availLoop(ctx context.Context, c *gsrpc.SubstrateAPI) error { currentNonceMutex.Unlock() localMode := mode + // if there are multiple modes, iterate through them, 'r' mode is supported here + if len(mode) > 1 { + localMode = string(mode[int(i+j)%(len(mode))]) + } // if we're doing random, we'll just pick one based on the current index if localMode == loadTestModeRandom { localMode = validLoadTestModes[int(i+j)%(len(validLoadTestModes)-1)]