Skip to content

Instantly share code, notes, and snippets.

@eadanfahey
Created May 28, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eadanfahey/ce2ba2733028e2b3b62a479ba2b9f62a to your computer and use it in GitHub Desktop.
Save eadanfahey/ce2ba2733028e2b3b62a479ba2b9f62a to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"io"
"math/rand"
"testing"
"github.com/iotafs/fastcdc-go"
"github.com/restic/chunker"
)
// 100MiB of random data
var benchData = randBytes(100*1024*1024, 345)
// loopReader implements io.Reader, looping over a given buffer a specified
// number of times.
type loopReader struct {
n int
data []byte
r *bytes.Reader
i int
}
func (lr *loopReader) Read(b []byte) (int, error) {
n, err := lr.r.Read(b)
if err == io.EOF && lr.i < lr.n {
lr.i++
lr.r.Reset(lr.data)
return n, nil
}
return n, err
}
func newLoopReader(data []byte, n int) *loopReader {
return &loopReader{n, data, bytes.NewReader(data), 0}
}
func BenchmarkRestic(b *testing.B) {
n := 100
r := newLoopReader(benchData, n)
b.ResetTimer()
pol := chunker.Pol(0x3DA3358B4DC173)
cnkr := chunker.New(r, pol)
buf := make([]byte, 8*1024*1024)
for {
_, err := cnkr.Next(buf)
if err == io.EOF {
break
}
}
b.SetBytes(int64(n * len(benchData)))
}
func BenchmarkFastCDC(b *testing.B) {
n := 100
r := newLoopReader(benchData, n)
b.ResetTimer()
cnkr, err := fastcdc.NewChunker(r, fastcdc.Options{
MinSize: 512 * 1024,
MaxSize: 8 * 1024 * 1024,
NormalSize: 1 * 1024 * 1024,
SmallBits: 21,
LargeBits: 19,
})
if err != nil {
b.Fatal(err)
}
for {
_, err := cnkr.Next()
if err == io.EOF {
break
}
}
b.SetBytes(int64(n * len(benchData)))
}
func randBytes(n int, seed int64) []byte {
rnd := rand.New(rand.NewSource(seed))
b := make([]byte, n)
rnd.Read(b)
return b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment