100 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8// These are bare-minimum 'smoke' tests of the time profiler. Not tested:9// - multi-threading10// - 'Total' entries11// - elision of short or ill-formed entries12// - detail callback13// - no calls to now() if profiling is disabled14// - suppression of contributions to total entries for nested entries15//===----------------------------------------------------------------------===//16 17#include "llvm/Support/TimeProfiler.h"18#include "gtest/gtest.h"19 20using namespace llvm;21 22namespace {23 24void setupProfiler() {25 timeTraceProfilerInitialize(/*TimeTraceGranularity=*/0, "test");26}27 28std::string teardownProfiler() {29 SmallVector<char, 1024> smallVector;30 raw_svector_ostream os(smallVector);31 timeTraceProfilerWrite(os);32 timeTraceProfilerCleanup();33 return os.str().str();34}35 36TEST(TimeProfiler, Scope_Smoke) {37 setupProfiler();38 39 { TimeTraceScope scope("event", "detail"); }40 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);44}45 46TEST(TimeProfiler, Begin_End_Smoke) {47 setupProfiler();48 49 timeTraceProfilerBegin("event", "detail");50 timeTraceProfilerEnd();51 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);55}56 57TEST(TimeProfiler, Async_Begin_End_Smoke) {58 setupProfiler();59 60 auto *Profiler = timeTraceAsyncProfilerBegin("event", "detail");61 timeTraceProfilerEnd(Profiler);62 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);66}67 68TEST(TimeProfiler, Begin_End_Disabled) {69 // Nothing should be observable here. The test is really just making sure70 // we've not got a stray nullptr deref.71 timeTraceProfilerBegin("event", "detail");72 timeTraceProfilerEnd();73}74 75TEST(TimeProfiler, Instant_Add_Smoke) {76 setupProfiler();77 78 timeTraceProfilerBegin("sync event", "sync detail");79 timeTraceAddInstantEvent("instant event", [&] { return "instant detail"; });80 timeTraceProfilerEnd();81 82 std::string json = teardownProfiler();83 ASSERT_TRUE(json.find(R"("name":"sync event")") != std::string::npos);84 ASSERT_TRUE(json.find(R"("detail":"sync detail")") != std::string::npos);85 ASSERT_TRUE(json.find(R"("name":"instant event")") != std::string::npos);86 ASSERT_TRUE(json.find(R"("detail":"instant detail")") != std::string::npos);87}88 89TEST(TimeProfiler, Instant_Not_Added_Smoke) {90 setupProfiler();91 92 timeTraceAddInstantEvent("instant event", [&] { return "instant detail"; });93 94 std::string json = teardownProfiler();95 ASSERT_TRUE(json.find(R"("name":"instant event")") == std::string::npos);96 ASSERT_TRUE(json.find(R"("detail":"instant detail")") == std::string::npos);97}98 99} // namespace100