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" |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 16 | #include "SkStream.h" |
| 17 | #include "SkString.h" |
| 18 | #include "SkTArray.h" |
| 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 | |
| 50 | // Flush to storage now please. |
| 51 | virtual void flush() {} |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 54 | /** |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 55 | NanoJSONResultsWriter writes the test results out in the following |
| 56 | format: |
| 57 | |
| 58 | { |
| 59 | "key": { |
| 60 | "arch": "Arm7", |
| 61 | "gpu": "SGX540", |
| 62 | "os": "Android", |
| 63 | "model": "GalaxyNexus", |
| 64 | } |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 65 | "gitHash": "d1830323662ae8ae06908b97f15180fd25808894", |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 66 | "build_number": "1234", |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 67 | "results" : { |
| 68 | "Xfermode_Luminosity_640_480" : { |
| 69 | "8888" : { |
| 70 | "median_ms" : 143.188128906250, |
| 71 | "min_ms" : 143.835957031250, |
| 72 | ... |
| 73 | }, |
| 74 | ... |
| 75 | */ |
| 76 | class NanoJSONResultsWriter : public ResultsWriter { |
| 77 | public: |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 78 | explicit NanoJSONResultsWriter(const char filename[]) |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 79 | : fFilename(filename) |
| 80 | , fRoot() |
| 81 | , fResults(fRoot["results"]) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 82 | , fBench(nullptr) |
| 83 | , fConfig(nullptr) {} |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 84 | |
| 85 | ~NanoJSONResultsWriter() { |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 86 | this->flush(); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 87 | } |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 88 | |
| 89 | // Added under "key". |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 90 | void key(const char name[], const char value[]) override { |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 91 | fRoot["key"][name] = value; |
| 92 | } |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 93 | // Inserted directly into the root. |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 94 | void property(const char name[], const char value[]) override { |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 95 | fRoot[name] = value; |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 96 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 97 | void bench(const char name[], int32_t x, int32_t y) override { |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 98 | SkString id = SkStringPrintf( "%s_%d_%d", name, x, y); |
| 99 | fResults[id.c_str()] = Json::Value(Json::objectValue); |
| 100 | fBench = &fResults[id.c_str()]; |
| 101 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 102 | void config(const char name[]) override { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 103 | SkASSERT(fBench); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 104 | fConfig = &(*fBench)[name]; |
| 105 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 106 | void configOption(const char name[], const char* value) override { |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 107 | (*fConfig)["options"][name] = value; |
| 108 | } |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 109 | void metric(const char name[], double ms) override { |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 110 | // Don't record if nan, or -nan. |
| 111 | if (sk_double_isnan(ms)) { |
| 112 | return; |
| 113 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 114 | SkASSERT(fConfig); |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 115 | (*fConfig)[name] = ms; |
| 116 | } |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 117 | |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 118 | // Flush to storage now please. |
joshualitt | e45c81c | 2015-12-02 09:05:37 -0800 | [diff] [blame] | 119 | void flush() override { |
borenet | 877a52a | 2015-08-20 09:12:39 -0700 | [diff] [blame] | 120 | SkString dirname = SkOSPath::Dirname(fFilename.c_str()); |
| 121 | if (!sk_exists(dirname.c_str(), kWrite_SkFILE_Flag)) { |
| 122 | if (!sk_mkdir(dirname.c_str())) { |
| 123 | SkDebugf("Failed to create directory."); |
| 124 | } |
| 125 | } |
mtklein | e070c2b | 2014-10-14 08:40:43 -0700 | [diff] [blame] | 126 | SkFILEWStream stream(fFilename.c_str()); |
| 127 | stream.writeText(Json::StyledWriter().write(fRoot).c_str()); |
| 128 | stream.flush(); |
| 129 | } |
| 130 | |
mtklein | 1915b62 | 2014-08-20 11:45:00 -0700 | [diff] [blame] | 131 | private: |
jcgregorio | bf5e523 | 2014-07-17 13:14:16 -0700 | [diff] [blame] | 132 | SkString fFilename; |
| 133 | Json::Value fRoot; |
| 134 | Json::Value& fResults; |
| 135 | Json::Value* fBench; |
| 136 | Json::Value* fConfig; |
| 137 | }; |
| 138 | |
| 139 | |
commit-bot@chromium.org | e3bb3bc | 2013-12-03 18:16:48 +0000 | [diff] [blame] | 140 | #endif |