-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanudb_benchmark.cpp
More file actions
620 lines (522 loc) · 24.1 KB
/
anudb_benchmark.cpp
File metadata and controls
620 lines (522 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <random>
#include <fstream>
#include <iomanip>
#include <memory>
#include <algorithm>
#include <functional>
#include <sstream>
#include <thread>
#include <mutex>
#include <atomic>
// AnuDB includes
#include "Database.h"
#include "json.hpp"
using json = nlohmann::json;
using namespace std::chrono;
#include <memory>
namespace std {
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
}
// Configuration constants
const int NUM_DOCUMENTS = 10000; // Number of documents to insert in each test
const int NUM_QUERIES = 1000; // Number of queries to execute in each test
const int NUM_THREADS = 10; // Number of concurrent threads for parallel tests
const std::string DB_PATH_ANUDB = "./benchmark_anudb";
const std::string DB_PATH_SQLITE = "./benchmark_sqlite.db";
const std::string COLLECTION_NAME = "products";
// Test case class for common functionality
class BenchmarkTest {
public:
BenchmarkTest(const std::string& name) : testName(name) {}
virtual ~BenchmarkTest() {}
virtual bool setup() = 0;
virtual bool cleanup() = 0;
virtual bool runInsertTest() = 0;
virtual bool runQueryTest() = 0;
virtual bool runUpdateTest() = 0;
virtual bool runDeleteTest() = 0;
virtual bool runParallelTest() = 0;
const std::string& getName() const { return testName; }
// Results storage
struct TestResult {
double insertTime = 0;
double queryTime = 0;
double updateTime = 0;
double deleteTime = 0;
double parallelTime = 0;
size_t insertOps = 0;
size_t queryOps = 0;
size_t updateOps = 0;
size_t deleteOps = 0;
size_t parallelOps = 0;
};
TestResult results;
protected:
std::string testName;
// Helper method to generate random product data
json generateRandomProduct(int index) {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_real_distribution<> price_dist(10.0, 2000.0);
static std::uniform_int_distribution<> stock_dist(0, 500);
static std::uniform_real_distribution<> rating_dist(1.0, 5.0);
static std::uniform_int_distribution<> category_dist(0, 3);
const std::vector<std::string> categories = {"Electronics", "Books", "Food", "Clothing"};
const std::vector<std::string> brands = {"TechMaster", "ReadBooks", "FoodDelight", "FashionStyle"};
int category_idx = category_dist(gen);
double price = std::round(price_dist(gen) * 100.0) / 100.0;
int stock = stock_dist(gen);
double rating = std::round(rating_dist(gen) * 10.0) / 10.0;
json product = {
{"id", "prod" + std::to_string(index)},
{"name", "Product " + std::to_string(index)},
{"price", price},
{"stock", stock},
{"category", categories[category_idx]},
{"brand", brands[category_idx]},
{"rating", rating},
{"available", (stock > 0)},
{"created_at", getCurrentTimeString()}
};
// Add category-specific attributes
switch (category_idx) {
case 0: // Electronics
product["specs"] = {
{"processor", "i" + std::to_string(5 + (index % 5))},
{"ram", std::to_string(4 * (1 + (index % 4))) + "GB"},
{"storage", std::to_string(128 * (1 + (index % 8))) + "GB"}
};
break;
case 1: // Books
product["author"] = "Author " + std::to_string(1 + (index % 20));
product["pages"] = 100 + (index % 500);
product["publisher"] = "Publisher " + std::to_string(1 + (index % 10));
break;
case 2: // Food
product["expiry_date"] = "2025-" +
std::to_string(1 + (index % 12)) + "-" +
std::to_string(1 + (index % 28));
product["weight"] = std::to_string((index % 10) * 100) + "g";
product["organic"] = (index % 2 == 0);
break;
case 3: // Clothing
product["size"] = (index % 6 == 0) ? "XS" :
(index % 6 == 1) ? "S" :
(index % 6 == 2) ? "M" :
(index % 6 == 3) ? "L" :
(index % 6 == 4) ? "XL" : "XXL";
product["color"] = (index % 7 == 0) ? "Red" :
(index % 7 == 1) ? "Blue" :
(index % 7 == 2) ? "Green" :
(index % 7 == 3) ? "Black" :
(index % 7 == 4) ? "White" :
(index % 7 == 5) ? "Yellow" : "Purple";
product["material"] = (index % 4 == 0) ? "Cotton" :
(index % 4 == 1) ? "Polyester" :
(index % 4 == 2) ? "Wool" : "Silk";
break;
}
return product;
}
// Get current time as formatted string
std::string getCurrentTimeString() {
auto now = system_clock::now();
auto now_time_t = system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&now_time_t), "%Y-%m-%d %H:%M:%S");
return ss.str();
}
// Timer function for benchmarking
template<typename Func>
double measureTime(Func&& func) {
auto start = high_resolution_clock::now();
func();
auto end = high_resolution_clock::now();
return duration_cast<milliseconds>(end - start).count() / 1000.0;
}
};
// AnuDB test implementation
class AnuDBTest : public BenchmarkTest {
public:
AnuDBTest() : BenchmarkTest("AnuDB") {}
bool setup() override {
db = std::make_unique<anudb::Database>(DB_PATH_ANUDB);
auto status = db->open();
if (!status.ok()) {
std::cerr << "Failed to open AnuDB database: " << status.message() << std::endl;
return false;
}
// Create collection
status = db->createCollection(COLLECTION_NAME);
if (!status.ok() && status.message().find("already exists") == std::string::npos) {
std::cerr << "Failed to create collection: " << status.message() << std::endl;
return false;
}
collection = db->getCollection(COLLECTION_NAME);
if (!collection) {
std::cerr << "Failed to get collection." << std::endl;
return false;
}
// Create indexes
for (const auto& field : {"price", "stock", "category", "rating", "available"}) {
status = collection->createIndex(field);
if (!status.ok() && status.message().find("already exists") == std::string::npos) {
std::cerr << "Failed to create index on " << field << ": " << status.message() << std::endl;
return false;
}
}
return true;
}
bool cleanup() override {
if (!db) return false;
auto status = db->close();
return status.ok();
}
bool runInsertTest() override {
if (!collection) return false;
int successCount = 0;
results.insertTime = measureTime([&]() {
for (int i = 0; i < NUM_DOCUMENTS; i++) {
json productData = generateRandomProduct(i);
std::string docId = "prod" + std::to_string(i);
anudb::Document doc(docId, productData);
auto status = collection->createDocument(doc);
if (status.ok()) {
successCount++;
}
}
});
results.insertOps = successCount;
return true;
}
bool runQueryTest() override {
if (!collection) return false;
int successCount = 0;
std::vector<json> queries = {
// Query by category
{{"$eq", {{"category", "Electronics"}}}},
{{"$eq", {{"category", "Books"}}}},
{{"$eq", {{"category", "Food"}}}},
{{"$eq", {{"category", "Clothing"}}}},
// Query by price range
{{"$gt", {{"price", 500.0}}}},
{{"$lt", {{"price", 100.0}}}},
{{"$and", {
{{"$gt", {{"price", 100.0}}}},
{{"$lt", {{"price", 500.0}}}}
}}},
// Query by rating
{{"$gt", {{"rating", 4.0}}}},
// Query by availability
{{"$eq", {{"available", true}}}},
// Combined queries
{{"$and", {
{{"$eq", {{"category", "Electronics"}}}},
{{"$gt", {{"price", 1000.0}}}}
}}}
};
results.queryTime = measureTime([&]() {
for (int i = 0; i < NUM_QUERIES; i++) {
const json& query = queries[i % queries.size()];
std::vector<std::string> docIds = collection->findDocument(query);
successCount += docIds.size() > 0 ? 1 : 0;
}
});
results.queryOps = NUM_QUERIES;
return true;
}
bool runUpdateTest() override {
if (!collection) return false;
int successCount = 0;
results.updateTime = measureTime([&]() {
for (int i = 0; i < NUM_DOCUMENTS / 2; i++) {
std::string docId = "prod" + std::to_string(i);
// Generate update data
json updateData = {
{"$set", {
{"price", 100.0 + (i % 10) * 50.0},
{"stock", 10 + (i % 20)},
{"updated_at", getCurrentTimeString()}
}}
};
auto status = collection->updateDocument(docId, updateData);
if (status.ok()) {
successCount++;
}
}
});
results.updateOps = successCount;
return true;
}
bool runDeleteTest() override {
if (!collection) return false;
int successCount = 0;
results.deleteTime = measureTime([&]() {
for (int i = 0; i < NUM_DOCUMENTS / 4; i++) {
std::string docId = "prod" + std::to_string(i * 3); // Delete every third document
auto status = collection->deleteDocument(docId);
if (status.ok()) {
successCount++;
}
}
});
results.deleteOps = successCount;
return true;
}
bool runParallelTest() override {
if (!collection) return false;
std::vector<std::thread> threads;
std::atomic<int> successCount(0);
std::mutex mtx;
// Create a barrier to synchronize thread start
std::atomic<int> barrier(0);
auto start = high_resolution_clock::now();
// Spawn threads
for (int t = 0; t < NUM_THREADS; t++) {
threads.emplace_back([&, t]() {
// Wait until all threads are ready
barrier.fetch_add(1);
while (barrier.load() < NUM_THREADS) {
std::this_thread::yield();
}
int threadSuccessCount = 0;
int docsPerThread = NUM_DOCUMENTS / NUM_THREADS;
int startIdx = t * docsPerThread;
int endIdx = startIdx + docsPerThread;
// Each thread performs a mix of operations
for (int i = startIdx; i < endIdx; i++) {
std::string docId = "parallel_prod" + std::to_string(i);
// Insert
json productData = generateRandomProduct(i + 10000); // Offset to avoid conflicts
anudb::Document doc(docId, productData);
auto status = collection->createDocument(doc);
if (status.ok()) {
threadSuccessCount++;
}
// Query
if (i % 5 == 0) {
json query = {{"$eq", {{"category", productData["category"].get<std::string>()}}}};
collection->findDocument(query);
}
// Update
if (i % 3 == 0) {
json updateData = {
{"$set", {
{"price", (double)productData["price"] * 1.1},
{"stock", i % 100},
{"updated_at", getCurrentTimeString()}
}}
};
collection->updateDocument(docId, updateData);
}
// Delete
if (i % 7 == 0) {
collection->deleteDocument(docId);
}
}
successCount.fetch_add(threadSuccessCount);
});
}
// Join all threads
for (auto& thread : threads) {
thread.join();
}
auto end = high_resolution_clock::now();
results.parallelTime = duration_cast<milliseconds>(end - start).count() / 1000.0;
results.parallelOps = successCount.load();
return true;
}
private:
std::unique_ptr<anudb::Database> db;
anudb::Collection* collection = nullptr;
};
// Function to run all tests and print results
void runBenchmarks() {
std::vector<std::unique_ptr<BenchmarkTest>> tests;
tests.push_back(std::make_unique<AnuDBTest>());
//tests.push_back(std::make_unique<SQLiteTest>());
std::cout << "===== Database Benchmark: AnuDB =====" << std::endl;
std::cout << "Configuration:" << std::endl;
std::cout << "- Documents: " << NUM_DOCUMENTS << std::endl;
std::cout << "- Queries: " << NUM_QUERIES << std::endl;
std::cout << "- Parallel Threads: " << NUM_THREADS << std::endl;
std::cout << std::endl;
for (auto& test : tests) {
std::cout << "Running " << test->getName() << " tests..." << std::endl;
// Setup test
if (!test->setup()) {
std::cerr << "Failed to set up " << test->getName() << " test." << std::endl;
continue;
}
// Run insert test
std::cout << " Running insert test..." << std::endl;
if (!test->runInsertTest()) {
std::cerr << "Failed to run insert test for " << test->getName() << std::endl;
}
// Run query test
std::cout << " Running query test..." << std::endl;
if (!test->runQueryTest()) {
std::cerr << "Failed to run query test for " << test->getName() << std::endl;
}
// Run update test
std::cout << " Running update test..." << std::endl;
if (!test->runUpdateTest()) {
std::cerr << "Failed to run update test for " << test->getName() << std::endl;
}
// Run delete test
std::cout << " Running delete test..." << std::endl;
if (!test->runDeleteTest()) {
std::cerr << "Failed to run delete test for " << test->getName() << std::endl;
}
// Run parallel test
std::cout << " Running parallel operations test..." << std::endl;
if (!test->runParallelTest()) {
std::cerr << "Failed to run parallel test for " << test->getName() << std::endl;
}
// Cleanup test
if (!test->cleanup()) {
std::cerr << "Failed to clean up " << test->getName() << " test." << std::endl;
}
std::cout << "Completed " << test->getName() << " tests." << std::endl;
std::cout << std::endl;
}
// Print results table
std::cout << "\n===== Benchmark Results =====" << std::endl;
// Header
std::cout << std::left << std::setw(20) << "Operation";
for (const auto& test : tests) {
std::cout << std::setw(15) << test->getName() + " Time(s)";
std::cout << std::setw(15) << test->getName() + " Ops";
std::cout << std::setw(15) << test->getName() + " Ops/s";
}
std::cout << std::endl;
// Insert results
std::cout << std::left << std::setw(20) << "Insert";
for (const auto& test : tests) {
const auto& results = test->results;
std::cout << std::setw(15) << std::fixed << std::setprecision(3) << results.insertTime;
std::cout << std::setw(15) << results.insertOps;
double opsPerSec = results.insertTime > 0 ? results.insertOps / results.insertTime : 0;
std::cout << std::setw(15) << std::fixed << std::setprecision(1) << opsPerSec;
}
std::cout << std::endl;
// Query results
std::cout << std::left << std::setw(20) << "Query";
for (const auto& test : tests) {
const auto& results = test->results;
std::cout << std::setw(15) << std::fixed << std::setprecision(3) << results.queryTime;
std::cout << std::setw(15) << results.queryOps;
double opsPerSec = results.queryTime > 0 ? results.queryOps / results.queryTime : 0;
std::cout << std::setw(15) << std::fixed << std::setprecision(1) << opsPerSec;
}
std::cout << std::endl;
// Update results
std::cout << std::left << std::setw(20) << "Update";
for (const auto& test : tests) {
const auto& results = test->results;
std::cout << std::setw(15) << std::fixed << std::setprecision(3) << results.updateTime;
std::cout << std::setw(15) << results.updateOps;
double opsPerSec = results.updateTime > 0 ? results.updateOps / results.updateTime : 0;
std::cout << std::setw(15) << std::fixed << std::setprecision(1) << opsPerSec;
}
std::cout << std::endl;
// Delete results
std::cout << std::left << std::setw(20) << "Delete";
for (const auto& test : tests) {
const auto& results = test->results;
std::cout << std::setw(15) << std::fixed << std::setprecision(3) << results.deleteTime;
std::cout << std::setw(15) << results.deleteOps;
double opsPerSec = results.deleteTime > 0 ? results.deleteOps / results.deleteTime : 0;
std::cout << std::setw(15) << std::fixed << std::setprecision(1) << opsPerSec;
}
std::cout << std::endl;
// Parallel results
std::cout << std::left << std::setw(20) << "Parallel";
for (const auto& test : tests) {
const auto& results = test->results;
std::cout << std::setw(15) << std::fixed << std::setprecision(3) << results.parallelTime;
std::cout << std::setw(15) << results.parallelOps;
double opsPerSec = results.parallelTime > 0 ? results.parallelOps / results.parallelTime : 0;
std::cout << std::setw(15) << std::fixed << std::setprecision(1) << opsPerSec;
}
std::cout << std::endl;
// Generate comparison ratios
if (tests.size() >= 2) {
std::cout << "\n===== Performance Comparison =====" << std::endl;
std::cout << "Ratio of AnuDB to SQLite3 (higher means AnuDB is faster)" << std::endl;
const auto& anudbResults = tests[0]->results;
const auto& sqliteResults = tests[1]->results;
std::cout << std::left << std::setw(20) << "Operation" << std::setw(15) << "Time Ratio" << std::setw(15) << "Ops/s Ratio" << std::endl;
// Calculate ratios for each operation
// Insert ratio
double insertTimeRatio = sqliteResults.insertTime / anudbResults.insertTime;
double insertOpsRatio = (anudbResults.insertOps / anudbResults.insertTime) / (sqliteResults.insertOps / sqliteResults.insertTime);
std::cout << std::left << std::setw(20) << "Insert"
<< std::setw(15) << std::fixed << std::setprecision(2) << insertTimeRatio
<< std::setw(15) << std::fixed << std::setprecision(2) << insertOpsRatio << std::endl;
// Query ratio
double queryTimeRatio = sqliteResults.queryTime / anudbResults.queryTime;
double queryOpsRatio = (anudbResults.queryOps / anudbResults.queryTime) / (sqliteResults.queryOps / sqliteResults.queryTime);
std::cout << std::left << std::setw(20) << "Query"
<< std::setw(15) << std::fixed << std::setprecision(2) << queryTimeRatio
<< std::setw(15) << std::fixed << std::setprecision(2) << queryOpsRatio << std::endl;
// Update ratio
double updateTimeRatio = sqliteResults.updateTime / anudbResults.updateTime;
double updateOpsRatio = (anudbResults.updateOps / anudbResults.updateTime) / (sqliteResults.updateOps / sqliteResults.updateTime);
std::cout << std::left << std::setw(20) << "Update"
<< std::setw(15) << std::fixed << std::setprecision(2) << updateTimeRatio
<< std::setw(15) << std::fixed << std::setprecision(2) << updateOpsRatio << std::endl;
// Delete ratio
double deleteTimeRatio = sqliteResults.deleteTime / anudbResults.deleteTime;
double deleteOpsRatio = (anudbResults.deleteOps / anudbResults.deleteTime) / (sqliteResults.deleteOps / sqliteResults.deleteTime);
std::cout << std::left << std::setw(20) << "Delete"
<< std::setw(15) << std::fixed << std::setprecision(2) << deleteTimeRatio
<< std::setw(15) << std::fixed << std::setprecision(2) << deleteOpsRatio << std::endl;
// Parallel ratio
double parallelTimeRatio = sqliteResults.parallelTime / anudbResults.parallelTime;
double parallelOpsRatio = (anudbResults.parallelOps / anudbResults.parallelTime) / (sqliteResults.parallelOps / sqliteResults.parallelTime);
std::cout << std::left << std::setw(20) << "Parallel"
<< std::setw(15) << std::fixed << std::setprecision(2) << parallelTimeRatio
<< std::setw(15) << std::fixed << std::setprecision(2) << parallelOpsRatio << std::endl;
}
// Generate benchmark report for visualization
// Write CSV file for easy import into graphing tools
std::ofstream reportFile("benchmark_results.csv");
if (reportFile.is_open()) {
reportFile << "Database,Operation,Time(s),Operations,Ops/s\n";
for (const auto& test : tests) {
const auto& results = test->results;
const std::string& dbName = test->getName();
// Insert
reportFile << dbName << ",Insert," << results.insertTime << ","
<< results.insertOps << "," << (results.insertTime > 0 ? results.insertOps / results.insertTime : 0) << "\n";
// Query
reportFile << dbName << ",Query," << results.queryTime << ","
<< results.queryOps << "," << (results.queryTime > 0 ? results.queryOps / results.queryTime : 0) << "\n";
// Update
reportFile << dbName << ",Update," << results.updateTime << ","
<< results.updateOps << "," << (results.updateTime > 0 ? results.updateOps / results.updateTime : 0) << "\n";
// Delete
reportFile << dbName << ",Delete," << results.deleteTime << ","
<< results.deleteOps << "," << (results.deleteTime > 0 ? results.deleteOps / results.deleteTime : 0) << "\n";
// Parallel
reportFile << dbName << ",Parallel," << results.parallelTime << ","
<< results.parallelOps << "," << (results.parallelTime > 0 ? results.parallelOps / results.parallelTime : 0) << "\n";
}
reportFile.close();
std::cout << "\nBenchmark results saved to 'benchmark_results.csv'" << std::endl;
}
}
int main() {
std::cout << "AnuDB Load Testing Benchmark" << std::endl;
std::cout << "=======================================" << std::endl;
runBenchmarks();
return 0;
}