blob: bf74d47826cca1243b8eb5eb01df361781f2674f [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"
18#include "SkTArray.h"
19#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
50 // Flush to storage now please.
51 virtual void flush() {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000052};
53
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000054/**
jcgregoriobf5e5232014-07-17 13:14:16 -070055 NanoJSONResultsWriter writes the test results out in the following
56 format:
57
58 {
59 "key": {
60 "arch": "Arm7",
61 "gpu": "SGX540",
62 "os": "Android",
63 "model": "GalaxyNexus",
64 }
jcgregoriobf5e5232014-07-17 13:14:16 -070065 "gitHash": "d1830323662ae8ae06908b97f15180fd25808894",
mtklein1915b622014-08-20 11:45:00 -070066 "build_number": "1234",
jcgregoriobf5e5232014-07-17 13:14:16 -070067 "results" : {
68 "Xfermode_Luminosity_640_480" : {
69 "8888" : {
70 "median_ms" : 143.188128906250,
71 "min_ms" : 143.835957031250,
72 ...
73 },
74 ...
75*/
76class NanoJSONResultsWriter : public ResultsWriter {
77public:
mtklein1915b622014-08-20 11:45:00 -070078 explicit NanoJSONResultsWriter(const char filename[])
jcgregoriobf5e5232014-07-17 13:14:16 -070079 : fFilename(filename)
80 , fRoot()
81 , fResults(fRoot["results"])
halcanary96fcdcc2015-08-27 07:41:13 -070082 , fBench(nullptr)
83 , fConfig(nullptr) {}
mtklein1915b622014-08-20 11:45:00 -070084
85 ~NanoJSONResultsWriter() {
mtkleine070c2b2014-10-14 08:40:43 -070086 this->flush();
jcgregoriobf5e5232014-07-17 13:14:16 -070087 }
mtklein1915b622014-08-20 11:45:00 -070088
89 // Added under "key".
joshualitte45c81c2015-12-02 09:05:37 -080090 void key(const char name[], const char value[]) override {
jcgregoriobf5e5232014-07-17 13:14:16 -070091 fRoot["key"][name] = value;
92 }
mtklein1915b622014-08-20 11:45:00 -070093 // Inserted directly into the root.
joshualitte45c81c2015-12-02 09:05:37 -080094 void property(const char name[], const char value[]) override {
mtklein1915b622014-08-20 11:45:00 -070095 fRoot[name] = value;
jcgregoriobf5e5232014-07-17 13:14:16 -070096 }
joshualitte45c81c2015-12-02 09:05:37 -080097 void bench(const char name[], int32_t x, int32_t y) override {
jcgregoriobf5e5232014-07-17 13:14:16 -070098 SkString id = SkStringPrintf( "%s_%d_%d", name, x, y);
99 fResults[id.c_str()] = Json::Value(Json::objectValue);
100 fBench = &fResults[id.c_str()];
101 }
joshualitte45c81c2015-12-02 09:05:37 -0800102 void config(const char name[]) override {
bsalomon49f085d2014-09-05 13:34:00 -0700103 SkASSERT(fBench);
jcgregoriobf5e5232014-07-17 13:14:16 -0700104 fConfig = &(*fBench)[name];
105 }
joshualitte45c81c2015-12-02 09:05:37 -0800106 void configOption(const char name[], const char* value) override {
jcgregoriobf5e5232014-07-17 13:14:16 -0700107 (*fConfig)["options"][name] = value;
108 }
joshualitte45c81c2015-12-02 09:05:37 -0800109 void metric(const char name[], double ms) override {
jcgregoriobf5e5232014-07-17 13:14:16 -0700110 // Don't record if nan, or -nan.
111 if (sk_double_isnan(ms)) {
112 return;
113 }
bsalomon49f085d2014-09-05 13:34:00 -0700114 SkASSERT(fConfig);
jcgregoriobf5e5232014-07-17 13:14:16 -0700115 (*fConfig)[name] = ms;
116 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700117
mtkleine070c2b2014-10-14 08:40:43 -0700118 // Flush to storage now please.
joshualitte45c81c2015-12-02 09:05:37 -0800119 void flush() override {
borenet877a52a2015-08-20 09:12:39 -0700120 SkString dirname = SkOSPath::Dirname(fFilename.c_str());
121 if (!sk_exists(dirname.c_str(), kWrite_SkFILE_Flag)) {
122 if (!sk_mkdir(dirname.c_str())) {
123 SkDebugf("Failed to create directory.");
124 }
125 }
mtkleine070c2b2014-10-14 08:40:43 -0700126 SkFILEWStream stream(fFilename.c_str());
127 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
128 stream.flush();
129 }
130
mtklein1915b622014-08-20 11:45:00 -0700131private:
jcgregoriobf5e5232014-07-17 13:14:16 -0700132 SkString fFilename;
133 Json::Value fRoot;
134 Json::Value& fResults;
135 Json::Value* fBench;
136 Json::Value* fConfig;
137};
138
139
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000140#endif