mirror of
https://github.com/octoleo/restic.git
synced 2024-11-26 06:46:34 +00:00
Add LoadBlob benchmark
This commit is contained in:
parent
8734c2466c
commit
215af5c60a
@ -2,12 +2,12 @@ package repository_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/rand"
|
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"io"
|
"io"
|
||||||
mrand "math/rand"
|
"math/rand"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"restic"
|
"restic"
|
||||||
"restic/archiver"
|
"restic/archiver"
|
||||||
@ -17,13 +17,15 @@ import (
|
|||||||
|
|
||||||
var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20}
|
var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20}
|
||||||
|
|
||||||
|
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
|
||||||
func TestSave(t *testing.T) {
|
func TestSave(t *testing.T) {
|
||||||
repo, cleanup := repository.TestRepository(t)
|
repo, cleanup := repository.TestRepository(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
for _, size := range testSizes {
|
for _, size := range testSizes {
|
||||||
data := make([]byte, size)
|
data := make([]byte, size)
|
||||||
_, err := io.ReadFull(rand.Reader, data)
|
_, err := io.ReadFull(rnd, data)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
id := restic.Hash(data)
|
id := restic.Hash(data)
|
||||||
@ -59,7 +61,7 @@ func TestSaveFrom(t *testing.T) {
|
|||||||
|
|
||||||
for _, size := range testSizes {
|
for _, size := range testSizes {
|
||||||
data := make([]byte, size)
|
data := make([]byte, size)
|
||||||
_, err := io.ReadFull(rand.Reader, data)
|
_, err := io.ReadFull(rnd, data)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
id := restic.Hash(data)
|
id := restic.Hash(data)
|
||||||
@ -94,7 +96,7 @@ func BenchmarkSaveAndEncrypt(t *testing.B) {
|
|||||||
size := 4 << 20 // 4MiB
|
size := 4 << 20 // 4MiB
|
||||||
|
|
||||||
data := make([]byte, size)
|
data := make([]byte, size)
|
||||||
_, err := io.ReadFull(rand.Reader, data)
|
_, err := io.ReadFull(rnd, data)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
id := restic.ID(sha256.Sum256(data))
|
id := restic.ID(sha256.Sum256(data))
|
||||||
@ -145,6 +147,36 @@ func BenchmarkLoadTree(t *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkLoadBlob(b *testing.B) {
|
||||||
|
repo, cleanup := repository.TestRepository(b)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
length := 1000000
|
||||||
|
buf := make([]byte, length)
|
||||||
|
_, err := io.ReadFull(rnd, buf)
|
||||||
|
OK(b, err)
|
||||||
|
|
||||||
|
id, err := repo.SaveBlob(restic.DataBlob, buf, restic.ID{})
|
||||||
|
OK(b, err)
|
||||||
|
OK(b, repo.Flush())
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
b.SetBytes(int64(length))
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
n, err := repo.LoadBlob(restic.DataBlob, id, buf)
|
||||||
|
OK(b, err)
|
||||||
|
if n != length {
|
||||||
|
b.Errorf("wanted %d bytes, got %d", length, n)
|
||||||
|
}
|
||||||
|
|
||||||
|
id2 := restic.Hash(buf[:n])
|
||||||
|
if !id.Equal(id2) {
|
||||||
|
b.Errorf("wrong data returned, wanted %v, got %v", id.Str(), id2.Str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadJSONUnpacked(t *testing.T) {
|
func TestLoadJSONUnpacked(t *testing.T) {
|
||||||
repo, cleanup := repository.TestRepository(t)
|
repo, cleanup := repository.TestRepository(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
@ -197,10 +229,10 @@ func BenchmarkLoadIndex(b *testing.B) {
|
|||||||
// saveRandomDataBlobs generates random data blobs and saves them to the repository.
|
// saveRandomDataBlobs generates random data blobs and saves them to the repository.
|
||||||
func saveRandomDataBlobs(t testing.TB, repo restic.Repository, num int, sizeMax int) {
|
func saveRandomDataBlobs(t testing.TB, repo restic.Repository, num int, sizeMax int) {
|
||||||
for i := 0; i < num; i++ {
|
for i := 0; i < num; i++ {
|
||||||
size := mrand.Int() % sizeMax
|
size := rand.Int() % sizeMax
|
||||||
|
|
||||||
buf := make([]byte, size)
|
buf := make([]byte, size)
|
||||||
_, err := io.ReadFull(rand.Reader, buf)
|
_, err := io.ReadFull(rnd, buf)
|
||||||
OK(t, err)
|
OK(t, err)
|
||||||
|
|
||||||
_, err = repo.SaveBlob(restic.DataBlob, buf, restic.ID{})
|
_, err = repo.SaveBlob(restic.DataBlob, buf, restic.ID{})
|
||||||
|
Loading…
Reference in New Issue
Block a user