forked from artheranet/perftool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
254 lines (216 loc) · 5.39 KB
/
main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"fmt"
"github.com/artheranet/arthera-node/cmd/arthera/launcher/flags"
"github.com/artheranet/arthera-node/genesis/fake"
"github.com/artheranet/lachesis/inter/idx"
"os"
"os/signal"
"sort"
"strconv"
"syscall"
_ "github.com/artheranet/arthera-node/version"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"gopkg.in/urfave/cli.v1"
)
var (
// The app that holds all commands and flags.
app = flags.NewApp("the transactions generator CLI")
mainCfg *PerfConfig
)
// init the CLI app.
func init() {
app.Version = params.VersionWithCommit("", "")
app.Commands = []cli.Command{
{
Action: importAcc,
Name: "importacc",
Usage: "<address> <encrypted keystore dir> [password]",
Description: `Decripts and imports account by <address> from <encrypted keystore dir> into the keystore dir.`,
},
{
Action: makeFakenetAccs,
Name: "fakeaccs",
Usage: "[offset=1 [count=1000]]",
Description: `Generates <count> fakenet accounts starting from <offset> and saves them in the keystore dir.`,
},
{
Action: initAccsBalances,
Name: "initbalance",
Usage: "[amount=1]",
Description: `Pays <amount> from config.Payer to each other account in the keystore dir.`,
},
{
Action: generateCalls,
Name: "calls",
Description: `Deploys a fake Contract and generates a lot of calls behalf of accounts in the keystore dir (except config.Payer).`,
},
{
Action: generateTransfers,
Name: "transfers",
Description: `Generates a lot of transfer txs between accounts in the keystore dir (except config.Payer).`,
},
}
sort.Sort(cli.CommandsByName(app.Commands))
app.Flags = append(app.Flags,
KeyStoreDirFlag,
ConfigFileFlag,
TpsLimitFlag,
utils.MetricsEnabledFlag,
MetricsPrometheusEndpointFlag,
VerbosityFlag,
)
app.Before = before
}
func main() {
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func before(ctx *cli.Context) error {
glogger := log.NewGlogHandler(log.StreamHandler(os.Stderr, log.TerminalFormat(false)))
glogger.Verbosity(log.Lvl(ctx.GlobalInt(VerbosityFlag.Name)))
log.Root().SetHandler(glogger)
SetupPrometheus(ctx)
mainCfg = OpenConfig(ctx)
return nil
}
// importAcc action.
func importAcc(ctx *cli.Context) error {
if ctx.NArg() < 2 {
return fmt.Errorf("Address and Keystore dir args expected")
}
acc := accounts.Account{
Address: common.HexToAddress(ctx.Args().Get(0)),
}
other, err := openKeyStore(ctx.Args().Get(1))
if err != nil {
return err
}
var password string
if ctx.NArg() > 2 {
password = ctx.Args().Get(2)
}
my, err := makeKeyStore(ctx)
if err != nil {
return err
}
decrypted, err := other.Export(acc, password, "")
if err != nil {
return err
}
_, err = my.Import(decrypted, "", "")
if err != nil {
return err
}
return nil
}
// makeFakenetAccs action.
func makeFakenetAccs(ctx *cli.Context) error {
var accsOffset = 1
if ctx.NArg() > 0 {
i64, err := strconv.ParseUint(ctx.Args().Get(0), 10, 64)
if err != nil {
return err
}
accsOffset = int(i64)
}
var accsCount = 1000
if ctx.NArg() > 1 {
i64, err := strconv.ParseUint(ctx.Args().Get(1), 10, 64)
if err != nil {
return err
}
accsCount = int(i64)
}
keyStore, err := makeKeyStore(ctx)
if err != nil {
return err
}
for i := accsOffset; i < (accsOffset + accsCount); i++ {
key := fake.FakeKey(idx.ValidatorID(i))
_, err := keyStore.ImportECDSA(key, "")
if err != nil {
return err
}
}
return nil
}
// initAccsBalances action.
func initAccsBalances(ctx *cli.Context) error {
cfg := mainCfg
cfg.URLs = cfg.URLs[:1] // txs from single payer should be sent by single sender
keyStore, err := makeKeyStore(ctx)
if err != nil {
return err
}
var amount int64 = 1e18
if ctx.NArg() > 0 {
i64, err := strconv.ParseUint(ctx.Args().Get(0), 10, 64)
if err != nil {
return err
}
amount = int64(i64)
}
maxTps := getTpsLimit(ctx)
generator := NewBalancesGenerator(cfg, keyStore, amount, "InitBalance")
err = generate(generator, maxTps)
return err
}
// generateCalls action.
func generateCalls(ctx *cli.Context) error {
cfg := mainCfg
keyStore, err := makeKeyStore(ctx)
if err != nil {
return err
}
maxTps := getTpsLimit(ctx)
generator := NewCallsGenerator(cfg, keyStore, "CallsGen")
err = generate(generator, maxTps)
return err
}
// generateTransfers action.
func generateTransfers(ctx *cli.Context) error {
cfg := mainCfg
keyStore, err := makeKeyStore(ctx)
if err != nil {
return err
}
maxTps := getTpsLimit(ctx)
generator := NewTransfersGenerator(cfg, keyStore, "TransfersGen")
err = generate(generator, maxTps)
return err
}
// generate is the main generate loop.
func generate(generator Generator, maxTps float64) error {
cfg := mainCfg
txs := generator.Start()
defer generator.Stop()
nodes := NewNodes(cfg, txs)
go func() {
for tps := range nodes.TPS() {
tps += 10.0 * float64(nodes.Count())
if maxTps > 0.0 && tps > maxTps {
tps = maxTps
}
generator.SetTPS(tps)
}
}()
waitForFinish(nodes.Done)
return nil
}
func waitForFinish(done <-chan struct{}) {
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
select {
case <-term:
break
case <-done:
break
}
}