| 1 |
// Copyright 2002 - 2008, 2010, 2011 National Technology Engineering |
| 2 |
// Solutions of Sandia, LLC (NTESS). Under the terms of Contract |
| 3 |
// DE-NA0003525 with NTESS, the U.S. Government retains certain rights |
| 4 |
// in this software. |
| 5 |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
| 7 |
// modification, are permitted provided that the following conditions are |
| 8 |
// met: |
| 9 |
// |
| 10 |
// * Redistributions of source code must retain the above copyright |
| 11 |
// notice, this list of conditions and the following disclaimer. |
| 12 |
// |
| 13 |
// * Redistributions in binary form must reproduce the above |
| 14 |
// copyright notice, this list of conditions and the following |
| 15 |
// disclaimer in the documentation and/or other materials provided |
| 16 |
// with the distribution. |
| 17 |
// |
| 18 |
// * Neither the name of NTESS nor the names of its contributors |
| 19 |
// may be used to endorse or promote products derived from this |
| 20 |
// software without specific prior written permission. |
| 21 |
// |
| 22 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 |
// |
| 34 |
|
| 35 |
//BEGINTimerTests |
| 36 |
#include "gtest/gtest.h" |
| 37 |
#include "stk_unit_test_utils/stringAndNumberComparisons.hpp" // for areStringsEqualWithToleran... |
| 38 |
#include "stk_util/diag/PrintTimer.hpp" // for printTimersTable |
| 39 |
#include "stk_util/diag/Timer.hpp" // for Timer, createRootTimer |
| 40 |
#include "stk_util/diag/TimerMetricTraits.hpp" // for METRICS_ALL |
| 41 |
#include <unistd.h> // for usleep |
| 42 |
#include <iosfwd> // for ostringstream |
| 43 |
#include <string> // for string |
| 44 |
|
| 45 |
namespace |
| 46 |
{ |
| 47 |
|
| 48 |
#if defined(NDEBUG) |
| 49 |
const double tolerance = 0.10; |
| 50 |
#else |
| 51 |
const double tolerance = 0.25; |
| 52 |
#endif |
| 53 |
|
| 54 |
void doWork() |
| 55 |
{ |
| 56 |
::usleep(1e5); |
| 57 |
} |
| 58 |
|
| 59 |
TEST(StkDiagTimerHowTo, useTheRootTimer) |
| 60 |
{ |
| 61 |
stk::diag::TimerSet enabledTimerSet(0); |
| 62 |
stk::diag::Timer rootTimer = createRootTimer("totalTestRuntime", enabledTimerSet); |
| 63 |
|
| 64 |
{ |
| 65 |
stk::diag::TimeBlock totalTestRuntime(rootTimer); |
| 66 |
doWork(); |
| 67 |
|
| 68 |
std::ostringstream outputStream; |
| 69 |
bool printTimingsOnlySinceLastPrint = false; |
| 70 |
stk::diag::printTimersTable(outputStream, rootTimer, stk::diag::METRICS_ALL, printTimingsOnlySinceLastPrint); |
| 71 |
|
| 72 |
std::string expectedOutput = " \ |
| 73 |
Timer Count CPU Time Wall Time \ |
| 74 |
---------------------------------------- ----- --------------------- --------------------- \ |
| 75 |
totalTestRuntime 1 SKIP SKIP 00:00:00.100 SKIP \ |
| 76 |
\ |
| 77 |
Took 0.0001 seconds to generate the table above. \ |
| 78 |
"; |
| 79 |
using stk::unit_test_util::areStringsEqualWithToleranceForNumbers; |
| 80 |
EXPECT_TRUE(areStringsEqualWithToleranceForNumbers(expectedOutput, outputStream.str(), tolerance)); |
| 81 |
} |
| 82 |
|
| 83 |
stk::diag::deleteRootTimer(rootTimer); |
| 84 |
} |
| 85 |
|
| 86 |
TEST(StkDiagTimerHowTo, useChildTimers) |
| 87 |
{ |
| 88 |
enum {CHILDMASK1 = 1, CHILDMASK2 = 2}; |
| 89 |
stk::diag::TimerSet enabledTimerSet(CHILDMASK1 | CHILDMASK2); |
| 90 |
stk::diag::Timer rootTimer = createRootTimer("totalTestRuntime", enabledTimerSet); |
| 91 |
rootTimer.start(); |
| 92 |
|
| 93 |
stk::diag::Timer childTimer1("childTimer1", CHILDMASK1, rootTimer); |
| 94 |
stk::diag::Timer childTimer2("childTimer2", CHILDMASK2, rootTimer); |
| 95 |
|
| 96 |
{ |
| 97 |
stk::diag::TimeBlock timeStuffInThisScope(childTimer1); |
| 98 |
stk::diag::TimeBlock timeStuffInThisScopeAgain(childTimer2); |
| 99 |
doWork(); |
| 100 |
} |
| 101 |
|
| 102 |
std::ostringstream outputStream; |
| 103 |
bool printTimingsOnlySinceLastPrint = false; |
| 104 |
stk::diag::printTimersTable(outputStream, rootTimer, stk::diag::METRICS_ALL, printTimingsOnlySinceLastPrint); |
| 105 |
|
| 106 |
{ |
| 107 |
stk::diag::TimeBlock timeStuffInThisScope(childTimer1); |
| 108 |
doWork(); |
| 109 |
} |
| 110 |
|
| 111 |
stk::diag::printTimersTable(outputStream, rootTimer, stk::diag::METRICS_ALL, printTimingsOnlySinceLastPrint); |
| 112 |
|
| 113 |
std::string expectedOutput = " \ |
| 114 |
Timer Count CPU Time Wall Time \ |
| 115 |
---------------------------------------- ----- --------------------- --------------------- \ |
| 116 |
totalTestRuntime 1 SKIP SKIP 00:00:00.100 SKIP \ |
| 117 |
childTimer1 1 SKIP SKIP 00:00:00.100 SKIP \ |
| 118 |
childTimer2 1 SKIP SKIP 00:00:00.100 SKIP \ |
| 119 |
\ |
| 120 |
Took 0.0001 seconds to generate the table above. \ |
| 121 |
Timer Count CPU Time Wall Time \ |
| 122 |
---------------------------------------- ----- --------------------- --------------------- \ |
| 123 |
totalTestRuntime 1 SKIP SKIP 00:00:00.200 SKIP \ |
| 124 |
childTimer1 2 SKIP SKIP 00:00:00.200 SKIP \ |
| 125 |
childTimer2 1 SKIP SKIP 00:00:00.100 SKIP \ |
| 126 |
\ |
| 127 |
Took 0.0001 seconds to generate the table above. \ |
| 128 |
"; |
| 129 |
using stk::unit_test_util::areStringsEqualWithToleranceForNumbers; |
| 130 |
EXPECT_TRUE(areStringsEqualWithToleranceForNumbers(expectedOutput, outputStream.str(), tolerance)); |
| 131 |
|
| 132 |
stk::diag::deleteRootTimer(rootTimer); |
| 133 |
} |
| 134 |
|
| 135 |
TEST(StkDiagTimerHowTo, disableChildTimers) |
| 136 |
{ |
| 137 |
enum {CHILDMASK1 = 1, CHILDMASK2 = 2}; |
| 138 |
stk::diag::TimerSet enabledTimerSet(CHILDMASK2); |
| 139 |
stk::diag::Timer rootTimer = createRootTimer("totalTestRuntime", enabledTimerSet); |
| 140 |
rootTimer.start(); |
| 141 |
|
| 142 |
stk::diag::Timer disabledTimer("disabledTimer", CHILDMASK1, rootTimer); |
| 143 |
stk::diag::Timer enabledTimer("enabledTimer", CHILDMASK2, rootTimer); |
| 144 |
|
| 145 |
{ |
| 146 |
stk::diag::TimeBlock timeStuffInThisScope(disabledTimer); |
| 147 |
stk::diag::TimeBlock timeStuffInThisScopeAgain(enabledTimer); |
| 148 |
doWork(); |
| 149 |
} |
| 150 |
|
| 151 |
std::ostringstream outputStream; |
| 152 |
bool printTimingsOnlySinceLastPrint = false; |
| 153 |
stk::diag::printTimersTable(outputStream, rootTimer, stk::diag::METRICS_ALL, printTimingsOnlySinceLastPrint); |
| 154 |
|
| 155 |
{ |
| 156 |
stk::diag::TimeBlock timeStuffInThisScope(disabledTimer); |
| 157 |
doWork(); |
| 158 |
} |
| 159 |
|
| 160 |
stk::diag::printTimersTable(outputStream, rootTimer, stk::diag::METRICS_ALL, printTimingsOnlySinceLastPrint); |
| 161 |
|
| 162 |
std::string expectedOutput = " \ |
| 163 |
Timer Count CPU Time Wall Time \ |
| 164 |
---------------------------------------- ----- --------------------- --------------------- \ |
| 165 |
totalTestRuntime 1 SKIP SKIP 00:00:00.100 SKIP \ |
| 166 |
enabledTimer 1 SKIP SKIP 00:00:00.100 SKIP \ |
| 167 |
\ |
| 168 |
Took 0.0001 seconds to generate the table above. \ |
| 169 |
Timer Count CPU Time Wall Time \ |
| 170 |
---------------------------------------- ----- --------------------- --------------------- \ |
| 171 |
totalTestRuntime 1 SKIP SKIP 00:00:00.200 SKIP \ |
| 172 |
enabledTimer 1 SKIP SKIP 00:00:00.100 SKIP \ |
| 173 |
\ |
| 174 |
Took 0.0001 seconds to generate the table above. \ |
| 175 |
"; |
| 176 |
using stk::unit_test_util::areStringsEqualWithToleranceForNumbers; |
| 177 |
EXPECT_TRUE(areStringsEqualWithToleranceForNumbers(expectedOutput, outputStream.str(), tolerance)); |
| 178 |
|
| 179 |
stk::diag::deleteRootTimer(rootTimer); |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
//ENDTimerTests |