commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 9 | |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 10 | #ifndef SkResultsWriter_DEFINED |
| 11 | #define SkResultsWriter_DEFINED |
| 12 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 13 | #include "BenchLogger.h" |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 14 | #include "SkJSONCPP.h" |
borenet | 877a52a | 2015-08-20 09:12:39 -0700 | [diff] [blame] | 15 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 16 | #include "SkOSPath.h" |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 17 | #include "SkStream.h" |
| 18 | #include "SkString.h" |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 19 | #include "SkTypes.h" |
| 20 | |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 21 | /** |
| 22 | * Base class for writing out the bench results. |
| 23 | * |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 24 | * Default implementation does nothing. |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 25 | */ |
| 26 | class ResultsWriter : SkNoncopyable { |
| 27 | public: |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 28 | virtual ~ResultsWriter() {} |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 29 | |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 30 | // Record one key value pair that makes up a unique key for this type of run, e.g. |
| 31 | // builder name, machine type, Debug/Release, etc. |
| 32 | virtual void key(const char name[], const char value[]) {} |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 33 | |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 34 | // Record one key value pair that describes the run instance, e.g. git hash, build number. |
| 35 | virtual void property(const char name[], const char value[]) {} |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 36 | |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 37 | // Denote the start of a specific benchmark. Once bench is called, |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 38 | // then config and metric can be called multiple times to record runs. |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 39 | virtual void bench(const char name[], int32_t x, int32_t y) {} |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 40 | |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 41 | // Record the specific configuration a bench is run under, such as "8888". |
| 42 | virtual void config(const char name[]) {} |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 43 | |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 44 | // Record the options for a configuration, such as "GL_RENDERER". |
| 45 | virtual void configOption(const char name[], const char* value) {} |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 46 | |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 47 | // Record a single test metric. |
mtklein | 051e56d | 2014-12-04 08:46:51 -0800 | [diff] [blame] | 48 | virtual void metric(const char name[], double ms) {} |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 49 | |
George Burgess IV | 75b5718 | 2016-12-06 10:53:52 -0800 | [diff] [blame] | 50 | // Record a list of test metrics. |
| 51 | virtual void metrics(const char name[], const SkTArray<double>& array) {} |
| 52 | |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 53 | // Flush to storage now please. |
| 54 | virtual void flush() {} |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 57 | /** |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 58 | NanoJSONResultsWriter writes the test results out in the following |
| 59 | format: |
| 60 | |
| 61 | { |
| 62 | "key": { |
| 63 | "arch": "Arm7", |
| 64 | "gpu": "SGX540", |
| 65 | "os": "Android", |
| 66 | "model": "GalaxyNexus", |
| 67 | } |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 68 | "gitHash": "d1830323662ae8ae06908b97f15180fd25808894", |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 69 | "build_number": "1234", |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 70 | "results" : { |
| 71 | "Xfermode_Luminosity_640_480" : { |
| 72 | "8888" : { |
| 73 | "median_ms" : 143.188128906250, |
| 74 | "min_ms" : 143.835957031250, |
| 75 | ... |
| 76 | }, |
| 77 | ... |
| 78 | */ |
| 79 | class NanoJSONResultsWriter : public ResultsWriter { |
| 80 | public: |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 81 | explicit NanoJSONResultsWriter(const char filename[]) |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 82 | : fFilename(filename) |
| 83 | , fRoot() |
| 84 | , fResults(fRoot["results"]) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 85 | , fBench(nullptr) |
| 86 | , fConfig(nullptr) {} |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 87 | |
| 88 | ~NanoJSONResultsWriter() { |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 89 | this->flush(); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 90 | } |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 91 | |
| 92 | // Added under "key". |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 93 | void key(const char name[], const char value[]) override { |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 94 | fRoot["key"][name] = value; |
| 95 | } |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 96 | // Inserted directly into the root. |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 97 | void property(const char name[], const char value[]) override { |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 98 | fRoot[name] = value; |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 99 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 100 | void bench(const char name[], int32_t x, int32_t y) override { |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 101 | SkString id = SkStringPrintf( "%s_%d_%d", name, x, y); |
| 102 | fResults[id.c_str()] = Json::Value(Json::objectValue); |
| 103 | fBench = &fResults[id.c_str()]; |
| 104 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 105 | void config(const char name[]) override { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 106 | SkASSERT(fBench); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 107 | fConfig = &(*fBench)[name]; |
| 108 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 109 | void configOption(const char name[], const char* value) override { |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 110 | (*fConfig)["options"][name] = value; |
| 111 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 112 | void metric(const char name[], double ms) override { |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 113 | // Don't record if nan, or -nan. |
| 114 | if (sk_double_isnan(ms)) { |
| 115 | return; |
| 116 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 117 | SkASSERT(fConfig); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 118 | (*fConfig)[name] = ms; |
| 119 | } |
George Burgess IV | 75b5718 | 2016-12-06 10:53:52 -0800 | [diff] [blame] | 120 | void metrics(const char name[], const SkTArray<double>& array) override { |
| 121 | SkASSERT(fConfig); |
| 122 | Json::Value value = Json::Value(Json::arrayValue); |
| 123 | value.resize(array.count()); |
| 124 | for (int i = 0; i < array.count(); i++) { |
| 125 | // Don't care about nan-ness. |
| 126 | value[i] = array[i]; |
| 127 | } |
| 128 | (*fConfig)[name] = std::move(value); |
| 129 | } |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 130 | |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 131 | // Flush to storage now please. |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 132 | void flush() override { |
borenet | 877a52a | 2015-08-20 09:12:39 -0700 | [diff] [blame] | 133 | SkString dirname = SkOSPath::Dirname(fFilename.c_str()); |
| 134 | if (!sk_exists(dirname.c_str(), kWrite_SkFILE_Flag)) { |
| 135 | if (!sk_mkdir(dirname.c_str())) { |
| 136 | SkDebugf("Failed to create directory."); |
| 137 | } |
| 138 | } |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 139 | SkFILEWStream stream(fFilename.c_str()); |
| 140 | stream.writeText(Json::StyledWriter().write(fRoot).c_str()); |
| 141 | stream.flush(); |
| 142 | } |
| 143 | |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 144 | private: |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 145 | SkString fFilename; |
| 146 | Json::Value fRoot; |
| 147 | Json::Value& fResults; |
| 148 | Json::Value* fBench; |
| 149 | Json::Value* fConfig; |
| 150 | }; |
| 151 | |
| 152 | |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 153 | #endif |