Skip to content

Commit

Permalink
add benchmark test
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Kumar Sahu <[email protected]>
  • Loading branch information
viveksahu26 committed Jul 2, 2024
1 parent 31fb0d3 commit 79f643b
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pkg/compliance/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package compliance

import (
"fmt"
"math/rand"
"testing"
)

const (
numRecords = 1000000 // Number of records to test with
)

// Generate large data set
func generateRecords(n int) []*record {
var records []*record
for i := 0; i < n; i++ {
records = append(records, &record{
check_key: rand.Intn(1000),
check_value: fmt.Sprintf("value_%d", i),
id: fmt.Sprintf("id_%d", rand.Intn(1000)),
score: rand.Float64() * 100,
required: rand.Intn(2) == 0,
})
}
return records
}

// Benchmark original db implementation
func BenchmarkOriginalDB(b *testing.B) {
records := generateRecords(numRecords)
db := newDB()

// Benchmark insertion
b.Run("Insert", func(b *testing.B) {
for i := 0; i < b.N; i++ {
db.addRecords(records)
}
})

// Benchmark retrieval by key
b.Run("GetByKey", func(b *testing.B) {
for i := 0; i < b.N; i++ {
db.getRecords(rand.Intn(1000))
}
})

// Benchmark retrieval by ID
b.Run("GetByID", func(b *testing.B) {
for i := 0; i < b.N; i++ {
db.getRecordsById(fmt.Sprintf("id_%d", rand.Intn(1000)))
}
})

// Benchmark for combined retrieval by key and ID case
b.Run("GetByKeyAndIDTogether", func(b *testing.B) {
for i := 0; i < b.N; i++ {
key := rand.Intn(1000)
id := fmt.Sprintf("id_%d", rand.Intn(1000))
db.getRecordsByKeyId(key, id)
}
})

// Benchmark for retrieval of all IDs case
b.Run("GetAllIDs", func(b *testing.B) {
for i := 0; i < b.N; i++ {
db.getAllIds()
}
})
}

0 comments on commit 79f643b

Please sign in to comment.