-
Notifications
You must be signed in to change notification settings - Fork 113
/
benchmark.go
158 lines (146 loc) · 3.67 KB
/
benchmark.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
package run
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/iost-official/go-iost/ilog"
"github.com/iost-official/go-iost/itest"
"github.com/urfave/cli"
)
// BenchmarkCommand is the subcommand for benchmark.
var BenchmarkCommand = cli.Command{
Name: "benchmark",
ShortName: "bench",
Usage: "Run benchmark by given tps",
Flags: BenchmarkFlags,
Action: BenchmarkAction,
}
// BenchmarkFlags is the list of flags for benchmark.
var BenchmarkFlags = []cli.Flag{
cli.IntFlag{
Name: "tps",
Value: 100,
Usage: "The expected ratio of transactions per second",
},
cli.StringFlag{
Name: "type",
Value: "t",
Usage: "The type of transaction, should be one of ['t'/'transfer', 'c'/'contract']",
},
cli.IntFlag{
Name: "memo, m",
Value: 0,
Usage: "The size of a random memo message that would be contained in the transaction",
},
}
// The type of transaction.
const (
None int = iota
TransferTx
ContractTransferTx
GasTx
RAMTx
AccountTx
ExchangeTransferTx
)
// BenchmarkAction is the action of benchmark.
var BenchmarkAction = func(c *cli.Context) error {
it, err := itest.Load(c.GlobalString("keys"), c.GlobalString("config"))
if err != nil {
return err
}
txType := None
cid := ""
switch c.String("type") {
case "t", "transfer":
txType = TransferTx
case "c", "contract":
txType = ContractTransferTx
contract, err := itest.LoadContract(c.GlobalString("code"), c.GlobalString("abi"))
if err != nil {
return err
}
cid, err = it.SetContract(contract)
if err != nil {
return err
}
case "g", "gas":
txType = GasTx
case "r", "ram":
txType = RAMTx
case "a", "account":
txType = AccountTx
err := it.Pledge(it.GetDefaultAccount(), "10000000", true)
if err != nil {
return err
}
itest.InitAmount = "10"
itest.InitPledge = "20"
itest.InitRAM = "1000"
case "e", "exchange":
txType = ExchangeTransferTx
default:
return fmt.Errorf("wrong transaction type: %v", txType)
}
accountFile := c.GlobalString("account")
accounts, err := itest.LoadAccounts(accountFile)
if err != nil {
if err := AccountCaseAction(c); err != nil {
return err
}
if accounts, err = itest.LoadAccounts(accountFile); err != nil {
return err
}
}
tps := c.Int("tps")
memoSize := c.Int("memo")
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
startTime := time.Now()
ticker := time.NewTicker(time.Second)
counter := 0
total := 0
slotTotal := 0
slotStartTime := startTime
for {
num := 0
if txType == TransferTx {
num, err = it.TransferN(tps, accounts, memoSize, false)
} else if txType == ContractTransferTx {
num, err = it.ContractTransferN(cid, tps, accounts, memoSize, false)
} else if txType == GasTx {
num, err = it.PledgeGasN("rand", tps, accounts, false)
} else if txType == RAMTx {
num, err = it.BuyRAMN("rand", tps, accounts, false)
} else if txType == AccountTx {
var accs []*itest.Account
accs, err = it.CreateAccountN(tps, true, false)
num = len(accs)
} else if txType == ExchangeTransferTx {
num, err = it.ExchangeTransferN(tps, accounts, memoSize, false)
} else {
panic("invalid tx type, check --type flag")
}
if err != nil {
ilog.Infoln(err)
}
select {
case <-sig:
return itest.DumpAccounts(accounts, accountFile)
case <-ticker.C:
}
counter++
slotTotal += num
if counter == 10 {
total += slotTotal
currentTps := float64(slotTotal) / time.Since(slotStartTime).Seconds()
averageTps := float64(total) / time.Since(startTime).Seconds()
ilog.Warnf("Current tps %v, Average tps %v, Total tx %v", currentTps, averageTps, total)
counter = 0
slotTotal = 0
slotStartTime = time.Now()
}
}
}