2017-03-25 00:16:41 +00:00
|
|
|
package storage
|
2017-03-16 06:20:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-03-22 06:16:39 +00:00
|
|
|
// Benchmarks returns a benchmark suite using the provided metadata store
|
|
|
|
// creation method
|
2017-03-25 00:16:41 +00:00
|
|
|
func Benchmarks(b *testing.B, name string, metaFn metaFactory) {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Run("StatActive", makeBench(b, name, metaFn, statActiveBenchmark))
|
|
|
|
b.Run("StatCommitted", makeBench(b, name, metaFn, statCommittedBenchmark))
|
|
|
|
b.Run("CreateActive", makeBench(b, name, metaFn, createActiveBenchmark))
|
|
|
|
b.Run("Remove", makeBench(b, name, metaFn, removeBenchmark))
|
|
|
|
b.Run("Commit", makeBench(b, name, metaFn, commitBenchmark))
|
|
|
|
b.Run("GetActive", makeBench(b, name, metaFn, getActiveBenchmark))
|
|
|
|
b.Run("WriteTransaction", openCloseWritable(b, name, metaFn))
|
|
|
|
b.Run("ReadTransaction", openCloseReadonly(b, name, metaFn))
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeBench creates a benchmark with a writable transaction
|
2017-03-25 00:16:41 +00:00
|
|
|
func makeBench(b *testing.B, name string, metaFn metaFactory, fn func(context.Context, *testing.B, *MetaStore)) func(b *testing.B) {
|
2017-03-16 06:20:04 +00:00
|
|
|
return func(b *testing.B) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpDir, err := ioutil.TempDir("", "metastore-bench-"+name+"-")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
ms, err := metaFn(tmpDir)
|
2017-03-16 06:20:04 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, t, err := ms.TransactionContext(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer t.Commit()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
fn(ctx, b, ms)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
func openCloseWritable(b *testing.B, name string, metaFn metaFactory) func(b *testing.B) {
|
2017-03-16 06:20:04 +00:00
|
|
|
return func(b *testing.B) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpDir, err := ioutil.TempDir("", "metastore-bench-"+name+"-")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
ms, err := metaFn(tmpDir)
|
2017-03-16 06:20:04 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, t, err := ms.TransactionContext(ctx, true)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := t.Commit(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
func openCloseReadonly(b *testing.B, name string, metaFn metaFactory) func(b *testing.B) {
|
2017-03-16 06:20:04 +00:00
|
|
|
return func(b *testing.B) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tmpDir, err := ioutil.TempDir("", "metastore-bench-"+name+"-")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
ms, err := metaFn(tmpDir)
|
2017-03-16 06:20:04 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, t, err := ms.TransactionContext(ctx, false)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := t.Rollback(); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
func createActiveFromBase(ctx context.Context, ms *MetaStore, active, base string) error {
|
|
|
|
if _, err := CreateActive(ctx, "bottom", "", false); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := CommitActive(ctx, "bottom", base); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
_, err := CreateActive(ctx, active, base, false)
|
2017-03-16 06:20:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
func statActiveBenchmark(ctx context.Context, b *testing.B, ms *MetaStore) {
|
2017-03-16 06:20:04 +00:00
|
|
|
if err := createActiveFromBase(ctx, ms, "active", "base"); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-03-25 00:16:41 +00:00
|
|
|
_, err := GetInfo(ctx, "active")
|
2017-03-16 06:20:04 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
func statCommittedBenchmark(ctx context.Context, b *testing.B, ms *MetaStore) {
|
2017-03-16 06:20:04 +00:00
|
|
|
if err := createActiveFromBase(ctx, ms, "active", "base"); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := CommitActive(ctx, "active", "committed"); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-03-25 00:16:41 +00:00
|
|
|
_, err := GetInfo(ctx, "committed")
|
2017-03-16 06:20:04 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
func createActiveBenchmark(ctx context.Context, b *testing.B, ms *MetaStore) {
|
2017-03-16 06:20:04 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := CreateActive(ctx, "active", "", false); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.StopTimer()
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, _, err := Remove(ctx, "active"); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
func removeBenchmark(ctx context.Context, b *testing.B, ms *MetaStore) {
|
2017-03-16 06:20:04 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
b.StopTimer()
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := CreateActive(ctx, "active", "", false); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, _, err := Remove(ctx, "active"); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
func commitBenchmark(ctx context.Context, b *testing.B, ms *MetaStore) {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.StopTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := CreateActive(ctx, "active", "", false); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.StartTimer()
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := CommitActive(ctx, "active", "committed"); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.StopTimer()
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, _, err := Remove(ctx, "committed"); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
func getActiveBenchmark(ctx context.Context, b *testing.B, ms *MetaStore) {
|
2017-03-16 06:20:04 +00:00
|
|
|
var base string
|
|
|
|
for i := 1; i <= 10; i++ {
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := CreateActive(ctx, "tmp", base, false); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatalf("create active failed: %+v", err)
|
|
|
|
}
|
|
|
|
base = fmt.Sprintf("base-%d", i)
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := CommitActive(ctx, "tmp", base); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatalf("commit failed: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := CreateActive(ctx, "active", base, false); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatalf("create active failed: %+v", err)
|
|
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-03-25 00:16:41 +00:00
|
|
|
if _, err := GetActive(ctx, "active"); err != nil {
|
2017-03-16 06:20:04 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|