-
Notifications
You must be signed in to change notification settings - Fork 211
/
main.go
106 lines (90 loc) · 3.09 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
// (c) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/ava-labs/subnet-evm/cmd/simulator/worker"
"github.com/spf13/cobra"
)
func init() {
cobra.EnablePrefixMatching = true
}
func main() {
rootCmd := newCommand()
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "simulator failed %v\n", err)
os.Exit(1)
}
}
var (
version = "v0.0.1"
versionFlag bool
timeout time.Duration
keysDir string
rpcEndpoints []string
concurrency int
baseFee uint64
priorityFee uint64
defaultLocalNetworkCChainEndpoints = []string{
"http://127.0.0.1:9650/ext/bc/C/rpc",
"http://127.0.0.1:9652/ext/bc/C/rpc",
"http://127.0.0.1:9654/ext/bc/C/rpc",
"http://127.0.0.1:9656/ext/bc/C/rpc",
"http://127.0.0.1:9658/ext/bc/C/rpc",
}
)
func newCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "simulator",
Short: "Load simulator for subnet-evm + C-chain",
SuggestFor: []string{"simulators"},
Run: runFunc,
}
cmd.PersistentFlags().BoolVarP(&versionFlag, "version", "v", false, "Print the version of the simulator and exit.")
cmd.PersistentFlags().DurationVarP(&timeout, "timeout", "t", time.Minute, "Duration to run simulator")
cmd.PersistentFlags().StringVarP(&keysDir, "keys", "k", ".simulator/keys", "Directory to find key files")
cmd.PersistentFlags().StringSliceVarP(&rpcEndpoints, "rpc-endpoints", "e", defaultLocalNetworkCChainEndpoints, `Specifies a comma separated list of RPC Endpoints to use for the load test. Ex. "http://127.0.0.1:9650/ext/bc/C/rpc,http://127.0.0.1:9652/ext/bc/C/rpc". Defaults to the default RPC Endpoints for the C-Chain on a 5 Node local network.`)
cmd.PersistentFlags().IntVarP(&concurrency, "concurrency", "c", 10, "Number of concurrent workers to use during the load test")
cmd.PersistentFlags().Uint64VarP(&baseFee, "base-fee", "f", 25, "Base fee to use for each transaction issued into the load test")
cmd.PersistentFlags().Uint64VarP(&priorityFee, "priority-fee", "p", 1, "Priority fee to use for each transaction issued into the load test")
return cmd
}
func runFunc(cmd *cobra.Command, args []string) {
if versionFlag {
fmt.Printf("%s\n", version)
return
}
// TODO: use geth logger
log.Printf("launching simulator with rpc endpoints %q timeout %v, concurrency %d, base fee %d, priority fee %d",
rpcEndpoints, timeout, concurrency, baseFee, priorityFee)
cfg := &worker.Config{
Endpoints: rpcEndpoints,
Concurrency: concurrency,
BaseFee: baseFee,
PriorityFee: priorityFee,
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
errc := make(chan error)
go func() {
errc <- worker.Run(ctx, cfg, keysDir)
}()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
select {
case sig := <-sigs:
log.Printf("received OS signal %v; canceling context", sig.String())
cancel()
case err := <-errc:
cancel()
if !errors.Is(err, context.DeadlineExceeded) {
log.Fatalf("worker.Run returned an error %v", err)
}
}
}