Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / unittests / Support / TimeProfilerTest.cpp
blobbb820ec99a393ef008c4be692d1a95538e376820
1 //===- unittests/TimeProfilerTest.cpp - TimeProfiler tests ----------------===//
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 // These are bare-minimum 'smoke' tests of the time profiler. Not tested:
9 // - multi-threading
10 // - 'Total' entries
11 // - elision of short or ill-formed entries
12 // - detail callback
13 // - no calls to now() if profiling is disabled
14 // - suppression of contributions to total entries for nested entries
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Support/TimeProfiler.h"
18 #include "gtest/gtest.h"
20 using namespace llvm;
22 namespace {
24 void setupProfiler() {
25 timeTraceProfilerInitialize(/*TimeTraceGranularity=*/0, "test");
28 std::string teardownProfiler() {
29 SmallVector<char, 1024> smallVector;
30 raw_svector_ostream os(smallVector);
31 timeTraceProfilerWrite(os);
32 timeTraceProfilerCleanup();
33 return os.str().str();
36 TEST(TimeProfiler, Scope_Smoke) {
37 setupProfiler();
39 { TimeTraceScope scope("event", "detail"); }
41 std::string json = teardownProfiler();
42 ASSERT_TRUE(json.find(R"("name":"event")") != std::string::npos);
43 ASSERT_TRUE(json.find(R"("detail":"detail")") != std::string::npos);
46 TEST(TimeProfiler, Begin_End_Smoke) {
47 setupProfiler();
49 timeTraceProfilerBegin("event", "detail");
50 timeTraceProfilerEnd();
52 std::string json = teardownProfiler();
53 ASSERT_TRUE(json.find(R"("name":"event")") != std::string::npos);
54 ASSERT_TRUE(json.find(R"("detail":"detail")") != std::string::npos);
57 TEST(TimeProfiler, Async_Begin_End_Smoke) {
58 setupProfiler();
60 auto *Profiler = timeTraceAsyncProfilerBegin("event", "detail");
61 timeTraceProfilerEnd(Profiler);
63 std::string json = teardownProfiler();
64 ASSERT_TRUE(json.find(R"("name":"event")") != std::string::npos);
65 ASSERT_TRUE(json.find(R"("detail":"detail")") != std::string::npos);
68 TEST(TimeProfiler, Begin_End_Disabled) {
69 // Nothing should be observable here. The test is really just making sure
70 // we've not got a stray nullptr deref.
71 timeTraceProfilerBegin("event", "detail");
72 timeTraceProfilerEnd();
75 } // namespace