blob: a9dac372548abd3abbc8c5faa0feacd9204cb362 [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>
Mike Kleincc300a12016-10-12 16:25:27 -040013#include <limits>
mtklein7e602c22016-07-11 11:27:30 -070014#include <regex>
15#include <stdio.h>
Mike Klein645999f2016-09-28 14:00:51 -040016#include <stdlib.h>
mtklein7e602c22016-07-11 11:27:30 -070017#include <string>
18#include <vector>
19
20int main(int argc, char** argv) {
21 SkGraphics::Init();
mtklein2052f312016-07-12 14:50:28 -070022 SkTaskGroup::Enabler enabled;
mtklein7e602c22016-07-11 11:27:30 -070023
24 using clock = std::chrono::high_resolution_clock;
25 using ns = std::chrono::duration<double, std::nano>;
26
27 std::regex pattern;
Mike Klein645999f2016-09-28 14:00:51 -040028 int limit = 2147483647;
29 if (argc > 1) { pattern = argv[1]; }
30 if (argc > 2) { limit = atoi(argv[2]); }
mtklein7e602c22016-07-11 11:27:30 -070031
32 struct Bench {
33 std::unique_ptr<Benchmark> b;
34 std::string name;
35 ns best;
36 };
37 std::vector<Bench> benches;
38
39 for (auto r = BenchRegistry::Head(); r; r = r->next()) {
40 std::unique_ptr<Benchmark> bench{ r->factory()(nullptr) };
41
42 std::string name = bench->getName();
43 if (std::regex_search(name, pattern) &&
44 bench->isSuitableFor(Benchmark::kNonRendering_Backend)) {
45 bench->delayedSetup();
Mike Kleincc300a12016-10-12 16:25:27 -040046 benches.emplace_back(Bench{std::move(bench), name,
47 ns{std::numeric_limits<double>::infinity()}});
mtklein7e602c22016-07-11 11:27:30 -070048 }
49 }
50
Mike Klein645999f2016-09-28 14:00:51 -040051 if (benches.size() == 0) {
52 printf("No bench matched.\n");
53 return 1;
54 }
55
mtklein7e602c22016-07-11 11:27:30 -070056 if (benches.size() > 1) {
57 int common_prefix = benches[0].name.size();
58 for (size_t i = 1; i < benches.size(); i++) {
59 int len = std::mismatch(benches[i-1].name.begin(), benches[i-1].name.end(),
60 benches[i-0].name.begin())
61 .first - benches[i-1].name.begin();
62 common_prefix = std::min(common_prefix, len);
63 }
64 std::string prefix = benches[0].name.substr(0, common_prefix);
65 if (common_prefix) {
66 for (auto& bench : benches) {
67 bench.name.replace(0, common_prefix, "…");
68 }
69 }
70
71 int common_suffix = benches[0].name.size();
72 for (size_t i = 1; i < benches.size(); i++) {
73 int len = std::mismatch(benches[i-1].name.rbegin(), benches[i-1].name.rend(),
74 benches[i-0].name.rbegin())
75 .first - benches[i-1].name.rbegin();
76 common_suffix = std::min(common_suffix, len);
77 }
78 std::string suffix = benches[0].name.substr(benches[0].name.size() - common_suffix);
79 if (common_suffix) {
80 for (auto& bench : benches) {
81 bench.name.replace(bench.name.size() - common_suffix, common_suffix, "…");
82 }
83 }
84
85 printf("%s…%s\n", prefix.c_str(), suffix.c_str());
86 }
87
88 int samples = 0;
Mike Klein645999f2016-09-28 14:00:51 -040089 while (samples < limit) {
mtklein7e602c22016-07-11 11:27:30 -070090 for (auto& bench : benches) {
91 for (int loops = 1; loops < 1000000000;) {
92 bench.b->preDraw(nullptr);
93 auto start = clock::now();
94 bench.b->draw(loops, nullptr);
95 ns elapsed = clock::now() - start;
96 bench.b->postDraw(nullptr);
97
98 if (elapsed < std::chrono::milliseconds{10}) {
99 loops *= 2;
100 continue;
101 }
102
103 bench.best = std::min(bench.best, elapsed / loops);
104 samples++;
105
106 std::sort(benches.begin(), benches.end(), [](const Bench& a, const Bench& b) {
107 return a.best < b.best;
108 });
109 printf("\r\033[K%d", samples);
110 for (auto& bench : benches) {
111 if (benches.size() == 1) {
112 printf(" %s %gns" , bench.name.c_str(), bench.best.count());
113 } else {
114 printf(" %s %.3gx", bench.name.c_str(), bench.best / benches[0].best);
115 }
116 }
117 fflush(stdout);
118 break;
119 }
120 }
121 }
122
123 return 0;
124}