Summary:
While removing TimeValue from this class I noticed a lot of room for small
simplifications here. Main are:
- instead of complicated start-stop dances to compute own time, each Timer
just starts the timer once, and keeps track of the durations of child
timers. Then the own time can be computed at the end by subtracting the two
values.
- remove double accounting in TimerStack - the stack object already knows the
number of timers.
The interface does not lend itself well to unit testing, but I have added a
couple of tests which can (and did) catch any obvious errors.
Reviewers: tberghammer, clayborg
Subscribers: mgorny, lldb-commits
Differential Revision: https://reviews.llvm.org/D26243
llvm-svn: 285890
76 lines
2.0 KiB
C++
76 lines
2.0 KiB
C++
//===-- TimerTest.cpp -------------------------------------------*- C++ -*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if defined(_MSC_VER) && (_HAS_EXCEPTIONS == 0)
|
|
// Workaround for MSVC standard library bug, which fails to include <thread>
|
|
// when exceptions are disabled.
|
|
#include <eh.h>
|
|
#endif
|
|
|
|
#include "lldb/Core/Timer.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "lldb/Core/StreamString.h"
|
|
#include <thread>
|
|
|
|
using namespace lldb_private;
|
|
|
|
TEST(TimerTest, CategoryTimes) {
|
|
Timer::ResetCategoryTimes();
|
|
{
|
|
Timer t("CAT1", "");
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
StreamString ss;
|
|
Timer::DumpCategoryTimes(&ss);
|
|
double seconds;
|
|
ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds));
|
|
EXPECT_LT(0.001, seconds);
|
|
EXPECT_GT(0.1, seconds);
|
|
}
|
|
|
|
TEST(TimerTest, CategoryTimesNested) {
|
|
Timer::ResetCategoryTimes();
|
|
{
|
|
Timer t1("CAT1", "");
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
{
|
|
Timer t2("CAT1", "");
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
}
|
|
StreamString ss;
|
|
Timer::DumpCategoryTimes(&ss);
|
|
double seconds;
|
|
ASSERT_EQ(1, sscanf(ss.GetData(), "%lf sec for CAT1", &seconds));
|
|
EXPECT_LT(0.002, seconds);
|
|
EXPECT_GT(0.2, seconds);
|
|
}
|
|
|
|
TEST(TimerTest, CategoryTimes2) {
|
|
Timer::ResetCategoryTimes();
|
|
{
|
|
Timer t1("CAT1", "");
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
{
|
|
Timer t2("CAT2", "");
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
}
|
|
StreamString ss;
|
|
Timer::DumpCategoryTimes(&ss);
|
|
double seconds1, seconds2;
|
|
ASSERT_EQ(2, sscanf(ss.GetData(), "%lf sec for CAT1%*[\n ]%lf sec for CAT2",
|
|
&seconds1, &seconds2));
|
|
EXPECT_LT(0.001, seconds1);
|
|
EXPECT_GT(0.1, seconds1);
|
|
EXPECT_LT(0.001, seconds2);
|
|
EXPECT_GT(0.1, seconds2);
|
|
}
|