blob: f56deae209fcf5a7c37b4fdfd458d61c22670413 [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"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000016#include "SkStream.h"
17#include "SkString.h"
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000018#include "SkTypes.h"
19
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000020/**
21 * Base class for writing out the bench results.
22 *
mtklein1915b622014-08-20 11:45:00 -070023 * Default implementation does nothing.
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000024 */
25class ResultsWriter : SkNoncopyable {
26public:
mtklein1915b622014-08-20 11:45:00 -070027 virtual ~ResultsWriter() {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000028
mtklein1915b622014-08-20 11:45:00 -070029 // Record one key value pair that makes up a unique key for this type of run, e.g.
30 // builder name, machine type, Debug/Release, etc.
31 virtual void key(const char name[], const char value[]) {}
jcgregoriobf5e5232014-07-17 13:14:16 -070032
mtklein1915b622014-08-20 11:45:00 -070033 // Record one key value pair that describes the run instance, e.g. git hash, build number.
34 virtual void property(const char name[], const char value[]) {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000035
mtklein1915b622014-08-20 11:45:00 -070036 // Denote the start of a specific benchmark. Once bench is called,
mtklein051e56d2014-12-04 08:46:51 -080037 // then config and metric can be called multiple times to record runs.
mtklein1915b622014-08-20 11:45:00 -070038 virtual void bench(const char name[], int32_t x, int32_t y) {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000039
mtklein1915b622014-08-20 11:45:00 -070040 // Record the specific configuration a bench is run under, such as "8888".
41 virtual void config(const char name[]) {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000042
mtklein1915b622014-08-20 11:45:00 -070043 // Record the options for a configuration, such as "GL_RENDERER".
44 virtual void configOption(const char name[], const char* value) {}
jcgregoriobf5e5232014-07-17 13:14:16 -070045
mtklein1915b622014-08-20 11:45:00 -070046 // Record a single test metric.
mtklein051e56d2014-12-04 08:46:51 -080047 virtual void metric(const char name[], double ms) {}
mtkleine070c2b2014-10-14 08:40:43 -070048
49 // Flush to storage now please.
50 virtual void flush() {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000051};
52
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000053/**
jcgregoriobf5e5232014-07-17 13:14:16 -070054 NanoJSONResultsWriter writes the test results out in the following
55 format:
56
57 {
58 "key": {
59 "arch": "Arm7",
60 "gpu": "SGX540",
61 "os": "Android",
62 "model": "GalaxyNexus",
63 }
jcgregoriobf5e5232014-07-17 13:14:16 -070064 "gitHash": "d1830323662ae8ae06908b97f15180fd25808894",
mtklein1915b622014-08-20 11:45:00 -070065 "build_number": "1234",
jcgregoriobf5e5232014-07-17 13:14:16 -070066 "results" : {
67 "Xfermode_Luminosity_640_480" : {
68 "8888" : {
69 "median_ms" : 143.188128906250,
70 "min_ms" : 143.835957031250,
71 ...
72 },
73 ...
74*/
75class NanoJSONResultsWriter : public ResultsWriter {
76public:
mtklein1915b622014-08-20 11:45:00 -070077 explicit NanoJSONResultsWriter(const char filename[])
jcgregoriobf5e5232014-07-17 13:14:16 -070078 : fFilename(filename)
79 , fRoot()
80 , fResults(fRoot["results"])
halcanary96fcdcc2015-08-27 07:41:13 -070081 , fBench(nullptr)
82 , fConfig(nullptr) {}
mtklein1915b622014-08-20 11:45:00 -070083
84 ~NanoJSONResultsWriter() {
mtkleine070c2b2014-10-14 08:40:43 -070085 this->flush();
jcgregoriobf5e5232014-07-17 13:14:16 -070086 }
mtklein1915b622014-08-20 11:45:00 -070087
88 // Added under "key".
joshualitte45c81c2015-12-02 09:05:37 -080089 void key(const char name[], const char value[]) override {
jcgregoriobf5e5232014-07-17 13:14:16 -070090 fRoot["key"][name] = value;
91 }
mtklein1915b622014-08-20 11:45:00 -070092 // Inserted directly into the root.
joshualitte45c81c2015-12-02 09:05:37 -080093 void property(const char name[], const char value[]) override {
mtklein1915b622014-08-20 11:45:00 -070094 fRoot[name] = value;
jcgregoriobf5e5232014-07-17 13:14:16 -070095 }
joshualitte45c81c2015-12-02 09:05:37 -080096 void bench(const char name[], int32_t x, int32_t y) override {
jcgregoriobf5e5232014-07-17 13:14:16 -070097 SkString id = SkStringPrintf( "%s_%d_%d", name, x, y);
98 fResults[id.c_str()] = Json::Value(Json::objectValue);
99 fBench = &fResults[id.c_str()];
100 }
joshualitte45c81c2015-12-02 09:05:37 -0800101 void config(const char name[]) override {
bsalomon49f085d2014-09-05 13:34:00 -0700102 SkASSERT(fBench);
jcgregoriobf5e5232014-07-17 13:14:16 -0700103 fConfig = &(*fBench)[name];
104 }
joshualitte45c81c2015-12-02 09:05:37 -0800105 void configOption(const char name[], const char* value) override {
jcgregoriobf5e5232014-07-17 13:14:16 -0700106 (*fConfig)["options"][name] = value;
107 }
joshualitte45c81c2015-12-02 09:05:37 -0800108 void metric(const char name[], double ms) override {
jcgregoriobf5e5232014-07-17 13:14:16 -0700109 // Don't record if nan, or -nan.
110 if (sk_double_isnan(ms)) {
111 return;
112 }
bsalomon49f085d2014-09-05 13:34:00 -0700113 SkASSERT(fConfig);
jcgregoriobf5e5232014-07-17 13:14:16 -0700114 (*fConfig)[name] = ms;
115 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700116
mtkleine070c2b2014-10-14 08:40:43 -0700117 // Flush to storage now please.
joshualitte45c81c2015-12-02 09:05:37 -0800118 void flush() override {
borenet877a52a2015-08-20 09:12:39 -0700119 SkString dirname = SkOSPath::Dirname(fFilename.c_str());
120 if (!sk_exists(dirname.c_str(), kWrite_SkFILE_Flag)) {
121 if (!sk_mkdir(dirname.c_str())) {
122 SkDebugf("Failed to create directory.");
123 }
124 }
mtkleine070c2b2014-10-14 08:40:43 -0700125 SkFILEWStream stream(fFilename.c_str());
126 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
127 stream.flush();
128 }
129
mtklein1915b622014-08-20 11:45:00 -0700130private:
jcgregoriobf5e5232014-07-17 13:14:16 -0700131 SkString fFilename;
132 Json::Value fRoot;
133 Json::Value& fResults;
134 Json::Value* fBench;
135 Json::Value* fConfig;
136};
137
138
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000139#endif