-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_loki_test.go
More file actions
240 lines (214 loc) · 5.75 KB
/
Copy pathbench_loki_test.go
File metadata and controls
240 lines (214 loc) · 5.75 KB
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
package logparser
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"testing"
"time"
)
type lokiQueryResponse struct {
Data struct {
Result []struct {
Values [][]string `json:"values"`
} `json:"result"`
} `json:"data"`
}
// fetchLokiLogs queries Loki for recent logs from a pod.
func fetchLokiLogs(lokiURL, podName string, limit int) ([]string, error) {
query := fmt.Sprintf(`{pod=~"%s.*"}`, podName)
params := url.Values{
"query": {query},
"limit": {fmt.Sprintf("%d", limit)},
}
reqURL := fmt.Sprintf("%s/loki/api/v1/query_range?%s&start=%d&end=%d",
lokiURL, params.Encode(),
time.Now().Add(-1*time.Hour).UnixNano(),
time.Now().UnixNano(),
)
resp, err := http.Get(reqURL)
if err != nil {
return nil, fmt.Errorf("loki request failed: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading response: %w", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("loki returned %d: %s", resp.StatusCode, string(body[:minInt(len(body), 200)]))
}
var result lokiQueryResponse
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
var lines []string
for _, stream := range result.Data.Result {
for _, entry := range stream.Values {
if len(entry) >= 2 {
lines = append(lines, entry[1])
}
}
}
return lines, nil
}
// TestLokiBenchmark fetches real logs from Loki and benchmarks sensitive data detection.
// Run with: go test -v -run TestLokiBenchmark -timeout 60s -count=1
// Configure via env vars:
//
// LOKI_URL — Loki base URL (default: http://localhost:3100)
// LOKI_POD — pod name prefix to query (default: example-pod)
//
// The test is skipped if Loki is unreachable, so it stays a no-op in CI.
func TestLokiBenchmark(t *testing.T) {
lokiURL := os.Getenv("LOKI_URL")
if lokiURL == "" {
lokiURL = "http://localhost:3100"
}
podName := os.Getenv("LOKI_POD")
if podName == "" {
podName = "example-pod"
}
logLimit := 5000
t.Logf("Fetching up to %d logs from Loki for pod %s...", logLimit, podName)
lines, err := fetchLokiLogs(lokiURL, podName, logLimit)
if err != nil {
t.Skipf("Skipping Loki benchmark (Loki not available): %v", err)
return
}
if len(lines) == 0 {
t.Skipf("No logs found for pod %s", podName)
return
}
t.Logf("Fetched %d log lines", len(lines))
// Show sample lines
for i, line := range lines {
if i >= 3 {
break
}
if len(line) > 120 {
line = line[:120] + "..."
}
t.Logf(" Sample[%d]: %s", i, line)
}
// Load patterns at each confidence level
configs := []struct {
name string
confidence string
}{
{"high-only", "high"},
{"medium+high", "medium"},
{"all (low+medium+high)", "low"},
}
for _, cfg := range configs {
patterns, err := LoadPatterns(cfg.confidence)
if err != nil {
t.Fatalf("LoadPatterns(%s): %v", cfg.confidence, err)
}
t.Logf("\n=== %s (%d patterns) ===", cfg.name, len(patterns))
// Benchmark: time all lines
start := time.Now()
totalMatches := 0
matchNames := map[string]int{}
for _, line := range lines {
matches := DetectSensitiveData(line, "bench", patterns)
totalMatches += len(matches)
for _, m := range matches {
matchNames[m.name]++
}
}
elapsed := time.Since(start)
perLine := elapsed / time.Duration(len(lines))
linesPerSec := float64(len(lines)) / elapsed.Seconds()
t.Logf(" Lines: %d", len(lines))
t.Logf(" Total time: %v", elapsed)
t.Logf(" Per line: %v", perLine)
t.Logf(" Lines/sec: %.0f", linesPerSec)
t.Logf(" Matches: %d", totalMatches)
if totalMatches > 0 {
t.Logf(" Match breakdown:")
for name, count := range matchNames {
t.Logf(" %s: %d", name, count)
}
// Show sample matched lines
t.Logf(" Sample matches:")
shown := 0
for _, line := range lines {
matches := DetectSensitiveData(line, "bench", patterns)
if len(matches) > 0 {
sample := line
if len(sample) > 150 {
sample = sample[:150] + "..."
}
t.Logf(" [%s] %s", matches[0].name, sample)
shown++
if shown >= 5 {
break
}
}
}
}
// Compare with old-style (no pre-filter)
startOld := time.Now()
for _, line := range lines {
for j := range patterns {
if patterns[j].Pattern.MatchString(line) {
_ = patterns[j].Pattern.FindString(line)
break
}
}
}
elapsedOld := time.Since(startOld)
speedup := float64(elapsedOld) / float64(elapsed)
t.Logf(" Old style: %v (%.1fx slower)", elapsedOld, speedup)
}
// Test with sampling
t.Logf("\n=== Sampling benchmark (medium confidence, 1-in-100) ===")
patterns, _ := LoadPatterns("medium")
start := time.Now()
sampled := 0
for i, line := range lines {
if i%100 != 0 {
continue
}
sampled++
DetectSensitiveData(line, "bench", patterns)
}
elapsed := time.Since(start)
t.Logf(" Sampled: %d/%d lines", sampled, len(lines))
t.Logf(" Total time: %v", elapsed)
if sampled > 0 {
t.Logf(" Per sampled: %v", elapsed/time.Duration(sampled))
}
// Estimate throughput at scale
t.Logf("\n=== Throughput estimates ===")
patternsHigh, _ := LoadPatterns("high")
patternsMed, _ := LoadPatterns("medium")
for _, scenario := range []struct {
name string
patterns []PrecompiledPattern
}{
{"high-only", patternsHigh},
{"medium+high", patternsMed},
} {
benchStart := time.Now()
iterations := 0
for time.Since(benchStart) < 2*time.Second {
for _, line := range lines {
DetectSensitiveData(line, "bench", scenario.patterns)
iterations++
}
}
benchElapsed := time.Since(benchStart)
lps := float64(iterations) / benchElapsed.Seconds()
t.Logf(" %s: %.0f lines/sec (%.0f pods @ 100 lines/sec)", scenario.name, lps, lps/100)
}
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}