Browse Source

Add benchmark of HashFile

Jakob Borg 10 years ago
parent
commit
1efd8d6c75
2 changed files with 40 additions and 0 deletions
  1. 1 0
      lib/scanner/.gitignore
  2. 39 0
      lib/scanner/walk_test.go

+ 1 - 0
lib/scanner/.gitignore

@@ -0,0 +1 @@
+_random.data

+ 39 - 0
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)
+	}
+}