Skip to content

Commit

Permalink
add support for multiple modes (#5)
Browse files Browse the repository at this point in the history
* add support for multiple modes

* update loadtest help

* compile -> must compile

* fix bug and add to avail

* update readme
  • Loading branch information
minhd-vu authored Oct 10, 2022
1 parent 25ecd83 commit c6f9dfe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
7 changes: 5 additions & 2 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
21 changes: 19 additions & 2 deletions cmd/loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"net/url"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit c6f9dfe

Please sign in to comment.