blob: af939ff926532acf46f85cf54af6d073205bd782 [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)
joshualittd0f0bce2015-10-14 07:49:28 -070045 : INHERITED(owner, true)
46 , fCurrentSample(0)
joshualitt189aef72015-09-08 07:08:11 -070047 , fResults(new ResultsWriter) {
joshualitt189aef72015-09-08 07:08:11 -070048 // Print header
joshualitt7d4b4582015-09-24 08:08:23 -070049 SkDebugf("curr/maxrss\tloops\tmin\tmedian\tmean\tmax\tstddev\t%-*s\tbench\n", FLAGS_samples,
50 "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;
89 if (FLAGS_msaa > 0) {
90 configName.appendf("msaa_%d", FLAGS_msaa);
91 } else {
92 configName.appendf("gpu");
93 }
joshualittd0f0bce2015-10-14 07:49:28 -070094 // Log bench name
95 fResults->bench(shortName, benchmark->getSize().fX, benchmark->getSize().fY);
96
joshualitt189aef72015-09-08 07:08:11 -070097 fResults->config(configName.c_str());
joshualitt7d4b4582015-09-24 08:08:23 -070098 fResults->configOption("name", shortName);
joshualitt189aef72015-09-08 07:08:11 -070099 SkASSERT(measurements.count());
100 Stats stats(measurements);
joshualitt7d4b4582015-09-24 08:08:23 -0700101 fResults->metric("min_ms", stats.min);
joshualitt189aef72015-09-08 07:08:11 -0700102
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;
joshualitt7d4b4582015-09-24 08:08:23 -0700111 SkDebugf("%4d/%-4dMB\t%d\t%s\t%s\t%s\t%s\t%.0f%%\t%s\t%s\n",
joshualitt189aef72015-09-08 07:08:11 -0700112 sk_tools::getCurrResidentSetSizeMB(),
113 sk_tools::getMaxResidentSetSizeMB(),
joshualittd0f0bce2015-10-14 07:49:28 -0700114 loops,
joshualitt189aef72015-09-08 07:08:11 -0700115 HUMANIZE(stats.min),
116 HUMANIZE(stats.median),
117 HUMANIZE(stats.mean),
118 HUMANIZE(stats.max),
119 stdDevPercent,
joshualitt7d4b4582015-09-24 08:08:23 -0700120 stats.plot.c_str(),
joshualitt189aef72015-09-08 07:08:11 -0700121 shortName);
122 }
123}
124
joshualittd0f0bce2015-10-14 07:49:28 -0700125bool 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;
joshualitt189aef72015-09-08 07:08:11 -0700132 return true;
133 }
joshualittd0f0bce2015-10-14 07:49:28 -0700134 return false;
joshualitt189aef72015-09-08 07:08:11 -0700135}
jvanverthf5d1b2d2015-09-15 07:40:56 -0700136
137bool VisualLightweightBenchModule::onHandleChar(SkUnichar c) {
138 return true;
139}