2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 * Classes for writing out bench results in various formats.
10 #ifndef SkResultsWriter_DEFINED
11 #define SkResultsWriter_DEFINED
13 #include "BenchLogger.h"
14 #include "SkJSONCPP.h"
21 * Base class for writing out the bench results.
23 * Default implementation does nothing.
25 class ResultsWriter : SkNoncopyable {
27 virtual ~ResultsWriter() {}
29 // 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[]) {}
33 // 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[]) {}
36 // Denote the start of a specific benchmark. Once bench is called,
37 // then config and metric can be called multiple times to record runs.
38 virtual void bench(const char name[], int32_t x, int32_t y) {}
40 // Record the specific configuration a bench is run under, such as "8888".
41 virtual void config(const char name[]) {}
43 // Record the options for a configuration, such as "GL_RENDERER".
44 virtual void configOption(const char name[], const char* value) {}
46 // Record a single test metric.
47 virtual void metric(const char name[], double ms) {}
49 // Flush to storage now please.
50 virtual void flush() {}
54 NanoJSONResultsWriter writes the test results out in the following
62 "model": "GalaxyNexus",
64 "gitHash": "d1830323662ae8ae06908b97f15180fd25808894",
65 "build_number": "1234",
67 "Xfermode_Luminosity_640_480" : {
69 "median_ms" : 143.188128906250,
70 "min_ms" : 143.835957031250,
75 class NanoJSONResultsWriter : public ResultsWriter {
77 explicit NanoJSONResultsWriter(const char filename[])
80 , fResults(fRoot["results"])
84 ~NanoJSONResultsWriter() {
89 virtual void key(const char name[], const char value[]) {
90 fRoot["key"][name] = value;
92 // Inserted directly into the root.
93 virtual void property(const char name[], const char value[]) {
96 virtual void bench(const char name[], int32_t x, int32_t y) {
97 SkString id = SkStringPrintf( "%s_%d_%d", name, x, y);
98 fResults[id.c_str()] = Json::Value(Json::objectValue);
99 fBench = &fResults[id.c_str()];
101 virtual void config(const char name[]) {
103 fConfig = &(*fBench)[name];
105 virtual void configOption(const char name[], const char* value) {
106 (*fConfig)["options"][name] = value;
108 virtual void metric(const char name[], double ms) {
109 // Don't record if nan, or -nan.
110 if (sk_double_isnan(ms)) {
114 (*fConfig)[name] = ms;
117 // Flush to storage now please.
118 virtual void flush() {
119 SkFILEWStream stream(fFilename.c_str());
120 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
127 Json::Value& fResults;
129 Json::Value* fConfig;