/
loadbot_command.go
190 lines (156 loc) · 3.66 KB
/
loadbot_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package loadbot
import (
"fmt"
"github.com/0xDaoChain/dao-chain/command"
"github.com/0xDaoChain/dao-chain/command/helper"
"github.com/spf13/cobra"
)
func GetCommand() *cobra.Command {
loadbotCmd := &cobra.Command{
Use: "loadbot",
Short: "Runs the loadbot to stress test the network",
PreRunE: runPreRun,
Run: runCommand,
}
helper.RegisterGRPCAddressFlag(loadbotCmd)
helper.RegisterJSONRPCFlag(loadbotCmd)
setFlags(loadbotCmd)
helper.SetRequiredFlags(loadbotCmd, params.getRequiredFlags())
return loadbotCmd
}
func setFlags(cmd *cobra.Command) {
cmd.Flags().Uint64Var(
¶ms.tps,
tpsFlag,
100,
"number of transactions to send per second.",
)
cmd.Flags().Uint64Var(
¶ms.chainID,
chainIDFlag,
100,
"the network chain ID.",
)
cmd.Flags().Uint64Var(
¶ms.count,
countFlag,
1000,
"the number of transactions to sent in total.",
)
cmd.Flags().StringVar(
¶ms.modeRaw,
modeFlag,
string(transfer),
"the mode of operation [transfer, deploy, erc20, erc721].",
)
cmd.Flags().StringVar(
¶ms.senderRaw,
senderFlag,
"",
"the account used to send the transactions",
)
cmd.Flags().StringVar(
¶ms.receiverRaw,
receiverFlag,
"",
"the account used to receive the transactions",
)
cmd.Flags().StringVar(
¶ms.valueRaw,
valueFlag,
"0x100",
"the value sent in each transaction in wei.",
)
// override default value for help output
cmd.Flag(valueFlag).DefValue = "100"
cmd.Flags().StringVar(
¶ms.gasPriceRaw,
gasPriceFlag,
"",
"the gas price that should be used for the transactions. If omitted, the average gas price is "+
"fetched from the network",
)
cmd.Flags().StringVar(
¶ms.gasLimitRaw,
gasLimitFlag,
"",
"the gas limit that should be used for the transactions. If omitted, the gas limit is "+
"estimated before starting the loadbot",
)
cmd.Flags().StringVar(
¶ms.contractPath,
contractFlag,
"",
"the path to the contract JSON artifact containing the bytecode. If omitted, a default "+
"contract is used",
)
cmd.Flags().BoolVar(
¶ms.detailed,
detailedFlag,
false,
"flag indicating if the error logs should be shown",
)
cmd.Flags().Uint64Var(
¶ms.maxConns,
maxConnsFlag,
1000000,
"sets the maximum no. of connections allowed per host.",
)
cmd.Flags().Uint64Var(
¶ms.maxWait,
maxWaitFlag,
0,
"sets the maximum wait time for transactions receipts in minutes.",
)
}
func runPreRun(cmd *cobra.Command, _ []string) error {
if err := params.validateFlags(); err != nil {
return err
}
// initialize raw parameters
if err := params.initRawParams(); err != nil {
return errInvalidValues
}
if _, err := helper.ParseGRPCAddress(
helper.GetGRPCAddress(cmd),
); err != nil {
return err
}
if _, err := helper.ParseJSONRPCAddress(
helper.GetJSONRPCAddress(cmd),
); err != nil {
return err
}
return nil
}
func runCommand(cmd *cobra.Command, _ []string) {
outputter := command.InitializeOutputter(cmd)
defer outputter.WriteOutput()
config := params.generateConfig(
helper.GetJSONRPCAddress(cmd),
helper.GetGRPCAddress(cmd),
)
runResults, err := runLoadbot(config, params.detailed)
if err != nil {
outputter.SetError(err)
return
}
outputter.SetCommandResult(runResults)
}
func runLoadbot(config *Configuration, detailed bool) (*LoadbotResult, error) {
loadbot := NewLoadbot(config)
if err := loadbot.Run(); err != nil {
return nil, fmt.Errorf(
"an error occurred while running the loadbot: %w",
err,
)
}
result := newLoadbotResult(
loadbot.GetMetrics(),
config.GeneratorMode,
)
if detailed {
result.initDetailedErrors(loadbot.GetGenerator())
}
return result, nil
}