Skip to content

Commit

Permalink
Add benchmarks for scanning files (elastic#36078)
Browse files Browse the repository at this point in the history
So, we can compare how much slower the fingerprinting mode against using
`device` and `inode` in the scanner.
  • Loading branch information
rdner authored and Scholar-Li committed Feb 5, 2024
1 parent e324552 commit e68c80f
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions filebeat/input/filestream/fswatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package filestream

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -693,6 +694,62 @@ scanner:
})
}

const benchmarkFileCount = 1000

func BenchmarkGetFiles(b *testing.B) {
dir := b.TempDir()
basenameFormat := "file-%d.log"

for i := 0; i < benchmarkFileCount; i++ {
filename := filepath.Join(dir, fmt.Sprintf(basenameFormat, i))
content := fmt.Sprintf("content-%d\n", i)
err := os.WriteFile(filename, []byte(strings.Repeat(content, 1024)), 0777)
require.NoError(b, err)
}

s := fileScanner{
paths: []string{filepath.Join(dir, "*.log")},
cfg: fileScannerConfig{
Fingerprint: fingerprintConfig{
Enabled: false,
},
},
}

for i := 0; i < b.N; i++ {
files := s.GetFiles()
require.Len(b, files, benchmarkFileCount)
}
}

func BenchmarkGetFilesWithFingerprint(b *testing.B) {
dir := b.TempDir()
basenameFormat := "file-%d.log"

for i := 0; i < benchmarkFileCount; i++ {
filename := filepath.Join(dir, fmt.Sprintf(basenameFormat, i))
content := fmt.Sprintf("content-%d\n", i)
err := os.WriteFile(filename, []byte(strings.Repeat(content, 1024)), 0777)
require.NoError(b, err)
}

s := fileScanner{
paths: []string{filepath.Join(dir, "*.log")},
cfg: fileScannerConfig{
Fingerprint: fingerprintConfig{
Enabled: true,
Offset: 0,
Length: 1024,
},
},
}

for i := 0; i < b.N; i++ {
files := s.GetFiles()
require.Len(b, files, benchmarkFileCount)
}
}

func createWatcherWithConfig(t *testing.T, paths []string, cfgStr string) loginp.FSWatcher {
cfg, err := conf.NewConfigWithYAML([]byte(cfgStr), cfgStr)
require.NoError(t, err)
Expand Down

0 comments on commit e68c80f

Please sign in to comment.