From 74f9f047afc7b57b2549561493fb959556e0da38 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Wed, 30 Oct 2019 21:29:31 +0000
Subject: [PATCH] Run the read benchmarks with many IPs

Reduce the effect of caches within the database layer.
---
 db/db_test.go | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/db/db_test.go b/db/db_test.go
index 544d9bc..2f18f7e 100644
--- a/db/db_test.go
+++ b/db/db_test.go
@@ -20,6 +20,14 @@ func randomIP() string {
 	return fmt.Sprintf("%d.%d.%d.%d", 1+rand.Intn(253), 1+rand.Intn(253), 1+rand.Intn(253), 1+rand.Intn(253))
 }
 
+func randomIPs(n int) []string {
+	out := make([]string, n)
+	for i := 0; i < n; i++ {
+		out[i] = randomIP()
+	}
+	return out
+}
+
 func randomType() string {
 	return "detection"
 }
@@ -174,12 +182,17 @@ func runReadBenchmark(b *testing.B, driver string, eventsPerIP int) {
 	n := manyEvents
 	db.AddAggregate(randomEvents(n))
 
-	refIP := "1.2.3.4"
-	a := new(ippb.Aggregate)
-	for i := 0; i < eventsPerIP; i++ {
-		a.AddEvent(&ippb.Event{Type: "test", Ip: refIP, Count: 1})
+	// For a bunch of random IPs, add a known (possibly large)
+	// number of events.
+	numIPs := 100
+	refIPs := randomIPs(numIPs)
+	for _, ip := range refIPs {
+		a := new(ippb.Aggregate)
+		for i := 0; i < eventsPerIP; i++ {
+			a.AddEvent(&ippb.Event{Type: "test", Ip: ip, Count: 1})
+		}
+		db.AddAggregate(a)
 	}
-	db.AddAggregate(a)
 
 	// Compact the database if it's LevelDB.
 	if ldb, ok := db.(*leveldb.DB); ok {
@@ -189,7 +202,13 @@ 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)
-- 
GitLab