diff --git a/filebeat/input/filestream/fswatch.go b/filebeat/input/filestream/fswatch.go index 371465e9b35..0c14bcee8ce 100644 --- a/filebeat/input/filestream/fswatch.go +++ b/filebeat/input/filestream/fswatch.go @@ -18,6 +18,7 @@ package filestream import ( + "bufio" "crypto/sha256" "encoding/hex" "fmt" @@ -453,7 +454,6 @@ func (s *fileScanner) toFileDescriptor(it *ingestTarget) (fd loginp.FileDescript return fd, fmt.Errorf("filesize of %q is %d bytes, expected at least %d bytes for fingerprinting", fd.Filename, fileSize, minSize) } - h := sha256.New() file, err := os.Open(it.originalFilename) if err != nil { return fd, fmt.Errorf("failed to open %q for fingerprinting: %w", it.originalFilename, err) @@ -467,9 +467,10 @@ func (s *fileScanner) toFileDescriptor(it *ingestTarget) (fd loginp.FileDescript } } - r := io.LimitReader(file, s.cfg.Fingerprint.Length) - buf := make([]byte, h.BlockSize()) - written, err := io.CopyBuffer(h, r, buf) + bfile := bufio.NewReaderSize(file, int(s.cfg.Fingerprint.Length)) + r := io.LimitReader(bfile, s.cfg.Fingerprint.Length) + h := sha256.New() + written, err := io.Copy(h, r) if err != nil { return fd, fmt.Errorf("failed to compute hash for first %d bytes of %q: %w", s.cfg.Fingerprint.Length, fd.Filename, err) } diff --git a/filebeat/input/filestream/fswatch_test.go b/filebeat/input/filestream/fswatch_test.go index 90f2f594c4c..4a05eecdb28 100644 --- a/filebeat/input/filestream/fswatch_test.go +++ b/filebeat/input/filestream/fswatch_test.go @@ -757,3 +757,31 @@ func filenames(m map[string]loginp.FileDescriptor) (result string) { } return result } + +func BenchmarkToFileDescriptor(b *testing.B) { + dir := b.TempDir() + basename := "created.log" + filename := filepath.Join(dir, basename) + err := os.WriteFile(filename, []byte(strings.Repeat("a", 1024)), 0777) + require.NoError(b, err) + + s := fileScanner{ + paths: []string{filename}, + cfg: fileScannerConfig{ + Fingerprint: fingerprintConfig{ + Enabled: true, + Offset: 0, + Length: 1024, + }, + }, + } + + it, err := s.getIngestTarget(filename) + require.NoError(b, err) + + for i := 0; i < b.N; i++ { + fd, err := s.toFileDescriptor(&it) + require.NoError(b, err) + require.Equal(b, "2edc986847e209b4016e141a6dc8716d3207350f416969382d431539bf292e4a", fd.Fingerprint) + } +}