Skip to content

Commit 861aa78

Browse files
committed
app/vlagent/kubernetescollector: move logfile benchmarks to a separate *_timing.go file
1 parent e6e8b0d commit 861aa78

File tree

2 files changed

+63
-56
lines changed

2 files changed

+63
-56
lines changed

app/vlagent/kubernetescollector/logfile_test.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -141,59 +141,3 @@ func writeToFile(t testing.TB, f *os.File, data string) {
141141
t.Fatalf("failed to write to file: %s", err)
142142
}
143143
}
144-
145-
func BenchmarkReadLinesBigSizeLines(b *testing.B) {
146-
// 10 MiB per iteration: 1024 bytes per line (including newline), 10_240 lines.
147-
benchmarkReadLines(b, 1023, 10_240)
148-
}
149-
150-
func BenchmarkReadLinesMediumSizeLines(b *testing.B) {
151-
// 10 MiB per iteration: 512 bytes per line (including newline), 20_480 lines.
152-
benchmarkReadLines(b, 511, 20_480)
153-
}
154-
155-
func BenchmarkReadLinesShortSizeLines(b *testing.B) {
156-
// 10 MiB per iteration: 32 bytes per line (including newline), 327_680 lines.
157-
benchmarkReadLines(b, 31, 327_680)
158-
}
159-
160-
func benchmarkReadLines(b *testing.B, lineLen, count int) {
161-
logFilePath := filepath.Join(b.TempDir(), "test.log")
162-
line := strings.Repeat("a", lineLen)
163-
var lines []string
164-
for i := 0; i < count; i++ {
165-
lines = append(lines, line)
166-
}
167-
writeLinesToFile(b, logFilePath, lines...)
168-
169-
// Total bytes processed per iteration (includes newline).
170-
totalBytes := int64((lineLen + 1) * count)
171-
b.SetBytes(totalBytes)
172-
b.ReportAllocs()
173-
174-
proc := noopLogFileHandler{}
175-
176-
stopCh := make(chan struct{})
177-
b.RunParallel(func(pb *testing.PB) {
178-
lf := newLogFile(logFilePath)
179-
defer lf.close()
180-
181-
for pb.Next() {
182-
lf.readLines(stopCh, proc)
183-
if lf.offset != totalBytes {
184-
b.Fatalf("unexpected offset; got %d; want %d", lf.offset, totalBytes)
185-
}
186-
187-
// Reset state to re-read the file from the beginning in the next iteration.
188-
lf.setOffset(0)
189-
}
190-
})
191-
}
192-
193-
type noopLogFileHandler struct{}
194-
195-
func (noopLogFileHandler) tryAddLine(_ []byte) bool {
196-
return true
197-
}
198-
199-
func (noopLogFileHandler) mustClose() {}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package kubernetescollector
2+
3+
import (
4+
"path/filepath"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func BenchmarkReadLinesBigSizeLines(b *testing.B) {
10+
// 10 MiB per iteration: 1024 bytes per line (including newline), 10_240 lines.
11+
benchmarkReadLines(b, 1023, 10_240)
12+
}
13+
14+
func BenchmarkReadLinesMediumSizeLines(b *testing.B) {
15+
// 10 MiB per iteration: 512 bytes per line (including newline), 20_480 lines.
16+
benchmarkReadLines(b, 511, 20_480)
17+
}
18+
19+
func BenchmarkReadLinesShortSizeLines(b *testing.B) {
20+
// 10 MiB per iteration: 32 bytes per line (including newline), 327_680 lines.
21+
benchmarkReadLines(b, 31, 327_680)
22+
}
23+
24+
func benchmarkReadLines(b *testing.B, lineLen, count int) {
25+
logFilePath := filepath.Join(b.TempDir(), "test.log")
26+
line := strings.Repeat("a", lineLen)
27+
var lines []string
28+
for i := 0; i < count; i++ {
29+
lines = append(lines, line)
30+
}
31+
writeLinesToFile(b, logFilePath, lines...)
32+
33+
// Total bytes processed per iteration (includes newline).
34+
totalBytes := int64((lineLen + 1) * count)
35+
b.SetBytes(totalBytes)
36+
b.ReportAllocs()
37+
38+
proc := noopLogFileHandler{}
39+
40+
stopCh := make(chan struct{})
41+
b.RunParallel(func(pb *testing.PB) {
42+
lf := newLogFile(logFilePath)
43+
defer lf.close()
44+
45+
for pb.Next() {
46+
lf.readLines(stopCh, proc)
47+
if lf.offset != totalBytes {
48+
b.Fatalf("unexpected offset; got %d; want %d", lf.offset, totalBytes)
49+
}
50+
51+
// Reset state to re-read the file from the beginning in the next iteration.
52+
lf.setOffset(0)
53+
}
54+
})
55+
}
56+
57+
type noopLogFileHandler struct{}
58+
59+
func (noopLogFileHandler) tryAddLine(_ []byte) bool {
60+
return true
61+
}
62+
63+
func (noopLogFileHandler) mustClose() {}

0 commit comments

Comments
 (0)