Skip to content
Snippets Groups Projects
Commit 7ca90739 authored by ale's avatar ale
Browse files

Add multi-threaded database benchmarks

parent 05de320f
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ import (
"math/rand"
"os"
"os/exec"
"sync"
"testing"
"time"
......@@ -163,7 +164,7 @@ func BenchmarkWrite_Sqlite(b *testing.B) {
runWriteBench(b, "sqlite")
}
func runReadBenchmark(b *testing.B, driver string, eventsPerIP int) {
func runReadBenchmark(b *testing.B, driver string, eventsPerIP int, threadCounts []int) {
dir, err := ioutil.TempDir("", "")
if err != nil {
b.Fatal(err)
......@@ -200,40 +201,58 @@ func runReadBenchmark(b *testing.B, driver string, eventsPerIP int) {
}
deadline := time.Now().Add(-1 * time.Hour)
b.ResetTimer()
ipCount := 0
for i := 0; i < b.N; i++ {
refIP := refIPs[ipCount]
ipCount++
if ipCount >= len(refIPs) {
ipCount = 0
}
m, err := db.ScanIP(deadline, refIP)
if err != nil {
b.Fatalf("ScanIP(%d): %v", i, err)
}
if len(m) == 0 {
b.Fatalf("ScanIP(%d): returned empty result", i)
}
if len(m["test"]) < 1 {
b.Fatalf("ScanIP(%d): returned bad results: %v", i, m)
fn := func(b *testing.B, off int) {
ipCount := (off * 13) % len(refIPs)
for i := 0; i < b.N; i++ {
refIP := refIPs[ipCount]
ipCount++
if ipCount >= len(refIPs) {
ipCount = 0
}
m, err := db.ScanIP(deadline, refIP)
if err != nil {
b.Fatalf("ScanIP(%d): %v", i, err)
}
if len(m) == 0 {
b.Fatalf("ScanIP(%d): returned empty result", i)
}
if len(m["test"]) < 1 {
b.Fatalf("ScanIP(%d): returned bad results: %v", i, m)
}
}
}
// Now run separate benchmarks for each of the threadCounts values.
for _, numThreads := range threadCounts {
b.Run(fmt.Sprintf("threads:%d", numThreads), func(b *testing.B) {
var wg sync.WaitGroup
for i := 0; i < numThreads; i++ {
wg.Add(1)
go func(i int) {
fn(b, i)
wg.Done()
}(i)
}
wg.Wait()
})
}
}
var threadTests = []int{1, 5, 50}
func BenchmarkRead_LevelDB_SmallAggregate(b *testing.B) {
runReadBenchmark(b, "leveldb", 5)
runReadBenchmark(b, "leveldb", 5, threadTests)
}
func BenchmarkRead_LevelDB_LargeAggregate(b *testing.B) {
runReadBenchmark(b, "leveldb", 1000)
runReadBenchmark(b, "leveldb", 1000, threadTests)
}
func BenchmarkRead_Sqlite_SmallAggregate(b *testing.B) {
runReadBenchmark(b, "sqlite", 5)
runReadBenchmark(b, "sqlite", 5, threadTests)
}
func BenchmarkRead_Sqlite_LargeAggregate(b *testing.B) {
runReadBenchmark(b, "sqlite", 1000)
runReadBenchmark(b, "sqlite", 1000, threadTests)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment