aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/TimerData.cpp
blob: 18d41e4de115dc8cdc521d824de659d08e1177a2 (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

/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "TimerData.h"

#include "BenchTimer.h"
#include <limits>

using namespace std;

TimerData::TimerData(const SkString& perIterTimeFormat, const SkString& normalTimeFormat)
: fWallStr(" msecs = ")
, fTruncatedWallStr(" Wmsecs = ")
, fCpuStr(" cmsecs = ")
, fTruncatedCpuStr(" Cmsecs = ")
, fGpuStr(" gmsecs = ")
, fWallSum(0.0)
, fWallMin((numeric_limits<double>::max)()) // Extra parens to make the windows build work, due to
                                            // 'max' macro
, fTruncatedWallSum(0.0)
, fTruncatedWallMin((numeric_limits<double>::max)())
, fCpuSum(0.0)
, fCpuMin((numeric_limits<double>::max)())
, fTruncatedCpuSum(0.0)
, fTruncatedCpuMin((numeric_limits<double>::max)())
, fGpuSum(0.0)
, fGpuMin((numeric_limits<double>::max)())
, fPerIterTimeFormat(perIterTimeFormat)
, fNormalTimeFormat(normalTimeFormat)
{}

static double Min(double a, double b) {
    return (a < b) ? a : b;
}

void TimerData::appendTimes(BenchTimer* timer, bool last) {
    SkASSERT(timer != NULL);
    SkString formatString(fPerIterTimeFormat);
    if (!last) {
        formatString.append(",");
    }
    const char* format = formatString.c_str();
    fWallStr.appendf(format, timer->fWall);
    fCpuStr.appendf(format, timer->fCpu);
    fTruncatedWallStr.appendf(format, timer->fTruncatedWall);
    fTruncatedCpuStr.appendf(format, timer->fTruncatedCpu);
    fGpuStr.appendf(format, timer->fGpu);

    // Store the minimum values. We do not need to special case the first time since we initialized
    // to max double.
    fWallMin = Min(fWallMin, timer->fWall);
    fCpuMin  = Min(fCpuMin,  timer->fCpu);
    fTruncatedWallMin = Min(fTruncatedWallMin, timer->fTruncatedWall);
    fTruncatedCpuMin  = Min(fTruncatedCpuMin,  timer->fTruncatedCpu);
    fGpuMin  = Min(fGpuMin,  timer->fGpu);

    // Tally the sum of each timer type.
    fWallSum += timer->fWall;
    fCpuSum += timer->fCpu;
    fTruncatedWallSum += timer->fTruncatedWall;
    fTruncatedCpuSum += timer->fTruncatedCpu;
    fGpuSum += timer->fGpu;

}

SkString TimerData::getResult(bool logPerIter, bool printMin, int repeatDraw,
                              const char *configName, bool showWallTime, bool showTruncatedWallTime,
                              bool showCpuTime, bool showTruncatedCpuTime, bool showGpuTime) {
    // output each repeat (no average) if logPerIter is set,
    // otherwise output only the average
    if (!logPerIter) {
        const char* format = fNormalTimeFormat.c_str();
        fWallStr.set(" msecs = ");
        fWallStr.appendf(format, printMin ? fWallMin : fWallSum / repeatDraw);
        fCpuStr.set(" cmsecs = ");
        fCpuStr.appendf(format, printMin ? fCpuMin : fCpuSum / repeatDraw);
        fTruncatedWallStr.set(" Wmsecs = ");
        fTruncatedWallStr.appendf(format,
                                  printMin ? fTruncatedWallMin : fTruncatedWallSum / repeatDraw);
        fTruncatedCpuStr.set(" Cmsecs = ");
        fTruncatedCpuStr.appendf(format,
                                 printMin ? fTruncatedCpuMin : fTruncatedCpuSum / repeatDraw);
        fGpuStr.set(" gmsecs = ");
        fGpuStr.appendf(format, printMin ? fGpuMin : fGpuSum / repeatDraw);
    }
    SkString str;
    str.printf("  %4s:", configName);
    if (showWallTime) {
        str += fWallStr;
    }
    if (showTruncatedWallTime) {
        str += fTruncatedWallStr;
    }
    if (showCpuTime) {
        str += fCpuStr;
    }
    if (showTruncatedCpuTime) {
        str += fTruncatedCpuStr;
    }
    if (showGpuTime && fGpuSum > 0) {
        str += fGpuStr;
    }
    return str;
}