Reworked benchmark code, store per frame times (instead of iteration totals)

Refs #269
This commit is contained in:
saschawillems 2017-08-28 20:48:52 +02:00
parent b42d0edb3b
commit 107db7d244
2 changed files with 49 additions and 27 deletions

View file

@ -17,30 +17,40 @@
namespace vks namespace vks
{ {
class Benchmark { class Benchmark {
private:
FILE *stream;
public: public:
bool active = false; bool active = false;
uint32_t framesPerIteration = 1000; uint32_t framesPerIteration = 1000;
uint32_t iterations = 10; uint32_t iterationCount = 10;
std::vector<double> iterationTimes; //std::vector<double> iterationTimes;
struct Iteration {
std::vector<double> frameTimes;
};
std::vector<Iteration> iterations;
std::string filename = "benchmarkresults.csv"; std::string filename = "benchmarkresults.csv";
void run(std::function<void()> renderFunc) { void run(std::function<void()> renderFunc) {
active = true; active = true;
#if defined(_WIN32) #if defined(_WIN32)
AttachConsole(ATTACH_PARENT_PROCESS); AttachConsole(ATTACH_PARENT_PROCESS);
FILE *stream;
freopen_s(&stream, "CONOUT$", "w+", stdout); freopen_s(&stream, "CONOUT$", "w+", stdout);
freopen_s(&stream, "CONOUT$", "w+", stderr); freopen_s(&stream, "CONOUT$", "w+", stderr);
#endif #endif
iterationTimes.resize(iterations); // "Warm up" run to get more stable frame rates
for (uint32_t i = 0; i < iterations; i++) { for (uint32_t f = 0; f < framesPerIteration; f++) {
renderFunc();
}
iterations.resize(iterationCount);
for (uint32_t i = 0; i < iterationCount; i++) {
iterations[i].frameTimes.resize(framesPerIteration);
for (uint32_t f = 0; f < framesPerIteration; f++) { for (uint32_t f = 0; f < framesPerIteration; f++) {
auto tStart = std::chrono::high_resolution_clock::now(); auto tStart = std::chrono::high_resolution_clock::now();
renderFunc(); renderFunc();
auto tEnd = std::chrono::high_resolution_clock::now(); auto tEnd = std::chrono::high_resolution_clock::now();
auto tDiff = std::chrono::duration<double, std::milli>(tEnd - tStart).count(); auto tDiff = std::chrono::duration<double, std::milli>(tEnd - tStart).count();
double frameTime = (double)tDiff / 1000.0; iterations[i].frameTimes[f] = tDiff;
iterationTimes[i] += frameTime;
} }
} }
} }
@ -48,30 +58,42 @@ namespace vks
void saveResults(std::string appinfo, std::string deviceinfo) { void saveResults(std::string appinfo, std::string deviceinfo) {
std::ofstream result(filename, std::ios::out); std::ofstream result(filename, std::ios::out);
if (result.is_open()) { if (result.is_open()) {
double tMinAll = std::numeric_limits<double>::max();
double tMaxAll = std::numeric_limits<double>::min();
double tAvgAll = 0.0;
double tMin = *std::min_element(iterationTimes.begin(), iterationTimes.end()); result << std::fixed << std::setprecision(4);
double tMax = *std::max_element(iterationTimes.begin(), iterationTimes.end()); result << "iteration,min(ms),max(ms),avg(ms),min(fps),max(fps),avg(fps)" << std::endl;
double tAvg = std::accumulate(iterationTimes.begin(), iterationTimes.end(), 0.0) / iterations; uint32_t index = 0;
for (auto iteration : iterations) {
result << std::fixed << std::setprecision(3); double tMin = *std::min_element(iteration.frameTimes.begin(), iteration.frameTimes.end());
result << appinfo << std::endl; double tMax = *std::max_element(iteration.frameTimes.begin(), iteration.frameTimes.end());
result << deviceinfo << std::endl; double tAvg = std::accumulate(iteration.frameTimes.begin(), iteration.frameTimes.end(), 0.0) / framesPerIteration;
result << ",iterations,time(ms),fps" << std::endl;; if (tMin < tMinAll) {
for (size_t i = 0; i < iterationTimes.size(); i++) { tMinAll = tMin;
result << "," << i << "," << iterationTimes[i] << "," << (framesPerIteration / iterationTimes[i]) << std::endl;
} }
result << ",summary" << std::endl; if (tMax > tMaxAll) {
result << ",,time(ms),fps" << std::endl; tMaxAll = tMax;
result << ",min," << tMin << "," << (1000.0 / tMin) << std::endl; }
result << ",max," << tMax << "," << (1000.0 / tMax) << std::endl; tAvgAll += tAvg;
result << ",avg," << tAvg << "," << (1000.0 / tAvg) << std::endl; index++;
result << index << "," << tMin << "," << tMax << "," << tAvg << "," << (1000.0 / tMax) << "," << (1000.0 / tMin) << "," << (1000.0 / tAvg) << std::endl;
}
tAvgAll /= static_cast<uint32_t>(iterations.size());
result << "summary,min(ms),max(ms),avg(ms),min(fps),max(fps),avg(fps)" << std::endl;
result << index << "," << tMinAll << "," << tMaxAll << "," << tAvgAll << "," << (1000.0 / tMaxAll) << "," << (1000.0 / tMinAll) << "," << (1000.0 / tAvgAll) << std::endl;
// Output averages to stdout // Output averages to stdout
std::cout << std::fixed << std::setprecision(3); std::cout << std::fixed << std::setprecision(3);
std::cout << "best : " << (1000.0 / tMin) << " fps (" << tMin << " ms)" << std::endl; std::cout << "best : " << (1000.0 / tMinAll) << " fps (" << tMinAll << " ms)" << std::endl;
std::cout << "worst: " << (1000.0 / tMax) << " fps (" << tMax << " ms)" << std::endl; std::cout << "worst: " << (1000.0 / tMaxAll) << " fps (" << tMaxAll << " ms)" << std::endl;
std::cout << "avg : " << (1000.0 / tAvg) << " fps (" << tAvg << " ms)" << std::endl; std::cout << "avg : " << (1000.0 / tAvgAll) << " fps (" << tAvgAll << " ms)" << std::endl;
std::cout << std::flush; std::cout << std::endl;
#if defined(_WIN32)
fclose(stream);
FreeConsole();
#endif
} }
} }
}; };

View file

@ -672,7 +672,7 @@ VulkanExampleBase::VulkanExampleBase(bool enableValidation)
if (args.size() > i + 2) { if (args.size() > i + 2) {
char* endptr; char* endptr;
uint32_t iterations = strtol(args[i + 2], &endptr, 10); uint32_t iterations = strtol(args[i + 2], &endptr, 10);
if (endptr != args[i + 2]) { benchmark.iterations = iterations; }; if (endptr != args[i + 2]) { benchmark.iterationCount = iterations; };
} }
} }
} }