-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransactions.go
306 lines (262 loc) · 8.74 KB
/
transactions.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
package main
import (
"fmt"
"time"
"github.com/chainbound/fiber-benchmarks/log"
"github.com/chainbound/fiber-benchmarks/sinks"
"github.com/chainbound/fiber-benchmarks/sources/bloxroute"
"github.com/chainbound/fiber-benchmarks/sources/fiber"
"github.com/chainbound/fiber-benchmarks/types"
f "github.com/chainbound/fiber-go"
"github.com/ethereum/go-ethereum/common"
"github.com/montanaflynn/stats"
"github.com/rs/zerolog"
)
type TransactionBenchmarker struct {
config *config
logger zerolog.Logger
fiberSource *fiber.FiberSource
otherSource TransactionSource
otherSourceName string
sink Sink
}
func runTransactionBenchmark(config *config) error {
// - For each interval, we collect all data from both streams
// - At the end of the interval, we print the stats and save the result
// - At the end of the benchmark, we print the overall stats
logger := log.NewLogger("benchmark")
if err := config.validate(); err != nil {
logger.Fatal().Err(err).Msg("Invalid config")
}
var fiberSource *fiber.FiberSource
var otherSource TransactionSource
var otherSourceName string
if config.fiberOnly {
firstEndpoint := config.fiberEndpoints[0]
fiberSource = fiber.NewFiberSource([]string{firstEndpoint}, config.fiberKey)
if err := fiberSource.Connect(); err != nil {
return err
}
otherSourceName = "fiber-2"
secondEndpoint := config.fiberEndpoints[1]
otherFiberSource := fiber.NewFiberSource([]string{secondEndpoint}, config.fiberKey)
if err := otherFiberSource.Connect(); err != nil {
return err
}
logger.Info().Str("fiber-1", firstEndpoint).Str("fiber-2", secondEndpoint).Msg("Running fiber-only benchmark")
otherSource = otherFiberSource
} else {
fiberSource = fiber.NewFiberSource(config.fiberEndpoints, config.fiberKey)
if err := fiberSource.Connect(); err != nil {
return err
}
if config.blxrEndpoint != "" && config.blxrKey != "" {
otherSource = bloxroute.NewBloxrouteSource(config.blxrEndpoint, config.blxrKey)
otherSourceName = "bloxroute"
}
}
sink, err := setupSink(config, sinks.Transactions)
if err != nil {
return err
}
benchmarker := &TransactionBenchmarker{
config: config,
logger: logger,
fiberSource: fiberSource,
otherSource: otherSource,
otherSourceName: otherSourceName,
sink: sink,
}
benchmarker.Run()
return nil
}
func (b *TransactionBenchmarker) Run() {
var (
fiberStream = b.fiberSource.SubscribeTransactionObservations()
otherStream = b.otherSource.SubscribeTransactionObservations()
payloadStream = b.fiberSource.SubscribeExecutionPayloads()
)
defer b.fiberSource.Close()
for i := 0; i < b.config.intervalCount; i++ {
start := time.Now()
b.logger.Info().Int("interval", i+1).Msg("Running benchmark interval")
fmt.Println()
stats, err := b.runInterval(fiberStream, otherStream, payloadStream)
if err != nil {
b.logger.Error().Err(err).Msg("Failed to run interval")
}
end := time.Now()
stats.StartTime = start
stats.EndTime = end
stats.BenchmarkID = b.config.benchmarkID
if b.sink != nil {
b.sink.RecordStats(&stats)
if err := b.sink.Flush(); err != nil {
b.logger.Error().Err(err).Msg("Failed to flush sink")
}
}
}
b.logger.Info().Msg("Benchmark complete")
}
// Runs the interval
func (b *TransactionBenchmarker) runInterval(fiberStream chan types.Observation, otherStream chan types.Observation, payloadStream chan *f.Block) (types.ObservationStatsRow, error) {
// Setup
var (
fiberMap = make(map[common.Hash]types.Observation)
otherMap = make(map[common.Hash]types.Observation)
truthMap = make(map[common.Hash]struct{})
)
// Initialize interval timer
timer := time.NewTimer(b.config.interval)
end := time.Now().Add(b.config.interval)
b.logger.Info().Int("fiber", len(fiberStream)).Int("other", len(otherStream)).Msg("Buffered observations")
loop:
for {
select {
case <-timer.C:
break loop
case fiberObs := <-fiberStream:
// If we're not in the tail window, continue as usual
if _, ok := fiberMap[fiberObs.Hash]; !ok {
fiberMap[fiberObs.Hash] = fiberObs
} else {
b.logger.Warn().Str("hash", fiberObs.Hash.Hex()).Str("source", "fiber").Msg("Duplicate hash during interval")
}
case otherObs := <-otherStream:
if _, ok := otherMap[otherObs.Hash]; !ok {
otherMap[otherObs.Hash] = otherObs
} else {
b.logger.Warn().Str("hash", otherObs.Hash.Hex()).Str("source", b.otherSourceName).Msg("Duplicate hash during interval")
}
case payload := <-payloadStream:
if b.config.crossCheck {
for _, tx := range payload.Transactions {
truthMap[tx.Hash()] = struct{}{}
}
if b.config.sink != "clickhouse" {
fmt.Printf("\033[1A\033[K")
b.logger.Info().Int("block_number", int(payload.Header.Number.Int64())).Int("amount_confirmed", len(truthMap)).Str("remaining", time.Until(end).String()).Msg("Recorded execution payload transactions")
}
}
}
}
return b.processIntervalResults(fiberMap, otherMap, truthMap)
}
func (b *TransactionBenchmarker) processIntervalResults(fiberMap, otherMap map[common.Hash]types.Observation, truthMap map[common.Hash]struct{}) (types.ObservationStatsRow, error) {
diffMap := make(map[common.Hash]float64, len(truthMap))
differences := make([]float64, 0, len(truthMap))
for hash := range truthMap {
var (
fiberSaw = false
otherSaw = false
)
fiberObs, ok := fiberMap[hash]
if ok {
fiberSaw = true
}
otherObs, ok := otherMap[hash]
if ok {
otherSaw = true
}
fiberTs := fiberObs.Timestamp
otherTs := otherObs.Timestamp
switch {
case fiberSaw && otherSaw:
microDiff := otherTs - fiberTs
milliDiff := float64(microDiff) / 1000
// Both saw the transaction. Record the difference
diffMap[hash] = milliDiff
differences = append(differences, milliDiff)
if b.sink != nil {
b.sink.RecordObservationRow(&types.ConfirmedObservationRow{
TxHash: hash.Hex(),
FiberTimestamp: fiberTs,
OtherTimestamp: otherTs,
Difference: microDiff,
BenchmarkID: b.config.benchmarkID,
From: fiberObs.From,
To: fiberObs.To,
CallDataSize: fiberObs.CallDataSize,
})
}
case fiberSaw && !otherSaw:
// Only Fiber saw the transaction
if b.config.logMissing {
b.logger.Warn().Str("hash", hash.Hex()).Msg(fmt.Sprintf("Fiber saw transaction but %s did not", b.otherSourceName))
}
if b.sink != nil {
b.sink.RecordObservationRow(&types.ConfirmedObservationRow{
TxHash: hash.Hex(),
FiberTimestamp: fiberTs,
OtherTimestamp: 0,
Difference: 0,
BenchmarkID: b.config.benchmarkID,
From: fiberObs.From,
To: fiberObs.To,
CallDataSize: fiberObs.CallDataSize,
})
}
case !fiberSaw && otherSaw:
// Only the other source saw the transaction
if b.config.logMissing {
b.logger.Warn().Str("hash", hash.Hex()).Msg(fmt.Sprintf("%s saw transaction but fiber did not", b.otherSourceName))
}
if b.sink != nil {
b.sink.RecordObservationRow(&types.ConfirmedObservationRow{
TxHash: hash.Hex(),
FiberTimestamp: 0,
OtherTimestamp: otherTs,
Difference: 0,
BenchmarkID: b.config.benchmarkID,
From: otherObs.From,
To: otherObs.To,
CallDataSize: otherObs.CallDataSize,
})
}
}
}
if b.config.sink != "clickhouse" {
fmt.Println(types.MakeHistogram(differences))
b.logger.Info().Msg(fmt.Sprintf("fiber total observations: %d", len(fiberMap)))
b.logger.Info().Msg(fmt.Sprintf("%s total observations: %d", b.otherSourceName, len(otherMap)))
}
b.printStats(differences)
return buildObservationStats(differences)
}
func (b *TransactionBenchmarker) printStats(differences []float64) {
fiberWon := float64(0)
blxrWon := float64(0)
for _, diff := range differences {
if diff > 0 {
fiberWon++
} else {
blxrWon++
}
}
mean, err := stats.Mean(differences)
if err != nil {
b.logger.Error().Err(err).Msg("Failed to calculate mean")
}
median, err := stats.Median(differences)
if err != nil {
b.logger.Error().Err(err).Msg("Failed to calculate median")
}
stdev, err := stats.StandardDeviation(differences)
if err != nil {
b.logger.Error().Err(err).Msg("Failed to calculate stdev")
}
min, err := stats.Min(differences)
if err != nil {
b.logger.Error().Err(err).Msg("Failed to calculate min")
}
max, err := stats.Max(differences)
if err != nil {
b.logger.Error().Err(err).Msg("Failed to calculate max")
}
b.logger.Info().Msg(fmt.Sprintf("Mean: %.4fms", mean))
b.logger.Info().Msg(fmt.Sprintf("Median: %.4fms", median))
b.logger.Info().Msg(fmt.Sprintf("Stdev: %.4fms", stdev))
b.logger.Info().Msg(fmt.Sprintf("Min: %.4fms | Max: %.4fms", min, max))
wonRatio := fiberWon / (fiberWon + blxrWon)
b.logger.Info().Msg(fmt.Sprintf("Fiber won: %.2f%%", wonRatio*100))
}