1
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
14 using namespace std;
15
TimerData(const SkString & perIterTimeFormat,const SkString & normalTimeFormat)16 TimerData::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)()) // Extra parens to make the windows build work, due to
24 // 'max' macro
25 , fTruncatedWallSum(0.0)
26 , fTruncatedWallMin((numeric_limits<double>::max)())
27 , fCpuSum(0.0)
28 , fCpuMin((numeric_limits<double>::max)())
29 , fTruncatedCpuSum(0.0)
30 , fTruncatedCpuMin((numeric_limits<double>::max)())
31 , fGpuSum(0.0)
32 , fGpuMin((numeric_limits<double>::max)())
33 , fPerIterTimeFormat(perIterTimeFormat)
34 , fNormalTimeFormat(normalTimeFormat)
35 {}
36
Min(double a,double b)37 static double Min(double a, double b) {
38 return (a < b) ? a : b;
39 }
40
appendTimes(BenchTimer * timer,bool last)41 void TimerData::appendTimes(BenchTimer* timer, bool last) {
42 SkASSERT(timer != NULL);
43 SkString formatString(fPerIterTimeFormat);
44 if (!last) {
45 formatString.append(",");
46 }
47 const char* format = formatString.c_str();
48 fWallStr.appendf(format, timer->fWall);
49 fCpuStr.appendf(format, timer->fCpu);
50 fTruncatedWallStr.appendf(format, timer->fTruncatedWall);
51 fTruncatedCpuStr.appendf(format, timer->fTruncatedCpu);
52 fGpuStr.appendf(format, timer->fGpu);
53
54 // Store the minimum values. We do not need to special case the first time since we initialized
55 // to max double.
56 fWallMin = Min(fWallMin, timer->fWall);
57 fCpuMin = Min(fCpuMin, timer->fCpu);
58 fTruncatedWallMin = Min(fTruncatedWallMin, timer->fTruncatedWall);
59 fTruncatedCpuMin = Min(fTruncatedCpuMin, timer->fTruncatedCpu);
60 fGpuMin = Min(fGpuMin, timer->fGpu);
61
62 // Tally the sum of each timer type.
63 fWallSum += timer->fWall;
64 fCpuSum += timer->fCpu;
65 fTruncatedWallSum += timer->fTruncatedWall;
66 fTruncatedCpuSum += timer->fTruncatedCpu;
67 fGpuSum += timer->fGpu;
68
69 }
70
getResult(bool logPerIter,bool printMin,int repeatDraw,const char * configName,bool showWallTime,bool showTruncatedWallTime,bool showCpuTime,bool showTruncatedCpuTime,bool showGpuTime)71 SkString TimerData::getResult(bool logPerIter, bool printMin, int repeatDraw,
72 const char *configName, bool showWallTime, bool showTruncatedWallTime,
73 bool showCpuTime, bool showTruncatedCpuTime, bool showGpuTime) {
74 // output each repeat (no average) if logPerIter is set,
75 // otherwise output only the average
76 if (!logPerIter) {
77 const char* format = fNormalTimeFormat.c_str();
78 fWallStr.set(" msecs = ");
79 fWallStr.appendf(format, printMin ? fWallMin : fWallSum / repeatDraw);
80 fCpuStr.set(" cmsecs = ");
81 fCpuStr.appendf(format, printMin ? fCpuMin : fCpuSum / repeatDraw);
82 fTruncatedWallStr.set(" Wmsecs = ");
83 fTruncatedWallStr.appendf(format,
84 printMin ? fTruncatedWallMin : fTruncatedWallSum / repeatDraw);
85 fTruncatedCpuStr.set(" Cmsecs = ");
86 fTruncatedCpuStr.appendf(format,
87 printMin ? fTruncatedCpuMin : fTruncatedCpuSum / repeatDraw);
88 fGpuStr.set(" gmsecs = ");
89 fGpuStr.appendf(format, printMin ? fGpuMin : fGpuSum / repeatDraw);
90 }
91 SkString str;
92 str.printf(" %4s:", configName);
93 if (showWallTime) {
94 str += fWallStr;
95 }
96 if (showTruncatedWallTime) {
97 str += fTruncatedWallStr;
98 }
99 if (showCpuTime) {
100 str += fCpuStr;
101 }
102 if (showTruncatedCpuTime) {
103 str += fTruncatedCpuStr;
104 }
105 if (showGpuTime && fGpuSum > 0) {
106 str += fGpuStr;
107 }
108 return str;
109 }
110