blob: 85c44136feaeebf3eb59cc06da2f1011513d0330 [file] [log] [blame]
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +00001/*
2 * Copyright 2013 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 * Classes for writing out bench results in various formats.
8 */
tfarinaf168b862014-06-19 12:32:29 -07009
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000010#ifndef SkResultsWriter_DEFINED
11#define SkResultsWriter_DEFINED
12
tfarinaf168b862014-06-19 12:32:29 -070013#include "BenchLogger.h"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000014#include "SkJSONCPP.h"
borenet877a52a2015-08-20 09:12:39 -070015#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050016#include "SkOSPath.h"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000017#include "SkStream.h"
18#include "SkString.h"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000019#include "SkTypes.h"
20
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000021/**
22 * Base class for writing out the bench results.
23 *
mtklein1915b622014-08-20 11:45:00 -070024 * Default implementation does nothing.
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000025 */
26class ResultsWriter : SkNoncopyable {
27public:
mtklein1915b622014-08-20 11:45:00 -070028 virtual ~ResultsWriter() {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000029
mtklein1915b622014-08-20 11:45:00 -070030 // Record one key value pair that makes up a unique key for this type of run, e.g.
31 // builder name, machine type, Debug/Release, etc.
32 virtual void key(const char name[], const char value[]) {}
jcgregoriobf5e5232014-07-17 13:14:16 -070033
mtklein1915b622014-08-20 11:45:00 -070034 // Record one key value pair that describes the run instance, e.g. git hash, build number.
35 virtual void property(const char name[], const char value[]) {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000036
mtklein1915b622014-08-20 11:45:00 -070037 // Denote the start of a specific benchmark. Once bench is called,
mtklein051e56d2014-12-04 08:46:51 -080038 // then config and metric can be called multiple times to record runs.
mtklein1915b622014-08-20 11:45:00 -070039 virtual void bench(const char name[], int32_t x, int32_t y) {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000040
mtklein1915b622014-08-20 11:45:00 -070041 // Record the specific configuration a bench is run under, such as "8888".
42 virtual void config(const char name[]) {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000043
mtklein1915b622014-08-20 11:45:00 -070044 // Record the options for a configuration, such as "GL_RENDERER".
45 virtual void configOption(const char name[], const char* value) {}
jcgregoriobf5e5232014-07-17 13:14:16 -070046
mtklein1915b622014-08-20 11:45:00 -070047 // Record a single test metric.
mtklein051e56d2014-12-04 08:46:51 -080048 virtual void metric(const char name[], double ms) {}
mtkleine070c2b2014-10-14 08:40:43 -070049
George Burgess IV75b57182016-12-06 10:53:52 -080050 // Record a list of test metrics.
51 virtual void metrics(const char name[], const SkTArray<double>& array) {}
52
mtkleine070c2b2014-10-14 08:40:43 -070053 // Flush to storage now please.
54 virtual void flush() {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000055};
56
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000057/**
jcgregoriobf5e5232014-07-17 13:14:16 -070058 NanoJSONResultsWriter writes the test results out in the following
59 format:
60
61 {
62 "key": {
63 "arch": "Arm7",
64 "gpu": "SGX540",
65 "os": "Android",
66 "model": "GalaxyNexus",
67 }
jcgregoriobf5e5232014-07-17 13:14:16 -070068 "gitHash": "d1830323662ae8ae06908b97f15180fd25808894",
mtklein1915b622014-08-20 11:45:00 -070069 "build_number": "1234",
jcgregoriobf5e5232014-07-17 13:14:16 -070070 "results" : {
71 "Xfermode_Luminosity_640_480" : {
72 "8888" : {
73 "median_ms" : 143.188128906250,
74 "min_ms" : 143.835957031250,
75 ...
76 },
77 ...
78*/
79class NanoJSONResultsWriter : public ResultsWriter {
80public:
mtklein1915b622014-08-20 11:45:00 -070081 explicit NanoJSONResultsWriter(const char filename[])
jcgregoriobf5e5232014-07-17 13:14:16 -070082 : fFilename(filename)
83 , fRoot()
84 , fResults(fRoot["results"])
halcanary96fcdcc2015-08-27 07:41:13 -070085 , fBench(nullptr)
86 , fConfig(nullptr) {}
mtklein1915b622014-08-20 11:45:00 -070087
Brian Salomond3b65972017-03-22 12:05:03 -040088 ~NanoJSONResultsWriter() override {
mtkleine070c2b2014-10-14 08:40:43 -070089 this->flush();
jcgregoriobf5e5232014-07-17 13:14:16 -070090 }
mtklein1915b622014-08-20 11:45:00 -070091
92 // Added under "key".
joshualitte45c81c2015-12-02 09:05:37 -080093 void key(const char name[], const char value[]) override {
jcgregoriobf5e5232014-07-17 13:14:16 -070094 fRoot["key"][name] = value;
95 }
mtklein1915b622014-08-20 11:45:00 -070096 // Inserted directly into the root.
joshualitte45c81c2015-12-02 09:05:37 -080097 void property(const char name[], const char value[]) override {
mtklein1915b622014-08-20 11:45:00 -070098 fRoot[name] = value;
jcgregoriobf5e5232014-07-17 13:14:16 -070099 }
joshualitte45c81c2015-12-02 09:05:37 -0800100 void bench(const char name[], int32_t x, int32_t y) override {
jcgregoriobf5e5232014-07-17 13:14:16 -0700101 SkString id = SkStringPrintf( "%s_%d_%d", name, x, y);
102 fResults[id.c_str()] = Json::Value(Json::objectValue);
103 fBench = &fResults[id.c_str()];
104 }
joshualitte45c81c2015-12-02 09:05:37 -0800105 void config(const char name[]) override {
bsalomon49f085d2014-09-05 13:34:00 -0700106 SkASSERT(fBench);
jcgregoriobf5e5232014-07-17 13:14:16 -0700107 fConfig = &(*fBench)[name];
108 }
joshualitte45c81c2015-12-02 09:05:37 -0800109 void configOption(const char name[], const char* value) override {
jcgregoriobf5e5232014-07-17 13:14:16 -0700110 (*fConfig)["options"][name] = value;
111 }
joshualitte45c81c2015-12-02 09:05:37 -0800112 void metric(const char name[], double ms) override {
jcgregoriobf5e5232014-07-17 13:14:16 -0700113 // Don't record if nan, or -nan.
114 if (sk_double_isnan(ms)) {
115 return;
116 }
bsalomon49f085d2014-09-05 13:34:00 -0700117 SkASSERT(fConfig);
jcgregoriobf5e5232014-07-17 13:14:16 -0700118 (*fConfig)[name] = ms;
119 }
George Burgess IV75b57182016-12-06 10:53:52 -0800120 void metrics(const char name[], const SkTArray<double>& array) override {
121 SkASSERT(fConfig);
122 Json::Value value = Json::Value(Json::arrayValue);
123 value.resize(array.count());
124 for (int i = 0; i < array.count(); i++) {
125 // Don't care about nan-ness.
126 value[i] = array[i];
127 }
128 (*fConfig)[name] = std::move(value);
129 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700130
mtkleine070c2b2014-10-14 08:40:43 -0700131 // Flush to storage now please.
joshualitte45c81c2015-12-02 09:05:37 -0800132 void flush() override {
borenet877a52a2015-08-20 09:12:39 -0700133 SkString dirname = SkOSPath::Dirname(fFilename.c_str());
134 if (!sk_exists(dirname.c_str(), kWrite_SkFILE_Flag)) {
135 if (!sk_mkdir(dirname.c_str())) {
136 SkDebugf("Failed to create directory.");
137 }
138 }
mtkleine070c2b2014-10-14 08:40:43 -0700139 SkFILEWStream stream(fFilename.c_str());
140 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
141 stream.flush();
142 }
143
mtklein1915b622014-08-20 11:45:00 -0700144private:
jcgregoriobf5e5232014-07-17 13:14:16 -0700145 SkString fFilename;
146 Json::Value fRoot;
147 Json::Value& fResults;
148 Json::Value* fBench;
149 Json::Value* fConfig;
150};
151
152
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000153#endif