Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / scudo / standalone / tests / timing_test.cpp
blob09a6c312246730e4ccba860dded227af42c48808
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 //===----------------------------------------------------------------------===//
9 #include "tests/scudo_unit_test.h"
11 #include "timing.h"
13 #include <string>
15 class ScudoTimingTest : public Test {
16 public:
17 void testFunc1() { scudo::ScopedTimer ST(Manager, __func__); }
19 void testFunc2() {
20 scudo::ScopedTimer ST(Manager, __func__);
21 testFunc1();
24 void testChainedCalls() {
25 scudo::ScopedTimer ST(Manager, __func__);
26 testFunc2();
29 void testIgnoredTimer() {
30 scudo::ScopedTimer ST(Manager, __func__);
31 ST.ignore();
34 void printAllTimersStats() { Manager.printAll(); }
36 scudo::TimingManager &getTimingManager() { return Manager; }
38 private:
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) {
47 #if SCUDO_LINUX
48 testing::internal::LogToStderr();
49 testing::internal::CaptureStderr();
50 #endif
52 testIgnoredTimer();
53 testChainedCalls();
54 printAllTimersStats();
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
65 TEST_F(ScudoTimingTest, NestedTimer) {
66 #if SCUDO_LINUX
67 testing::internal::LogToStderr();
68 testing::internal::CaptureStderr();
69 #endif
72 scudo::ScopedTimer Outer(getTimingManager(), "Outer");
74 scudo::ScopedTimer Inner1(getTimingManager(), Outer, "Inner1");
75 { scudo::ScopedTimer Inner2(getTimingManager(), Inner1, "Inner2"); }
78 printAllTimersStats();
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