2016-07-11 18:27:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Benchmark.h"
|
2016-10-19 17:27:07 +00:00
|
|
|
#include "OverwriteLine.h"
|
2016-07-11 18:27:30 +00:00
|
|
|
#include "SkGraphics.h"
|
2017-05-15 16:19:39 +00:00
|
|
|
#include "SkSurface.h"
|
2016-07-12 21:50:28 +00:00
|
|
|
#include "SkTaskGroup.h"
|
2016-07-11 18:27:30 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <chrono>
|
2016-10-12 20:25:27 +00:00
|
|
|
#include <limits>
|
2016-07-11 18:27:30 +00:00
|
|
|
#include <regex>
|
2016-09-28 18:00:51 +00:00
|
|
|
#include <stdlib.h>
|
2016-07-11 18:27:30 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-10-19 17:27:07 +00:00
|
|
|
|
|
|
|
#if defined(SK_BUILD_FOR_WIN32)
|
|
|
|
static const char* kEllipsis = "...";
|
|
|
|
#else
|
|
|
|
static const char* kEllipsis = "…";
|
|
|
|
#endif
|
|
|
|
|
2016-07-11 18:27:30 +00:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
SkGraphics::Init();
|
2016-07-12 21:50:28 +00:00
|
|
|
SkTaskGroup::Enabler enabled;
|
2016-07-11 18:27:30 +00:00
|
|
|
|
|
|
|
using clock = std::chrono::high_resolution_clock;
|
|
|
|
using ns = std::chrono::duration<double, std::nano>;
|
|
|
|
|
|
|
|
std::regex pattern;
|
2016-09-28 18:00:51 +00:00
|
|
|
int limit = 2147483647;
|
|
|
|
if (argc > 1) { pattern = argv[1]; }
|
|
|
|
if (argc > 2) { limit = atoi(argv[2]); }
|
2016-07-11 18:27:30 +00:00
|
|
|
|
|
|
|
struct Bench {
|
|
|
|
std::unique_ptr<Benchmark> b;
|
|
|
|
std::string name;
|
|
|
|
ns best;
|
|
|
|
};
|
|
|
|
std::vector<Bench> benches;
|
|
|
|
|
|
|
|
for (auto r = BenchRegistry::Head(); r; r = r->next()) {
|
|
|
|
std::unique_ptr<Benchmark> bench{ r->factory()(nullptr) };
|
|
|
|
|
|
|
|
std::string name = bench->getName();
|
|
|
|
if (std::regex_search(name, pattern) &&
|
2017-05-15 16:19:39 +00:00
|
|
|
(bench->isSuitableFor(Benchmark::kNonRendering_Backend) ||
|
|
|
|
bench->isSuitableFor(Benchmark:: kRaster_Backend))) {
|
2016-07-11 18:27:30 +00:00
|
|
|
bench->delayedSetup();
|
2016-10-12 20:25:27 +00:00
|
|
|
benches.emplace_back(Bench{std::move(bench), name,
|
|
|
|
ns{std::numeric_limits<double>::infinity()}});
|
2016-07-11 18:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 18:00:51 +00:00
|
|
|
if (benches.size() == 0) {
|
2016-10-19 17:27:07 +00:00
|
|
|
SkDebugf("No bench matched.\n");
|
2016-09-28 18:00:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-11 18:27:30 +00:00
|
|
|
if (benches.size() > 1) {
|
|
|
|
int common_prefix = benches[0].name.size();
|
|
|
|
for (size_t i = 1; i < benches.size(); i++) {
|
|
|
|
int len = std::mismatch(benches[i-1].name.begin(), benches[i-1].name.end(),
|
|
|
|
benches[i-0].name.begin())
|
|
|
|
.first - benches[i-1].name.begin();
|
|
|
|
common_prefix = std::min(common_prefix, len);
|
|
|
|
}
|
|
|
|
std::string prefix = benches[0].name.substr(0, common_prefix);
|
|
|
|
if (common_prefix) {
|
|
|
|
for (auto& bench : benches) {
|
2016-10-19 17:27:07 +00:00
|
|
|
bench.name.replace(0, common_prefix, kEllipsis);
|
2016-07-11 18:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int common_suffix = benches[0].name.size();
|
|
|
|
for (size_t i = 1; i < benches.size(); i++) {
|
|
|
|
int len = std::mismatch(benches[i-1].name.rbegin(), benches[i-1].name.rend(),
|
|
|
|
benches[i-0].name.rbegin())
|
|
|
|
.first - benches[i-1].name.rbegin();
|
|
|
|
common_suffix = std::min(common_suffix, len);
|
|
|
|
}
|
|
|
|
std::string suffix = benches[0].name.substr(benches[0].name.size() - common_suffix);
|
|
|
|
if (common_suffix) {
|
|
|
|
for (auto& bench : benches) {
|
2016-10-19 17:27:07 +00:00
|
|
|
bench.name.replace(bench.name.size() - common_suffix, common_suffix, kEllipsis);
|
2016-07-11 18:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 17:27:07 +00:00
|
|
|
SkDebugf("%s%s%s\n", prefix.c_str(), kEllipsis, suffix.c_str());
|
2016-07-11 18:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int samples = 0;
|
2016-09-28 18:00:51 +00:00
|
|
|
while (samples < limit) {
|
2017-01-05 19:08:50 +00:00
|
|
|
std::random_shuffle(benches.begin(), benches.end());
|
2016-07-11 18:27:30 +00:00
|
|
|
for (auto& bench : benches) {
|
2017-05-15 16:19:39 +00:00
|
|
|
sk_sp<SkSurface> dst;
|
|
|
|
SkCanvas* canvas = nullptr;
|
|
|
|
if (!bench.b->isSuitableFor(Benchmark::kNonRendering_Backend)) {
|
|
|
|
dst = SkSurface::MakeRaster(SkImageInfo::MakeS32(bench.b->getSize().x(),
|
|
|
|
bench.b->getSize().y(),
|
|
|
|
kPremul_SkAlphaType));
|
|
|
|
canvas = dst->getCanvas();
|
|
|
|
bench.b->perCanvasPreDraw(canvas);
|
|
|
|
}
|
2016-07-11 18:27:30 +00:00
|
|
|
for (int loops = 1; loops < 1000000000;) {
|
2017-05-15 16:19:39 +00:00
|
|
|
|
|
|
|
bench.b->preDraw(canvas);
|
2016-07-11 18:27:30 +00:00
|
|
|
auto start = clock::now();
|
2017-05-15 16:19:39 +00:00
|
|
|
bench.b->draw(loops, canvas);
|
2016-07-11 18:27:30 +00:00
|
|
|
ns elapsed = clock::now() - start;
|
2017-05-15 16:19:39 +00:00
|
|
|
bench.b->postDraw(canvas);
|
2016-07-11 18:27:30 +00:00
|
|
|
|
|
|
|
if (elapsed < std::chrono::milliseconds{10}) {
|
|
|
|
loops *= 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bench.best = std::min(bench.best, elapsed / loops);
|
|
|
|
samples++;
|
|
|
|
|
2017-01-05 19:23:57 +00:00
|
|
|
struct Result { const char* name; ns best; };
|
|
|
|
std::vector<Result> sorted(benches.size());
|
|
|
|
for (size_t i = 0; i < benches.size(); i++) {
|
|
|
|
sorted[i].name = benches[i].name.c_str();
|
|
|
|
sorted[i].best = benches[i].best;
|
|
|
|
}
|
|
|
|
std::sort(sorted.begin(), sorted.end(), [](const Result& a, const Result& b) {
|
2016-07-11 18:27:30 +00:00
|
|
|
return a.best < b.best;
|
|
|
|
});
|
2017-01-05 19:23:57 +00:00
|
|
|
|
2016-10-19 17:27:07 +00:00
|
|
|
SkDebugf("%s%d", kSkOverwriteLine, samples);
|
2017-01-05 19:23:57 +00:00
|
|
|
for (auto& result : sorted) {
|
|
|
|
if (sorted.size() == 1) {
|
|
|
|
SkDebugf(" %s %gns" , result.name, result.best.count());
|
2016-07-11 18:27:30 +00:00
|
|
|
} else {
|
2017-01-05 19:23:57 +00:00
|
|
|
SkDebugf(" %s %.3gx", result.name, result.best / sorted[0].best);
|
2016-07-11 18:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-05-15 16:19:39 +00:00
|
|
|
if (canvas) {
|
|
|
|
bench.b->perCanvasPostDraw(canvas);
|
|
|
|
}
|
2016-07-11 18:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|