Skip to content

Instantly share code, notes, and snippets.

@divinity76
Created January 22, 2024 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divinity76/dc54f5df4d36aae1050c83d32fc789e3 to your computer and use it in GitHub Desktop.
Save divinity76/dc54f5df4d36aae1050c83d32fc789e3 to your computer and use it in GitHub Desktop.
test system resource limits (ram allocation, open file handles, concurrent threads)
#include <iostream>
#include <fstream>
#include <thread>
#include <vector>
#include <string>
#include <cstdlib>
char **global_argv;
void testMemory() {
std::cout << "Starting Memory Test" << std::endl;
std::vector<char*> allocations;
try {
while (true) {
char* block = new char[1024]; // Allocate 1KB at a time
for(int i = 0; i < 1024; ++i) {
block[i] = i % 256;
}
allocations.push_back(block);
if (allocations.size() % 100000 == 0) { // Print status every 100,000 KB
std::cout << "Allocated " << allocations.size() / 1000 << " MB" << std::endl;
}
}
} catch (std::bad_alloc& e) {
std::cout << "Failed after allocating " << allocations.size() / 1000 << " MB" << std::endl;
// Free memory
for (char* block : allocations) {
delete[] block;
}
}
}
void testFiles() {
std::cout << "Starting File Handle Test" << std::endl;
std::vector<std::ifstream> files;
int count = 0;
while (true) {
try {
files.emplace_back(global_argv[0]);
count++;
if (count % 1000 == 0) { // Print status every 1000 files
std::cout << "Opened " << count << " files" << std::endl;
}
} catch (...) {
std::cout << "Failed after opening " << count << " files" << std::endl;
break;
}
}
}
void threadFunction() {
std::this_thread::sleep_for(std::chrono::hours(1));
}
void testThreads() {
std::cout << "Starting Thread Test" << std::endl;
std::vector<std::thread> threads;
int count = 0;
try {
while (true) {
threads.emplace_back(threadFunction);
count++;
if (count % 100 == 0) { // Print status every 100 threads
std::cout << "Started " << count << " threads" << std::endl;
}
}
} catch (...) {
std::cout << "Failed after starting " << count << " threads" << std::endl;
for (auto& thread : threads) {
if (thread.joinable()) {
thread.join();
}
}
}
}
void runAllTests(const std::string& programName) {
std::string commandMemory = programName + " memory";
std::string commandFiles = programName + " files";
std::string commandThreads = programName + " threads";
std::cout << "Starting Memory Test" << std::endl;
if (system(commandMemory.c_str()) != 0) {
std::cerr << "Memory test failed or was interrupted" << std::endl;
}
std::cout << "Starting File Handle Test" << std::endl;
if (system(commandFiles.c_str()) != 0) {
std::cerr << "File handle test failed or was interrupted" << std::endl;
}
std::cout << "Starting Thread Test" << std::endl;
if (system(commandThreads.c_str()) != 0) {
std::cerr << "Thread test failed or was interrupted" << std::endl;
}
}
int main(int argc, char* argv[]) {
global_argv = argv;
if (argc != 2) {
std::cout << "Usage: program [memory|files|threads|all]" << std::endl;
return 1;
}
std::string arg = argv[1];
if (arg == "memory") {
testMemory();
} else if (arg == "files") {
testFiles();
} else if (arg == "threads") {
testThreads();
} else if (arg == "all") {
runAllTests(argv[0]);
} else {
std::cout << "Invalid argument" << std::endl;
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment