/
keeper_benchmark.go
327 lines (295 loc) · 11.7 KB
/
keeper_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
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
package testreporters
import (
"encoding/csv"
"encoding/json"
"fmt"
"math"
"os"
"path/filepath"
"sort"
"sync"
"testing"
"github.com/rs/zerolog/log"
"github.com/slack-go/slack"
"github.com/smartcontractkit/chainlink-testing-framework/testreporters"
"github.com/smartcontractkit/chainlink/integration-tests/client"
)
// KeeperBenchmarkTestReporter enables reporting on the keeper benchmark test
type KeeperBenchmarkTestReporter struct {
Reports []KeeperBenchmarkTestReport `json:"reports"`
ReportMutex sync.Mutex
AttemptedChainlinkTransactions []*client.TransactionsData `json:"attemptedChainlinkTransactions"`
NumRevertedUpkeeps int64
NumStaleUpkeepReports int64
Summary KeeperBenchmarkTestSummary `json:"summary"`
namespace string
keeperReportFile string
attemptedTransactionsFile string
keeperSummaryFile string
}
type KeeperBenchmarkTestSummary struct {
Load KeeperBenchmarkTestLoad `json:"load"`
Config KeeperBenchmarkTestConfig `json:"config"`
Metrics KeeperBenchmarkTestMetrics `json:"metrics"`
TestInputs map[string]interface{} `json:"testInputs"`
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
}
type KeeperBenchmarkTestLoad struct {
TotalCheckGasPerBlock int64 `json:"totalCheckGasPerBlock"`
TotalPerformGasPerBlock int64 `json:"totalPerformGasPerBlock"`
AverageExpectedPerformsPerBlock float64 `json:"averageExpectedPerformsPerBlock"`
}
type KeeperBenchmarkTestConfig struct {
Chainlink map[string]map[string]string `json:"chainlink"`
Geth map[string]map[string]string `json:"geth"`
}
type KeeperBenchmarkTestMetrics struct {
Delay map[string]interface{} `json:"delay"`
PercentWithinSLA float64 `json:"percentWithinSLA"`
PercentRevert float64 `json:"percentRevert"`
PercentStale float64 `json:"percentStale"`
TotalTimesEligible int64 `json:"totalTimesEligible"`
TotalTimesPerformed int64 `json:"totalTimesPerformed"`
TotalStaleReports int64 `json:"totalStaleReports"`
AverageActualPerformsPerBlock float64 `json:"averageActualPerformsPerBlock"`
}
// KeeperBenchmarkTestReport holds a report information for a single Upkeep Consumer contract
type KeeperBenchmarkTestReport struct {
RegistryAddress string `json:"registryAddress"`
ContractAddress string `json:"contractAddress"`
TotalEligibleCount int64 `json:"totalEligibleCount"`
TotalSLAMissedUpkeeps int64 `json:"totalSLAMissedUpkeeps"`
TotalPerformedUpkeeps int64 `json:"totalPerformedUpkeeps"`
AllCheckDelays []int64 `json:"allCheckDelays"` // List of the delays since checkUpkeep for all performs
}
func (k *KeeperBenchmarkTestReporter) SetNamespace(namespace string) {
k.namespace = namespace
}
func (k *KeeperBenchmarkTestReporter) WriteReport(folderLocation string) error {
k.keeperReportFile = filepath.Join(folderLocation, "./benchmark_report.csv")
k.keeperSummaryFile = filepath.Join(folderLocation, "./benchmark_summary.json")
// k.keeperSummaryCsvFile = filepath.Join(folderLocation, "./benchmark_summary.csv")
k.attemptedTransactionsFile = filepath.Join(folderLocation, "./attempted_transactions_report.json")
keeperReportFile, err := os.Create(k.keeperReportFile)
if err != nil {
return err
}
defer keeperReportFile.Close()
keeperReportWriter := csv.NewWriter(keeperReportFile)
var totalEligibleCount, totalPerformed, totalMissedSLA, totalReverted, totalStaleReports int64
var allDelays []int64
for _, report := range k.Reports {
totalEligibleCount += report.TotalEligibleCount
totalPerformed += report.TotalPerformedUpkeeps
totalMissedSLA += report.TotalSLAMissedUpkeeps
allDelays = append(allDelays, report.AllCheckDelays...)
}
totalReverted = k.NumRevertedUpkeeps
totalStaleReports = k.NumStaleUpkeepReports
pctWithinSLA := (1.0 - float64(totalMissedSLA)/float64(totalEligibleCount)) * 100
var pctReverted, pctStale float64
if totalPerformed > 0 {
pctReverted = (float64(totalReverted) / float64(totalPerformed)) * 100
pctStale = (float64(totalStaleReports) / float64(totalPerformed)) * 100
}
err = keeperReportWriter.Write([]string{"Full Test Summary"})
if err != nil {
return err
}
err = keeperReportWriter.Write([]string{
"Total Times Eligible",
"Total Performed",
"Total Reverted",
"Total Stale Reports",
"Average Perform Delay",
"Median Perform Delay",
"90th pct Perform Delay",
"99th pct Perform Delay",
"Max Perform Delay",
"Percent Within SLA",
"Percent Revert",
"Percent Stale",
})
if err != nil {
return err
}
avg, median, ninetyPct, ninetyNinePct, max := IntListStats(allDelays)
err = keeperReportWriter.Write([]string{
fmt.Sprint(totalEligibleCount),
fmt.Sprint(totalPerformed),
fmt.Sprint(totalReverted),
fmt.Sprint(totalStaleReports),
fmt.Sprintf("%.2f", avg),
fmt.Sprint(median),
fmt.Sprint(ninetyPct),
fmt.Sprint(ninetyNinePct),
fmt.Sprint(max),
fmt.Sprintf("%.2f%%", pctWithinSLA),
fmt.Sprintf("%.2f%%", pctReverted),
fmt.Sprintf("%.2f%%", pctStale),
})
if err != nil {
return err
}
keeperReportWriter.Flush()
log.Info().
Int64("Total Times Eligible", totalEligibleCount).
Int64("Total Performed", totalPerformed).
Int64("Total Reverted", totalReverted).
Float64("Average Perform Delay", avg).
Int64("Median Perform Delay", median).
Int64("90th pct Perform Delay", ninetyPct).
Int64("99th pct Perform Delay", ninetyNinePct).
Int64("Max Perform Delay", max).
Float64("Percent Within SLA", pctWithinSLA).
Float64("Percent Reverted", pctReverted).
Msg("Calculated Aggregate Results")
err = keeperReportWriter.Write([]string{
"Contract Index",
"RegistryAddress",
"Contract Address",
"Total Times Eligible",
"Total Performed Upkeeps",
"Average Perform Delay",
"Median Perform Delay",
"90th pct Perform Delay",
"99th pct Perform Delay",
"Largest Perform Delay",
"Percent Within SLA",
})
if err != nil {
return err
}
for contractIndex, report := range k.Reports {
avg, median, ninetyPct, ninetyNinePct, max = IntListStats(report.AllCheckDelays)
err = keeperReportWriter.Write([]string{
fmt.Sprint(contractIndex),
report.RegistryAddress,
report.ContractAddress,
fmt.Sprint(report.TotalEligibleCount),
fmt.Sprint(report.TotalPerformedUpkeeps),
fmt.Sprintf("%.2f", avg),
fmt.Sprint(median),
fmt.Sprint(ninetyPct),
fmt.Sprint(ninetyNinePct),
fmt.Sprint(max),
fmt.Sprintf("%.2f%%", (1.0-float64(report.TotalSLAMissedUpkeeps)/float64(report.TotalEligibleCount))*100),
})
if err != nil {
return err
}
}
keeperReportWriter.Flush()
txs, err := json.Marshal(k.AttemptedChainlinkTransactions)
if err != nil {
return err
}
err = os.WriteFile(k.attemptedTransactionsFile, txs, 0600)
if err != nil {
return err
}
log.Info().Msg("Successfully wrote report on Keeper Benchmark")
k.Summary.Metrics.Delay = map[string]interface{}{
"mean": avg,
"median": median,
"90p": ninetyPct,
"99p": ninetyNinePct,
"max": max,
}
k.Summary.Metrics.PercentWithinSLA = pctWithinSLA
k.Summary.Metrics.PercentRevert = pctReverted
k.Summary.Metrics.TotalTimesEligible = totalEligibleCount
k.Summary.Metrics.TotalTimesPerformed = totalPerformed
k.Summary.Metrics.TotalStaleReports = totalStaleReports
k.Summary.Metrics.PercentStale = pctStale
k.Summary.Metrics.AverageActualPerformsPerBlock = float64(totalPerformed) / float64(k.Summary.TestInputs["BlockRange"].(int64))
// TODO: Set test expectations
/* Expect(int64(pctWithinSLA)).Should(BeNumerically(">=", int64(80)), "Expected PercentWithinSLA to be greater than or equal to 80, but got %f", pctWithinSLA)
Expect(int64(pctReverted)).Should(BeNumerically("<=", int64(10)), "Expected PercentRevert to be less than or equal to 10, but got %f", pctReverted)
Expect(k.Summary.Metrics.AverageActualPerformsPerBlock).Should(BeNumerically("~", k.Summary.Load.AverageExpectedPerformsPerBlock, 10), "Expected PercentRevert to be less than 10, but got %f", pctReverted) */
res, err := json.MarshalIndent(k.Summary, "", " ")
if err != nil {
return err
}
err = os.WriteFile(k.keeperSummaryFile, res, 0600)
if err != nil {
return err
}
log.Info().Str("Summary", string(res)).Msg("Successfully wrote summary on Keeper Benchmark")
return nil
}
// SendSlackNotification sends a slack notification on the results of the test
func (k *KeeperBenchmarkTestReporter) SendSlackNotification(t *testing.T, slackClient *slack.Client, grafanaUrlProvider testreporters.GrafanaURLProvider) error {
if slackClient == nil {
slackClient = slack.New(testreporters.SlackAPIKey)
}
testFailed := t.Failed()
headerText := ":white_check_mark: Automation Benchmark Test FINISHED :white_check_mark:"
if testFailed {
headerText = ":x: Automation Benchmark Test FAILED :x:"
}
messageBlocks := testreporters.CommonSlackNotificationBlocks(
headerText, k.namespace, k.keeperReportFile,
)
ts, err := testreporters.SendSlackMessage(slackClient, slack.MsgOptionBlocks(messageBlocks...))
if err != nil {
return err
}
grafanaUrl, err := grafanaUrlProvider.GetGrafanaBaseURL()
if err != nil {
return err
}
dashboardUrl, err := grafanaUrlProvider.GetGrafanaDashboardURL()
if err != nil {
return err
}
formattedDashboardUrl := fmt.Sprintf("%s%s?from=%d&to=%d&var-namespace=%s&var-cl_node=chainlink-0-0", grafanaUrl, dashboardUrl, k.Summary.StartTime, k.Summary.EndTime, k.namespace)
log.Info().Str("Dashboard", formattedDashboardUrl).Msg("Dashboard URL")
if err := testreporters.UploadSlackFile(slackClient, slack.FileUploadParameters{
Title: fmt.Sprintf("Automation Benchmark Test Summary %s", k.namespace),
Filetype: "json",
Filename: fmt.Sprintf("automation_benchmark_summary_%s.json", k.namespace),
File: k.keeperSummaryFile,
InitialComment: fmt.Sprintf("Automation Benchmark Test Summary %s.\n<%s|Test Dashboard> ", k.namespace, formattedDashboardUrl),
Channels: []string{testreporters.SlackChannel},
ThreadTimestamp: ts,
}); err != nil {
return err
}
if err := testreporters.UploadSlackFile(slackClient, slack.FileUploadParameters{
Title: fmt.Sprintf("Automation Benchmark Test Report %s", k.namespace),
Filetype: "csv",
Filename: fmt.Sprintf("automation_benchmark_report_%s.csv", k.namespace),
File: k.keeperReportFile,
InitialComment: fmt.Sprintf("Automation Benchmark Test Report %s", k.namespace),
Channels: []string{testreporters.SlackChannel},
ThreadTimestamp: ts,
}); err != nil {
return err
}
return testreporters.UploadSlackFile(slackClient, slack.FileUploadParameters{
Title: fmt.Sprintf("Automation Benchmark Attempted Chainlink Txs %s", k.namespace),
Filetype: "json",
Filename: fmt.Sprintf("attempted_cl_txs_%s.json", k.namespace),
File: k.attemptedTransactionsFile,
InitialComment: fmt.Sprintf("Automation Benchmark Attempted Txs %s", k.namespace),
Channels: []string{testreporters.SlackChannel},
ThreadTimestamp: ts,
})
}
// intListStats helper calculates some statistics on an int list: avg, median, 90pct, 99pct, max
//
//nolint:revive
func IntListStats(in []int64) (float64, int64, int64, int64, int64) {
length := len(in)
if length == 0 {
return 0, 0, 0, 0, 0
}
sort.Slice(in, func(i, j int) bool { return in[i] < in[j] })
var sum int64
for _, num := range in {
sum += num
}
return float64(sum) / float64(length), in[int(math.Floor(float64(length)*0.5))], in[int(math.Floor(float64(length)*0.9))], in[int(math.Floor(float64(length)*0.99))], in[length-1]
}