From d94d00316519696c0632f654385fc702896e3660 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 16 Feb 2015 20:00:23 +0100 Subject: [PATCH] Add test and benchmark for SaveFrom() --- server_test.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/server_test.go b/server_test.go index 9272fa24a..9b9172de8 100644 --- a/server_test.go +++ b/server_test.go @@ -2,8 +2,10 @@ package restic_test import ( "bytes" + "crypto/rand" "crypto/sha256" "encoding/json" + "io" "testing" "github.com/restic/restic" @@ -47,9 +49,6 @@ func BenchmarkSaveJSON(t *testing.B) { key := setupKey(t, be, "geheim") server := restic.NewServerWithKey(be, key) - t.ResetTimer() - //t.SetBytes(int64(size)) - obj := serverTests[0] data, err := json.Marshal(obj) @@ -57,6 +56,8 @@ func BenchmarkSaveJSON(t *testing.B) { data = append(data, '\n') h := sha256.Sum256(data) + t.ResetTimer() + for i := 0; i < t.N; i++ { blob, err := server.SaveJSON(backend.Tree, obj) ok(t, err) @@ -66,3 +67,59 @@ func BenchmarkSaveJSON(t *testing.B) { h, blob.ID) } } + +var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20} + +func TestSaveFrom(t *testing.T) { + be := setupBackend(t) + defer teardownBackend(t, be) + key := setupKey(t, be, "geheim") + server := restic.NewServerWithKey(be, key) + + for _, size := range testSizes { + data := make([]byte, size) + _, err := io.ReadFull(rand.Reader, data) + ok(t, err) + + id := sha256.Sum256(data) + + // save + blob, err := server.SaveFrom(backend.Data, id[:], uint(size), bytes.NewReader(data)) + ok(t, err) + + // read back + buf, err := server.Load(backend.Data, blob) + + assert(t, len(buf) == len(data), + "number of bytes read back does not match: expected %d, got %d", + len(data), len(buf)) + + assert(t, bytes.Equal(buf, data), + "data does not match: expected %02x, got %02x", + data, buf) + } +} + +func BenchmarkSaveFrom(t *testing.B) { + be := setupBackend(t) + defer teardownBackend(t, be) + key := setupKey(t, be, "geheim") + server := restic.NewServerWithKey(be, key) + + size := 4 << 20 // 4MiB + + data := make([]byte, size) + _, err := io.ReadFull(rand.Reader, data) + ok(t, err) + + id := sha256.Sum256(data) + + t.ResetTimer() + t.SetBytes(int64(size)) + + for i := 0; i < t.N; i++ { + // save + _, err := server.SaveFrom(backend.Data, id[:], uint(size), bytes.NewReader(data)) + ok(t, err) + } +}