cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/diags/problems_bench_test.go
47lines · modecode
| 1 | package diags |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/cloudflare/pint/internal/output" |
| 8 | ) |
| 9 | |
| 10 | // BenchmarkInjectDiagnosticsLongExpr measures diagnostic injection for a |
| 11 | // long single-line PromQL expression that triggers AST trimming. |
| 12 | func BenchmarkInjectDiagnosticsLongExpr(b *testing.B) { |
| 13 | // 180-byte PromQL line that would need trimming. |
| 14 | input := "expr: sum by (instance) (rate(http_requests_total{job=\"api\",status=~\"5..\"}[5m])) / sum by (instance) (rate(up{job=\"api\"}[5m])) > 0.01\n" |
| 15 | lines := strings.Split(input, "\n") |
| 16 | pos := PositionRanges{{Line: 1, FirstColumn: 7, LastColumn: len(lines[0]) - 1}} |
| 17 | // 4 diagnostics inside the expression. |
| 18 | diags := []Diagnostic{ |
| 19 | {Message: "dead code", Pos: pos, FirstColumn: 10, LastColumn: 20}, |
| 20 | {Message: "dead code", Pos: pos, FirstColumn: 40, LastColumn: 60}, |
| 21 | {Message: "dead code", Pos: pos, FirstColumn: 80, LastColumn: 100}, |
| 22 | {Message: "dead code", Pos: pos, FirstColumn: 120, LastColumn: 140}, |
| 23 | } |
| 24 | |
| 25 | b.ReportAllocs() |
| 26 | b.ResetTimer() |
| 27 | for i := 0; i < b.N; i++ { |
| 28 | _ = InjectDiagnostics(input, diags, output.None) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // BenchmarkInjectDiagnosticsShortExpr measures the fast path where no trimming |
| 33 | // is needed. |
| 34 | func BenchmarkInjectDiagnosticsShortExpr(b *testing.B) { |
| 35 | input := "expr: up == 0\n" |
| 36 | lines := strings.Split(input, "\n") |
| 37 | pos := PositionRanges{{Line: 1, FirstColumn: 7, LastColumn: len(lines[0]) - 1}} |
| 38 | diags := []Diagnostic{ |
| 39 | {Message: "dead code", Pos: pos, FirstColumn: 7, LastColumn: 9}, |
| 40 | } |
| 41 | |
| 42 | b.ReportAllocs() |
| 43 | b.ResetTimer() |
| 44 | for i := 0; i < b.N; i++ { |
| 45 | _ = InjectDiagnostics(input, diags, output.None) |
| 46 | } |
| 47 | } |
| 48 | |