blob: 18d41e4de115dc8cdc521d824de659d08e1177a2 [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
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000016TimerData::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
37static double Min(double a, double b) {
38 return (a < b) ? a : b;
scroggo@google.com9a412522012-09-07 15:21:18 +000039}
40
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000041void TimerData::appendTimes(BenchTimer* timer, bool last) {
scroggo@google.com9a412522012-09-07 15:21:18 +000042 SkASSERT(timer != NULL);
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000043 SkString formatString(fPerIterTimeFormat);
44 if (!last) {
45 formatString.append(",");
scroggo@google.com9a412522012-09-07 15:21:18 +000046 }
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000047 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);
scroggo@google.com9a412522012-09-07 15:21:18 +000053
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000054 // 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);
scroggo@google.com9a412522012-09-07 15:21:18 +000061
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000062 // 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;
scroggo@google.com9a412522012-09-07 15:21:18 +000068
69}
70
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000071SkString 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);
scroggo@google.com9a412522012-09-07 15:21:18 +000090 }
91 SkString str;
92 str.printf(" %4s:", configName);
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000093 if (showWallTime) {
94 str += fWallStr;
scroggo@google.com9a412522012-09-07 15:21:18 +000095 }
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000096 if (showTruncatedWallTime) {
97 str += fTruncatedWallStr;
scroggo@google.com9a412522012-09-07 15:21:18 +000098 }
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000099 if (showCpuTime) {
100 str += fCpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000101 }
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +0000102 if (showTruncatedCpuTime) {
103 str += fTruncatedCpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000104 }
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +0000105 if (showGpuTime && fGpuSum > 0) {
106 str += fGpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000107 }
108 return str;
109}