예제 #1
0
func testSize(t *testing.T, drv string) {
	count := 10
	if os.Getenv("BLOBSTORE_TEST_ALL") != "" {
		count = -1
	}
	if testing.Short() {
		t.SkipNow()
	}
	dir, err := ioutil.TempDir("", "pool-benchmark")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)
	cfg := drv + "://" + dir
	u, err := config.ParseURL(cfg)
	if err != nil {
		t.Fatal(err)
	}
	store, err := New(u)
	if err != nil {
		t.Fatal(err)
	}
	defer store.Close()
	start1 := time.Now()
	stored := storeBinaries(t, store, nil, count)
	stored += storeBinaries(t, store, make([]byte, 32), count)
	t.Logf("took %s to store", time.Since(start1))
	t.Logf("%s size is %s", drv, formatutil.Size(uint64(totalSize(dir))))
	start2 := time.Now()
	// Verify that all files are OK
	iter, err := store.Iter()
	if err != nil {
		t.Fatal(err)
	}
	var id string
	found := 0
	for iter.Next(&id) {
		found++
		f, err := store.Open(id)
		if err != nil {
			t.Error(err)
			continue
		}
		if err := f.Check(); err != nil {
			t.Error(err)
		}
		f.Close()
	}
	if found != stored {
		t.Errorf("stored %d files, %d found", stored, found)
	}
	t.Logf("took %s to verify", time.Since(start2))
}
예제 #2
0
func benchmarkDriver(b *testing.B, drv string, size int, mode int, f func(int) []byte) {
	b.ReportAllocs()
	s := int64(opCount * size)
	b.SetBytes(s)
	dir, err := ioutil.TempDir("", "pool-benchmark")
	if err != nil {
		b.Fatal(err)
	}
	defer os.RemoveAll(dir)
	cfg := drv + "://" + dir
	u, err := config.ParseURL(cfg)
	if err != nil {
		b.Fatal(err)
	}
	store, err := New(u)
	if err != nil {
		b.Fatal(err)
	}
	defer store.Close()
	b.ResetTimer()
	switch {
	case mode&modeR != 0 && mode&modeW != 0:
		b.SetBytes(s * 2)
		for ii := 0; ii < b.N; ii++ {
			ids := benchmarkWrite(b, store, size, f)
			benchmarkRead(b, store, ids)
		}
	case mode&modeW != 0:
		for ii := 0; ii < b.N; ii++ {
			benchmarkWrite(b, store, size, f)
		}
	case mode&modeR != 0:
		ids := benchmarkWrite(b, store, size, f)
		b.ResetTimer()
		for ii := 0; ii < b.N; ii++ {
			benchmarkRead(b, store, ids)
		}
	}
}
예제 #3
0
func testStore(t *testing.T, meta *Meta, cfg string) {
	t.Logf("Testing store with config %s", cfg)
	u, err := config.ParseURL(cfg)
	if err != nil {
		t.Fatal(err)
	}
	store, err := New(u)
	if err != nil {
		t.Fatal(err)
	}
	defer store.Close()
	var ids []string
	var hashes []uint32
	for ii := 0; ii < 10; ii++ {
		var r []byte
		if ii%2 == 0 {
			r = linearData(dataSize)
		} else {
			r = randData(dataSize)
		}
		id, err := store.Store(r, meta)
		if err != nil {
			t.Error(err)
			continue
		}
		t.Logf("Stored file with id %s", id)
		ids = append(ids, id)
		hashes = append(hashes, adler32.Checksum(r))
	}
	for ii, v := range ids {
		f, err := store.Open(v)
		if err != nil {
			t.Error(err)
			continue
		}
		defer f.Close()
		t.Logf("Opened file %s", v)
		s, err := f.Size()
		if err != nil {
			t.Error(err)
		} else if s != dataSize {
			t.Errorf("Invalid data size for file %s. Want %v, got %v.", v, dataSize, s)
		}
		if meta != nil {
			var m Meta
			if err := f.GetMeta(&m); err != nil {
				t.Errorf("error loading metadata from %v: %s", v, err)
			} else {
				if m.Foo != meta.Foo {
					t.Errorf("Invalid metadata value. Want %v, got %v.", meta.Foo, m.Foo)
				}
			}
		}
		if err := f.Check(); err != nil {
			t.Errorf("error checking file %v: %s", v, err)
		}
		b, err := f.ReadAll()
		if err != nil {
			t.Error(err)
			continue
		}
		if len(b) != dataSize {
			t.Errorf("expecting %d bytes, got %d instead", dataSize, len(b))
			continue
		}
		if h := adler32.Checksum(b); h != hashes[ii] {
			t.Errorf("invalid hash %v for file %v, expected %v", h, v, hashes[ii])
		}
	}
	return
	// Now remove all the files
	for _, v := range ids {
		if err := store.Remove(v); err != nil {
			t.Error(err)
		} else {
			t.Logf("deleted file %s", v)
		}
	}
	// Check that the files do not exist
	for _, v := range ids {
		if f, err := store.Open(v); err == nil || f != nil {
			t.Errorf("expecting nil file and non-nil err, got file %v and err %v instead", f, err)
			if f == nil {
				f.Close()
			}
		} else {
			t.Logf("file %s was deleted", v)
		}
	}
}