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
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)
bsalomon@google.comf3f2d162013-07-31 18:52:31 +000023, fWallMin(numeric_limits<double>::max())
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000024, fTruncatedWallSum(0.0)
bsalomon@google.comf3f2d162013-07-31 18:52:31 +000025, fTruncatedWallMin(numeric_limits<double>::max())
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000026, fCpuSum(0.0)
bsalomon@google.comf3f2d162013-07-31 18:52:31 +000027, fCpuMin(numeric_limits<double>::max())
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000028, fTruncatedCpuSum(0.0)
bsalomon@google.comf3f2d162013-07-31 18:52:31 +000029, fTruncatedCpuMin(numeric_limits<double>::max())
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000030, fGpuSum(0.0)
bsalomon@google.comf3f2d162013-07-31 18:52:31 +000031, fGpuMin(numeric_limits<double>::max())
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000032, fPerIterTimeFormat(perIterTimeFormat)
33, fNormalTimeFormat(normalTimeFormat)
34{}
35
36static double Min(double a, double b) {
37 return (a < b) ? a : b;
scroggo@google.com9a412522012-09-07 15:21:18 +000038}
39
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000040void TimerData::appendTimes(BenchTimer* timer, bool last) {
scroggo@google.com9a412522012-09-07 15:21:18 +000041 SkASSERT(timer != NULL);
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000042 SkString formatString(fPerIterTimeFormat);
43 if (!last) {
44 formatString.append(",");
scroggo@google.com9a412522012-09-07 15:21:18 +000045 }
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000046 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);
scroggo@google.com9a412522012-09-07 15:21:18 +000052
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000053 // 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);
scroggo@google.com9a412522012-09-07 15:21:18 +000060
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000061 // 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;
scroggo@google.com9a412522012-09-07 15:21:18 +000067
68}
69
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000070SkString 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);
scroggo@google.com9a412522012-09-07 15:21:18 +000089 }
90 SkString str;
91 str.printf(" %4s:", configName);
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000092 if (showWallTime) {
93 str += fWallStr;
scroggo@google.com9a412522012-09-07 15:21:18 +000094 }
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000095 if (showTruncatedWallTime) {
96 str += fTruncatedWallStr;
scroggo@google.com9a412522012-09-07 15:21:18 +000097 }
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +000098 if (showCpuTime) {
99 str += fCpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000100 }
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +0000101 if (showTruncatedCpuTime) {
102 str += fTruncatedCpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000103 }
bsalomon@google.com68f2a0d2013-07-25 21:30:52 +0000104 if (showGpuTime && fGpuSum > 0) {
105 str += fGpuStr;
scroggo@google.com9a412522012-09-07 15:21:18 +0000106 }
107 return str;
108}