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. |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include "VisualLightweightBenchModule.h" |
| 10 | |
| 11 | #include "ProcStats.h" |
| 12 | #include "SkApplication.h" |
| 13 | #include "SkCanvas.h" |
| 14 | #include "SkCommandLineFlags.h" |
| 15 | #include "SkForceLinking.h" |
| 16 | #include "SkGraphics.h" |
| 17 | #include "SkGr.h" |
| 18 | #include "SkImageDecoder.h" |
| 19 | #include "SkOSFile.h" |
| 20 | #include "SkStream.h" |
| 21 | #include "Stats.h" |
| 22 | #include "gl/GrGLInterface.h" |
| 23 | |
| 24 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 25 | |
| 26 | // Between samples we reset context |
| 27 | // Between frames we swap buffers |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 28 | DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver."); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 29 | DEFINE_string(outResultsFile, "", "If given, write results here as JSON."); |
mtklein | ca6f43b | 2015-09-15 13:29:20 -0700 | [diff] [blame] | 30 | DEFINE_string(key, "", |
| 31 | "Space-separated key/value pairs to add to JSON identifying this builder."); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 32 | DEFINE_string(properties, "", |
| 33 | "Space-separated key/value pairs to add to JSON identifying this run."); |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 34 | DEFINE_int32(samples, 10, "Number of times to time each skp."); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 35 | |
| 36 | static SkString humanize(double ms) { |
| 37 | if (FLAGS_verbose) { |
| 38 | return SkStringPrintf("%llu", (uint64_t)(ms*1e6)); |
| 39 | } |
| 40 | return HumanizeMs(ms); |
| 41 | } |
| 42 | |
| 43 | #define HUMANIZE(time) humanize(time).c_str() |
| 44 | |
| 45 | VisualLightweightBenchModule::VisualLightweightBenchModule(VisualBench* owner) |
| 46 | : fCurrentSample(0) |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 47 | , fHasBeenReset(false) |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 48 | , fOwner(SkRef(owner)) |
| 49 | , fResults(new ResultsWriter) { |
| 50 | fBenchmarkStream.reset(new VisualBenchmarkStream); |
| 51 | |
| 52 | // Print header |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 53 | SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tbench\n", FLAGS_samples, |
| 54 | "samples"); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 55 | |
| 56 | // setup json logging if required |
| 57 | if (!FLAGS_outResultsFile.isEmpty()) { |
| 58 | fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0])); |
| 59 | } |
| 60 | |
mtklein | ca6f43b | 2015-09-15 13:29:20 -0700 | [diff] [blame] | 61 | if (1 == FLAGS_key.count() % 2) { |
| 62 | SkDebugf("ERROR: --key must be passed with an even number of arguments.\n"); |
| 63 | } else { |
| 64 | for (int i = 1; i < FLAGS_key.count(); i += 2) { |
| 65 | fResults->key(FLAGS_key[i - 1], FLAGS_key[i]); |
| 66 | } |
| 67 | } |
| 68 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 69 | if (1 == FLAGS_properties.count() % 2) { |
| 70 | SkDebugf("ERROR: --properties must be passed with an even number of arguments.\n"); |
| 71 | } else { |
| 72 | for (int i = 1; i < FLAGS_properties.count(); i += 2) { |
| 73 | fResults->property(FLAGS_properties[i - 1], FLAGS_properties[i]); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | inline void VisualLightweightBenchModule::renderFrame(SkCanvas* canvas) { |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 79 | fBenchmark->draw(fTSM.loops(), canvas); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 80 | canvas->flush(); |
| 81 | fOwner->present(); |
| 82 | } |
| 83 | |
| 84 | void VisualLightweightBenchModule::printStats() { |
| 85 | const SkTArray<double>& measurements = fRecords.back().fMeasurements; |
| 86 | const char* shortName = fBenchmark->getUniqueName(); |
| 87 | |
| 88 | // update log |
| 89 | // Note: We currently log only the minimum. It would be interesting to log more information |
| 90 | SkString configName; |
| 91 | if (FLAGS_msaa > 0) { |
| 92 | configName.appendf("msaa_%d", FLAGS_msaa); |
| 93 | } else { |
| 94 | configName.appendf("gpu"); |
| 95 | } |
| 96 | fResults->config(configName.c_str()); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 97 | fResults->configOption("name", shortName); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 98 | SkASSERT(measurements.count()); |
| 99 | Stats stats(measurements); |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 100 | fResults->metric("min_ms", stats.min); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 101 | |
| 102 | // Print output |
| 103 | if (FLAGS_verbose) { |
| 104 | for (int i = 0; i < measurements.count(); i++) { |
| 105 | SkDebugf("%s ", HUMANIZE(measurements[i])); |
| 106 | } |
| 107 | SkDebugf("%s\n", shortName); |
| 108 | } else { |
| 109 | const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean; |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 110 | 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] | 111 | sk_tools::getCurrResidentSetSizeMB(), |
| 112 | sk_tools::getMaxResidentSetSizeMB(), |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 113 | fTSM.loops(), |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 114 | HUMANIZE(stats.min), |
| 115 | HUMANIZE(stats.median), |
| 116 | HUMANIZE(stats.mean), |
| 117 | HUMANIZE(stats.max), |
| 118 | stdDevPercent, |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 119 | stats.plot.c_str(), |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 120 | shortName); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | bool VisualLightweightBenchModule::advanceRecordIfNecessary(SkCanvas* canvas) { |
| 125 | if (fBenchmark) { |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | fBenchmark.reset(fBenchmarkStream->next()); |
| 130 | if (!fBenchmark) { |
| 131 | return false; |
| 132 | } |
| 133 | |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 134 | fOwner->clear(canvas, SK_ColorWHITE, 2); |
| 135 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 136 | fRecords.push_back(); |
| 137 | |
| 138 | // Log bench name |
| 139 | fResults->bench(fBenchmark->getUniqueName(), fBenchmark->getSize().fX, |
| 140 | fBenchmark->getSize().fY); |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 141 | |
| 142 | fBenchmark->delayedSetup(); |
| 143 | fBenchmark->preTimingHooks(canvas); |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 147 | void VisualLightweightBenchModule::draw(SkCanvas* canvas) { |
| 148 | if (!this->advanceRecordIfNecessary(canvas)) { |
| 149 | SkDebugf("Exiting VisualBench successfully\n"); |
| 150 | fOwner->closeWindow(); |
| 151 | return; |
| 152 | } |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 153 | |
| 154 | if (fHasBeenReset) { |
| 155 | fHasBeenReset = false; |
| 156 | fBenchmark->preTimingHooks(canvas); |
| 157 | } |
| 158 | |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 159 | this->renderFrame(canvas); |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 160 | TimingStateMachine::ParentEvents event = fTSM.nextFrame(true); |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 161 | switch (event) { |
| 162 | case TimingStateMachine::kReset_ParentEvents: |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 163 | fBenchmark->postTimingHooks(canvas); |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 164 | fOwner->reset(); |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 165 | fHasBeenReset = true; |
joshualitt | 7d4b458 | 2015-09-24 08:08:23 -0700 | [diff] [blame] | 166 | break; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 167 | case TimingStateMachine::kTiming_ParentEvents: |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 168 | break; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 169 | case TimingStateMachine::kTimingFinished_ParentEvents: |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 170 | fBenchmark->postTimingHooks(canvas); |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 171 | fOwner->reset(); |
| 172 | fRecords.back().fMeasurements.push_back(fTSM.lastMeasurement()); |
| 173 | if (++fCurrentSample > FLAGS_samples) { |
| 174 | this->printStats(); |
| 175 | fTSM.nextBenchmark(canvas, fBenchmark); |
| 176 | fCurrentSample = 0; |
| 177 | fBenchmark.reset(nullptr); |
joshualitt | dc5db59 | 2015-10-05 13:24:55 -0700 | [diff] [blame] | 178 | } else { |
joshualitt | cb54e8e | 2015-10-05 13:58:26 -0700 | [diff] [blame] | 179 | fHasBeenReset = true; |
joshualitt | 98d2e2f | 2015-10-05 07:23:30 -0700 | [diff] [blame] | 180 | } |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 181 | break; |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 182 | } |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 183 | } |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 184 | |
| 185 | bool VisualLightweightBenchModule::onHandleChar(SkUnichar c) { |
| 186 | return true; |
| 187 | } |