mtklein | 7e602c2 | 2016-07-11 11:27:30 -0700 | [diff] [blame] | 1 | /* |
| 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" |
mtklein | 2052f31 | 2016-07-12 14:50:28 -0700 | [diff] [blame] | 10 | #include "SkTaskGroup.h" |
mtklein | 7e602c2 | 2016-07-11 11:27:30 -0700 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | #include <chrono> |
| 13 | #include <regex> |
| 14 | #include <stdio.h> |
Mike Klein | 645999f | 2016-09-28 14:00:51 -0400 | [diff] [blame^] | 15 | #include <stdlib.h> |
mtklein | 7e602c2 | 2016-07-11 11:27:30 -0700 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <vector> |
| 18 | |
| 19 | int main(int argc, char** argv) { |
| 20 | SkGraphics::Init(); |
mtklein | 2052f31 | 2016-07-12 14:50:28 -0700 | [diff] [blame] | 21 | SkTaskGroup::Enabler enabled; |
mtklein | 7e602c2 | 2016-07-11 11:27:30 -0700 | [diff] [blame] | 22 | |
| 23 | using clock = std::chrono::high_resolution_clock; |
| 24 | using ns = std::chrono::duration<double, std::nano>; |
| 25 | |
| 26 | std::regex pattern; |
Mike Klein | 645999f | 2016-09-28 14:00:51 -0400 | [diff] [blame^] | 27 | int limit = 2147483647; |
| 28 | if (argc > 1) { pattern = argv[1]; } |
| 29 | if (argc > 2) { limit = atoi(argv[2]); } |
mtklein | 7e602c2 | 2016-07-11 11:27:30 -0700 | [diff] [blame] | 30 | |
| 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 Klein | 645999f | 2016-09-28 14:00:51 -0400 | [diff] [blame^] | 49 | if (benches.size() == 0) { |
| 50 | printf("No bench matched.\n"); |
| 51 | return 1; |
| 52 | } |
| 53 | |
mtklein | 7e602c2 | 2016-07-11 11:27:30 -0700 | [diff] [blame] | 54 | 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 Klein | 645999f | 2016-09-28 14:00:51 -0400 | [diff] [blame^] | 87 | while (samples < limit) { |
mtklein | 7e602c2 | 2016-07-11 11:27:30 -0700 | [diff] [blame] | 88 | 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 | } |