|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math" |
| 7 | + "strconv" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/smartcontractkit/chainlink-testing-framework/wasp" |
| 15 | + "github.com/smartcontractkit/chainlink-testing-framework/wasp/benchspy" |
| 16 | +) |
| 17 | + |
| 18 | +// both should give the same results |
| 19 | +func TestBenchSpy_Standard_Direct_And_Loki_Metrics(t *testing.T) { |
| 20 | + // this test requires CTFv2 node_set with observability stack to be running |
| 21 | + |
| 22 | + label := "benchspy-direct-loki" |
| 23 | + |
| 24 | + gen, err := wasp.NewGenerator(&wasp.Config{ |
| 25 | + T: t, |
| 26 | + GenName: "vu", |
| 27 | + CallTimeout: 100 * time.Millisecond, |
| 28 | + LoadType: wasp.VU, |
| 29 | + Schedule: wasp.Plain(1, 10*time.Second), |
| 30 | + VU: wasp.NewMockVU(&wasp.MockVirtualUserConfig{ |
| 31 | + CallSleep: 50 * time.Millisecond, |
| 32 | + }), |
| 33 | + Labels: map[string]string{ |
| 34 | + "branch": label, |
| 35 | + "commit": label, |
| 36 | + }, |
| 37 | + LokiConfig: wasp.NewEnvLokiConfig(), |
| 38 | + }) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + gen.Run(true) |
| 42 | + |
| 43 | + baseLineReport, err := benchspy.NewStandardReport( |
| 44 | + "91ee9e3c903d52de12f3d0c1a07ac3c2a6d141fb", |
| 45 | + benchspy.WithStandardQueries(benchspy.StandardQueryExecutor_Direct, benchspy.StandardQueryExecutor_Loki), |
| 46 | + benchspy.WithGenerators(gen), |
| 47 | + ) |
| 48 | + require.NoError(t, err, "failed to create original report") |
| 49 | + |
| 50 | + fetchCtx, cancelFn := context.WithTimeout(context.Background(), 60*time.Second) |
| 51 | + defer cancelFn() |
| 52 | + |
| 53 | + fetchErr := baseLineReport.FetchData(fetchCtx) |
| 54 | + require.NoError(t, fetchErr, "failed to fetch current report") |
| 55 | + |
| 56 | + currentAsLokiSlices := benchspy.MustAllLokiResults(baseLineReport) |
| 57 | + currentAsDirectFloats := benchspy.MustAllDirectResults(baseLineReport) |
| 58 | + |
| 59 | + require.NotEmpty(t, currentAsLokiSlices[string(benchspy.MedianLatency)], "%s results were missing for loki", string(benchspy.MedianLatency)) |
| 60 | + require.NotEmpty(t, currentAsDirectFloats[string(benchspy.MedianLatency)], "%s results were missing for direct", string(benchspy.MedianLatency)) |
| 61 | + |
| 62 | + var compareValues = func(t *testing.T, metricName string, lokiFloat, directFloat, maxDiffPrecentage float64) { |
| 63 | + var diffPrecentage float64 |
| 64 | + if lokiFloat != 0.0 && directFloat != 0.0 { |
| 65 | + diffPrecentage = (directFloat - lokiFloat) / lokiFloat * 100 |
| 66 | + } else if lokiFloat == 0.0 && directFloat == 0.0 { |
| 67 | + diffPrecentage = 0.0 |
| 68 | + } else { |
| 69 | + diffPrecentage = 100.0 |
| 70 | + } |
| 71 | + assert.LessOrEqual(t, math.Abs(diffPrecentage), maxDiffPrecentage, "%s are more than 1% different", metricName, fmt.Sprintf("%.4f", diffPrecentage)) |
| 72 | + } |
| 73 | + |
| 74 | + lokiFloatSlice, err := benchspy.StringSliceToFloat64Slice(currentAsLokiSlices[string(benchspy.MedianLatency)]) |
| 75 | + require.NoError(t, err, "failed to convert %s results to float64 slice", string(benchspy.MedianLatency)) |
| 76 | + lokiMedian := benchspy.CalculatePercentile(lokiFloatSlice, 0.5) |
| 77 | + |
| 78 | + compareValues(t, string(benchspy.MedianLatency), lokiMedian, currentAsDirectFloats[string(benchspy.MedianLatency)], 1.0) |
| 79 | + |
| 80 | + lokip95 := benchspy.CalculatePercentile(lokiFloatSlice, 0.95) |
| 81 | + // here the max diff is 1.5% because of higher impact of data aggregation in loki |
| 82 | + compareValues(t, string(benchspy.Percentile95Latency), lokip95, currentAsDirectFloats[string(benchspy.Percentile95Latency)], 1.5) |
| 83 | + |
| 84 | + lokiErrorRate := 0 |
| 85 | + for _, v := range currentAsLokiSlices[string(benchspy.ErrorRate)] { |
| 86 | + asInt, err := strconv.Atoi(v) |
| 87 | + require.NoError(t, err) |
| 88 | + lokiErrorRate += int(asInt) |
| 89 | + } |
| 90 | + |
| 91 | + lokiErrorRate = lokiErrorRate / len(currentAsLokiSlices[string(benchspy.ErrorRate)]) |
| 92 | + compareValues(t, string(benchspy.ErrorRate), float64(lokiErrorRate), currentAsDirectFloats[string(benchspy.ErrorRate)], 1.0) |
| 93 | +} |
0 commit comments