blob: 756e24f101a86ce17ee40e65533d4ad0768e63ce [file] [log] [blame]
mtklein7e602c22016-07-11 11:27:30 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Benchmark.h"
9#include "SkGraphics.h"
mtklein2052f312016-07-12 14:50:28 -070010#include "SkTaskGroup.h"
mtklein7e602c22016-07-11 11:27:30 -070011#include <algorithm>
12#include <chrono>
13#include <regex>
14#include <stdio.h>
Mike Klein645999f2016-09-28 14:00:51 -040015#include <stdlib.h>
mtklein7e602c22016-07-11 11:27:30 -070016#include <string>
17#include <vector>
18
19int main(int argc, char** argv) {
20 SkGraphics::Init();
mtklein2052f312016-07-12 14:50:28 -070021 SkTaskGroup::Enabler enabled;
mtklein7e602c22016-07-11 11:27:30 -070022
23 using clock = std::chrono::high_resolution_clock;
24 using ns = std::chrono::duration<double, std::nano>;
25
26 std::regex pattern;
Mike Klein645999f2016-09-28 14:00:51 -040027 int limit = 2147483647;
28 if (argc > 1) { pattern = argv[1]; }
29 if (argc > 2) { limit = atoi(argv[2]); }
mtklein7e602c22016-07-11 11:27:30 -070030
31 struct Bench {
32 std::unique_ptr<Benchmark> b;
33 std::string name;
34 ns best;
35 };
36 std::vector<Bench> benches;
37
38 for (auto r = BenchRegistry::Head(); r; r = r->next()) {
39 std::unique_ptr<Benchmark> bench{ r->factory()(nullptr) };
40
41 std::string name = bench->getName();
42 if (std::regex_search(name, pattern) &&
43 bench->isSuitableFor(Benchmark::kNonRendering_Backend)) {
44 bench->delayedSetup();
45 benches.emplace_back(Bench{std::move(bench), name, ns{1.0/0.0}});
46 }
47 }
48
Mike Klein645999f2016-09-28 14:00:51 -040049 if (benches.size() == 0) {
50 printf("No bench matched.\n");
51 return 1;
52 }
53
mtklein7e602c22016-07-11 11:27:30 -070054 if (benches.size() > 1) {
55 int common_prefix = benches[0].name.size();
56 for (size_t i = 1; i < benches.size(); i++) {
57 int len = std::mismatch(benches[i-1].name.begin(), benches[i-1].name.end(),
58 benches[i-0].name.begin())
59 .first - benches[i-1].name.begin();
60 common_prefix = std::min(common_prefix, len);
61 }
62 std::string prefix = benches[0].name.substr(0, common_prefix);
63 if (common_prefix) {
64 for (auto& bench : benches) {
65 bench.name.replace(0, common_prefix, "…");
66 }
67 }
68
69 int common_suffix = benches[0].name.size();
70 for (size_t i = 1; i < benches.size(); i++) {
71 int len = std::mismatch(benches[i-1].name.rbegin(), benches[i-1].name.rend(),
72 benches[i-0].name.rbegin())
73 .first - benches[i-1].name.rbegin();
74 common_suffix = std::min(common_suffix, len);
75 }
76 std::string suffix = benches[0].name.substr(benches[0].name.size() - common_suffix);
77 if (common_suffix) {
78 for (auto& bench : benches) {
79 bench.name.replace(bench.name.size() - common_suffix, common_suffix, "…");
80 }
81 }
82
83 printf("%s…%s\n", prefix.c_str(), suffix.c_str());
84 }
85
86 int samples = 0;
Mike Klein645999f2016-09-28 14:00:51 -040087 while (samples < limit) {
mtklein7e602c22016-07-11 11:27:30 -070088 for (auto& bench : benches) {
89 for (int loops = 1; loops < 1000000000;) {
90 bench.b->preDraw(nullptr);
91 auto start = clock::now();
92 bench.b->draw(loops, nullptr);
93 ns elapsed = clock::now() - start;
94 bench.b->postDraw(nullptr);
95
96 if (elapsed < std::chrono::milliseconds{10}) {
97 loops *= 2;
98 continue;
99 }
100
101 bench.best = std::min(bench.best, elapsed / loops);
102 samples++;
103
104 std::sort(benches.begin(), benches.end(), [](const Bench& a, const Bench& b) {
105 return a.best < b.best;
106 });
107 printf("\r\033[K%d", samples);
108 for (auto& bench : benches) {
109 if (benches.size() == 1) {
110 printf(" %s %gns" , bench.name.c_str(), bench.best.count());
111 } else {
112 printf(" %s %.3gx", bench.name.c_str(), bench.best / benches[0].best);
113 }
114 }
115 fflush(stdout);
116 break;
117 }
118 }
119 }
120
121 return 0;
122}