/
output.go
458 lines (415 loc) · 14.1 KB
/
output.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package loadtest
import (
"context"
"encoding/json"
"github.com/montanaflynn/stats"
"math"
"math/big"
"sort"
"sync"
"time"
"golang.org/x/time/rate"
"github.com/maticnetwork/polygon-cli/rpctypes"
"github.com/maticnetwork/polygon-cli/util"
"golang.org/x/exp/constraints"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/number"
_ "embed"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func printBlockSummary(c *ethclient.Client, bs map[uint64]blockSummary, startNonce, endNonce uint64) {
filterBlockSummary(bs, startNonce, endNonce)
mapKeys := getSortedMapKeys(bs)
if len(mapKeys) == 0 {
return
}
var totalTransactions uint64 = 0
var totalGasUsed uint64 = 0
p := message.NewPrinter(language.English)
allLatencies := make([]time.Duration, 0)
summaryOutputMode := *inputLoadTestParams.SummaryOutputMode
jsonSummaryList := []Summary{}
for _, v := range mapKeys {
summary := bs[v]
gasUsed := getTotalGasUsed(summary.Receipts)
blockLatencies := getMapValues(summary.Latencies)
minLatency, medianLatency, maxLatency := getMinMedianMax(blockLatencies)
allLatencies = append(allLatencies, blockLatencies...)
blockUtilization := float64(gasUsed) / summary.Block.GasLimit.ToFloat64()
if gasUsed == 0 {
blockUtilization = 0
}
// if we're at trace, debug, or info level we'll output the block level metrics
if zerolog.GlobalLevel() <= zerolog.InfoLevel {
if summaryOutputMode == "text" {
_, _ = p.Printf("Block number: %v\tTime: %s\tGas Limit: %v\tGas Used: %v\tNum Tx: %v\tUtilization %v\tLatencies: %v\t%v\t%v\n",
number.Decimal(summary.Block.Number.ToUint64()),
time.Unix(summary.Block.Timestamp.ToInt64(), 0),
number.Decimal(summary.Block.GasLimit.ToUint64()),
number.Decimal(gasUsed),
number.Decimal(len(summary.Block.Transactions)),
number.Percent(blockUtilization),
number.Decimal(minLatency.Seconds()),
number.Decimal(medianLatency.Seconds()),
number.Decimal(maxLatency.Seconds()))
} else if summaryOutputMode == "json" {
jsonSummary := Summary{}
jsonSummary.BlockNumber = summary.Block.Number.ToUint64()
jsonSummary.Time = time.Unix(summary.Block.Timestamp.ToInt64(), 0)
jsonSummary.GasLimit = summary.Block.GasLimit.ToUint64()
jsonSummary.GasUsed = gasUsed
jsonSummary.NumTx = len(summary.Block.Transactions)
jsonSummary.Utilization = blockUtilization
latencies := Latency{}
latencies.Min = minLatency.Seconds()
latencies.Median = medianLatency.Seconds()
latencies.Max = maxLatency.Seconds()
jsonSummary.Latencies = latencies
jsonSummaryList = append(jsonSummaryList, jsonSummary)
} else {
log.Error().Str("mode", summaryOutputMode).Msg("Invalid mode for summary output")
}
}
totalTransactions += uint64(len(summary.Block.Transactions))
totalGasUsed += gasUsed
}
parentOfFirstBlock, _ := c.BlockByNumber(context.Background(), big.NewInt(bs[mapKeys[0]].Block.Number.ToInt64()-1))
lastBlock := bs[mapKeys[len(mapKeys)-1]].Block
totalMiningTime := time.Duration(lastBlock.Timestamp.ToUint64()-parentOfFirstBlock.Time()) * time.Second
tps := float64(totalTransactions) / totalMiningTime.Seconds()
gaspersec := float64(totalGasUsed) / totalMiningTime.Seconds()
minLatency, medianLatency, maxLatency := getMinMedianMax(allLatencies)
successfulTx, totalTx := getSuccessfulTransactionCount(bs)
if summaryOutputMode == "text" {
p.Printf("Successful Tx: %v\tTotal Tx: %v\n", number.Decimal(successfulTx), number.Decimal(totalTx))
p.Printf("Total Mining Time: %s\n", totalMiningTime)
p.Printf("Total Transactions: %v\n", number.Decimal(totalTransactions))
p.Printf("Total Gas Used: %v\n", number.Decimal(totalGasUsed))
p.Printf("Transactions per sec: %v\n", number.Decimal(tps))
p.Printf("Gas Per Second: %v\n", number.Decimal(gaspersec))
p.Printf("Latencies - Min: %v\tMedian: %v\tMax: %v\n", number.Decimal(minLatency.Seconds()), number.Decimal(medianLatency.Seconds()), number.Decimal(maxLatency.Seconds()))
// TODO: Add some kind of indication of block time variance
} else if summaryOutputMode == "json" {
summaryOutput := SummaryOutput{}
summaryOutput.Summaries = jsonSummaryList
summaryOutput.SuccessfulTx = successfulTx
summaryOutput.TotalTx = totalTx
summaryOutput.TotalMiningTime = totalMiningTime
summaryOutput.TotalGasUsed = totalGasUsed
summaryOutput.TransactionsPerSec = tps
summaryOutput.GasPerSecond = gaspersec
latencies := Latency{}
latencies.Min = minLatency.Seconds()
latencies.Median = medianLatency.Seconds()
latencies.Max = maxLatency.Seconds()
summaryOutput.Latencies = latencies
val, _ := json.MarshalIndent(summaryOutput, "", " ")
p.Println(string(val))
} else {
log.Error().Str("mode", summaryOutputMode).Msg("Invalid mode for summary output")
}
}
func filterBlockSummary(blockSummaries map[uint64]blockSummary, startNonce, endNonce uint64) {
validTx := make(map[ethcommon.Hash]struct{}, 0)
var minBlock uint64 = math.MaxUint64
var maxBlock uint64 = 0
for _, bs := range blockSummaries {
for _, tx := range bs.Block.Transactions {
if tx.Nonce.ToUint64() >= startNonce && tx.Nonce.ToUint64() <= endNonce {
validTx[tx.Hash.ToHash()] = struct{}{}
if tx.BlockNumber.ToUint64() < minBlock {
minBlock = tx.BlockNumber.ToUint64()
}
if tx.BlockNumber.ToUint64() > maxBlock {
maxBlock = tx.BlockNumber.ToUint64()
}
}
}
}
keys := getSortedMapKeys(blockSummaries)
for _, k := range keys {
if k < minBlock {
delete(blockSummaries, k)
}
if k > maxBlock {
delete(blockSummaries, k)
}
}
for _, bs := range blockSummaries {
filteredTransactions := make([]rpctypes.RawTransactionResponse, 0)
for txKey, tx := range bs.Block.Transactions {
if _, hasKey := validTx[tx.Hash.ToHash()]; hasKey {
filteredTransactions = append(filteredTransactions, bs.Block.Transactions[txKey])
}
}
bs.Block.Transactions = filteredTransactions
filteredReceipts := make(map[ethcommon.Hash]rpctypes.RawTxReceipt, 0)
for receiptKey, receipt := range bs.Receipts {
if _, hasKey := validTx[receipt.TransactionHash.ToHash()]; hasKey {
filteredReceipts[receipt.TransactionHash.ToHash()] = bs.Receipts[receiptKey]
}
}
bs.Receipts = filteredReceipts
}
}
func getMapValues[K constraints.Ordered, V any](m map[K]V) []V {
newSlice := make([]V, 0)
for _, val := range m {
newSlice = append(newSlice, val)
}
return newSlice
}
func getMinMedianMax[V constraints.Float | constraints.Integer](values []V) (V, V, V) {
if len(values) == 0 {
return 0, 0, 0
}
sort.Slice(values, func(i, j int) bool {
return values[i] < values[j]
})
half := len(values) / 2
median := values[half]
if len(values)%2 == 0 {
median = (median + values[half-1]) / V(2)
}
var min V
var max V
for k, v := range values {
if k == 0 {
min = v
max = v
continue
}
if v < min {
min = v
}
if v > max {
max = v
}
}
return min, median, max
}
func getSortedMapKeys[V any, K constraints.Ordered](m map[K]V) []K {
keys := make([]K, 0)
for k := range m {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
return keys
}
func getSuccessfulTransactionCount(bs map[uint64]blockSummary) (successful, total int64) {
for _, block := range bs {
total += int64(len(block.Receipts))
for _, receipt := range block.Receipts {
successful += receipt.Status.ToInt64()
}
}
return
}
func getTotalGasUsed(receipts map[ethcommon.Hash]rpctypes.RawTxReceipt) uint64 {
var totalGasUsed uint64 = 0
for _, receipt := range receipts {
totalGasUsed += receipt.GasUsed.ToUint64()
}
return totalGasUsed
}
type Latency struct {
Min float64
Median float64
Max float64
}
type Summary struct {
BlockNumber uint64
Time time.Time
GasLimit uint64
GasUsed uint64
NumTx int
Utilization float64
Latencies Latency
}
type SummaryOutput struct {
Summaries []Summary
SuccessfulTx int64
TotalTx int64
TotalMiningTime time.Duration
TotalGasUsed uint64
TransactionsPerSec float64
GasPerSecond float64
Latencies Latency
}
func summarizeTransactions(ctx context.Context, c *ethclient.Client, rpc *ethrpc.Client, startBlockNumber, startNonce, lastBlockNumber, endNonce uint64) error {
ltp := inputLoadTestParams
var err error
log.Trace().Msg("Starting block range capture")
// confirm start block number is ok
_, err = c.BlockByNumber(ctx, new(big.Int).SetUint64(startBlockNumber))
if err != nil {
return err
}
rawBlocks, err := util.GetBlockRange(ctx, startBlockNumber, lastBlockNumber, rpc)
if err != nil {
return err
}
// TODO: Add some kind of decimation to avoid summarizing for 10 minutes?
batchSize := *ltp.BatchSize
goRoutineLimit := *ltp.Concurrency
var txGroup sync.WaitGroup
threadPool := make(chan bool, goRoutineLimit)
log.Trace().Msg("Starting tx receipt capture")
rawTxReceipts := make([]*json.RawMessage, 0)
var rawTxReceiptsLock sync.Mutex
var txGroupErr error
startReceipt := time.Now()
for k := range rawBlocks {
threadPool <- true
txGroup.Add(1)
go func(b *json.RawMessage) {
var receipt []*json.RawMessage
receipt, err = util.GetReceipts(ctx, []*json.RawMessage{b}, rpc, batchSize)
if err != nil {
txGroupErr = err
return
}
rawTxReceiptsLock.Lock()
rawTxReceipts = append(rawTxReceipts, receipt...)
rawTxReceiptsLock.Unlock()
<-threadPool
txGroup.Done()
}(rawBlocks[k])
}
endReceipt := time.Now()
txGroup.Wait()
if txGroupErr != nil {
log.Error().Err(err).Msg("One of the threads fetching tx receipts failed")
return err
}
blocks := make([]rpctypes.RawBlockResponse, 0)
for _, b := range rawBlocks {
var block rpctypes.RawBlockResponse
err = json.Unmarshal(*b, &block)
if err != nil {
log.Error().Err(err).Msg("Error decoding block response")
return err
}
blocks = append(blocks, block)
}
log.Info().Int("len", len(blocks)).Msg("Block summary")
txReceipts := make([]rpctypes.RawTxReceipt, 0)
log.Trace().Int("len", len(rawTxReceipts)).Msg("Raw receipts")
for _, r := range rawTxReceipts {
if isEmptyJSONResponse(r) {
continue
}
var receipt rpctypes.RawTxReceipt
err = json.Unmarshal(*r, &receipt)
if err != nil {
log.Error().Err(err).Msg("Error decoding tx receipt response")
return err
}
txReceipts = append(txReceipts, receipt)
}
log.Info().Int("len", len(txReceipts)).Msg("Receipt summary")
blockData := make(map[uint64]blockSummary, 0)
for k, b := range blocks {
bs := blockSummary{}
bs.Block = &blocks[k]
bs.Receipts = make(map[ethcommon.Hash]rpctypes.RawTxReceipt, 0)
bs.Latencies = make(map[uint64]time.Duration, 0)
blockData[b.Number.ToUint64()] = bs
}
for _, r := range txReceipts {
bn := r.BlockNumber.ToUint64()
bs := blockData[bn]
if bs.Receipts == nil {
log.Error().Uint64("blocknumber", bn).Msg("Block number from receipts does not exist in block data")
}
bs.Receipts[r.TransactionHash.ToHash()] = r
blockData[bn] = bs
}
nonceTimes := make(map[uint64]time.Time, 0)
for _, ltr := range loadTestResults {
nonceTimes[ltr.Nonce] = ltr.RequestTime
}
minLatency := time.Millisecond * 100
for _, bs := range blockData {
for _, tx := range bs.Block.Transactions {
// TODO: What happens when the system clock of the load tester isn't in sync with the system clock of the miner?
// TODO: the timestamp in the chain only has granularity down to the second. How to deal with this
mineTime := time.Unix(bs.Block.Timestamp.ToInt64(), 0)
requestTime := nonceTimes[tx.Nonce.ToUint64()]
txLatency := mineTime.Sub(requestTime)
if txLatency.Hours() > 2 {
log.Debug().Float64("txHours", txLatency.Hours()).Uint64("nonce", tx.Nonce.ToUint64()).Uint64("blockNumber", bs.Block.Number.ToUint64()).Time("mineTime", mineTime).Time("requestTime", requestTime).Msg("Encountered transaction with more than 2 hours latency")
}
bs.Latencies[tx.Nonce.ToUint64()] = txLatency
if txLatency < minLatency {
minLatency = txLatency
}
}
}
// TODO this might be a hack, but not sure what's a better way to deal with time discrepancies
if minLatency < time.Millisecond*100 {
log.Trace().Str("minLatency", minLatency.String()).Msg("Minimum latency is below expected threshold")
shiftSize := ((time.Millisecond * 100) - minLatency) + time.Millisecond + 100
for _, bs := range blockData {
for _, tx := range bs.Block.Transactions {
bs.Latencies[tx.Nonce.ToUint64()] += shiftSize
}
}
}
printBlockSummary(c, blockData, startNonce, endNonce)
log.Trace().Str("summaryTime", (endReceipt.Sub(startReceipt)).String()).Msg("Total Summary Time")
return nil
}
func isEmptyJSONResponse(r *json.RawMessage) bool {
rawJson := []byte(*r)
return len(rawJson) == 0
}
func lightSummary(lts []loadTestSample, startTime, endTime time.Time, rl *rate.Limiter) {
if len(lts) == 0 {
log.Error().Msg("No results recorded")
return
}
log.Info().Msg("* Results")
log.Info().Int("samples", len(lts)).Msg("Samples")
var numErrors uint64 = 0
latencies := make([]float64, 0)
for _, s := range lts {
if s.IsError {
numErrors++
}
latencies = append(latencies, s.WaitTime.Seconds())
}
testDuration := endTime.Sub(startTime)
tps := float64(len(loadTestResults)) / testDuration.Seconds()
var rlLimit float64
if rl != nil {
rlLimit = float64(rl.Limit())
}
meanLat, _ := stats.Mean(latencies)
medianLat, _ := stats.Median(latencies)
minLat, _ := stats.Min(latencies)
maxLat, _ := stats.Max(latencies)
stddevLat, _ := stats.StandardDeviation(latencies)
log.Info().Time("startTime", startTime).Msg("Start time of loadtest (first transaction sent)")
log.Info().Time("endTime", endTime).Msg("End time of loadtest (final transaction mined)")
log.Info().Float64("tps", tps).Msg("Overall Requests Per Second")
log.Info().
Float64("mean", meanLat).
Float64("median", medianLat).
Float64("min", minLat).
Float64("max", maxLat).
Float64("stddev", stddevLat).
Msg("Request Latency Stats")
log.Info().
Float64("testDuration", testDuration.Seconds()).
Float64("finalRateLimit", rlLimit).
Msg("Rough test summary")
log.Info().Uint64("numErrors", numErrors).Msg("Num errors")
}