-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from viveksahu26/issue_265_db_optimized
Add a optimized database
- Loading branch information
Showing
2 changed files
with
123 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
}) | ||
} |