mtklein | f372321 | 2014-06-25 14:08:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 "CrashHandler.h" |
| 10 | #include "Stats.h" |
| 11 | #include "Timer.h" |
| 12 | |
| 13 | #include "SkCanvas.h" |
| 14 | #include "SkCommandLineFlags.h" |
| 15 | #include "SkForceLinking.h" |
| 16 | #include "SkGraphics.h" |
| 17 | #include "SkString.h" |
| 18 | #include "SkSurface.h" |
| 19 | |
| 20 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 21 | |
| 22 | DEFINE_int32(samples, 10, "Number of samples to measure for each bench."); |
| 23 | DEFINE_int32(overheadLoops, 100000, "Loops to estimate timer overhead."); |
| 24 | DEFINE_double(overheadGoal, 0.0001, |
| 25 | "Loop until timer overhead is at most this fraction of our measurments."); |
| 26 | DEFINE_string(match, "", "The usual filters on file names of benchmarks to measure."); |
| 27 | DEFINE_bool2(quiet, q, false, "Print only bench name and minimum sample."); |
| 28 | DEFINE_bool2(verbose, v, false, "Print all samples."); |
| 29 | DEFINE_string(config, "8888 nonrendering", |
| 30 | "Configs to measure. Options: 565 8888 nonrendering"); |
| 31 | |
| 32 | // TODO: GPU benches |
| 33 | |
| 34 | static SkString humanize(double ms) { |
| 35 | if (ms > 1e+3) return SkStringPrintf("%.3gs", ms/1e3); |
| 36 | if (ms < 1e-3) return SkStringPrintf("%.3gns", ms*1e6); |
| 37 | if (ms < 1) return SkStringPrintf("%.3gµs", ms*1e3); |
| 38 | return SkStringPrintf("%.3gms", ms); |
| 39 | } |
| 40 | |
| 41 | static double estimate_timer_overhead() { |
| 42 | double overhead = 0; |
| 43 | WallTimer timer; |
| 44 | for (int i = 0; i < FLAGS_overheadLoops; i++) { |
| 45 | timer.start(); |
| 46 | timer.end(); |
| 47 | overhead += timer.fWall; |
| 48 | } |
| 49 | return overhead / FLAGS_overheadLoops; |
| 50 | } |
| 51 | |
| 52 | static void safe_flush(SkCanvas* canvas) { |
| 53 | if (canvas) { |
| 54 | canvas->flush(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static int guess_loops(double overhead, Benchmark* bench, SkCanvas* canvas) { |
| 59 | WallTimer timer; |
| 60 | |
| 61 | // Measure timer overhead and bench time together. |
| 62 | do { |
| 63 | timer.start(); |
| 64 | bench->draw(1, canvas); |
| 65 | safe_flush(canvas); |
| 66 | timer.end(); |
| 67 | } while (timer.fWall < overhead); // Shouldn't normally happen. |
| 68 | |
| 69 | // Later we'll just start and stop the timer once, but loop N times. |
| 70 | // We'll pick N to make timer overhead negligible: |
| 71 | // |
| 72 | // Timer Overhead |
| 73 | // ------------------------------- < FLAGS_overheadGoal |
| 74 | // Timer Overhead + N * Bench Time |
| 75 | // |
| 76 | // where timer.fWall ≈ Timer Overhead + Bench Time. |
| 77 | // |
| 78 | // Doing some math, we get: |
| 79 | // |
| 80 | // (Timer Overhead / FLAGS_overheadGoal) - Timer Overhead |
| 81 | // ----------------------------------------------------- < N |
| 82 | // (timer.fWall - Timer Overhead) |
| 83 | // |
| 84 | // Luckily, this also works well in practice. :) |
| 85 | const double numer = overhead / FLAGS_overheadGoal - overhead; |
| 86 | const double denom = timer.fWall - overhead; |
| 87 | return (int)ceil(numer / denom); |
| 88 | } |
| 89 | |
| 90 | static bool push_config_if_enabled(const char* config, SkTDArray<const char*>* configs) { |
| 91 | if (FLAGS_config.contains(config)) { |
| 92 | configs->push(config); |
| 93 | return true; |
| 94 | } |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | static void create_surfaces(Benchmark* bench, |
| 99 | SkTDArray<SkSurface*>* surfaces, |
| 100 | SkTDArray<const char*>* configs) { |
| 101 | |
| 102 | if (bench->isSuitableFor(Benchmark::kNonRendering_Backend) |
| 103 | && push_config_if_enabled("nonrendering", configs)) { |
| 104 | surfaces->push(NULL); |
| 105 | } |
| 106 | |
| 107 | if (bench->isSuitableFor(Benchmark::kRaster_Backend)) { |
| 108 | const int w = bench->getSize().fX, |
| 109 | h = bench->getSize().fY; |
| 110 | |
| 111 | if (push_config_if_enabled("8888", configs)) { |
| 112 | const SkImageInfo info = { w, h, kN32_SkColorType, kPremul_SkAlphaType }; |
| 113 | surfaces->push(SkSurface::NewRaster(info)); |
| 114 | } |
| 115 | |
| 116 | if (push_config_if_enabled("565", configs)) { |
| 117 | const SkImageInfo info = { w, h, kRGB_565_SkColorType, kOpaque_SkAlphaType }; |
| 118 | surfaces->push(SkSurface::NewRaster(info)); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | int tool_main(int argc, char** argv); |
| 124 | int tool_main(int argc, char** argv) { |
| 125 | SetupCrashHandler(); |
| 126 | SkAutoGraphics ag; |
| 127 | SkCommandLineFlags::Parse(argc, argv); |
| 128 | |
| 129 | const double overhead = estimate_timer_overhead(); |
| 130 | |
| 131 | if (FLAGS_verbose) { |
| 132 | // No header. |
| 133 | } else if (FLAGS_quiet) { |
| 134 | SkDebugf("min\tbench\tconfig\n"); |
| 135 | } else { |
| 136 | SkDebugf("loops\tmin\tmean\tmax\tstddev\tbench\tconfig\n"); |
| 137 | } |
| 138 | |
| 139 | for (const BenchRegistry* r = BenchRegistry::Head(); r != NULL; r = r->next()) { |
| 140 | SkAutoTDelete<Benchmark> bench(r->factory()(NULL)); |
| 141 | if (SkCommandLineFlags::ShouldSkip(FLAGS_match, bench->getName())) { |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | SkTDArray<SkSurface*> surfaces; |
| 146 | SkTDArray<const char*> configs; |
| 147 | create_surfaces(bench.get(), &surfaces, &configs); |
| 148 | |
| 149 | bench->preDraw(); |
| 150 | for (int j = 0; j < surfaces.count(); j++) { |
| 151 | SkCanvas* canvas = surfaces[j] ? surfaces[j]->getCanvas() : NULL; |
| 152 | const char* config = configs[j]; |
| 153 | |
| 154 | bench->draw(1, canvas); // Just paranoid warmup. |
| 155 | safe_flush(canvas); |
| 156 | const int loops = guess_loops(overhead, bench.get(), canvas); |
| 157 | |
| 158 | SkAutoTMalloc<double> samples(FLAGS_samples); |
| 159 | WallTimer timer; |
| 160 | for (int i = 0; i < FLAGS_samples; i++) { |
| 161 | timer.start(); |
| 162 | bench->draw(loops, canvas); |
| 163 | safe_flush(canvas); |
| 164 | timer.end(); |
| 165 | samples[i] = timer.fWall / loops; |
| 166 | } |
| 167 | |
| 168 | Stats stats(samples.get(), FLAGS_samples); |
| 169 | |
| 170 | if (FLAGS_verbose) { |
| 171 | for (int i = 0; i < FLAGS_samples; i++) { |
| 172 | SkDebugf("%s ", humanize(samples[i]).c_str()); |
| 173 | } |
| 174 | SkDebugf("%s\n", bench->getName()); |
| 175 | } else if (FLAGS_quiet) { |
| 176 | if (configs.count() == 1) { |
| 177 | config = ""; // Only print the config if we run the same bench on more than one. |
| 178 | } |
| 179 | SkDebugf("%s\t%s\t%s\n", humanize(stats.min).c_str(), bench->getName(), config); |
| 180 | } else { |
| 181 | const double stddev_percent = 100 * sqrt(stats.var) / stats.mean; |
| 182 | SkDebugf("%d\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n" |
| 183 | , loops |
| 184 | , humanize(stats.min).c_str() |
| 185 | , humanize(stats.mean).c_str() |
| 186 | , humanize(stats.max).c_str() |
| 187 | , stddev_percent |
| 188 | , bench->getName() |
| 189 | , config |
| 190 | ); |
| 191 | } |
| 192 | } |
| 193 | surfaces.deleteAll(); |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | #if !defined SK_BUILD_FOR_IOS |
| 200 | int main(int argc, char * const argv[]) { |
| 201 | return tool_main(argc, (char**) argv); |
| 202 | } |
| 203 | #endif |