2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-29 16:23:59 +00:00

Remove Go 1.5 compatibility code from PackerManager benchmark

This alone is enough to speed up the benchmark by ~10%.
This commit is contained in:
greatroar 2020-03-05 18:50:55 +01:00
parent 52abec967f
commit b7c3039eb2

View File

@ -14,31 +14,6 @@ import (
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
) )
type randReader struct {
src rand.Source
rand *rand.Rand
}
func newRandReader(src rand.Source) *randReader {
return &randReader{
src: src,
rand: rand.New(src),
}
}
// Read generates len(p) random bytes and writes them into p. It
// always returns len(p) and a nil error.
func (r *randReader) Read(p []byte) (n int, err error) {
for i := 0; i < len(p); i += 7 {
val := r.src.Int63()
for j := 0; i+j < len(p) && j < 7; j++ {
p[i+j] = byte(val)
val >>= 8
}
}
return len(p), nil
}
func randomID(rd io.Reader) restic.ID { func randomID(rd io.Reader) restic.ID {
id := restic.ID{} id := restic.ID{}
_, err := io.ReadFull(rd, id[:]) _, err := io.ReadFull(rd, id[:])
@ -73,17 +48,17 @@ func saveFile(t testing.TB, be Saver, length int, f *os.File, id restic.ID) {
} }
} }
func fillPacks(t testing.TB, rnd *randReader, 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.rand.Intn(1 << 20) l := rnd.Intn(1 << 20)
seed := rnd.rand.Int63() seed := rnd.Int63()
packer, err := pm.findPacker() packer, err := pm.findPacker()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
rd := newRandReader(rand.NewSource(seed)) rd := rand.New(rand.NewSource(seed))
id := randomID(rd) id := randomID(rd)
buf = buf[:l] buf = buf[:l]
_, err = io.ReadFull(rd, buf) _, err = io.ReadFull(rd, buf)
@ -117,7 +92,7 @@ func fillPacks(t testing.TB, rnd *randReader, be Saver, pm *packerManager, buf [
return bytes return bytes
} }
func flushRemainingPacks(t testing.TB, rnd *randReader, be Saver, pm *packerManager) (bytes int) { func flushRemainingPacks(t testing.TB, be Saver, pm *packerManager) (bytes int) {
if pm.countPacker() > 0 { if pm.countPacker() > 0 {
for _, packer := range pm.packers { for _, packer := range pm.packers {
n, err := packer.Finalize() n, err := packer.Finalize()
@ -135,7 +110,7 @@ func flushRemainingPacks(t testing.TB, rnd *randReader, be Saver, pm *packerMana
} }
func TestPackerManager(t *testing.T) { func TestPackerManager(t *testing.T) {
rnd := newRandReader(rand.NewSource(23)) rnd := rand.New(rand.NewSource(23))
be := mem.New() be := mem.New()
pm := newPackerManager(be, crypto.NewRandomKey()) pm := newPackerManager(be, crypto.NewRandomKey())
@ -143,13 +118,13 @@ func TestPackerManager(t *testing.T) {
blobBuf := make([]byte, maxBlobSize) blobBuf := make([]byte, maxBlobSize)
bytes := fillPacks(t, rnd, be, pm, blobBuf) bytes := fillPacks(t, rnd, be, pm, blobBuf)
bytes += flushRemainingPacks(t, rnd, be, pm) bytes += flushRemainingPacks(t, be, pm)
t.Logf("saved %d bytes", bytes) t.Logf("saved %d bytes", bytes)
} }
func BenchmarkPackerManager(t *testing.B) { func BenchmarkPackerManager(t *testing.B) {
rnd := newRandReader(rand.NewSource(23)) rnd := rand.New(rand.NewSource(23))
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 },
@ -162,7 +137,7 @@ func BenchmarkPackerManager(t *testing.B) {
bytes := 0 bytes := 0
pm := newPackerManager(be, crypto.NewRandomKey()) pm := newPackerManager(be, crypto.NewRandomKey())
bytes += fillPacks(t, rnd, be, pm, blobBuf) bytes += fillPacks(t, rnd, be, pm, blobBuf)
bytes += flushRemainingPacks(t, rnd, be, pm) bytes += flushRemainingPacks(t, be, pm)
t.Logf("saved %d bytes", bytes) t.Logf("saved %d bytes", bytes)
} }
} }