Skip to content

Commit

Permalink
Got stress-benchmark working on Win, proves fix works!
Browse files Browse the repository at this point in the history
part? of CURA-11360
  • Loading branch information
rburema committed Dec 5, 2023
1 parent b8cee06 commit 9eda366
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/WallToolPaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const std::vector<VariableWidthLines>& WallToolPaths::generate()
return toolpaths_;
}

//if (false)
{ // begin: test fix polygons
using map_pt = mapbox::geometry::point<coord_t>;
using map_ring = mapbox::geometry::linear_ring<coord_t>;
Expand Down
94 changes: 71 additions & 23 deletions stress_benchmark/stress_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -132,21 +135,32 @@ struct Resource
}
};

std::vector<Resource> getResources()
auto getResourceFiles()
{
const auto resource_path = std::filesystem::path(std::source_location::current().file_name()).parent_path().append("resources");

std::vector<Resource> resources;
std::vector<std::pair<std::filesystem::path, std::filesystem::path>> 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<Resource> loadResources(const std::vector<std::pair<std::filesystem::path, std::filesystem::path>>& resource_files)
{
std::vector<Resource> 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;
};

Expand All @@ -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))
Expand All @@ -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;
}
Expand Down Expand Up @@ -230,11 +244,14 @@ int main(int argc, const char** argv)
constexpr std::string_view version = "0.1.0";
const std::map<std::string, docopt::value> 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<std::string> extra_infos;

#ifndef WIN32
const auto resources = loadResources(getResourceFiles());
num_resources = resources.size();

for (const auto& resource : resources)
{
const auto& shapes = resource.polygons();
Expand All @@ -255,64 +272,95 @@ 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);
CloseHandle(process_info.hThread);
}
}
}
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<double>(crash_count) / static_cast<double>(resources.size()) * 100.0;
const double stress_level = static_cast<double>(crash_count) / static_cast<double>(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;
}

0 comments on commit 9eda366

Please sign in to comment.