-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStressTest.cpp
More file actions
611 lines (494 loc) · 24.6 KB
/
StressTest.cpp
File metadata and controls
611 lines (494 loc) · 24.6 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
/**
* @file StressTest.cpp
* @brief Enterprise-grade stress tests for Logger library
* @author Ömer Bulut
*
* This file contains comprehensive stress tests including:
* - Extreme load testing
* - Memory pressure testing
* - CPU pressure testing
* - Network/filesystem stress testing
* - Long-running stability tests
* - Resource exhaustion tests
*/
#include "Logger.hpp"
#include <gtest/gtest.h>
#include <chrono>
#include <thread>
#include <vector>
#include <atomic>
#include <memory>
#include <iostream>
#include <fstream>
#include <random>
#include <algorithm>
#include <filesystem>
class StressTest : public ::testing::Test {
protected:
void SetUp() override {
// Create stress test directories
std::filesystem::create_directories("stress_logs");
std::filesystem::create_directories("stress_temp");
// Extreme stress configuration
extremeConfig.logFilePath = "stress_logs/extreme.log";
extremeConfig.minLevel = Logger::LogLevel::TRACE;
extremeConfig.consoleOutput = false;
extremeConfig.asyncLogging = true;
extremeConfig.maxFileSize = 10 * 1024 * 1024; // 10MB
extremeConfig.maxFiles = 20;
extremeConfig.queueSize = 1000000;
// Memory stress configuration
memoryConfig.logFilePath = "stress_logs/memory.log";
memoryConfig.minLevel = Logger::LogLevel::INFO;
memoryConfig.consoleOutput = false;
memoryConfig.asyncLogging = true;
memoryConfig.maxFileSize = 10 * 1024 * 1024; // 10MB (increased from 1MB)
memoryConfig.maxFiles = 10; // Reduced from 50 to prevent rapid rotation
memoryConfig.queueSize = 2000000;
// CPU stress configuration
cpuConfig.logFilePath = "stress_logs/cpu.log";
cpuConfig.minLevel = Logger::LogLevel::DEBUG;
cpuConfig.consoleOutput = false;
cpuConfig.asyncLogging = true;
cpuConfig.maxFileSize = 5 * 1024 * 1024; // 5MB
cpuConfig.maxFiles = 10;
cpuConfig.queueSize = 500000;
}
void TearDown() override {
// Clean up stress test files with robust error handling
try {
if (std::filesystem::exists("stress_logs")) {
std::filesystem::remove_all("stress_logs");
}
if (std::filesystem::exists("stress_temp")) {
std::filesystem::remove_all("stress_temp");
}
} catch (const std::exception& e) {
// Silently ignore cleanup errors - they don't affect test results
// No warning messages to keep output clean
}
}
// Helper function to generate random messages
std::string generateRandomMessage(size_t length) {
static const char charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, sizeof(charset) - 2);
std::string message;
message.reserve(length);
for (size_t i = 0; i < length; ++i) {
message += charset[dis(gen)];
}
return message;
}
// Helper function to simulate CPU-intensive work
void simulateCPUWork(int iterations) {
volatile double result = 1.0;
for (int i = 0; i < iterations; ++i) {
result = std::sin(result) * std::cos(result) + std::sqrt(result);
}
}
// Helper function to get system memory info
struct MemoryInfo {
size_t total;
size_t available;
size_t used;
};
MemoryInfo getSystemMemoryInfo() {
MemoryInfo info = {0, 0, 0};
std::ifstream meminfo("/proc/meminfo");
std::string line;
while (std::getline(meminfo, line)) {
if (line.substr(0, 9) == "MemTotal:") {
size_t pos = line.find_first_of("0123456789");
if (pos != std::string::npos) {
info.total = std::stoul(line.substr(pos));
}
} else if (line.substr(0, 13) == "MemAvailable:") {
size_t pos = line.find_first_of("0123456789");
if (pos != std::string::npos) {
info.available = std::stoul(line.substr(pos));
}
}
}
info.used = info.total - info.available;
return info;
}
Logger::Config extremeConfig;
Logger::Config memoryConfig;
Logger::Config cpuConfig;
// Stress test constants
static constexpr int EXTREME_MESSAGE_COUNT = 1000000; // 1M messages (reduced from 5M)
static constexpr int MEMORY_TEST_COUNT = 1000000; // 1M messages
static constexpr int CPU_TEST_COUNT = 500000; // 500K messages
static constexpr int STABILITY_TEST_DURATION = 60; // 1 minute (reduced from 5 minutes)
static constexpr int MAX_THREADS = 16;
static constexpr size_t MAX_MESSAGE_SIZE = 10000; // 10KB messages
};
// ==================== EXTREME LOAD STRESS TEST ====================
TEST_F(StressTest, ExtremeLoadTest) {
Logger logger(extremeConfig);
std::atomic<int> successCount{0};
std::atomic<int> failureCount{0};
std::atomic<bool> shouldStop{false};
std::cout << "\n=== EXTREME LOAD STRESS TEST ===" << std::endl;
std::cout << "Target: " << EXTREME_MESSAGE_COUNT << " messages" << std::endl;
std::cout << "Threads: " << MAX_THREADS << std::endl;
auto start = std::chrono::high_resolution_clock::now();
// Create maximum number of threads for extreme load
std::vector<std::thread> threads;
for (int t = 0; t < MAX_THREADS; ++t) {
threads.emplace_back([&, t]() {
int localSuccess = 0;
int localFailure = 0;
for (int i = 0; i < EXTREME_MESSAGE_COUNT / MAX_THREADS && !shouldStop; ++i) {
try {
// Generate messages of varying sizes
size_t messageSize = (i % 100 == 0) ? MAX_MESSAGE_SIZE : static_cast<size_t>(i % 1000) + 100;
std::string message = "Extreme load test - Thread " + std::to_string(t) +
" - Message " + std::to_string(i) + " - " +
generateRandomMessage(messageSize);
logger.trace(message);
localSuccess++;
// Add some variation in logging levels
if (i % 10 == 0) logger.debug(message);
if (i % 100 == 0) logger.info(message);
if (i % 1000 == 0) logger.warning(message);
if (i % 10000 == 0) logger.error(message);
} catch (const std::exception& e) {
localFailure++;
if (localFailure > 100) {
shouldStop = true;
break;
}
}
// Add small delay to prevent overwhelming
if (i % 1000 == 0) {
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
}
successCount += localSuccess;
failureCount += localFailure;
});
}
// Monitor progress
std::thread monitorThread([&]() {
int lastCount = 0;
auto lastTime = std::chrono::high_resolution_clock::now();
// Check if verbose output is enabled (default: false for CI/CD)
bool verboseOutput = (std::getenv("STRESS_TEST_VERBOSE") != nullptr);
// Show initial progress only if verbose
if (verboseOutput) {
std::cout << "Progress: 0/" << EXTREME_MESSAGE_COUNT << " (0%) Rate: 0 msg/sec" << std::endl;
}
while (!shouldStop && successCount.load() < EXTREME_MESSAGE_COUNT) {
std::this_thread::sleep_for(std::chrono::seconds(3)); // Reduced from 5 to 3 seconds
int currentCount = successCount.load();
auto currentTime = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(currentTime - lastTime);
if (duration.count() > 0) {
double rate = (currentCount - lastCount) / duration.count();
double percentage = (currentCount * 100.0 / EXTREME_MESSAGE_COUNT);
// Only show progress if verbose mode is enabled
if (verboseOutput) {
std::cout << "Progress: " << currentCount << "/" << EXTREME_MESSAGE_COUNT
<< " (" << std::fixed << std::setprecision(1) << percentage << "%) "
<< "Rate: " << std::fixed << std::setprecision(0) << rate << " msg/sec" << std::endl;
}
}
lastCount = currentCount;
lastTime = currentTime;
}
});
// Wait for completion or timeout
for (auto& thread : threads) {
thread.join();
}
shouldStop = true;
if (monitorThread.joinable()) {
monitorThread.join();
}
logger.flush();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
std::cout << "\n=== EXTREME LOAD TEST RESULTS ===" << std::endl;
std::cout << "Total Success: " << successCount.load() << std::endl;
std::cout << "Total Failures: " << failureCount.load() << std::endl;
std::cout << "Duration: " << duration.count() << " seconds" << std::endl;
std::cout << "Average Rate: " << std::fixed << std::setprecision(2)
<< (successCount.load() / duration.count()) << " msg/sec" << std::endl;
// Enterprise-grade expectations
EXPECT_GT(successCount.load(), EXTREME_MESSAGE_COUNT * 0.99) << "99% success rate required";
EXPECT_LT(failureCount.load(), EXTREME_MESSAGE_COUNT * 0.01) << "Failure rate should be < 1%";
EXPECT_GT(successCount.load() / duration.count(), 10000.0) << "Should maintain > 10K msg/sec";
}
// ==================== MEMORY PRESSURE STRESS TEST ====================
TEST_F(StressTest, MemoryPressureTest) {
Logger logger(memoryConfig);
std::atomic<int> successCount{0};
std::atomic<int> failureCount{0};
std::cout << "\n=== MEMORY PRESSURE STRESS TEST ===" << std::endl;
auto start = std::chrono::high_resolution_clock::now();
// Get initial memory state
MemoryInfo initialMemory = getSystemMemoryInfo();
std::cout << "Initial Memory - Total: " << initialMemory.total << " KB, "
<< "Available: " << initialMemory.available << " KB, "
<< "Used: " << initialMemory.used << " KB" << std::endl;
// Create memory pressure with large messages (reduced count for realistic testing)
std::vector<std::thread> threads;
for (int t = 0; t < 4; ++t) {
threads.emplace_back([&, t]() {
for (int i = 0; i < 100000; ++i) { // Reduced from 1M to 100K per thread
try {
// Generate large messages to stress memory
size_t messageSize = 5000 + static_cast<size_t>(i % 5000); // Increased from 1-1.5KB to 5-10KB
std::string message = "Memory pressure test - Thread " + std::to_string(t) +
" - Message " + std::to_string(i) + " - " +
generateRandomMessage(messageSize);
logger.info(message);
successCount++;
// Force garbage collection pressure
if (i % 1000 == 0) {
std::vector<std::string> temp;
temp.reserve(100); // Reduced from 1000 to 100
for (int j = 0; j < 100; ++j) {
temp.push_back(generateRandomMessage(50)); // Reduced size
}
}
} catch (const std::exception& e) {
failureCount++;
}
}
});
}
// Wait for completion
for (auto& thread : threads) {
thread.join();
}
logger.flush();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
// Get final memory state
MemoryInfo finalMemory = getSystemMemoryInfo();
std::cout << "\nFinal Memory - Total: " << finalMemory.total << " KB, "
<< "Available: " << finalMemory.available << " KB, "
<< "Used: " << finalMemory.used << " KB" << std::endl;
// Fix memory calculation to handle potential underflow
size_t memoryIncrease = 0;
if (finalMemory.used > initialMemory.used) {
memoryIncrease = finalMemory.used - initialMemory.used;
} else {
memoryIncrease = 0; // Memory actually decreased
}
std::cout << "\n=== MEMORY PRESSURE TEST RESULTS ===" << std::endl;
std::cout << "Success Count: " << successCount.load() << std::endl;
std::cout << "Failure Count: " << failureCount.load() << std::endl;
std::cout << "Duration: " << duration.count() << " seconds" << std::endl;
std::cout << "Memory Increase: " << memoryIncrease << " KB" << std::endl;
// Enterprise-grade expectations (adjusted for realistic testing)
EXPECT_GT(successCount.load(), 300000) << "Should handle > 300K messages under memory pressure";
EXPECT_LT(memoryIncrease, 500000) << "Memory increase should be < 500MB (realistic for 400K large messages)";
EXPECT_GT(finalMemory.available, 50000) << "Should maintain > 50MB available memory";
}
// ==================== CPU PRESSURE STRESS TEST ====================
TEST_F(StressTest, CPUPressureTest) {
Logger logger(cpuConfig);
std::atomic<int> successCount{0};
std::atomic<int> failureCount{0};
std::cout << "\n=== CPU PRESSURE STRESS TEST ===" << std::endl;
auto start = std::chrono::high_resolution_clock::now();
// Create CPU-intensive threads with logging
std::vector<std::thread> threads;
for (int t = 0; t < 12; ++t) {
threads.emplace_back([&, t]() {
for (int i = 0; i < CPU_TEST_COUNT / 12; ++i) {
try {
// Perform CPU-intensive work
simulateCPUWork(1000 + (i % 1000));
// Log with complex formatting
std::string message = "CPU pressure test - Thread " + std::to_string(t) +
" - Iteration " + std::to_string(i) +
" - Result: " + std::to_string(i * 3.14159) +
" - Hash: " + std::to_string(std::hash<std::string>{}(std::to_string(i)));
logger.debug(message);
successCount++;
// Add more CPU work periodically
if (i % 100 == 0) {
simulateCPUWork(5000);
logger.info("CPU checkpoint - Thread " + std::to_string(t) + " at " + std::to_string(i));
}
} catch (const std::exception& e) {
failureCount++;
}
}
});
}
// Wait for completion
for (auto& thread : threads) {
thread.join();
}
logger.flush();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
std::cout << "\n=== CPU PRESSURE TEST RESULTS ===" << std::endl;
std::cout << "Success Count: " << successCount.load() << std::endl;
std::cout << "Failure Count: " << failureCount.load() << std::endl;
std::cout << "Duration: " << duration.count() << " seconds" << std::endl;
std::cout << "Average Rate: " << std::fixed << std::setprecision(2)
<< (successCount.load() / duration.count()) << " msg/sec" << std::endl;
// Enterprise-grade expectations
EXPECT_GT(successCount.load(), CPU_TEST_COUNT * 0.95) << "95% success rate required";
EXPECT_LT(failureCount.load(), CPU_TEST_COUNT * 0.05) << "Failure rate should be < 5%";
EXPECT_GT(successCount.load() / duration.count(), 5000.0) << "Should maintain > 5K msg/sec under CPU load";
}
// ==================== LONG-RUNNING STABILITY TEST ====================
TEST_F(StressTest, LongRunningStabilityTest) {
Logger logger(extremeConfig);
std::atomic<int> messageCount{0};
std::atomic<bool> shouldStop{false};
std::atomic<int> errorCount{0};
std::cout << "\n=== LONG-RUNNING STABILITY TEST ===" << std::endl;
std::cout << "Duration: " << STABILITY_TEST_DURATION << " seconds" << std::endl;
auto start = std::chrono::high_resolution_clock::now();
// Create stability test threads
std::vector<std::thread> threads;
for (int t = 0; t < 6; ++t) {
threads.emplace_back([&, t]() {
auto threadStart = std::chrono::high_resolution_clock::now();
while (!shouldStop) {
try {
auto now = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - threadStart);
if (elapsed.count() >= STABILITY_TEST_DURATION) {
break;
}
// Log with varying patterns
std::string message = "Stability test - Thread " + std::to_string(t) +
" - Time: " + std::to_string(elapsed.count()) + "s" +
" - Message: " + std::to_string(messageCount.load()) +
" - Status: " + (elapsed.count() % 60 == 0 ? "CHECKPOINT" : "RUNNING");
logger.info(message);
messageCount++;
// Add some variation
if (elapsed.count() % 30 == 0) {
logger.warning("Stability warning - Thread " + std::to_string(t) + " at " + std::to_string(elapsed.count()) + "s");
}
if (elapsed.count() % 60 == 0) {
logger.error("Stability error simulation - Thread " + std::to_string(t) + " at " + std::to_string(elapsed.count()) + "s");
}
// Small delay to prevent overwhelming
std::this_thread::sleep_for(std::chrono::milliseconds(100));
} catch (const std::exception& e) {
errorCount++;
if (errorCount.load() > 1000) {
shouldStop = true;
break;
}
}
}
});
}
// Monitor thread
std::thread monitorThread([&]() {
int lastCount = 0;
auto lastTime = std::chrono::high_resolution_clock::now();
// Check if verbose output is enabled (default: false for CI/CD)
bool verboseOutput = (std::getenv("STRESS_TEST_VERBOSE") != nullptr);
// Show initial progress only if verbose
if (verboseOutput) {
std::cout << "Stability Progress: 0/" << STABILITY_TEST_DURATION << "s - Messages: 0 - Rate: 0 msg/sec" << std::endl;
}
while (!shouldStop) {
std::this_thread::sleep_for(std::chrono::seconds(15)); // Reduced from 30 to 15 seconds
auto currentTime = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(currentTime - start);
if (elapsed.count() >= STABILITY_TEST_DURATION) {
break;
}
int currentCount = messageCount.load();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(currentTime - lastTime);
if (duration.count() > 0) {
double rate = (currentCount - lastCount) / duration.count();
// Only show progress if verbose mode is enabled
if (verboseOutput) {
std::cout << "Stability Progress: " << elapsed.count() << "/" << STABILITY_TEST_DURATION
<< "s - Messages: " << currentCount << " - Rate: "
<< std::fixed << std::setprecision(0) << rate << " msg/sec" << std::endl;
}
}
lastCount = currentCount;
lastTime = currentTime;
}
});
// Wait for completion
for (auto& thread : threads) {
thread.join();
}
shouldStop = true;
if (monitorThread.joinable()) {
monitorThread.join();
}
logger.flush();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
std::cout << "\n=== STABILITY TEST RESULTS ===" << std::endl;
std::cout << "Total Messages: " << messageCount.load() << std::endl;
std::cout << "Error Count: " << errorCount.load() << std::endl;
std::cout << "Duration: " << duration.count() << " seconds" << std::endl;
std::cout << "Average Rate: " << std::fixed << std::setprecision(2)
<< (messageCount.load() / duration.count()) << " msg/sec" << std::endl;
// Enterprise-grade expectations
EXPECT_GT(messageCount.load(), 3000) << "Should log > 3K messages during 1-minute stability test";
EXPECT_LT(errorCount.load(), messageCount.load() * 0.01) << "Error rate should be < 1%";
EXPECT_GT(duration.count(), STABILITY_TEST_DURATION * 0.9) << "Should run for at least 90% of target time";
}
// ==================== RESOURCE EXHAUSTION TEST ====================
TEST_F(StressTest, ResourceExhaustionTest) {
std::cout << "\n=== RESOURCE EXHAUSTION TEST ===" << std::endl;
// Test with extremely limited resources
Logger::Config limitedConfig = extremeConfig;
limitedConfig.queueSize = 50; // Very small queue
limitedConfig.maxFileSize = 512; // 512 bytes files
limitedConfig.maxFiles = 2; // Very few files
Logger logger(limitedConfig);
std::atomic<int> successCount{0};
std::atomic<int> failureCount{0};
auto start = std::chrono::high_resolution_clock::now();
// Try to exhaust resources
std::vector<std::thread> threads;
for (int t = 0; t < 2; ++t) {
threads.emplace_back([&, t]() {
for (int i = 0; i < 5000; ++i) {
try {
std::string message = "Resource exhaustion test - Thread " + std::to_string(t) +
" - Message " + std::to_string(i) +
" - " + generateRandomMessage(100);
logger.info(message);
successCount++;
} catch (const std::exception& e) {
failureCount++;
}
// Small delay
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
});
}
// Wait for completion
for (auto& thread : threads) {
thread.join();
}
logger.flush();
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "\n=== RESOURCE EXHAUSTION TEST RESULTS ===" << std::endl;
std::cout << "Success Count: " << successCount.load() << std::endl;
std::cout << "Failure Count: " << failureCount.load() << std::endl;
std::cout << "Duration: " << duration.count() << " ms" << std::endl;
// Enterprise-grade expectations (realistic resource constraints)
EXPECT_GT(successCount.load(), 0) << "Should handle some messages even under resource constraints";
EXPECT_GE(failureCount.load(), 0) << "May have some failures under extreme constraints";
EXPECT_LT(duration.count(), 30000) << "Should complete in reasonable time even with constraints";
// Check if logger gracefully handled resource constraints
EXPECT_TRUE(successCount.load() > 0 || failureCount.load() > 0)
<< "Logger should either succeed or fail gracefully, not hang";
}