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 timer 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 timer(const char name[], double ms) {}
51 NanoJSONResultsWriter writes the test results out in the following
59 "model": "GalaxyNexus",
61 "gitHash": "d1830323662ae8ae06908b97f15180fd25808894",
62 "build_number": "1234",
64 "Xfermode_Luminosity_640_480" : {
66 "median_ms" : 143.188128906250,
67 "min_ms" : 143.835957031250,
72 class NanoJSONResultsWriter : public ResultsWriter {
74 explicit NanoJSONResultsWriter(const char filename[])
77 , fResults(fRoot["results"])
81 ~NanoJSONResultsWriter() {
82 SkFILEWStream stream(fFilename.c_str());
83 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
88 virtual void key(const char name[], const char value[]) {
89 fRoot["key"][name] = value;
91 // Inserted directly into the root.
92 virtual void property(const char name[], const char value[]) {
95 virtual void bench(const char name[], int32_t x, int32_t y) {
96 SkString id = SkStringPrintf( "%s_%d_%d", name, x, y);
97 fResults[id.c_str()] = Json::Value(Json::objectValue);
98 fBench = &fResults[id.c_str()];
100 virtual void config(const char name[]) {
102 fConfig = &(*fBench)[name];
104 virtual void configOption(const char name[], const char* value) {
105 (*fConfig)["options"][name] = value;
107 virtual void timer(const char name[], double ms) {
108 // Don't record if nan, or -nan.
109 if (sk_double_isnan(ms)) {
113 (*fConfig)[name] = ms;
119 Json::Value& fResults;
121 Json::Value* fConfig;