blob: 182a9d9b4e3c950cabebe660736876a72d5f851b [file] [log] [blame]
joshualitt189aef72015-09-08 07:08:11 -07001/*
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.
joshualitt189aef72015-09-08 07:08:11 -07006 */
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
joshualitt189aef72015-09-08 07:08:11 -070027DEFINE_bool2(verbose, v, false, "enable verbose output from the test driver.");
joshualitt189aef72015-09-08 07:08:11 -070028DEFINE_string(outResultsFile, "", "If given, write results here as JSON.");
mtkleinca6f43b2015-09-15 13:29:20 -070029DEFINE_string(key, "",
30 "Space-separated key/value pairs to add to JSON identifying this builder.");
joshualitt189aef72015-09-08 07:08:11 -070031DEFINE_string(properties, "",
32 "Space-separated key/value pairs to add to JSON identifying this run.");
joshualitt98d2e2f2015-10-05 07:23:30 -070033DEFINE_int32(samples, 10, "Number of times to time each skp.");
joshualitt189aef72015-09-08 07:08:11 -070034
35static 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
44VisualLightweightBenchModule::VisualLightweightBenchModule(VisualBench* owner)
cdalton266e2022015-11-13 08:28:49 -080045 : INHERITED(owner)
joshualittd0f0bce2015-10-14 07:49:28 -070046 , fCurrentSample(0)
joshualitt189aef72015-09-08 07:08:11 -070047 , fResults(new ResultsWriter) {
joshualitt189aef72015-09-08 07:08:11 -070048 // Print header
cdaltonbaf8fcb2015-11-24 09:20:24 -080049 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tconfig\tbench\n",
50 FLAGS_samples, "samples");
joshualitt189aef72015-09-08 07:08:11 -070051
52 // setup json logging if required
53 if (!FLAGS_outResultsFile.isEmpty()) {
54 fResults.reset(new NanoJSONResultsWriter(FLAGS_outResultsFile[0]));
55 }
56
mtkleinca6f43b2015-09-15 13:29:20 -070057 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
joshualitt189aef72015-09-08 07:08:11 -070065 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 }
joshualittd0f0bce2015-10-14 07:49:28 -070072
73 // seed an initial record
74 fRecords.push_back();
joshualitt189aef72015-09-08 07:08:11 -070075}
76
joshualittd0f0bce2015-10-14 07:49:28 -070077void VisualLightweightBenchModule::renderFrame(SkCanvas* canvas, Benchmark* benchmark, int loops) {
78 benchmark->draw(loops, canvas);
joshualitt189aef72015-09-08 07:08:11 -070079 canvas->flush();
joshualitt189aef72015-09-08 07:08:11 -070080}
81
joshualittd0f0bce2015-10-14 07:49:28 -070082void VisualLightweightBenchModule::printStats(Benchmark* benchmark, int loops) {
joshualitt189aef72015-09-08 07:08:11 -070083 const SkTArray<double>& measurements = fRecords.back().fMeasurements;
joshualittd0f0bce2015-10-14 07:49:28 -070084 const char* shortName = benchmark->getUniqueName();
joshualitt189aef72015-09-08 07:08:11 -070085
86 // update log
87 // Note: We currently log only the minimum. It would be interesting to log more information
88 SkString configName;
cdaltonbaf8fcb2015-11-24 09:20:24 -080089 if (FLAGS_cpu) {
90 configName.append("cpu");
91 } else if (FLAGS_nvpr) {
92 if (FLAGS_offscreen) {
93 configName.appendf("nvpr_%d", FLAGS_msaa);
94 } else {
95 configName.appendf("nvpr_msaa_%d", FLAGS_msaa);
96 }
97 } else if (FLAGS_msaa > 0) {
98 if (FLAGS_offscreen) {
99 configName.appendf("offscreen_msaa_%d", FLAGS_msaa);
100 } else {
101 configName.appendf("msaa_%d", FLAGS_msaa);
102 }
joshualitt189aef72015-09-08 07:08:11 -0700103 } else {
cdaltonbaf8fcb2015-11-24 09:20:24 -0800104 if (FLAGS_offscreen) {
105 configName.append("offscreen");
106 } else {
107 configName.append("gpu");
108 }
joshualitt189aef72015-09-08 07:08:11 -0700109 }
joshualittd0f0bce2015-10-14 07:49:28 -0700110 // Log bench name
111 fResults->bench(shortName, benchmark->getSize().fX, benchmark->getSize().fY);
112
joshualitt189aef72015-09-08 07:08:11 -0700113 fResults->config(configName.c_str());
joshualitt7d4b4582015-09-24 08:08:23 -0700114 fResults->configOption("name", shortName);
joshualitt189aef72015-09-08 07:08:11 -0700115 SkASSERT(measurements.count());
116 Stats stats(measurements);
joshualitt7d4b4582015-09-24 08:08:23 -0700117 fResults->metric("min_ms", stats.min);
joshualitt189aef72015-09-08 07:08:11 -0700118
119 // Print output
120 if (FLAGS_verbose) {
121 for (int i = 0; i < measurements.count(); i++) {
122 SkDebugf("%s ", HUMANIZE(measurements[i]));
123 }
124 SkDebugf("%s\n", shortName);
125 } else {
126 const double stdDevPercent = 100 * sqrt(stats.var) / stats.mean;
cdaltonbaf8fcb2015-11-24 09:20:24 -0800127 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\t%s\n",
joshualitt189aef72015-09-08 07:08:11 -0700128 sk_tools::getCurrResidentSetSizeMB(),
129 sk_tools::getMaxResidentSetSizeMB(),
joshualittd0f0bce2015-10-14 07:49:28 -0700130 loops,
joshualitt189aef72015-09-08 07:08:11 -0700131 HUMANIZE(stats.min),
132 HUMANIZE(stats.median),
133 HUMANIZE(stats.mean),
134 HUMANIZE(stats.max),
135 stdDevPercent,
joshualitt7d4b4582015-09-24 08:08:23 -0700136 stats.plot.c_str(),
cdaltonbaf8fcb2015-11-24 09:20:24 -0800137 configName.c_str(),
joshualitt189aef72015-09-08 07:08:11 -0700138 shortName);
139 }
140}
141
joshualittd0f0bce2015-10-14 07:49:28 -0700142bool VisualLightweightBenchModule::timingFinished(Benchmark* benchmark, int loops,
143 double measurement) {
144 fRecords.back().fMeasurements.push_back(measurement);
145 if (++fCurrentSample > FLAGS_samples) {
146 this->printStats(benchmark, loops);
147 fRecords.push_back();
148 fCurrentSample = 0;
joshualitt189aef72015-09-08 07:08:11 -0700149 return true;
150 }
joshualittd0f0bce2015-10-14 07:49:28 -0700151 return false;
joshualitt189aef72015-09-08 07:08:11 -0700152}
jvanverthf5d1b2d2015-09-15 07:40:56 -0700153
154bool VisualLightweightBenchModule::onHandleChar(SkUnichar c) {
155 return true;
156}