diff --git a/lib/scanner/.gitignore b/lib/scanner/.gitignore new file mode 100644 index 000000000..46765e06e --- /dev/null +++ b/lib/scanner/.gitignore @@ -0,0 +1 @@ +_random.data diff --git a/lib/scanner/walk_test.go b/lib/scanner/walk_test.go index 45cc0e31b..689f3fc57 100644 --- a/lib/scanner/walk_test.go +++ b/lib/scanner/walk_test.go @@ -8,13 +8,16 @@ package scanner import ( "bytes" + "crypto/rand" "fmt" + "io" "os" "path/filepath" "reflect" "runtime" rdebug "runtime/debug" "sort" + "sync" "testing" "github.com/syncthing/syncthing/lib/ignore" @@ -372,3 +375,39 @@ func TestSymlinkTypeEqual(t *testing.T) { } } } + +var initOnce sync.Once + +const ( + testdataSize = 17 << 20 + testdataName = "_random.data" +) + +func BenchmarkHashFile(b *testing.B) { + initOnce.Do(initTestFile) + b.ResetTimer() + + for i := 0; i < b.N; i++ { + if _, err := HashFile(testdataName, protocol.BlockSize, testdataSize, nil); err != nil { + b.Fatal(err) + } + } + + b.ReportAllocs() +} + +func initTestFile() { + fd, err := os.Create(testdataName) + if err != nil { + panic(err) + } + + lr := io.LimitReader(rand.Reader, testdataSize) + if _, err := io.Copy(fd, lr); err != nil { + panic(err) + } + + if err := fd.Close(); err != nil { + panic(err) + } +}