/
readApiCallTC.go
182 lines (149 loc) · 4.64 KB
/
readApiCallTC.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
package readApiCallTC
import (
"context"
"encoding/json"
"errors"
"log"
"math/big"
"math/rand"
"strings"
"sync"
"time"
"github.com/klaytn/klaytn-load-tester/klayslave/account"
"github.com/klaytn/klaytn-load-tester/klayslave/clipool"
"github.com/klaytn/klaytn/client"
"github.com/klaytn/klaytn/networks/rpc"
"github.com/myzhan/boomer"
"github.com/tidwall/gjson"
)
var (
gasPrice *big.Int
endPoint string
cliPool clipool.ClientPool
mutex sync.Mutex
initialized = false
nAcc int
accGrp []*account.Account
latestBlockNumber *big.Int
count uint64
)
func Init(accs []*account.Account, ep string, gp *big.Int) {
mutex.Lock()
defer mutex.Unlock()
if initialized {
return
}
initialized = true
latestBlockNumber = big.NewInt(0)
count = 0
gasPrice = gp
endPoint = ep
cliCreate := func() interface{} {
c, err := rpc.Dial(endPoint)
if err != nil {
log.Fatalf("Failed to connect RPC: %v", err)
}
return c
}
cliPool.Init(20, 300, cliCreate)
for _, acc := range accs {
accGrp = append(accGrp, acc)
}
nAcc = len(accGrp)
}
func sendBoomerEvent(tcName string, logString string, elapsed int64, cli *rpc.Client, err error) {
if err == nil {
boomer.Events.Publish("request_success", "http", tcName+" to "+endPoint, elapsed, int64(10))
cliPool.Free(cli)
} else {
log.Printf("[TC] %s: %s, err=%v\n", tcName, logString, err)
boomer.Events.Publish("request_failure", "http", tcName+" to "+endPoint, elapsed, err.Error())
cli.Close()
}
}
func getRandomBlockNumber(cli *client.Client, ctx context.Context) *big.Int {
mutex.Lock()
defer mutex.Unlock()
count %= 10000000
if count%10000 == 0 {
bn, err := cli.BlockNumber(ctx)
if err != nil {
log.Printf("Failed to update the current block number. err=%s\n", err)
} else {
log.Printf("Update the current block number. blockNumber=0x%s\n", bn.Text(16))
latestBlockNumber.Set(bn)
}
}
count += 1
return big.NewInt(0).Rand(rand.New(rand.NewSource(time.Now().UnixNano())), latestBlockNumber)
}
func GasPrice() {
ctx := context.Background()
rpcCli := cliPool.Alloc().(*rpc.Client)
cli := client.NewClient(rpcCli)
start := boomer.Now()
gp, err := cli.SuggestGasPrice(ctx)
elapsed := boomer.Now() - start
if err == nil && gp.Cmp(gasPrice) != 0 {
err = errors.New("wrong gas price: " + gp.String() + ", answer: " + gasPrice.String())
}
sendBoomerEvent("readGasPrice", "Failed to call klay_gasPrice", elapsed, rpcCli, err)
}
func BlockNumber() {
ctx := context.Background()
rpcCli := cliPool.Alloc().(*rpc.Client)
cli := client.NewClient(rpcCli)
start := boomer.Now()
bn, err := cli.BlockNumber(ctx)
if err == nil && bn.Cmp(big.NewInt(0)) != 1 {
err = errors.New("wrong block number: 0x" + bn.Text(16) + ", answer: smaller than 0")
}
elapsed := boomer.Now() - start
sendBoomerEvent("readBlockNumber", "Failed to call klay_blockNumber", elapsed, rpcCli, err)
}
func GetBlockByNumber() {
ctx := context.Background()
rpcCli := cliPool.Alloc().(*rpc.Client)
cli := client.NewClient(rpcCli)
ansBN := getRandomBlockNumber(cli, ctx)
start := boomer.Now()
block, err := cli.BlockByNumber(ctx, ansBN) //read the random block
if err == nil && block.Header().Number.Cmp(ansBN) != 0 {
err = errors.New("wrong block: 0x" + block.Header().Number.Text(16) + ", answer: 0x" + ansBN.Text(16))
}
elapsed := boomer.Now() - start
sendBoomerEvent("readGetBlockByNumber", "Failed to call klay_getBlockByNumber", elapsed, rpcCli, err)
}
func GetAccount() {
ctx := context.Background()
rpcCli := cliPool.Alloc().(*rpc.Client)
var j json.RawMessage
fromAccount := accGrp[rand.Int()%nAcc]
start := boomer.Now()
err := rpcCli.CallContext(ctx, &j, "klay_getAccount", fromAccount.GetAddress(), "latest")
if err == nil {
ret := gjson.Get(string(j), "accType").String()
if ret != "1" {
err = errors.New("wrong account type: " + ret + ", answer: 1")
}
}
elapsed := boomer.Now() - start
sendBoomerEvent("readGetAccount", "Failed to call klay_getAccount", elapsed, rpcCli, err)
}
func GetBlockWithConsensusInfoByNumber() {
ctx := context.Background()
rpcCli := cliPool.Alloc().(*rpc.Client)
ansBN := getRandomBlockNumber(client.NewClient(rpcCli), ctx)
start := boomer.Now()
var j json.RawMessage
err := rpcCli.CallContext(ctx, &j, "klay_getBlockWithConsensusInfoByNumber", "0x"+ansBN.Text(16))
if err == nil {
ret := gjson.Get(string(j), "number").String()
if !strings.Contains(ret, "0x"+ansBN.Text(16)) {
err = errors.New("wrong block: " + ret + ", answer: " + "0x" + ansBN.Text(16))
}
}
elapsed := boomer.Now() - start
sendBoomerEvent("readGetBlockWithConsensusInfoByNumber",
"Failed to call klay_GetBlockWithConsensusInfoByNumber", elapsed, rpcCli, err)
}