1 //===-- timing_test.cpp -----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "tests/scudo_unit_test.h"
15 class ScudoTimingTest
: public Test
{
17 void testFunc1() { scudo::ScopedTimer
ST(Manager
, __func__
); }
20 scudo::ScopedTimer
ST(Manager
, __func__
);
24 void testChainedCalls() {
25 scudo::ScopedTimer
ST(Manager
, __func__
);
29 void testIgnoredTimer() {
30 scudo::ScopedTimer
ST(Manager
, __func__
);
34 void printAllTimersStats() { Manager
.printAll(); }
36 scudo::TimingManager
&getTimingManager() { return Manager
; }
39 scudo::TimingManager Manager
;
42 // Given that the output of statistics of timers are dumped through
43 // `scudo::Printf` which is platform dependent, so we don't have a reliable way
44 // to catch the output and verify the details. Now we only verify the number of
45 // invocations on linux.
46 TEST_F(ScudoTimingTest
, SimpleTimer
) {
48 testing::internal::LogToStderr();
49 testing::internal::CaptureStderr();
54 printAllTimersStats();
57 std::string output
= testing::internal::GetCapturedStderr();
58 EXPECT_TRUE(output
.find("testIgnoredTimer (1)") == std::string::npos
);
59 EXPECT_TRUE(output
.find("testChainedCalls (1)") != std::string::npos
);
60 EXPECT_TRUE(output
.find("testFunc2 (1)") != std::string::npos
);
61 EXPECT_TRUE(output
.find("testFunc1 (1)") != std::string::npos
);
65 TEST_F(ScudoTimingTest
, NestedTimer
) {
67 testing::internal::LogToStderr();
68 testing::internal::CaptureStderr();
72 scudo::ScopedTimer
Outer(getTimingManager(), "Outer");
74 scudo::ScopedTimer
Inner1(getTimingManager(), Outer
, "Inner1");
75 { scudo::ScopedTimer
Inner2(getTimingManager(), Inner1
, "Inner2"); }
78 printAllTimersStats();
81 std::string output
= testing::internal::GetCapturedStderr();
82 EXPECT_TRUE(output
.find("Outer (1)") != std::string::npos
);
83 EXPECT_TRUE(output
.find("Inner1 (1)") != std::string::npos
);
84 EXPECT_TRUE(output
.find("Inner2 (1)") != std::string::npos
);