joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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. |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "VisualLightweightBenchModule.h" |
| 9 | |
| 10 | #include "ProcStats.h" |
| 11 | #include "SkApplication.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkCommandLineFlags.h" |
| 14 | #include "SkForceLinking.h" |
| 15 | #include "SkGraphics.h" |
| 16 | #include "SkGr.h" |
| 17 | #include "SkImageDecoder.h" |
| 18 | #include "SkOSFile.h" |
| 19 | #include "SkStream.h" |
| 20 | #include "Stats.h" |
| 21 | #include "gl/GrGLInterface.h" |
| 22 | |
| 23 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 24 | |
| 25 | // Between samples we reset context |
| 26 | // Between frames we swap buffers |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 27 | DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver."); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 28 | DEFINE_string(outResultsFile, "", "If given, write results here as JSON."); |
mtklein | ca6f43b | 2015-09-15 13:29:20 -0700 | [diff] [blame] | 29 | DEFINE_string(key, "", |
| 30 | "Space-separated key/value pairs to add to JSON identifying this builder."); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 31 | DEFINE_string(properties, "", |
| 32 | "Space-separated key/value pairs to add to JSON identifying this run."); |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 33 | DEFINE_int32(samples, 10, "Number of times to time each skp."); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 34 | |
| 35 | static SkString humanize(double ms) { |
| 36 | if (FLAGS_verbose) { |
| 37 | return SkStringPrintf("%llu", (uint64_t)(ms*1e6)); |
| 38 | } |
| 39 | return HumanizeMs(ms); |
| 40 | } |
| 41 | |
| 42 | #define HUMANIZE(time) humanize(time).c_str() |
| 43 | |
| 44 | VisualLightweightBenchModule::VisualLightweightBenchModule(VisualBench* owner) |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 45 | : INHERITED(owner, true) |
| 46 | , fCurrentSample(0) |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 47 | , fResults(new ResultsWriter) { |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 48 | // Print header |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 49 | SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tbench\n", FLAGS_samples, |
| 50 | "samples"); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 51 | |
| 52 | // setup json logging if required |
| 53 | if (!FLAGS_outResultsFile.isEmpty()) { |
| 54 | fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0])); |
| 55 | } |
| 56 | |
mtklein | ca6f43b | 2015-09-15 13:29:20 -0700 | [diff] [blame] | 57 | if (1 == FLAGS_key.count() % 2) { |
| 58 | SkDebugf("ERROR: --key must be passed with an even number of arguments.\n"); |
| 59 | } else { |
| 60 | for (int i = 1; i < FLAGS_key.count(); i += 2) { |
| 61 | fResults->key(FLAGS_key[i - 1], FLAGS_key[i]); |
| 62 | } |
| 63 | } |
| 64 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 65 | if (1 == FLAGS_properties.count() % 2) { |
| 66 | SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n"); |
| 67 | } else { |
| 68 | for (int i = 1; i < FLAGS_properties.count(); i += 2) { |
| 69 | fResults->property(FLAGS_properties[i - 1], FLAGS_properties[i]); |
| 70 | } |
| 71 | } |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 72 | |
| 73 | // seed an initial record |
| 74 | fRecords.push_back(); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 75 | } |
| 76 | |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 77 | void VisualLightweightBenchModule::renderFrame(SkCanvas* canvas, Benchmark* benchmark, int loops) { |
| 78 | benchmark->draw(loops, canvas); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 79 | canvas->flush(); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 80 | } |
| 81 | |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 82 | void VisualLightweightBenchModule::printStats(Benchmark* benchmark, int loops) { |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 83 | const SkTArray<double>& measurements = fRecords.back().fMeasurements; |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 84 | const char* shortName = benchmark->getUniqueName(); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 85 | |
| 86 | // update log |
| 87 | // Note: We currently log only the minimum. It would be interesting to log more information |
| 88 | SkString configName; |
| 89 | if (FLAGS_msaa > 0) { |
| 90 | configName.appendf("msaa_%d", FLAGS_msaa); |
| 91 | } else { |
| 92 | configName.appendf("gpu"); |
| 93 | } |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 94 | // Log bench name |
| 95 | fResults->bench(shortName, benchmark->getSize().fX, benchmark->getSize().fY); |
| 96 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 97 | fResults->config(configName.c_str()); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 98 | fResults->configOption("name", shortName); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 99 | SkASSERT(measurements.count()); |
| 100 | Stats stats(measurements); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 101 | fResults->metric("min_ms", stats.min); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 102 | |
| 103 | // Print output |
| 104 | if (FLAGS_verbose) { |
| 105 | for (int i = 0; i < measurements.count(); i++) { |
| 106 | SkDebugf("%s ", HUMANIZE(measurements[i])); |
| 107 | } |
| 108 | SkDebugf("%s\n", shortName); |
| 109 | } else { |
| 110 | const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean; |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 111 | SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n", |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 112 | sk_tools::getCurrResidentSetSizeMB(), |
| 113 | sk_tools::getMaxResidentSetSizeMB(), |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 114 | loops, |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 115 | HUMANIZE(stats.min), |
| 116 | HUMANIZE(stats.median), |
| 117 | HUMANIZE(stats.mean), |
| 118 | HUMANIZE(stats.max), |
| 119 | stdDevPercent, |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 120 | stats.plot.c_str(), |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 121 | shortName); |
| 122 | } |
| 123 | } |
| 124 | |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 125 | bool VisualLightweightBenchModule::timingFinished(Benchmark* benchmark, int loops, |
| 126 | double measurement) { |
| 127 | fRecords.back().fMeasurements.push_back(measurement); |
| 128 | if (++fCurrentSample > FLAGS_samples) { |
| 129 | this->printStats(benchmark, loops); |
| 130 | fRecords.push_back(); |
| 131 | fCurrentSample = 0; |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 132 | return true; |
| 133 | } |
joshualitt | d0f0bce | 2015-10-14 07:49:28 -0700 | [diff] [blame] | 134 | return false; |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 135 | } |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 136 | |
| 137 | bool VisualLightweightBenchModule::onHandleChar(SkUnichar c) { |
| 138 | return true; |
| 139 | } |