From 9eda366b211ce27175c4e1c75f8c2cf8f948f183 Mon Sep 17 00:00:00 2001 From: Remco Burema Date: Tue, 5 Dec 2023 23:26:45 +0100 Subject: [PATCH] Got stress-benchmark working on Win, proves fix works! part? of CURA-11360 --- src/WallToolPaths.cpp | 1 + stress_benchmark/stress_benchmark.cpp | 94 ++++++++++++++++++++------- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/src/WallToolPaths.cpp b/src/WallToolPaths.cpp index 558907124e..2eec80af9c 100644 --- a/src/WallToolPaths.cpp +++ b/src/WallToolPaths.cpp @@ -120,6 +120,7 @@ const std::vector& WallToolPaths::generate() return toolpaths_; } + //if (false) { // begin: test fix polygons using map_pt = mapbox::geometry::point; using map_ring = mapbox::geometry::linear_ring; diff --git a/stress_benchmark/stress_benchmark.cpp b/stress_benchmark/stress_benchmark.cpp index 349006757c..ee1738c9a1 100644 --- a/stress_benchmark/stress_benchmark.cpp +++ b/stress_benchmark/stress_benchmark.cpp @@ -43,12 +43,15 @@ Executes a Stress Benchmark on CuraEngine. stress_benchmark [-h | --help] stress_benchmark --version stress_benchmark -r N + stress_benchmark -p FILE -s FILE Options: -h --help Show this screen. --version Show version. -o FILE Specify the output Json file. -r N Run N-th resource/testcase intead of anything else. + -p FILE Run this polygon resource file; '-s' should also be present. + -s FILE Run with these settings; '-p' should also be present. )"; struct Resource @@ -132,21 +135,32 @@ struct Resource } }; -std::vector getResources() +auto getResourceFiles() { const auto resource_path = std::filesystem::path(std::source_location::current().file_name()).parent_path().append("resources"); - std::vector resources; + std::vector> resource_files; for (const auto& p : std::filesystem::recursive_directory_iterator(resource_path)) { if (p.path().extension() == ".wkt") { auto settings = p.path(); settings.replace_extension(".settings"); - spdlog::info("Adding resources for: {}", p.path().filename().stem().string()); - resources.emplace_back(Resource{ .wkt_file = p, .settings_file = settings }); + spdlog::info("Found resources: {}", p.path().filename().stem().string()); + resource_files.push_back({p.path(), settings}); } } + return resource_files; +} + +std::vector loadResources(const std::vector>& resource_files) +{ + std::vector resources; + for (const auto& [p, settings] : resource_files) + { + spdlog::info("Adding resources for: {}", p.filename().stem().string()); + resources.emplace_back(Resource{ .wkt_file = p, .settings_file = settings }); + } return resources; }; @@ -165,7 +179,7 @@ void handleChildProcess(const auto& shapes, const auto& settings) exit(EXIT_SUCCESS); } -size_t checkCrashCount(size_t crashCount, int status, const auto& resource) +size_t checkCrashCount(size_t crashCount, int status, const std::string& resource_name) { #ifndef WIN32 if (WIFSIGNALED(status)) @@ -174,7 +188,7 @@ size_t checkCrashCount(size_t crashCount, int status, const auto& resource) #endif // !WIN32 { ++crashCount; - spdlog::error("Crash detected for: {} (with exit code {}).", resource.stem(), status); + spdlog::error("Crash detected for: {} (with exit code {}).", resource_name, status); } return crashCount; } @@ -230,11 +244,14 @@ int main(int argc, const char** argv) constexpr std::string_view version = "0.1.0"; const std::map args = docopt::docopt(fmt::format("{}", USAGE), { argv + 1, argv + argc }, show_help, fmt::format("{}", version)); - const auto resources = getResources(); + size_t num_resources = 0; size_t crash_count = 0; std::vector extra_infos; #ifndef WIN32 + const auto resources = loadResources(getResourceFiles()); + num_resources = resources.size(); + for (const auto& resource : resources) { const auto& shapes = resource.polygons(); @@ -255,47 +272,57 @@ int main(int argc, const char** argv) int status; waitpid(pid, &status, 0); const auto old_crash_count = crash_count; - crash_count = checkCrashCount(crash_count, status, resource); + crash_count = checkCrashCount(crash_count, status, resource.stem().string()); if (old_crash_count != crash_count) { - extra_infos.emplace_back(resource.stem()); + extra_infos.emplace_back(resource.stem().string()); } } } #else // !WIN32 - - if (args.at("-r").kind() == docopt::Kind::Empty) + const bool arg_has_r = args.at("-r").kind() != docopt::Kind::Empty; + const bool arg_has_p_s = (args.at("-p").kind() != docopt::Kind::Empty) && (args.at("-s").kind() != docopt::Kind::Empty); + if ((! arg_has_r) && (! arg_has_p_s)) { + const auto resource_files = getResourceFiles(); + num_resources = resource_files.size(); + constexpr auto bufflen = 256; char buff[bufflen]; - for (const auto& [r, resource] : resources | ranges::views::enumerate) + for (const auto& [r, resource] : resource_files | ranges::views::enumerate) { - const auto path = std::filesystem::path(std::source_location::current().file_name()); - std::snprintf(buff, bufflen, "C:\\Windows\\System32\\cmd.exe /C %s -r %lld", path.string().c_str(), r); // Couldn't use fmt here for some reason :-/ + if (! GetModuleFileName(nullptr, buff, bufflen)) + { + spdlog::critical("Coudln't get current executable file location."); + return EXIT_FAILURE; + } + std::string exec_file{ buff }; + std::snprintf(buff, bufflen, "%s -p %s -s %s", exec_file.c_str(), resource.first.string().c_str(), resource.second.string().c_str()); // Couldn't use fmt here for some reason :-/ STARTUPINFO info = { sizeof(info) }; PROCESS_INFORMATION process_info; if (! CreateProcess(nullptr, buff, nullptr, nullptr, true, 0, nullptr, nullptr, &info, &process_info)) { - spdlog::critical("Unable to CreateProcess"); + spdlog::critical("Unable to CreateProcess '{}'", buff); return EXIT_FAILURE; } else { DWORD status = 0; - WaitForSingleObject(process_info.hProcess, INFINITE); + WaitForSingleObject(process_info.hProcess, 30000); if (! GetExitCodeProcess(process_info.hProcess, &status)) { - spdlog::critical("Unable to get exit-code"); + spdlog::critical("Unable to get exit-code for '{}'", buff); return EXIT_FAILURE; } + const auto filename_stem = resource.first.stem().string(); const auto old_crash_count = crash_count; - crash_count = checkCrashCount(crash_count, status, resource); + crash_count = checkCrashCount(crash_count, status, filename_stem); if (old_crash_count != crash_count) { - extra_infos.emplace_back(resource.stem()); + extra_infos.emplace_back(filename_stem); } CloseHandle(process_info.hProcess); @@ -303,16 +330,37 @@ int main(int argc, const char** argv) } } } - else + else if (arg_has_r) { + const auto resource_files = getResourceFiles(); const int r = args.at("-r").asLong(); - handleChildProcess(resources[r].polygons(), resources[r].settings()); + if (r < 0 || r >= resource_files.size()) + { + spdlog::critical("Resource {} wasn't found, resource files available are 0 through {}", r, resource_files.size()); + return EXIT_FAILURE; + } + spdlog::info("Handling resource #{}: {},{}", r, resource_files[r].first.string(), resource_files[r].second.string()); + const auto resource = Resource{ .wkt_file = resource_files[r].first, .settings_file = resource_files[r].second }; + handleChildProcess(resource.polygons(), resource.settings()); + } + else if (arg_has_p_s) + { + const auto wkt_filename = args.at("-p").asString(); + const auto settings_filename = args.at("-s").asString(); + spdlog::info("Handling resource(s) in {},{}", wkt_filename, settings_filename); + const auto resource = Resource{ .wkt_file = std::filesystem::path(wkt_filename), .settings_file = std::filesystem::path(settings_filename)}; + handleChildProcess(resource.polygons(), resource.settings()); + } + else + { + spdlog::critical("Not sure what to do with the given arguments, specify '-h' or '--help' for options."); + return EXIT_FAILURE; } #endif // !WIN32 - const double stress_level = static_cast(crash_count) / static_cast(resources.size()) * 100.0; + const double stress_level = static_cast(crash_count) / static_cast(num_resources) * 100.0; spdlog::info("Stress level: {:.2f} [%]", stress_level); - createAndWriteJson(std::filesystem::path{ args.at("-o").asString() }, stress_level, fmt::format("Crashes in: {}", fmt::join(extra_infos, ", ")), resources.size()); + createAndWriteJson(std::filesystem::path{ args.at("-o").asString() }, stress_level, fmt::format("Crashes in: {}", fmt::join(extra_infos, ", ")), num_resources); return EXIT_SUCCESS; }