blob: f17bce3d930dbd9de8111d937cafb4f4ab003fd0 [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,
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000037 // then config and timer 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.
47 virtual void timer(const char name[], double ms) {}
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000048};
49
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +000050/**
jcgregoriobf5e5232014-07-17 13:14:16 -070051 NanoJSONResultsWriter writes the test results out in the following
52 format:
53
54 {
55 "key": {
56 "arch": "Arm7",
57 "gpu": "SGX540",
58 "os": "Android",
59 "model": "GalaxyNexus",
60 }
jcgregoriobf5e5232014-07-17 13:14:16 -070061 "gitHash": "d1830323662ae8ae06908b97f15180fd25808894",
mtklein1915b622014-08-20 11:45:00 -070062 "build_number": "1234",
jcgregoriobf5e5232014-07-17 13:14:16 -070063 "results" : {
64 "Xfermode_Luminosity_640_480" : {
65 "8888" : {
66 "median_ms" : 143.188128906250,
67 "min_ms" : 143.835957031250,
68 ...
69 },
70 ...
71*/
72class NanoJSONResultsWriter : public ResultsWriter {
73public:
mtklein1915b622014-08-20 11:45:00 -070074 explicit NanoJSONResultsWriter(const char filename[])
jcgregoriobf5e5232014-07-17 13:14:16 -070075 : fFilename(filename)
76 , fRoot()
77 , fResults(fRoot["results"])
78 , fBench(NULL)
mtklein1915b622014-08-20 11:45:00 -070079 , fConfig(NULL) {}
80
81 ~NanoJSONResultsWriter() {
82 SkFILEWStream stream(fFilename.c_str());
83 stream.writeText(Json::StyledWriter().write(fRoot).c_str());
84 stream.flush();
jcgregoriobf5e5232014-07-17 13:14:16 -070085 }
mtklein1915b622014-08-20 11:45:00 -070086
87 // Added under "key".
jcgregoriobf5e5232014-07-17 13:14:16 -070088 virtual void key(const char name[], const char value[]) {
89 fRoot["key"][name] = value;
90 }
mtklein1915b622014-08-20 11:45:00 -070091 // Inserted directly into the root.
92 virtual void property(const char name[], const char value[]) {
93 fRoot[name] = value;
jcgregoriobf5e5232014-07-17 13:14:16 -070094 }
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()];
99 }
100 virtual void config(const char name[]) {
bsalomon49f085d2014-09-05 13:34:00 -0700101 SkASSERT(fBench);
jcgregoriobf5e5232014-07-17 13:14:16 -0700102 fConfig = &(*fBench)[name];
103 }
104 virtual void configOption(const char name[], const char* value) {
105 (*fConfig)["options"][name] = value;
106 }
107 virtual void timer(const char name[], double ms) {
108 // Don't record if nan, or -nan.
109 if (sk_double_isnan(ms)) {
110 return;
111 }
bsalomon49f085d2014-09-05 13:34:00 -0700112 SkASSERT(fConfig);
jcgregoriobf5e5232014-07-17 13:14:16 -0700113 (*fConfig)[name] = ms;
114 }
jcgregoriobf5e5232014-07-17 13:14:16 -0700115
mtklein1915b622014-08-20 11:45:00 -0700116private:
jcgregoriobf5e5232014-07-17 13:14:16 -0700117 SkString fFilename;
118 Json::Value fRoot;
119 Json::Value& fResults;
120 Json::Value* fBench;
121 Json::Value* fConfig;
122};
123
124
commit-bot@chromium.orge3bb3bc2013-12-03 18:16:48 +0000125#endif