1 //===-- timing_test.cpp -----------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "tests/scudo_unit_test.h"
10
11 #include "timing.h"
12
13 #include <string>
14
15 class ScudoTimingTest : public Test {
16 public:
testFunc1()17 void testFunc1() { scudo::ScopedTimer ST(Manager, __func__); }
18
testFunc2()19 void testFunc2() {
20 scudo::ScopedTimer ST(Manager, __func__);
21 testFunc1();
22 }
23
testChainedCalls()24 void testChainedCalls() {
25 scudo::ScopedTimer ST(Manager, __func__);
26 testFunc2();
27 }
28
testIgnoredTimer()29 void testIgnoredTimer() {
30 scudo::ScopedTimer ST(Manager, __func__);
31 ST.ignore();
32 }
33
printAllTimersStats()34 void printAllTimersStats() { Manager.printAll(); }
35
getTimingManager()36 scudo::TimingManager &getTimingManager() { return Manager; }
37
38 private:
39 scudo::TimingManager Manager;
40 };
41
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.
TEST_F(ScudoTimingTest,SimpleTimer)46 TEST_F(ScudoTimingTest, SimpleTimer) {
47 #if SCUDO_LINUX
48 testing::internal::LogToStderr();
49 testing::internal::CaptureStderr();
50 #endif
51
52 testIgnoredTimer();
53 testChainedCalls();
54 printAllTimersStats();
55
56 #if SCUDO_LINUX
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);
62 #endif
63 }
64
TEST_F(ScudoTimingTest,NestedTimer)65 TEST_F(ScudoTimingTest, NestedTimer) {
66 #if SCUDO_LINUX
67 testing::internal::LogToStderr();
68 testing::internal::CaptureStderr();
69 #endif
70
71 {
72 scudo::ScopedTimer Outer(getTimingManager(), "Outer");
73 {
74 scudo::ScopedTimer Inner1(getTimingManager(), Outer, "Inner1");
75 { scudo::ScopedTimer Inner2(getTimingManager(), Inner1, "Inner2"); }
76 }
77 }
78 printAllTimersStats();
79
80 #if SCUDO_LINUX
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);
85 #endif
86 }
87