Make SKP bench JSON ouput better
[platform/upstream/libSkiaSharp.git] / bench / ResultsWriter.h
1 /*
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  */
9
10 #ifndef SkResultsWriter_DEFINED
11 #define SkResultsWriter_DEFINED
12
13 #include "BenchLogger.h"
14 #include "SkJSONCPP.h"
15 #include "SkStream.h"
16 #include "SkString.h"
17 #include "SkTArray.h"
18 #include "SkTypes.h"
19
20 /**
21  * Base class for writing out the bench results.
22  *
23  * TODO(jcgregorio) Add info if tests fail to converge?
24  */
25 class ResultsWriter : SkNoncopyable {
26 public:
27     virtual ~ResultsWriter() {};
28
29     // Records one option set for this run. All options must be set before
30     // calling bench().
31     virtual void option(const char name[], const char value[]) = 0;
32
33     // Denotes the start of a specific benchmark. Once bench is called,
34     // then config and timer can be called multiple times to record runs.
35     virtual void bench(const char name[], int32_t x, int32_t y) = 0;
36
37     // Records the specific configuration a bench is run under, such as "8888".
38     virtual void config(const char name[]) = 0;
39
40     // Records a single test metric.
41     virtual void timer(const char name[], double ms) = 0;
42
43     // Call when all results are finished.
44     virtual void end() = 0;
45 };
46
47 /**
48  * This ResultsWriter handles writing out the human readable format of the
49  * bench results.
50  */
51 class LoggerResultsWriter : public ResultsWriter {
52 public:
53     explicit LoggerResultsWriter(BenchLogger& logger, const char* timeFormat)
54         : fLogger(logger)
55         , fTimeFormat(timeFormat) {
56         fLogger.logProgress("skia bench:");
57     }
58     virtual void option(const char name[], const char value[]) {
59         fLogger.logProgress(SkStringPrintf(" %s=%s", name, value));
60     }
61     virtual void bench(const char name[], int32_t x, int32_t y) {
62         fLogger.logProgress(SkStringPrintf(
63             "\nrunning bench [%3d %3d] %40s", x, y, name));
64     }
65     virtual void config(const char name[]) {
66         fLogger.logProgress(SkStringPrintf("   %s:", name));
67     }
68     virtual void timer(const char name[], double ms) {
69         fLogger.logProgress(SkStringPrintf("  %s = ", name));
70         fLogger.logProgress(SkStringPrintf(fTimeFormat, ms));
71     }
72     virtual void end() {
73         fLogger.logProgress("\n");
74     }
75 private:
76     BenchLogger& fLogger;
77     const char* fTimeFormat;
78 };
79
80 /**
81  * This ResultsWriter handles writing out the results in JSON.
82  *
83  * The output looks like (except compressed to a single line):
84  *
85  *  {
86  *   "options" : {
87  *      "alpha" : "0xFF",
88  *      "scale" : "0",
89  *      ...
90  *      "system" : "UNIX"
91  *   },
92  *   "results" : [
93  *      {
94  *      "name" : "Xfermode_Luminosity_640_480",
95  *      "results" : [
96  *         {
97  *            "name": "565",
98  *            "cmsecs" : 143.188128906250,
99  *            "msecs" : 143.835957031250
100  *         },
101  *         ...
102  */
103
104 Json::Value* SkFindNamedNode(Json::Value* root, const char name[]);
105 Json::Value SkMakeBuilderJSON(const SkString &buildername);
106
107 class JSONResultsWriter : public ResultsWriter {
108 public:
109     explicit JSONResultsWriter(const char filename[])
110         : fFilename(filename)
111         , fRoot()
112         , fResults(fRoot["results"])
113         , fBench(NULL)
114         , fConfig(NULL) {
115     }
116     virtual void option(const char name[], const char value[]) {
117         fRoot["options"][name] = value;
118     }
119     virtual void bench(const char name[], int32_t x, int32_t y) {
120         SkString sk_name(name);
121         sk_name.append("_");
122         sk_name.appendS32(x);
123         sk_name.append("_");
124         sk_name.appendS32(y);
125         Json::Value* bench_node = SkFindNamedNode(&fResults, sk_name.c_str());
126         fBench = &(*bench_node)["results"];
127     }
128     virtual void config(const char name[]) {
129         SkASSERT(NULL != fBench);
130         fConfig = SkFindNamedNode(fBench, name);
131     }
132     virtual void timer(const char name[], double ms) {
133         SkASSERT(NULL != fConfig);
134         (*fConfig)[name] = ms;
135     }
136     virtual void end() {
137         SkFILEWStream stream(fFilename.c_str());
138         stream.writeText(Json::FastWriter().write(fRoot).c_str());
139         stream.flush();
140     }
141 private:
142
143     SkString fFilename;
144     Json::Value fRoot;
145     Json::Value& fResults;
146     Json::Value* fBench;
147     Json::Value* fConfig;
148 };
149
150 /**
151  * This ResultsWriter writes out to multiple ResultsWriters.
152  */
153 class MultiResultsWriter : public ResultsWriter {
154 public:
155     MultiResultsWriter() : writers() {
156     };
157     void add(ResultsWriter* writer) {
158       writers.push_back(writer);
159     }
160     virtual void option(const char name[], const char value[]) {
161         for (int i = 0; i < writers.count(); ++i) {
162             writers[i]->option(name, value);
163         }
164     }
165     virtual void bench(const char name[], int32_t x, int32_t y) {
166         for (int i = 0; i < writers.count(); ++i) {
167             writers[i]->bench(name, x, y);
168         }
169     }
170     virtual void config(const char name[]) {
171         for (int i = 0; i < writers.count(); ++i) {
172             writers[i]->config(name);
173         }
174     }
175     virtual void timer(const char name[], double ms) {
176         for (int i = 0; i < writers.count(); ++i) {
177             writers[i]->timer(name, ms);
178         }
179     }
180     virtual void end() {
181         for (int i = 0; i < writers.count(); ++i) {
182             writers[i]->end();
183         }
184     }
185 private:
186     SkTArray<ResultsWriter *> writers;
187 };
188
189 /**
190  * Calls the end() method of T on destruction.
191  */
192 template <typename T> class CallEnd : SkNoncopyable {
193 public:
194     CallEnd(T& obj) : fObj(obj) {}
195     ~CallEnd() { fObj.end(); }
196 private:
197     T&  fObj;
198 };
199
200 #endif