-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstress_test_concurrent.cpp
More file actions
294 lines (246 loc) · 8.68 KB
/
stress_test_concurrent.cpp
File metadata and controls
294 lines (246 loc) · 8.68 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
// Phase 0: Concurrent Stress Test
// Tests thread-safety of PRTree under concurrent operations
// Must pass cleanly under ThreadSanitizer (TSan)
#include "workloads.h"
#include "benchmark_utils.h"
#include <iostream>
#include <vector>
#include <array>
#include <thread>
#include <atomic>
#include <chrono>
#include <mutex>
#include <algorithm>
#include <cassert>
// Simple BB class
template <int D = 2>
class BB {
private:
float values[2 * D];
public:
BB() {
for (int i = 0; i < 2 * D; i++) values[i] = 0.0f;
}
BB(const float *minima, const float *maxima) {
for (int i = 0; i < D; i++) {
values[i] = minima[i];
values[i + D] = maxima[i];
}
}
BB(const std::array<float, 2*D>& arr) {
for (int i = 0; i < 2*D; i++) values[i] = arr[i];
}
inline float min(int i) const { return values[i]; }
inline float max(int i) const { return values[i + D]; }
bool intersects(const BB<D>& other) const {
for (int i = 0; i < D; i++) {
if (max(i) < other.min(i) || min(i) > other.max(i)) {
return false;
}
}
return true;
}
};
// Simple DataType class
template <class T, int D = 2>
class DataType {
public:
T first;
BB<D> second;
DataType() = default;
DataType(const T &f, const BB<D> &s) : first(f), second(s) {}
};
// Thread-safe tree for stress testing
template<typename T, int D>
class ThreadSafeTreeStub {
public:
using BBox = std::array<float, D * 2>;
using Data = DataType<T, D>;
void construct(const std::vector<BBox>& data) {
std::lock_guard<std::mutex> lock(mutex_);
elements_.clear();
elements_.reserve(data.size());
for (size_t i = 0; i < data.size(); ++i) {
float minima[D], maxima[D];
for (int d = 0; d < D; ++d) {
minima[d] = data[i][d];
maxima[d] = data[i][d + D];
}
BB<D> bb(minima, maxima);
elements_.emplace_back(static_cast<T>(i), bb);
}
}
std::vector<T> query(const BBox& query_box) const {
std::lock_guard<std::mutex> lock(mutex_);
std::vector<T> results;
BB<D> query_bb(query_box);
for (const auto& elem : elements_) {
if (elem.second.intersects(query_bb)) {
results.push_back(elem.first);
}
}
return results;
}
size_t size() const {
std::lock_guard<std::mutex> lock(mutex_);
return elements_.size();
}
private:
mutable std::mutex mutex_;
std::vector<Data> elements_;
};
// Test 1: Concurrent queries while rebuilding
void test_concurrent_build_and_query() {
std::cout << "\nTest 1: Concurrent Build and Query\n";
std::cout << std::string(40, '-') << "\n";
constexpr int NUM_QUERY_THREADS = 8;
constexpr int NUM_ITERATIONS = 100;
constexpr int DATASET_SIZE = 1000;
ThreadSafeTreeStub<int64_t, 2> tree;
std::atomic<bool> keep_running{true};
std::atomic<int> query_count{0};
std::atomic<int> build_count{0};
benchmark::DataGenerator<2> generator;
benchmark::WorkloadConfig config("stress_test", DATASET_SIZE,
benchmark::Distribution::UNIFORM,
100, benchmark::QuerySize::SMALL);
auto data = generator.generate(config);
auto queries = generator.generate_queries(config, data);
// Initial build
tree.construct(data);
// Builder thread
std::thread builder([&]() {
for (int i = 0; i < NUM_ITERATIONS; ++i) {
tree.construct(data);
build_count++;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
keep_running = false;
});
// Query threads
std::vector<std::thread> query_threads;
for (int t = 0; t < NUM_QUERY_THREADS; ++t) {
query_threads.emplace_back([&, t]() {
while (keep_running) {
size_t query_idx = query_count % queries.size();
auto results = tree.query(queries[query_idx]);
query_count++;
// Small delay to prevent tight spinning
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
});
}
// Wait for completion
builder.join();
for (auto& th : query_threads) {
th.join();
}
std::cout << "Builds completed: " << build_count << "\n";
std::cout << "Queries completed: " << query_count << "\n";
std::cout << "✓ Test passed - no crashes or data races\n";
}
// Test 2: Concurrent queries from multiple threads
void test_concurrent_queries() {
std::cout << "\nTest 2: Concurrent Queries\n";
std::cout << std::string(40, '-') << "\n";
constexpr int NUM_THREADS = 8;
constexpr int QUERIES_PER_THREAD = 1000;
constexpr int DATASET_SIZE = 10000;
ThreadSafeTreeStub<int64_t, 2> tree;
std::atomic<int> total_queries{0};
benchmark::DataGenerator<2> generator;
benchmark::WorkloadConfig config("stress_test", DATASET_SIZE,
benchmark::Distribution::UNIFORM,
100, benchmark::QuerySize::MEDIUM);
auto data = generator.generate(config);
auto queries = generator.generate_queries(config, data);
// Build tree
tree.construct(data);
// Query threads
std::vector<std::thread> threads;
for (int t = 0; t < NUM_THREADS; ++t) {
threads.emplace_back([&, t]() {
for (int i = 0; i < QUERIES_PER_THREAD; ++i) {
size_t query_idx = (t * QUERIES_PER_THREAD + i) % queries.size();
auto results = tree.query(queries[query_idx]);
total_queries++;
}
});
}
for (auto& th : threads) {
th.join();
}
std::cout << "Total queries completed: " << total_queries << "\n";
assert(total_queries == NUM_THREADS * QUERIES_PER_THREAD);
std::cout << "✓ Test passed\n";
}
// Test 3: Long-running torture test
void test_torture() {
std::cout << "\nTest 3: Torture Test (10 seconds)\n";
std::cout << std::string(40, '-') << "\n";
constexpr int NUM_THREADS = 8;
constexpr int TEST_DURATION_SEC = 10;
ThreadSafeTreeStub<int64_t, 2> tree;
std::atomic<bool> keep_running{true};
std::atomic<long> total_operations{0};
benchmark::DataGenerator<2> generator;
benchmark::WorkloadConfig config("stress_test", 5000,
benchmark::Distribution::UNIFORM,
100, benchmark::QuerySize::MIXED);
auto data = generator.generate(config);
auto queries = generator.generate_queries(config, data);
// Initial build
tree.construct(data);
// Timer thread
std::thread timer([&]() {
std::this_thread::sleep_for(std::chrono::seconds(TEST_DURATION_SEC));
keep_running = false;
});
// Worker threads (mix of builds and queries)
std::vector<std::thread> threads;
for (int t = 0; t < NUM_THREADS; ++t) {
threads.emplace_back([&, t]() {
while (keep_running) {
// 90% queries, 10% rebuilds
if (t == 0 && (total_operations % 10 == 0)) {
tree.construct(data);
} else {
size_t query_idx = total_operations % queries.size();
auto results = tree.query(queries[query_idx]);
}
total_operations++;
std::this_thread::sleep_for(std::chrono::microseconds(500));
}
});
}
timer.join();
for (auto& th : threads) {
th.join();
}
std::cout << "Total operations: " << total_operations << "\n";
std::cout << "Operations/sec: " << (total_operations / TEST_DURATION_SEC) << "\n";
std::cout << "✓ Test passed\n";
}
int main(int argc, char** argv) {
std::cout << "PRTree Phase 0: Concurrent Stress Test\n";
std::cout << "=======================================\n";
std::cout << "\nThis test MUST run clean under ThreadSanitizer!\n";
std::cout << "Build with: cmake -DENABLE_TSAN=ON\n\n";
try {
test_concurrent_build_and_query();
test_concurrent_queries();
test_torture();
std::cout << "\n" << std::string(60, '=') << "\n";
std::cout << "ALL STRESS TESTS PASSED ✓\n";
std::cout << std::string(60, '=') << "\n";
std::cout << "\nNext steps:\n";
std::cout << "1. Run under TSan: ./stress_test_concurrent\n";
std::cout << "2. Check for data race warnings\n";
std::cout << "3. Run for extended period (1 hour)\n";
std::cout << " timeout 3600 ./stress_test_concurrent\n";
return 0;
} catch (const std::exception& e) {
std::cerr << "\n❌ STRESS TEST FAILED: " << e.what() << "\n";
return 1;
}
}