Skip to content

Commit

Permalink
cr fix - introduce startCluster flag
Browse files Browse the repository at this point in the history
  • Loading branch information
stana-miric committed May 16, 2023
1 parent 726fbea commit 6e075ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
12 changes: 7 additions & 5 deletions benchmark/root_child_send_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@ var (
RootJSONRPC = flag.String("rootJSONRPC", "", "JSONRPC address of the root node")
ChildJSONRPC = flag.String("childJSONRPC", "", "JSONRPC address of the child node")
PrivateKey = flag.String("privateKey", "", "private key that will be used to send tx")
StartCluster = flag.Bool("startCluster", true, "starts the cluster if sets to true")
)

// The rootChildSendTx function executes test cases that measure transaction execution on both the root and child chains
// To do this, it first calls RootChildSendTxSetUp to set up the testing environment,
// which may include starting the cluster, deploying contracts, and building the test cases.
// After building the test cases, rootChildSendTx returns them along with a cleanup function that should be called
// after the test cases have been executed. The test cases are executed by the TxTestCasesExecutor.
// The rootJSONRPC, childJSONRPC, and privateKey flags are used to configure the testing environment.
// If all of these flags are set, then the local cluster will not be started and the provided addresses
// The rootJSONRPC, childJSONRPC, privateKey and startCluster flags are used to configure the testing environment.
// If startCluster is false, then the local cluster will not be started and the provided addresses
// will be used as the endpoints to the root and child chains.
// If any of these flags is not set, the local cluster will be started automatically.
// If startCluster is set to true, the local cluster will be started automatically.
// If the private key is specified, it will be used as the transaction sender.
// Otherwise, the local cluster will generate a sender key.
// If the cluster is not run locally, then the sender must have enough funds for sending transactions.
// If startCluster is set to false, then the sender must have enough funds for sending transactions.
func rootChildSendTx(b *testing.B) {
b.Helper()
// set up environment, get test cases and clean up fn
Expand All @@ -58,7 +59,8 @@ func RootChildSendTxSetUp(b *testing.B) ([]TxTestCase, func()) {
rootNodeAddr := *RootJSONRPC
childNodeAddr := *ChildJSONRPC
privateKeyRaw := *PrivateKey
startCluster := rootNodeAddr == "" || childNodeAddr == "" || privateKeyRaw == ""
startCluster := *StartCluster
require.True(b, startCluster || rootNodeAddr != "" && childNodeAddr != "" && privateKeyRaw != "")

var sender ethgo.Key
// if the privateKey flag is set then recover the key, otherwise recover the key
Expand Down
26 changes: 23 additions & 3 deletions command/benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package benchmark

import (
"bytes"
"errors"
"fmt"
"testing"

Expand All @@ -14,12 +15,14 @@ const (
rootJSONRPCFlag = "rootJSONRPC"
childJSONRPCFlag = "childJSONRPC"
privateKeyFlag = "privateKey"
startClusterFlag = "startCluster"
)

type benchmarkParams struct {
rootJSONRPC string
childJSONRPC string
privateKey string
startCluster bool
}

type benchmarkResult struct {
Expand All @@ -36,6 +39,7 @@ func GetCommand() *cobra.Command {
Use: "benchmark-test",
Short: "Run benchmark tests",
Example: getCmdExample(),
PreRunE: preRunCommand,
Run: runCommand,
}

Expand All @@ -55,6 +59,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
benchmark.RootJSONRPC = &params.rootJSONRPC
benchmark.ChildJSONRPC = &params.childJSONRPC
benchmark.PrivateKey = &params.privateKey
benchmark.StartCluster = &params.startCluster

// set up environment, get test cases and clean up fn
testCases, cleanUpFn := benchmark.RootChildSendTxSetUp(&testing.B{})
Expand Down Expand Up @@ -94,10 +99,15 @@ func setFlags(cmd *cobra.Command) {
"",
"private key that will be used to send tx (it is expected that this address has enough funds)",
)
cmd.Flags().BoolVar(
&params.startCluster,
startClusterFlag,
false,
"startCluster tells if the local cluster should be started",
)

_ = cmd.MarkFlagRequired(rootJSONRPCFlag)
_ = cmd.MarkFlagRequired(childJSONRPCFlag)
_ = cmd.MarkFlagRequired(privateKeyFlag)
cmd.MarkFlagsMutuallyExclusive(startClusterFlag, rootJSONRPCFlag)
cmd.MarkFlagsMutuallyExclusive(startClusterFlag, childJSONRPCFlag)
}

func (br *benchmarkResult) GetOutput() string {
Expand All @@ -113,3 +123,13 @@ func getCmdExample() string {
"--childJSONRPC=\"http://127.0.0.1:12001\"", "--rootJSONRPC=\"http://127.0.0.1:8545\"",
"--privateKey=\"aa75e9a7d427efc732f8e4f1a5b7646adcc61fd5bae40f80d13c8419c9f43d6d\"")
}

func preRunCommand(_ *cobra.Command, _ []string) error {
if !params.startCluster {
if params.rootJSONRPC == "" || params.childJSONRPC == "" || params.privateKey == "" {
return errors.New("if startCluster is not set then rootJSONRPC, childJSONRPC and privateKey must be set")
}
}

return nil
}

0 comments on commit 6e075ab

Please sign in to comment.