2016-12-14 23:30:29 +00:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-12-14 23:30:29 +00:00
|
|
|
|
|
|
|
package weakhash
|
|
|
|
|
|
|
|
import (
|
2019-02-25 09:29:31 +00:00
|
|
|
"bytes"
|
2018-07-04 07:07:33 +00:00
|
|
|
"context"
|
2019-02-25 09:29:31 +00:00
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
vadler32 "hash/adler32"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
2016-12-14 23:30:29 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2017-01-04 21:04:13 +00:00
|
|
|
|
|
|
|
"github.com/chmduquesne/rollinghash/adler32"
|
2018-05-05 08:24:44 +00:00
|
|
|
"github.com/chmduquesne/rollinghash/bozo32"
|
|
|
|
"github.com/chmduquesne/rollinghash/buzhash32"
|
|
|
|
"github.com/chmduquesne/rollinghash/buzhash64"
|
2016-12-14 23:30:29 +00:00
|
|
|
)
|
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
const testFile = "../model/testdata/tmpfile"
|
2017-01-04 21:04:13 +00:00
|
|
|
const size = 128 << 10
|
2016-12-14 23:30:29 +00:00
|
|
|
|
|
|
|
func BenchmarkFind1MFile(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(1 << 20)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
fd, err := os.Open(testFile)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2018-07-04 07:07:33 +00:00
|
|
|
_, err = Find(context.Background(), fd, []uint32{0, 1, 2}, size)
|
2016-12-14 23:30:29 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
}
|
|
|
|
}
|
2017-01-04 21:04:13 +00:00
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
type RollingHash interface {
|
|
|
|
hash.Hash
|
|
|
|
Roll(byte)
|
2018-05-05 08:24:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
func BenchmarkBlock(b *testing.B) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
hash hash.Hash
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"adler32", adler32.New(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bozo32", bozo32.New(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"buzhash32", buzhash32.New(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"buzhash64", buzhash64.New(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"vanilla-adler32", vadler32.New(),
|
|
|
|
},
|
2018-05-05 08:24:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
sizes := []int64{128 << 10, 16 << 20}
|
|
|
|
|
|
|
|
buf := make([]byte, 16<<20)
|
|
|
|
rand.Read(buf)
|
|
|
|
|
|
|
|
for _, testSize := range sizes {
|
|
|
|
for _, test := range tests {
|
|
|
|
b.Run(test.name+"-"+fmt.Sprint(testSize), func(bb *testing.B) {
|
|
|
|
bb.Run("", func(bbb *testing.B) {
|
|
|
|
bbb.ResetTimer()
|
|
|
|
for i := 0; i < bbb.N; i++ {
|
|
|
|
lr := io.LimitReader(bytes.NewReader(buf), testSize)
|
|
|
|
n, err := io.Copy(test.hash, lr)
|
|
|
|
if err != nil {
|
|
|
|
bbb.Error(err)
|
|
|
|
}
|
|
|
|
if n != testSize {
|
|
|
|
bbb.Errorf("%d != %d", n, testSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
test.hash.Sum(nil)
|
|
|
|
test.hash.Reset()
|
|
|
|
}
|
|
|
|
|
2020-03-29 15:07:25 +00:00
|
|
|
bbb.SetBytes(testSize)
|
2019-02-25 09:29:31 +00:00
|
|
|
bbb.ReportAllocs()
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
2018-05-05 08:24:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
func BenchmarkRoll(b *testing.B) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
hash RollingHash
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"adler32", adler32.New(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bozo32", bozo32.New(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"buzhash32", buzhash32.New(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"buzhash64", buzhash64.New(),
|
|
|
|
},
|
2018-05-05 08:24:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
sizes := []int64{128 << 10, 16 << 20}
|
2018-05-05 08:24:44 +00:00
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
for _, testSize := range sizes {
|
|
|
|
for _, test := range tests {
|
|
|
|
b.Run(test.name+"-"+fmt.Sprint(testSize), func(bb *testing.B) {
|
|
|
|
bb.Run("", func(bbb *testing.B) {
|
|
|
|
data := make([]byte, testSize)
|
2018-05-05 08:24:44 +00:00
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
if _, err := test.hash.Write(data); err != nil {
|
|
|
|
bbb.Error(err)
|
|
|
|
}
|
2018-05-05 08:24:44 +00:00
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
bbb.ResetTimer()
|
2018-05-05 08:24:44 +00:00
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
for i := 0; i < bbb.N; i++ {
|
|
|
|
for j := int64(0); j <= testSize; j++ {
|
|
|
|
test.hash.Roll('a')
|
|
|
|
}
|
|
|
|
}
|
2018-05-05 08:24:44 +00:00
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
bbb.SetBytes(testSize)
|
|
|
|
bbb.ReportAllocs()
|
|
|
|
})
|
2018-05-05 08:24:44 +00:00
|
|
|
|
2019-02-25 09:29:31 +00:00
|
|
|
})
|
2018-05-05 08:24:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|