blob: f53a980de43d87c79e3ff45570e7064fdcdaf594 [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"
15#include "SkStream.h"
16#include "SkString.h"
17#include "SkTArray.h"
18#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"])
81 , fBench(NULL)
mtklein1915b622014-08-20 11:45:00 -070082 , fConfig(NULL) {}
83
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".
jcgregoriobf5e5232014-07-17 13:14:16 -070089 virtual void key(const char name[], const char value[]) {
90 fRoot["key"][name] = value;
91 }
mtklein1915b622014-08-20 11:45:00 -070092 // Inserted directly into the root.
93 virtual void property(const char name[], const char value[]) {
94 fRoot[name] = value;
jcgregoriobf5e5232014-07-17 13:14:16 -070095 }
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()];
100 }
101 virtual void config(const char name[]) {
bsalomon49f085d2014-09-05 13:34:00 -0700102 SkASSERT(fBench);
jcgregoriobf5e5232014-07-17 13:14:16 -0700103 fConfig = &(*fBench)[name];
104 }
105 virtual void configOption(const char name[], const char* value) {
106 (*fConfig)["options"][name] = value;
107 }
mtklein051e56d2014-12-04 08:46:51 -0800108 virtual void metric(const char name[], double ms) {
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.
118 virtual void flush() {
119 SkFILEWStream stream(fFilename.c_str());
120 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
121 stream.flush();
122 }
123
mtklein1915b622014-08-20 11:45:00 -0700124private:
jcgregoriobf5e5232014-07-17 13:14:16 -0700125 SkString fFilename;
126 Json::Value fRoot;
127 Json::Value& fResults;
128 Json::Value* fBench;
129 Json::Value* fConfig;
130};
131
132
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000133#endif