From b4c15425975a943a5bc957f01e2edf13cce997a6 Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 16 Oct 2019 19:24:18 +0300 Subject: [PATCH] performance_tests: free_space_check (#133) --- tests/performance_tests/free_space_check.h | 87 ++++++++++++++++++++++ tests/performance_tests/main.cpp | 5 +- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tests/performance_tests/free_space_check.h diff --git a/tests/performance_tests/free_space_check.h b/tests/performance_tests/free_space_check.h new file mode 100644 index 00000000..16ea0afb --- /dev/null +++ b/tests/performance_tests/free_space_check.h @@ -0,0 +1,87 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include + +#include "misc_log_ex.h" + +std::string exec(const char* cmd) +{ + std::array buffer; + +#if defined(WIN32) + std::unique_ptr pipe(_popen(cmd, "r"), _pclose); +#else + std::unique_ptr pipe(popen(cmd, "r"), pclose); +#endif + + if (!pipe) + throw std::runtime_error("popen() failed!"); + + + std::string result; + while (fgets(buffer.data(), static_cast(buffer.size()), pipe.get()) != nullptr) + result += buffer.data(); + + return result; +} + +void free_space_check() +{ + namespace fs = boost::filesystem; + + std::string output; + +#ifdef WIN32 + output = exec("dir"); +#else + output = exec("df -h"); +#endif + + LOG_PRINT_L0("test command output:" << std::endl << output); + + boost::filesystem::path current_path("."); + + size_t counter = 0; + while (true) + { + std::this_thread::sleep_for(std::chrono::milliseconds( 900 )); + try + { + fs::space_info si = fs::space(current_path); + if (si.available > 1024) + { + // free space is ok + counter = (counter + 1) % 4; + std::cout << '\b'; + std::cout << ( counter == 0 ? '*' : counter == 1 ? '\\' : counter == 2 ? '|' : '/' ); + continue; + } + // free space is not ok! + LOG_PRINT_YELLOW("free space available: " << si.available, LOG_LEVEL_0); +#ifdef WIN32 + output = exec("dir"); +#else + output = exec("df -h"); +#endif + LOG_PRINT_YELLOW(output, LOG_LEVEL_0); + } + catch (std::exception& e) + { + LOG_ERROR("failed to determine free space: " << e.what()); + } + catch (...) + { + LOG_ERROR("failed to determine free space: unknown exception"); + } + } + + +} + diff --git a/tests/performance_tests/main.cpp b/tests/performance_tests/main.cpp index dfd93054..8c04a1bf 100644 --- a/tests/performance_tests/main.cpp +++ b/tests/performance_tests/main.cpp @@ -20,6 +20,7 @@ #include "keccak_test.h" #include "blake2_test.h" #include "print_struct_to_json.h" +#include "free_space_check.h" int main(int argc, char** argv) { @@ -36,7 +37,9 @@ int main(int argc, char** argv) //test_blake2(); - print_struct_to_json(); + free_space_check(); + + //print_struct_to_json(); //performance_timer timer; //timer.start();