From 71a42800fe1f2db7a953077e96512d9bd60c3abf Mon Sep 17 00:00:00 2001 From: James Mills <1290234+prologic@users.noreply.github.com> Date: Thu, 14 Mar 2019 18:17:20 +1000 Subject: [PATCH] Improved benchmark test suite for various key/value sizes --- README.md | 26 +++++++++++++++-- bitcask_test.go | 78 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 83 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index c97b3fb..5a99185 100644 --- a/README.md +++ b/README.md @@ -97,12 +97,32 @@ Benchmarks run on a 11" Macbook with a 1.4Ghz Intel Core i7: ``` $ make bench ... -BenchmarkGet-4 300000 5065 ns/op 144 B/op 4 allocs/op -BenchmarkPut-4 100000 14640 ns/op 699 B/op 7 allocs/op +BenchmarkGet/128B-4 200000 5780 ns/op 400 B/op 5 allocs/op +BenchmarkGet/256B-4 200000 6138 ns/op 656 B/op 5 allocs/op +BenchmarkGet/512B-4 200000 5967 ns/op 1200 B/op 5 allocs/op +BenchmarkGet/1K-4 200000 6290 ns/op 2288 B/op 5 allocs/op +BenchmarkGet/2K-4 200000 6293 ns/op 4464 B/op 5 allocs/op +BenchmarkGet/4K-4 200000 7673 ns/op 9072 B/op 5 allocs/op +BenchmarkGet/8K-4 200000 10373 ns/op 17776 B/op 5 allocs/op +BenchmarkGet/16K-4 100000 14227 ns/op 34928 B/op 5 allocs/op +BenchmarkGet/32K-4 100000 25953 ns/op 73840 B/op 5 allocs/op +BenchmarkPut/128B-4 100000 17353 ns/op 680 B/op 5 allocs/op +BenchmarkPut/256B-4 100000 18620 ns/op 808 B/op 5 allocs/op +BenchmarkPut/512B-4 100000 19068 ns/op 1096 B/op 5 allocs/op +BenchmarkPut/1K-4 100000 23738 ns/op 1673 B/op 5 allocs/op +BenchmarkPut/2K-4 50000 25118 ns/op 2826 B/op 5 allocs/op +BenchmarkPut/4K-4 50000 44605 ns/op 5389 B/op 5 allocs/op +BenchmarkPut/8K-4 30000 55237 ns/op 10001 B/op 5 allocs/op +BenchmarkPut/16K-4 20000 78966 ns/op 18972 B/op 5 allocs/op +BenchmarkPut/32K-4 10000 116253 ns/op 41520 B/op 5 allocs/op ``` +For 128B values: + * ~180,000 reads/sec -* ~60,000 writes/sec +* ~60,000 writes/sec + +The full benchmark above shows linear performance as you increase key/value sizes. ## License diff --git a/bitcask_test.go b/bitcask_test.go index a2a916d..366cf0b 100644 --- a/bitcask_test.go +++ b/bitcask_test.go @@ -294,6 +294,11 @@ func TestLocking(t *testing.T) { assert.Equal("error: cannot acquire lock", err.Error()) } +type benchmarkTestCase struct { + name string + size int +} + func BenchmarkGet(b *testing.B) { testdir, err := ioutil.TempDir("", "bitcask") if err != nil { @@ -306,20 +311,39 @@ func BenchmarkGet(b *testing.B) { } defer db.Close() - err = db.Put("foo", []byte("bar")) - if err != nil { - b.Fatal(err) + tests := []benchmarkTestCase{ + {"128B", 128}, + {"256B", 256}, + {"512B", 512}, + {"1K", 1024}, + {"2K", 2048}, + {"4K", 4096}, + {"8K", 8192}, + {"16K", 16384}, + {"32K", 32768}, } - b.ResetTimer() - for i := 0; i < b.N; i++ { - val, err := db.Get("foo") - if err != nil { - b.Fatal(err) - } - if string(val) != "bar" { - b.Errorf("expected val=bar got=%s", val) - } + for _, tt := range tests { + b.Run(tt.name, func(b *testing.B) { + key := "foo" + value := []byte(strings.Repeat(" ", tt.size)) + + err = db.Put(key, value) + if err != nil { + b.Fatal(err) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + val, err := db.Get(key) + if err != nil { + b.Fatal(err) + } + if string(val) != string(value) { + b.Errorf("unexpected value") + } + } + }) } } @@ -335,11 +359,29 @@ func BenchmarkPut(b *testing.B) { } defer db.Close() - b.ResetTimer() - for i := 0; i < b.N; i++ { - err := db.Put(fmt.Sprintf("key%d", i), []byte("bar")) - if err != nil { - b.Fatal(err) - } + tests := []benchmarkTestCase{ + {"128B", 128}, + {"256B", 256}, + {"512B", 512}, + {"1K", 1024}, + {"2K", 2048}, + {"4K", 4096}, + {"8K", 8192}, + {"16K", 16384}, + {"32K", 32768}, + } + + for _, tt := range tests { + b.Run(tt.name, func(b *testing.B) { + key := "foo" + value := []byte(strings.Repeat(" ", tt.size)) + b.ResetTimer() + for i := 0; i < b.N; i++ { + err := db.Put(key, value) + if err != nil { + b.Fatal(err) + } + } + }) } }