Skip to content

Commit caf0f09

Browse files
committed
Add benchmark
1 parent 18cebe4 commit caf0f09

File tree

1 file changed

+36
-5
lines changed
  • internal/component/loki/source/file/internal/tail

1 file changed

+36
-5
lines changed

internal/component/loki/source/file/internal/tail/file_test.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tail
22

33
import (
4+
"bytes"
45
"compress/gzip"
56
"compress/zlib"
67
"context"
@@ -472,9 +473,9 @@ func compressionTest(t *testing.T, name, compression string, enc *encoding.Encod
472473
})
473474
}
474475

475-
func createFile(t *testing.T, name, content string) string {
476-
path := t.TempDir() + "/" + name
477-
require.NoError(t, os.WriteFile(path, []byte(content), 0600))
476+
func createFile(tb testing.TB, name, content string) string {
477+
path := tb.TempDir() + "/" + name
478+
require.NoError(tb, os.WriteFile(path, []byte(content), 0600))
478479
return path
479480
}
480481

@@ -494,8 +495,8 @@ func truncateFile(t *testing.T, name, content string) {
494495
require.NoError(t, err)
495496
}
496497

497-
func removeFile(t *testing.T, name string) {
498-
require.NoError(t, os.Remove(name))
498+
func removeFile(tb testing.TB, name string) {
499+
require.NoError(tb, os.Remove(name))
499500
}
500501

501502
func rotateFile(t *testing.T, name, newContent string) {
@@ -548,3 +549,33 @@ func verifyResult(t *testing.T, f *File, expectedLine *Line, expectedErr error)
548549
require.Equal(t, expectedLine.Offset, line.Offset)
549550
}
550551
}
552+
553+
var benchLine *Line
554+
555+
func BenchmarkFile(b *testing.B) {
556+
// we create a file with 1000 lines and each line is 500 bytes
557+
line := bytes.Repeat([]byte{'a'}, 500)
558+
lines := strings.Repeat(string(line)+"\n", 1000)
559+
name := createFile(b, "benchfile", lines)
560+
defer removeFile(b, name)
561+
562+
b.ReportAllocs()
563+
564+
for b.Loop() {
565+
file, err := NewFile(log.NewNopLogger(), &Config{
566+
Filename: name,
567+
WatcherConfig: WatcherConfig{},
568+
})
569+
require.NoError(b, err)
570+
// we set EOF here so tailer will stop after we have consumed the whole file
571+
file.waitAtEOF = false
572+
573+
for {
574+
var err error
575+
benchLine, err = file.Next()
576+
if errors.Is(err, io.EOF) {
577+
break
578+
}
579+
}
580+
}
581+
}

0 commit comments

Comments
 (0)