-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_optimization_standalone.cpp
More file actions
129 lines (107 loc) · 5.46 KB
/
test_optimization_standalone.cpp
File metadata and controls
129 lines (107 loc) · 5.46 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
/*
╔═════════════════════════════════════════════════════════════════════╗
║ ThemisDB - Hybrid Database System ║
╠═════════════════════════════════════════════════════════════════════╣
File: test_optimization_standalone.cpp ║
Version: 0.0.34 ║
Last Modified: 2026-03-09 03:52:20 ║
Author: unknown ║
╠═════════════════════════════════════════════════════════════════════╣
Quality Metrics: ║
• Maturity Level: 🟢 PRODUCTION-READY ║
• Quality Score: 99.0/100 ║
• Total Lines: 128 ║
• Open Issues: TODOs: 0, Stubs: 0 ║
╠═════════════════════════════════════════════════════════════════════╣
Revision History: ║
• 2a1fb0423 2026-03-03 Merge branch 'develop' into copilot/audit-src-module-docu... ║
• a629043ab 2026-02-22 Audit: document gaps found - benchmarks and stale annotat... ║
╠═════════════════════════════════════════════════════════════════════╣
Status: ✅ Production Ready ║
╚═════════════════════════════════════════════════════════════════════╝
*/
// Standalone test for concurrent_cache and RocksDB metrics
#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
#include <cassert>
#define THEMIS_DEBUG(fmt, ...) std::cout << "[DEBUG] " << fmt << std::endl
#define THEMIS_INFO(fmt, ...) std::cout << "[INFO] " << fmt << std::endl
#define THEMIS_ERROR(fmt, ...) std::cerr << "[ERROR] " << fmt << std::endl
#include "utils/concurrent_cache.h"
using namespace themis;
int main() {
std::cout << "=== Testing ConcurrentCache ===" << std::endl;
// Test 1: Basic insert and get
{
ConcurrentCache<int, std::string> cache;
cache.insert(1, "one");
cache.insert(2, "two");
auto val1 = cache.get(1);
auto val2 = cache.get(2);
auto val3 = cache.get(3);
assert(val1.has_value() && val1.value() == "one");
assert(val2.has_value() && val2.value() == "two");
assert(!val3.has_value());
std::cout << "✅ Test 1: Basic insert/get passed" << std::endl;
}
// Test 2: Concurrent inserts
{
ConcurrentCache<int, int> cache;
constexpr int num_threads = 8;
constexpr int items_per_thread = 1000;
std::vector<std::thread> threads;
for (int t = 0; t < num_threads; ++t) {
threads.emplace_back([&cache, t]() {
for (int i = 0; i < items_per_thread; ++i) {
int key = t * items_per_thread + i;
cache.insert(key, key * 2);
}
});
}
for (auto& th : threads) {
th.join();
}
assert(cache.size() == num_threads * items_per_thread);
std::cout << "✅ Test 2: Concurrent inserts (" << cache.size() << " items) passed" << std::endl;
}
// Test 3: Concurrent read/write
{
ConcurrentCache<int, int> cache;
// Pre-populate
for (int i = 0; i < 1000; ++i) {
cache.insert(i, i);
}
std::atomic<int> read_count{0};
std::atomic<int> write_count{0};
std::vector<std::thread> threads;
// Readers
for (int t = 0; t < 4; ++t) {
threads.emplace_back([&cache, &read_count]() {
for (int i = 0; i < 5000; ++i) {
auto val = cache.get(i % 1000);
if (val.has_value()) {
read_count++;
}
}
});
}
// Writers
for (int t = 0; t < 2; ++t) {
threads.emplace_back([&cache, &write_count]() {
for (int i = 1000; i < 2000; ++i) {
cache.insert(i, i * 3);
write_count++;
}
});
}
for (auto& th : threads) {
th.join();
}
std::cout << "✅ Test 3: Concurrent read/write (reads: " << read_count
<< ", writes: " << write_count << ") passed" << std::endl;
}
std::cout << "\n=== All ConcurrentCache tests passed! ===" << std::endl;
return 0;
}