aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/PictureResultsWriter.h
blob: 272a50c4c3dcd9ddaa5db91d639939efc9dfe3ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Classes for writing out bench results in various formats.
 */
#ifndef SkPictureResultsWriter_DEFINED
#define SkPictureResultsWriter_DEFINED

#include "ResultsWriter.h"
#include "SkBenchLogger.h"
#include "SkJSONCPP.h"
#include "SkStream.h"
#include "SkString.h"
#include "SkTArray.h"

/**
 * Base class for writing picture bench results.
 */
class PictureResultsWriter : SkNoncopyable {
public:
    enum TileFlags {kPurging, kAvg};

    PictureResultsWriter() {}
    virtual ~PictureResultsWriter() {}

    virtual void bench(const char name[], int32_t x, int32_t y) = 0;
    virtual void tileConfig(SkString configName) = 0;
    virtual void tileMeta(int x, int y, int tx, int ty) = 0;
    virtual void addTileFlag(PictureResultsWriter::TileFlags flag) = 0;
    virtual void tileData(
            TimerData* data,
            const char format[],
            const TimerData::Result result,
            uint32_t timerTypes,
            int numInnerLoops = 1) = 0;
   virtual void end() = 0;
};

/**
 * This class allows bench data to be piped into multiple
 * PictureResultWriter classes. It does not own any classes
 * passed to it, so the owner is required to manage any classes
 * passed to PictureResultsMultiWriter */
class PictureResultsMultiWriter : public PictureResultsWriter {
public:
    PictureResultsMultiWriter()
        : fWriters() {}
    void add(PictureResultsWriter* newWriter) {
        fWriters.push_back(newWriter);
    }
    virtual ~PictureResultsMultiWriter() {}
    virtual void bench(const char name[], int32_t x, int32_t y) {
        for(int i=0; i<fWriters.count(); ++i) {
            fWriters[i]->bench(name, x, y);
        }
    }
    virtual void tileConfig(SkString configName) {
        for(int i=0; i<fWriters.count(); ++i) {
            fWriters[i]->tileConfig(configName);
        }
    }
    virtual void tileMeta(int x, int y, int tx, int ty) {
        for(int i=0; i<fWriters.count(); ++i) {
            fWriters[i]->tileMeta(x, y, tx, ty);
        }
    }
    virtual void addTileFlag(PictureResultsWriter::TileFlags flag) {
        for(int i=0; i<fWriters.count(); ++i) {
            fWriters[i]->addTileFlag(flag);
        }
    }
    virtual void tileData(
            TimerData* data,
            const char format[],
            const TimerData::Result result,
            uint32_t timerTypes,
            int numInnerLoops = 1) {
        for(int i=0; i<fWriters.count(); ++i) {
            fWriters[i]->tileData(data, format, result, timerTypes,
                                 numInnerLoops);
        }
    }
   virtual void end() {
        for(int i=0; i<fWriters.count(); ++i) {
            fWriters[i]->end();
        }
   }
private:
    SkTArray<PictureResultsWriter*> fWriters;
};

/**
 * Writes to SkBenchLogger to mimic original behavior
 */
class PictureResultsLoggerWriter : public PictureResultsWriter {
private:
    void logProgress(const char str[]) {
        if(fLogger != NULL) {
            fLogger->logProgress(str);
        }
    }
public:
    PictureResultsLoggerWriter(SkBenchLogger* log)
          : fLogger(log), currentLine() {}
    virtual void bench(const char name[], int32_t x, int32_t y) {
        SkString result;
        result.printf("running bench [%i %i] %s ", x, y, name);
        this->logProgress(result.c_str());
    }
    virtual void tileConfig(SkString configName) {
        currentLine = configName;
    }
    virtual void tileMeta(int x, int y, int tx, int ty) {
        currentLine.appendf(": tile [%i,%i] out of [%i,%i]", x, y, tx, ty);
    }
    virtual void addTileFlag(PictureResultsWriter::TileFlags flag) {
        if(flag == PictureResultsWriter::kPurging) {
            currentLine.append(" <withPurging>");
        } else if(flag == PictureResultsWriter::kAvg) {
            currentLine.append(" <averaged>");
        }
    }
    virtual void tileData(
            TimerData* data,
            const char format[],
            const TimerData::Result result,
            uint32_t timerTypes,
            int numInnerLoops = 1) {
        SkString results = data->getResult(format, result,
                currentLine.c_str(), timerTypes, numInnerLoops);
        results.append("\n");
        this->logProgress(results.c_str());
    }
    virtual void end() {}
private:
    SkBenchLogger* fLogger;
    SkString currentLine;
};

/**
 * This PictureResultsWriter collects data in a JSON node
 *
 * The format is something like
 * {
 *      benches: [
 *          {
 *              name: "Name_of_test"
 *              tilesets: [
 *                  {
 *                      name: "Name of the configuration"
 *                      tiles: [
 *                          {
 *                              flags: {
 *                                  purging: true //Flags for the current tile
 *                                              // are put here
 *                              }
 *                              data: {
 *                                  wsecs: [....] //Actual data ends up here
 *                              }
 *                          }
 *                      ]
 *                  }
 *              ]
 *          }
 *      ]
 * }*/

class PictureJSONResultsWriter : public PictureResultsWriter {
public:
    PictureJSONResultsWriter(const char filename[])
        : fFilename(filename),
          fRoot(),
          fCurrentBench(NULL),
          fCurrentTileSet(NULL),
          fCurrentTile(NULL) {}

    virtual void bench(const char name[], int32_t x, int32_t y) {
        SkString sk_name(name);
        sk_name.append("_");
        sk_name.appendS32(x);
        sk_name.append("_");
        sk_name.appendS32(y);
        Json::Value* bench_node = SkFindNamedNode(&fRoot["benches"], sk_name.c_str());
        fCurrentBench = &(*bench_node)["tileSets"];
    }
    virtual void tileConfig(SkString configName) {
        SkASSERT(fCurrentBench != NULL);
        fCurrentTileSet = SkFindNamedNode(fCurrentBench, configName.c_str());
        fCurrentTile = &(*fCurrentTileSet)["tiles"][0];
    }
    virtual void tileMeta(int x, int y, int tx, int ty) {
        SkASSERT(fCurrentTileSet != NULL);
        (*fCurrentTileSet)["tx"] = tx;
        (*fCurrentTileSet)["ty"] = ty;
        fCurrentTile = &(*fCurrentTileSet)["tiles"][x+tx*y];
    }
    virtual void addTileFlag(PictureResultsWriter::TileFlags flag) {
        SkASSERT(fCurrentTile != NULL);
        if(flag == PictureResultsWriter::kPurging) {
            (*fCurrentTile)["flags"]["purging"] = true;
        } else if(flag == PictureResultsWriter::kAvg) {
            (*fCurrentTile)["flags"]["averaged"] = true;
        }
    }
    virtual void tileData(
            TimerData* data,
            const char format[],
            const TimerData::Result result,
            uint32_t timerTypes,
            int numInnerLoops = 1) {
        SkASSERT(fCurrentTile != NULL);
        (*fCurrentTile)["data"] = data->getJSON(timerTypes, result, numInnerLoops);
    }
    virtual void end() {
       SkFILEWStream stream(fFilename.c_str());
       stream.writeText(Json::FastWriter().write(fRoot).c_str());
       stream.flush();
    }
private:
    SkString fFilename;
    Json::Value fRoot;
    Json::Value *fCurrentBench;
    Json::Value *fCurrentTileSet;
    Json::Value *fCurrentTile;
};

#endif