mirror of
https://github.com/octoleo/restic.git
synced 2024-11-29 16:23:59 +00:00
Improve PackerManager benchmark
The previous benchmark spent much of its time allocating RNGs and generating too many random numbers. It now spends 90% of its time hashing and half of the rest writing to files. name old time/op new time/op delta PackerManager-8 319ms ± 1% 247ms ± 1% -22.48% (p=0.000 n=20+18) name old speed new speed delta PackerManager-8 143MB/s ± 1% 213MB/s ± 1% +48.63% (p=0.000 n=10+18) name old alloc/op new alloc/op delta PackerManager-8 635kB ± 0% 92kB ± 0% -85.48% (p=0.000 n=10+19) name old allocs/op new allocs/op delta PackerManager-8 1.64k ± 0% 1.43k ± 0% -12.76% (p=0.000 n=10+20)
This commit is contained in:
parent
b7c3039eb2
commit
b592614061
@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/backend/mem"
|
"github.com/restic/restic/internal/backend/mem"
|
||||||
@ -25,6 +26,13 @@ func randomID(rd io.Reader) restic.ID {
|
|||||||
|
|
||||||
const maxBlobSize = 1 << 20
|
const maxBlobSize = 1 << 20
|
||||||
|
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func saveFile(t testing.TB, be Saver, length int, f *os.File, id restic.ID) {
|
func saveFile(t testing.TB, be Saver, length int, f *os.File, id restic.ID) {
|
||||||
h := restic.Handle{Type: restic.DataFile, Name: id.String()}
|
h := restic.Handle{Type: restic.DataFile, Name: id.String()}
|
||||||
t.Logf("save file %v", h)
|
t.Logf("save file %v", h)
|
||||||
@ -50,21 +58,17 @@ func saveFile(t testing.TB, be Saver, length int, f *os.File, id restic.ID) {
|
|||||||
|
|
||||||
func fillPacks(t testing.TB, rnd *rand.Rand, be Saver, pm *packerManager, buf []byte) (bytes int) {
|
func fillPacks(t testing.TB, rnd *rand.Rand, be Saver, pm *packerManager, buf []byte) (bytes int) {
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 100; i++ {
|
||||||
l := rnd.Intn(1 << 20)
|
l := rnd.Intn(maxBlobSize)
|
||||||
seed := rnd.Int63()
|
|
||||||
|
|
||||||
packer, err := pm.findPacker()
|
packer, err := pm.findPacker()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
rd := rand.New(rand.NewSource(seed))
|
id := randomID(rnd)
|
||||||
id := randomID(rd)
|
|
||||||
buf = buf[:l]
|
buf = buf[:l]
|
||||||
_, err = io.ReadFull(rd, buf)
|
// Only change a few bytes so we know we're not benchmarking the RNG.
|
||||||
if err != nil {
|
rnd.Read(buf[:min(l, 4)])
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
n, err := packer.Add(restic.DataBlob, id, buf)
|
n, err := packer.Add(restic.DataBlob, id, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -109,8 +113,20 @@ func flushRemainingPacks(t testing.TB, be Saver, pm *packerManager) (bytes int)
|
|||||||
return bytes
|
return bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const randomSeed = 23
|
||||||
|
|
||||||
|
var (
|
||||||
|
once sync.Once
|
||||||
|
totalSize int64
|
||||||
|
)
|
||||||
|
|
||||||
func TestPackerManager(t *testing.T) {
|
func TestPackerManager(t *testing.T) {
|
||||||
rnd := rand.New(rand.NewSource(23))
|
bytes := testPackerManager(t)
|
||||||
|
once.Do(func() { totalSize = bytes })
|
||||||
|
}
|
||||||
|
|
||||||
|
func testPackerManager(t testing.TB) int64 {
|
||||||
|
rnd := rand.New(rand.NewSource(randomSeed))
|
||||||
|
|
||||||
be := mem.New()
|
be := mem.New()
|
||||||
pm := newPackerManager(be, crypto.NewRandomKey())
|
pm := newPackerManager(be, crypto.NewRandomKey())
|
||||||
@ -121,23 +137,30 @@ func TestPackerManager(t *testing.T) {
|
|||||||
bytes += flushRemainingPacks(t, be, pm)
|
bytes += flushRemainingPacks(t, be, pm)
|
||||||
|
|
||||||
t.Logf("saved %d bytes", bytes)
|
t.Logf("saved %d bytes", bytes)
|
||||||
|
return int64(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkPackerManager(t *testing.B) {
|
func BenchmarkPackerManager(t *testing.B) {
|
||||||
rnd := rand.New(rand.NewSource(23))
|
// Run testPackerManager if it hasn't run already, to set totalSize.
|
||||||
|
once.Do(func() {
|
||||||
|
totalSize = testPackerManager(t)
|
||||||
|
})
|
||||||
|
|
||||||
|
rnd := rand.New(rand.NewSource(randomSeed))
|
||||||
|
|
||||||
be := &mock.Backend{
|
be := &mock.Backend{
|
||||||
SaveFn: func(context.Context, restic.Handle, restic.RewindReader) error { return nil },
|
SaveFn: func(context.Context, restic.Handle, restic.RewindReader) error { return nil },
|
||||||
}
|
}
|
||||||
blobBuf := make([]byte, maxBlobSize)
|
blobBuf := make([]byte, maxBlobSize)
|
||||||
|
|
||||||
|
t.ReportAllocs()
|
||||||
|
t.SetBytes(totalSize)
|
||||||
t.ResetTimer()
|
t.ResetTimer()
|
||||||
|
|
||||||
for i := 0; i < t.N; i++ {
|
for i := 0; i < t.N; i++ {
|
||||||
bytes := 0
|
rnd.Seed(randomSeed)
|
||||||
pm := newPackerManager(be, crypto.NewRandomKey())
|
pm := newPackerManager(be, crypto.NewRandomKey())
|
||||||
bytes += fillPacks(t, rnd, be, pm, blobBuf)
|
fillPacks(t, rnd, be, pm, blobBuf)
|
||||||
bytes += flushRemainingPacks(t, be, pm)
|
flushRemainingPacks(t, be, pm)
|
||||||
t.Logf("saved %d bytes", bytes)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user