From 309821c985583c4a2787f80dd852b9d0ffe97a20 Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 23 Oct 2019 14:04:57 +0300 Subject: [PATCH] performance tests: free space check test improved (#133) --- tests/performance_tests/free_space_check.h | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/performance_tests/free_space_check.h b/tests/performance_tests/free_space_check.h index f7fb2d5c..9ab54e13 100644 --- a/tests/performance_tests/free_space_check.h +++ b/tests/performance_tests/free_space_check.h @@ -11,6 +11,8 @@ #include "misc_log_ex.h" +namespace fs = boost::filesystem; + std::string exec(const char* cmd) { std::array buffer; @@ -32,11 +34,59 @@ std::string exec(const char* cmd) return result; } +bool try_write_test_file(size_t size_bytes) +{ + + static const std::string filename = "test_out_file"; + static const fs::path filename_p = filename; + + try + { + fs::ofstream s; + s.open(filename, std::ios_base::binary | std::ios_base::out| std::ios::trunc); + if(s.fail()) + return false; + + uint8_t block[32 * 1024] = {}; + crypto::generate_random_bytes(sizeof block, &block); + size_t size_total = 0; + for (size_t i = 0; i < size_bytes / (sizeof block); ++i) + { + s.write((const char*)&block, sizeof block); + size_total += sizeof block; + } + + if (size_bytes > size_total) + s.write((const char*)&block, size_bytes - size_total); + + s.close(); + + size_t size_actual = fs::file_size(filename_p); + CHECK_AND_ASSERT_MES(size_bytes == size_actual, false, "size_bytes = " << size_bytes << ", size_actual = " << size_actual); + + CHECK_AND_ASSERT_MES(fs::remove(filename_p), false, "remove failed"); + } + catch (std::exception& e) + { + LOG_PRINT_RED("caught: " << e.what(), LOG_LEVEL_0); + return false; + } + catch (...) + { + LOG_PRINT_RED("caught unknown exception", LOG_LEVEL_0); + return false; + } + + return true; +} + void free_space_check() { + static const size_t test_file_size = 1024 * 1024; namespace fs = boost::filesystem; std::string output; + bool r = false; #ifdef WIN32 output = exec("dir"); @@ -46,6 +96,9 @@ void free_space_check() LOG_PRINT_L0("test command output:" << std::endl << output); + r = try_write_test_file(test_file_size); + LOG_PRINT_L0("test file write: " << (r ? "OK" : "fail")); + boost::filesystem::path current_path("."); size_t counter = 0; @@ -80,6 +133,12 @@ void free_space_check() si = fs::space(current_path); LOG_PRINT_YELLOW("2) fs::space() : available: " << si.available << ", free: " << si.free << ", capacity: " << si.capacity, LOG_LEVEL_0); + if (!try_write_test_file(test_file_size)) + { + LOG_PRINT_YELLOW("try_write_test_file(" << test_file_size << ") failed", LOG_LEVEL_0); + } + + need_backspace = false; } catch (std::exception& e)