From 8757025671b43fef651ecb0ff8d4c76d81443936 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sat, 17 Apr 2021 17:03:17 +0100
Subject: [PATCH] Batch inserts in benchmarks

---
 db/db_test.go | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/db/db_test.go b/db/db_test.go
index ee003ca..dd5d82f 100644
--- a/db/db_test.go
+++ b/db/db_test.go
@@ -19,6 +19,9 @@ const (
 	// How many events to fill up the db, test/bench cases.
 	manyEventsTest  = 100000
 	manyEventsBench = 1000000
+
+	// When writing events to the db, do it in batches of this size.
+	writeBatchSize = 1000
 )
 
 func randomIP() string {
@@ -151,10 +154,9 @@ func runWriteBench(b *testing.B, driver string) {
 	}
 	defer db.Close()
 
-	// Chunks of 100.
-	chunkSize := 1000
-	nchunks := b.N / chunkSize
-	events := randomEvents(chunkSize)
+	// Write events in chunks.
+	nchunks := b.N / writeBatchSize
+	events := randomEvents(writeBatchSize)
 
 	b.ResetTimer()
 
@@ -188,9 +190,10 @@ func runReadBenchmark(b *testing.B, driver string, eventsPerIP int, threadCounts
 
 	// Write a lot of events just to bulk up the database, but
 	// also write a couple of records for a known IP, that we're
-	// going to read in the benchmark.
-	n := manyEventsBench
-	db.AddAggregate(randomEvents(n))
+	// going to read in the benchmark. Do it in batches.
+	for i := 0; i < manyEventsBench; i += writeBatchSize {
+		db.AddAggregate(randomEvents(writeBatchSize))
+	}
 
 	// For a bunch of random IPs, add a known (possibly large)
 	// number of events.
-- 
GitLab