Files
llvm-project/lldb/unittests/Utility/TimerTest.cpp
T

73 lines
2.2 KiB
C++
Raw Normal View History

2016-11-03 09:14:09 +00:00
//===-- TimerTest.cpp -------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
2017-02-02 21:39:50 +00:00
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/Timer.h"
#include "gtest/gtest.h"
2016-11-03 09:14:09 +00:00
#include <thread>
using namespace lldb_private;
TEST(TimerTest, CategoryTimes) {
Timer::ResetCategoryTimes();
{
2017-05-15 13:02:37 +00:00
static Timer::Category tcat("CAT1");
Timer t(tcat, "");
2016-11-03 09:14:09 +00:00
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();
{
2017-05-15 13:02:37 +00:00
static Timer::Category tcat1("CAT1");
Timer t1(tcat1, "");
2016-11-03 09:14:09 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(10));
2017-05-15 13:02:37 +00:00
// Explicitly testing the same category as above.
Timer t2(tcat1, "");
2016-11-03 10:07:47 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(10));
2016-11-03 09:14:09 +00:00
}
StreamString ss;
Timer::DumpCategoryTimes(&ss);
double seconds;
2017-05-15 13:02:37 +00:00
// It should only appear once.
ASSERT_EQ(ss.GetString().count("CAT1"), 1U);
2016-11-03 09:14:09 +00:00
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();
{
2017-05-15 13:02:37 +00:00
static Timer::Category tcat1("CAT1");
Timer t1(tcat1, "");
2016-11-03 10:07:47 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(100));
2017-05-15 13:02:37 +00:00
static Timer::Category tcat2("CAT2");
Timer t2(tcat2, "");
2016-11-03 09:14:09 +00:00
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",
2016-11-03 10:07:47 +00:00
&seconds1, &seconds2))
2016-11-16 21:45:11 +00:00
<< "String: " << ss.GetData();
2016-11-03 10:07:47 +00:00
EXPECT_LT(0.01, seconds1);
EXPECT_GT(1, seconds1);
2016-11-03 09:14:09 +00:00
EXPECT_LT(0.001, seconds2);
EXPECT_GT(0.1, seconds2);
}