2016-03-05 14:58:39 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2017-06-05 21:56:59 +00:00
|
|
|
"context"
|
2016-03-05 14:58:39 +00:00
|
|
|
"io"
|
|
|
|
"math/rand"
|
2020-03-05 19:54:52 +00:00
|
|
|
"sync"
|
2016-03-05 14:58:39 +00:00
|
|
|
"testing"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/crypto"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2021-08-22 13:10:00 +00:00
|
|
|
"github.com/restic/restic/internal/test"
|
2016-03-05 14:58:39 +00:00
|
|
|
)
|
|
|
|
|
2016-08-31 18:29:54 +00:00
|
|
|
func randomID(rd io.Reader) restic.ID {
|
|
|
|
id := restic.ID{}
|
2016-03-05 14:58:39 +00:00
|
|
|
_, err := io.ReadFull(rd, id[:])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2016-03-06 11:26:25 +00:00
|
|
|
const maxBlobSize = 1 << 20
|
|
|
|
|
2020-03-05 19:54:52 +00:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2021-08-22 13:10:00 +00:00
|
|
|
func fillPacks(t testing.TB, rnd *rand.Rand, pm *packerManager, buf []byte) (bytes int) {
|
2022-07-02 21:30:26 +00:00
|
|
|
for i := 0; i < 102; i++ {
|
2020-03-05 19:54:52 +00:00
|
|
|
l := rnd.Intn(maxBlobSize)
|
|
|
|
id := randomID(rnd)
|
2016-03-06 11:26:25 +00:00
|
|
|
buf = buf[:l]
|
2020-03-05 19:54:52 +00:00
|
|
|
// Only change a few bytes so we know we're not benchmarking the RNG.
|
|
|
|
rnd.Read(buf[:min(l, 4)])
|
2016-03-05 14:58:39 +00:00
|
|
|
|
2021-08-22 13:10:00 +00:00
|
|
|
n, err := pm.SaveBlob(context.TODO(), restic.DataBlob, id, buf, 0)
|
2018-10-19 01:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-22 13:10:00 +00:00
|
|
|
if n != l+37 && n != l+37+36 {
|
2022-06-05 09:39:57 +00:00
|
|
|
t.Errorf("Add() returned invalid number of bytes: want %v, got %v", l, n)
|
2016-03-05 14:58:39 +00:00
|
|
|
}
|
2021-08-22 13:10:00 +00:00
|
|
|
bytes += n
|
2016-03-05 14:58:39 +00:00
|
|
|
}
|
2021-08-22 13:10:00 +00:00
|
|
|
err := pm.Flush(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-03-05 14:58:39 +00:00
|
|
|
}
|
|
|
|
return bytes
|
|
|
|
}
|
|
|
|
|
2020-03-05 19:54:52 +00:00
|
|
|
const randomSeed = 23
|
|
|
|
|
|
|
|
var (
|
|
|
|
once sync.Once
|
|
|
|
totalSize int64
|
|
|
|
)
|
|
|
|
|
2016-03-05 14:58:39 +00:00
|
|
|
func TestPackerManager(t *testing.T) {
|
2020-03-05 19:54:52 +00:00
|
|
|
bytes := testPackerManager(t)
|
|
|
|
once.Do(func() { totalSize = bytes })
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPackerManager(t testing.TB) int64 {
|
|
|
|
rnd := rand.New(rand.NewSource(randomSeed))
|
2016-03-05 14:58:39 +00:00
|
|
|
|
2021-08-22 13:10:00 +00:00
|
|
|
savedBytes := int(0)
|
2024-05-19 14:10:48 +00:00
|
|
|
pm := newPackerManager(crypto.NewRandomKey(), restic.DataBlob, DefaultPackSize, func(ctx context.Context, tp restic.BlobType, p *packer) error {
|
2021-08-22 13:10:00 +00:00
|
|
|
err := p.Finalize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
savedBytes += int(p.Size())
|
|
|
|
return nil
|
|
|
|
})
|
2016-03-05 14:58:39 +00:00
|
|
|
|
2016-03-06 11:26:25 +00:00
|
|
|
blobBuf := make([]byte, maxBlobSize)
|
|
|
|
|
2021-08-22 13:10:00 +00:00
|
|
|
bytes := fillPacks(t, rnd, pm, blobBuf)
|
|
|
|
// bytes does not include the last packs header
|
|
|
|
test.Equals(t, savedBytes, bytes+36)
|
2016-03-05 14:58:39 +00:00
|
|
|
|
|
|
|
t.Logf("saved %d bytes", bytes)
|
2020-03-05 19:54:52 +00:00
|
|
|
return int64(bytes)
|
2016-03-05 14:58:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-28 18:58:45 +00:00
|
|
|
func TestPackerManagerWithOversizeBlob(t *testing.T) {
|
|
|
|
packFiles := int(0)
|
|
|
|
sizeLimit := uint(512 * 1024)
|
2024-05-19 14:10:48 +00:00
|
|
|
pm := newPackerManager(crypto.NewRandomKey(), restic.DataBlob, sizeLimit, func(ctx context.Context, tp restic.BlobType, p *packer) error {
|
2023-09-28 18:58:45 +00:00
|
|
|
packFiles++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, i := range []uint{sizeLimit / 2, sizeLimit, sizeLimit / 3} {
|
|
|
|
_, err := pm.SaveBlob(context.TODO(), restic.DataBlob, restic.ID{}, make([]byte, i), 0)
|
|
|
|
test.OK(t, err)
|
|
|
|
}
|
|
|
|
test.OK(t, pm.Flush(context.TODO()))
|
|
|
|
|
|
|
|
// oversized blob must be stored in a separate packfile
|
|
|
|
test.Equals(t, packFiles, 2)
|
|
|
|
}
|
|
|
|
|
2016-03-05 14:58:39 +00:00
|
|
|
func BenchmarkPackerManager(t *testing.B) {
|
2020-03-05 19:54:52 +00:00
|
|
|
// Run testPackerManager if it hasn't run already, to set totalSize.
|
|
|
|
once.Do(func() {
|
|
|
|
totalSize = testPackerManager(t)
|
|
|
|
})
|
|
|
|
|
|
|
|
rnd := rand.New(rand.NewSource(randomSeed))
|
2016-03-06 11:26:25 +00:00
|
|
|
blobBuf := make([]byte, maxBlobSize)
|
2016-03-05 14:58:39 +00:00
|
|
|
|
2020-03-05 19:54:52 +00:00
|
|
|
t.ReportAllocs()
|
|
|
|
t.SetBytes(totalSize)
|
2016-03-05 14:58:39 +00:00
|
|
|
t.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < t.N; i++ {
|
2020-03-05 19:54:52 +00:00
|
|
|
rnd.Seed(randomSeed)
|
2024-05-19 14:10:48 +00:00
|
|
|
pm := newPackerManager(crypto.NewRandomKey(), restic.DataBlob, DefaultPackSize, func(ctx context.Context, t restic.BlobType, p *packer) error {
|
2021-08-22 13:10:00 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
fillPacks(t, rnd, pm, blobBuf)
|
2016-03-05 14:58:39 +00:00
|
|
|
}
|
|
|
|
}
|