blob: 3b4baacfcde4aac38fa842c770c892214b8451b5 [file] [log] [blame]
scroggo@google.com9a412522012-09-07 15:21:18 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "TimerData.h"
10
11#include "BenchTimer.h"
12#include <limits>
13
14using namespace std;
15
16TimerData::TimerData(const SkString& perIterTimeFormat, const SkString& normalTimeFormat)
17: fWallStr(" msecs = ")
18, fTruncatedWallStr(" Wmsecs = ")
19, fCpuStr(" cmsecs = ")
20, fTruncatedCpuStr(" Cmsecs = ")
21, fGpuStr(" gmsecs = ")
22, fWallSum(0.0)
23, fWallMin(numeric_limits<double>::max())
24, fTruncatedWallSum(0.0)
25, fTruncatedWallMin(numeric_limits<double>::max())
26, fCpuSum(0.0)
27, fCpuMin(numeric_limits<double>::max())
28, fTruncatedCpuSum(0.0)
29, fTruncatedCpuMin(numeric_limits<double>::max())
30, fGpuSum(0.0)
31, fGpuMin(numeric_limits<double>::max())
32, fPerIterTimeFormat(perIterTimeFormat)
33, fNormalTimeFormat(normalTimeFormat)
34{}
35
36static double Min(double a, double b) {
37 return (a < b) ? a : b;
38}
39
40void TimerData::appendTimes(BenchTimer* timer, bool last) {
41 SkASSERT(timer != NULL);
42 SkString formatString(fPerIterTimeFormat);
43 if (!last) {
44 formatString.append(",");
45 }
46 const char* format = formatString.c_str();
47 fWallStr.appendf(format, timer->fWall);
48 fCpuStr.appendf(format, timer->fCpu);
49 fTruncatedWallStr.appendf(format, timer->fTruncatedWall);
50 fTruncatedCpuStr.appendf(format, timer->fTruncatedCpu);
51 fGpuStr.appendf(format, timer->fGpu);
52
53 // Store the minimum values. We do not need to special case the first time since we initialized
54 // to max double.
55 fWallMin = Min(fWallMin, timer->fWall);
56 fCpuMin = Min(fCpuMin, timer->fCpu);
57 fTruncatedWallMin = Min(fTruncatedWallMin, timer->fTruncatedWall);
58 fTruncatedCpuMin = Min(fTruncatedCpuMin, timer->fTruncatedCpu);
59 fGpuMin = Min(fGpuMin, timer->fGpu);
60
61 // Tally the sum of each timer type.
62 fWallSum += timer->fWall;
63 fCpuSum += timer->fCpu;
64 fTruncatedWallSum += timer->fTruncatedWall;
65 fTruncatedCpuSum += timer->fTruncatedCpu;
66 fGpuSum += timer->fGpu;
67
68}
69
70SkString TimerData::getResult(bool logPerIter, bool printMin, int repeatDraw,
71 const char *configName, bool showWallTime, bool showTruncatedWallTime,
72 bool showCpuTime, bool showTruncatedCpuTime, bool showGpuTime) {
73 // output each repeat (no average) if logPerIter is set,
74 // otherwise output only the average
75 if (!logPerIter) {
76 const char* format = fNormalTimeFormat.c_str();
77 fWallStr.set(" msecs = ");
78 fWallStr.appendf(format, printMin ? fWallMin : fWallSum / repeatDraw);
79 fCpuStr.set(" cmsecs = ");
80 fCpuStr.appendf(format, printMin ? fCpuMin : fCpuSum / repeatDraw);
81 fTruncatedWallStr.set(" Wmsecs = ");
82 fTruncatedWallStr.appendf(format,
83 printMin ? fTruncatedWallMin : fTruncatedWallSum / repeatDraw);
84 fTruncatedCpuStr.set(" Cmsecs = ");
85 fTruncatedCpuStr.appendf(format,
86 printMin ? fTruncatedCpuMin : fTruncatedCpuSum / repeatDraw);
87 fGpuStr.set(" gmsecs = ");
88 fGpuStr.appendf(format, printMin ? fGpuMin : fGpuSum / repeatDraw);
89 }
90 SkString str;
91 str.printf(" %4s:", configName);
92 if (showWallTime) {
93 str += fWallStr;
94 }
95 if (showTruncatedWallTime) {
96 str += fTruncatedWallStr;
97 }
98 if (showCpuTime) {
99 str += fCpuStr;
100 }
101 if (showTruncatedCpuTime) {
102 str += fTruncatedCpuStr;
103 }
104 if (showGpuTime && fGpuSum > 0) {
105 str += fGpuStr;
106 }
107 return str;
108}