-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
389 lines (343 loc) · 9.78 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/INFURA/go-ethlibs/node"
ethspam "github.com/p2p-org/ethspam/lib"
"github.com/bojand/ghz/runner"
"github.com/golang/protobuf/proto"
"github.com/jessevdk/go-flags"
"github.com/jhump/protoreflect/desc"
"google.golang.org/protobuf/encoding/protojson"
"gopkg.in/yaml.v3"
"io"
"math/rand"
"os"
"github.com/p2p-org/drpc-provider-estimator/dshackle"
"github.com/p2p-org/drpc-provider-estimator/gas"
"time"
"sync/atomic"
)
type Options struct {
Host string `long:"target" short:"t" description:"target host" required:"true"`
Chain int `long:"chain" short:"c" description:"chain id" default:"100"`
StepDuration uint64 `long:"step-duration" short:"d" description:"step duration in minutes" default:"3"`
SourceHost string `long:"source" short:"s" description:"source eth host" default:"https://eth.drpc.org"`
StopOnEvents bool `long:"stop-on-events" short:"e" description:"stop on events"`
CsvOutput string `long:"csv-output" short:"o" description:"csv output file"`
Mode string `long:"mode" short:"m" description:"mode. Can be spam or prepared" default:"spam"`
SpamProfile string `long:"spam-profile" short:"p" description:"spam profile"`
PreparedRequests string `long:"prepared-requests" short:"r" description:"prepared requests folder"`
PreparedCU uint64 `long:"prepared-cu" short:"u" description:"prepared request cu cost" default:"0"`
RequestLabel string `long:"request-label" short:"l" description:"request label for dshackle"`
LoadLevels string `long:"load-levels" short:"a" description:"load levels"`
Insecure bool `long:"insecure" short:"i" description:"certificate"`
}
var DefaultProfile = map[string]int64{
"eth_getCode": 100,
"eth_getLogs": 250,
"eth_getTransactionByHash": 250,
"eth_blockNumber": 350,
"eth_getTransactionCount": 400,
"eth_getBlockByNumber": 400,
"eth_getBalance": 550,
"eth_getTransactionReceipt": 600,
"eth_call": 2000,
}
func main() {
options := Options{}
_, err := flags.Parse(&options)
if err != nil {
return
}
var load []uint
if options.LoadLevels != "" {
err := json.Unmarshal([]byte("["+options.LoadLevels+"]"), &load)
if err != nil {
exit(1, "error during fetching loading load level: %v", err)
}
} else {
load = []uint{10, 50, 100, 500, 1000, 5000, 10000}
}
prevRps := 0.0
prevMean := time.Hour * 1
var maxCu uint64 = 0
var printers PrinterHolder
printers.Printers = append(printers.Printers, &TextPrinter{
Out: os.Stdout,
})
if options.CsvOutput != "" {
f, err := os.Create(options.CsvOutput)
if err != nil {
exit(1, "error during creating output: %v", err)
}
defer f.Close()
printers.Printers = append(printers.Printers, &CsvPrinter{
Out: f,
})
}
printers.Iterate(func(printer Printer) {
printer.PrintHeader()
})
var dataFuncProvider func(*uint64) func(*desc.MethodDescriptor, *runner.CallData) []byte
fmt.Println(options.Mode)
if options.Mode == "spam" {
var profile map[string]int64
if options.SpamProfile == "" {
profile = DefaultProfile
} else {
profile = map[string]int64{}
profileRaw, _ := os.ReadFile(options.SpamProfile)
err := yaml.Unmarshal(profileRaw, profile)
if err != nil {
exit(1, "error during fetching spam profile: %v", err)
}
}
fmt.Println(profile)
dataFuncProvider = func(cuCount *uint64) func(mtd *desc.MethodDescriptor, callData *runner.CallData) []byte {
return NewEthSpamBinaryDataFunc(context.Background(), profile, options.SourceHost, dshackle.ChainRef(options.Chain), cuCount)
}
} else if options.Mode == "prepared" {
files, err := os.ReadDir(options.PreparedRequests)
if err != nil {
exit(1, "error during fetching requests: %v", err)
}
reqs := make([]*dshackle.NativeCallRequest, 0)
for _, file := range files {
if file.IsDir() {
continue
}
name := options.PreparedRequests + "/" + file.Name()
raw, err := os.ReadFile(name)
if err != nil {
panic(err)
}
jsonrpc := JsonRpcRequest{}
err = json.Unmarshal(raw, &jsonrpc)
fmt.Printf("loading %s\n", name)
if err != nil {
panic(err)
}
req := &dshackle.NativeCallRequest{
Chain: dshackle.ChainRef(options.Chain),
Items: []*dshackle.NativeCallItem{
{
Id: uint32(jsonrpc.Id),
Method: jsonrpc.Method,
Payload: jsonrpc.Params,
},
},
}
if options.RequestLabel != "" {
selector := dshackle.Selector{}
err = protojson.Unmarshal([]byte(options.RequestLabel), &selector)
if err != nil {
exit(1, "error during fetching selector: %v", err)
}
req.Selector = &selector
}
fmt.Printf("request added to load: %s from %s\n", jsonrpc.Method, name)
reqs = append(reqs, req)
}
dataFuncProvider = func(cuCount *uint64) func(mtd *desc.MethodDescriptor, callData *runner.CallData) []byte {
pointer := 0
return func(mtd *desc.MethodDescriptor, callData *runner.CallData) []byte {
req := reqs[pointer]
pointer = (pointer + 1) % len(reqs)
data, _ := proto.Marshal(req)
if options.PreparedCU > 0 {
*cuCount += options.PreparedCU
} else {
*cuCount += gas.CountGas(req.Items[0].Method)
}
return data
}
}
} else {
exit(1, "unknown mode")
}
for _, l := range load {
printers.Iterate(func(printer Printer) {
printer.PrintPreLine(l)
})
var curCu uint64 = 0
ghzOpts := make([]runner.Option, 0)
ghzOpts = append(ghzOpts,
runner.WithBinaryDataFunc(dataFuncProvider(&curCu)),
runner.WithEnableCompression(true),
runner.WithConnections(10),
runner.WithTotalRequests(500),
runner.WithConcurrency(l),
runner.WithInsecure(options.Insecure),
runner.WithRunDuration(time.Duration(options.StepDuration)*time.Minute),
)
report, err := runner.Run(
"emerald.Blockchain.NativeCall",
options.Host,
ghzOpts...,
)
if err != nil {
exit(1, "error during test execution: %v", err)
}
succRate := calcErrorRate(report.StatusCodeDist)
printers.Iterate(func(printer Printer) {
printer.PrintLine(l, report.Rps, report.Average, succRate, curCu/options.StepDuration, report.ErrorDist)
})
if curCu > maxCu {
maxCu = curCu
}
if prevRps > report.Rps {
printers.Iterate(func(printer Printer) {
printer.PrintEvent("RPS DECREASED")
})
if options.StopOnEvents {
break
}
}
if prevMean*100 < report.Average {
printers.Iterate(func(printer Printer) {
printer.PrintEvent("LATENCY INCREASED")
})
if options.StopOnEvents {
break
}
}
if succRate < 0.85 {
printers.Iterate(func(printer Printer) {
printer.PrintEvent("ERROR RATE INCREASED")
})
if options.StopOnEvents {
break
}
}
prevRps = report.Rps
prevMean = report.Average
}
maxCu = maxCu / options.StepDuration
printers.Iterate(func(printer Printer) {
printer.PrintFooter(maxCu)
})
}
func calcErrorRate(dist map[string]int) float64 {
total := 0.0
nok := 0.0
for k, v := range dist {
if k == "Canceled" {
continue
}
total += float64(v)
if k != "OK" {
nok += float64(v)
}
}
return 1 - (nok / total)
}
func NewEthSpamBinaryDataFunc(ctx context.Context, queryParams map[string]int64, parentHost string, chain dshackle.ChainRef, cuCount *uint64) func(mtd *desc.MethodDescriptor, callData *runner.CallData) []byte {
generator, err := ethspam.MakeQueriesGenerator(queryParams)
if err != nil {
exit(1, "failed to install defaults: %s", err)
}
client, err := node.NewClient(ctx, parentHost)
if err != nil {
exit(1, "failed to make a new client: %s", err)
}
mkState := ethspam.StateProducer{
Client: client,
}
stateChannel := make(chan ethspam.State, 1)
go func() {
randSrc := rand.NewSource(time.Now().UnixNano())
state := ethspam.LiveState{
IdGen: ðspam.IdGenerator{},
RandSrc: randSrc,
}
defer close(stateChannel)
for {
newState, err := mkState.Refresh(&state)
if err != nil {
// It can happen in some testnets that most of the blocks
// are empty(no transaction included), don't refresh the
// QueriesGenerator state without new inclusion.
if err == ethspam.ErrEmptyBlock {
select {
case <-time.After(5 * time.Second):
case <-ctx.Done():
return
}
continue
}
fmt.Printf("failed to refresh state: %s", err)
<-time.After(1 * time.Second)
continue
}
select {
case stateChannel <- newState:
case <-ctx.Done():
return
}
select {
case <-time.After(15 * time.Second):
case <-ctx.Done():
return
}
}
}()
state := <-stateChannel
queries := make(chan ethspam.QueryContent, 1000)
go func() {
defer close(queries)
for {
// Update state when a new one is emitted
select {
case state = <-stateChannel:
case <-ctx.Done():
return
default:
}
if q, err := generator.Query(state); err == io.EOF {
return
} else if err != nil {
exit(2, "failed to write generated query: %s", err)
} else {
select {
case queries <- q:
case <-ctx.Done():
break
}
}
}
}()
return func(mtd *desc.MethodDescriptor, callData *runner.CallData) []byte {
raw, ok := <-queries
if !ok {
panic("no more queries")
}
req := dshackle.NativeCallRequest{
Chain: chain,
Items: []*dshackle.NativeCallItem{
{
Id: uint32(raw.Id),
Method: raw.Method,
Payload: []byte(raw.Params),
},
},
}
data, err := proto.Marshal(&req)
if err != nil {
panic(err)
}
atomic.AddUint64(cuCount, gas.CountGas(raw.Method))
return data
}
}
func exit(code int, format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
os.Exit(code)
}
type JsonRpcRequest struct {
Id int `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Params json.RawMessage `json:"params"`
Method string `json:"method"`
}