solvespace/bench/harness.cpp
whitequark 1442ee5ec3 Refactor InitPlatform.
This commit performs three related cleanups:
  * The slvs library no longer uses explicit platform initialization
    (which drags in the side effects of InitPlatform that are not
    desirable in a library). Instead, it just ensures that it has
    the temporary heap, which is what it was callingInitPlatform for.
  * InitPlatform is simplified and moved to platform.cpp, next to
    other path related functions.
  * InitPlatform is renamed to InitCli and is called from InitGui
    implementations. GUI toolkits sometimes have options they use
    internally (that's the case for for GTK and Cocoa at least),
    and we shouldn't try to parse those as a file to open.
2020-05-10 08:29:25 +00:00

79 lines
2.2 KiB
C++

//-----------------------------------------------------------------------------
// Our harness for running benchmarks.
//
// Copyright 2016 whitequark
//-----------------------------------------------------------------------------
#include "solvespace.h"
static bool RunBenchmark(std::function<void()> setupFn,
std::function<bool()> benchFn,
std::function<void()> teardownFn,
size_t minIter = 5, double minTime = 5.0) {
// Warmup
setupFn();
if(!benchFn()) {
fprintf(stderr, "Benchmark failed\n");
return false;
}
teardownFn();
// Benchmark
size_t iter = 0;
double time = 0.0;
while(iter < minIter || time < minTime) {
setupFn();
auto testStartTime = std::chrono::steady_clock::now();
benchFn();
auto testEndTime = std::chrono::steady_clock::now();
teardownFn();
std::chrono::duration<double> testTime = testEndTime - testStartTime;
time += testTime.count();
iter += 1;
}
// Report
fprintf(stdout, "Iterations: %zd\n", iter);
fprintf(stdout, "Time: %.3f s\n", time);
fprintf(stdout, "Per iter.: %.3f s\n", time / (double)iter);
return true;
}
int main(int argc, char **argv) {
std::vector<std::string> args = Platform::InitCli(argc, argv);
std::string mode;
Platform::Path filename;
if(args.size() == 3) {
mode = args[1];
filename = Platform::Path::From(args[2]);
} else {
fprintf(stderr, "Usage: %s [mode] [filename]\n", args[0].c_str());
fprintf(stderr, "Mode can be one of: load.\n");
return 1;
}
bool result = false;
if(mode == "load") {
result = RunBenchmark(
[] {
SS.Init();
},
[&] {
if(!SS.LoadFromFile(filename))
return false;
SS.AfterNewFile();
return true;
},
[] {
SK.Clear();
SS.Clear();
});
} else {
fprintf(stderr, "Unknown mode \"%s\"\n", mode.c_str());
}
return (result == true ? 0 : 1);
}