17061a3f3Smbs //===- unittests/TimeProfilerTest.cpp - TimeProfiler tests ----------------===// 27061a3f3Smbs // 37061a3f3Smbs // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 47061a3f3Smbs // See https://llvm.org/LICENSE.txt for license information. 57061a3f3Smbs // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 67061a3f3Smbs // 77061a3f3Smbs //===----------------------------------------------------------------------===// 87061a3f3Smbs // These are bare-minimum 'smoke' tests of the time profiler. Not tested: 97061a3f3Smbs // - multi-threading 107061a3f3Smbs // - 'Total' entries 117061a3f3Smbs // - elision of short or ill-formed entries 127061a3f3Smbs // - detail callback 137061a3f3Smbs // - no calls to now() if profiling is disabled 147061a3f3Smbs // - suppression of contributions to total entries for nested entries 157061a3f3Smbs //===----------------------------------------------------------------------===// 167061a3f3Smbs 177061a3f3Smbs #include "llvm/Support/TimeProfiler.h" 187061a3f3Smbs #include "gtest/gtest.h" 197061a3f3Smbs 207061a3f3Smbs using namespace llvm; 217061a3f3Smbs 227061a3f3Smbs namespace { 237061a3f3Smbs 247061a3f3Smbs void setupProfiler() { 257061a3f3Smbs timeTraceProfilerInitialize(/*TimeTraceGranularity=*/0, "test"); 267061a3f3Smbs } 277061a3f3Smbs 287061a3f3Smbs std::string teardownProfiler() { 297061a3f3Smbs SmallVector<char, 1024> smallVector; 307061a3f3Smbs raw_svector_ostream os(smallVector); 317061a3f3Smbs timeTraceProfilerWrite(os); 327061a3f3Smbs timeTraceProfilerCleanup(); 337061a3f3Smbs return os.str().str(); 347061a3f3Smbs } 357061a3f3Smbs 367061a3f3Smbs TEST(TimeProfiler, Scope_Smoke) { 377061a3f3Smbs setupProfiler(); 387061a3f3Smbs 397061a3f3Smbs { TimeTraceScope scope("event", "detail"); } 407061a3f3Smbs 417061a3f3Smbs std::string json = teardownProfiler(); 427061a3f3Smbs ASSERT_TRUE(json.find(R"("name":"event")") != std::string::npos); 437061a3f3Smbs ASSERT_TRUE(json.find(R"("detail":"detail")") != std::string::npos); 447061a3f3Smbs } 457061a3f3Smbs 467061a3f3Smbs TEST(TimeProfiler, Begin_End_Smoke) { 477061a3f3Smbs setupProfiler(); 487061a3f3Smbs 497061a3f3Smbs timeTraceProfilerBegin("event", "detail"); 507061a3f3Smbs timeTraceProfilerEnd(); 517061a3f3Smbs 527061a3f3Smbs std::string json = teardownProfiler(); 537061a3f3Smbs ASSERT_TRUE(json.find(R"("name":"event")") != std::string::npos); 547061a3f3Smbs ASSERT_TRUE(json.find(R"("detail":"detail")") != std::string::npos); 557061a3f3Smbs } 567061a3f3Smbs 57e61922d3STakuto Ikuta TEST(TimeProfiler, Async_Begin_End_Smoke) { 58e61922d3STakuto Ikuta setupProfiler(); 59e61922d3STakuto Ikuta 60e61922d3STakuto Ikuta auto *Profiler = timeTraceAsyncProfilerBegin("event", "detail"); 61e61922d3STakuto Ikuta timeTraceProfilerEnd(Profiler); 62e61922d3STakuto Ikuta 63e61922d3STakuto Ikuta std::string json = teardownProfiler(); 64e61922d3STakuto Ikuta ASSERT_TRUE(json.find(R"("name":"event")") != std::string::npos); 65e61922d3STakuto Ikuta ASSERT_TRUE(json.find(R"("detail":"detail")") != std::string::npos); 66e61922d3STakuto Ikuta } 67e61922d3STakuto Ikuta 687061a3f3Smbs TEST(TimeProfiler, Begin_End_Disabled) { 697061a3f3Smbs // Nothing should be observable here. The test is really just making sure 707061a3f3Smbs // we've not got a stray nullptr deref. 717061a3f3Smbs timeTraceProfilerBegin("event", "detail"); 727061a3f3Smbs timeTraceProfilerEnd(); 737061a3f3Smbs } 747061a3f3Smbs 75*871f69f0Sivanaivanovska TEST(TimeProfiler, Instant_Add_Smoke) { 76*871f69f0Sivanaivanovska setupProfiler(); 77*871f69f0Sivanaivanovska 78*871f69f0Sivanaivanovska timeTraceProfilerBegin("sync event", "sync detail"); 79*871f69f0Sivanaivanovska timeTraceAddInstantEvent("instant event", [&] { return "instant detail"; }); 80*871f69f0Sivanaivanovska timeTraceProfilerEnd(); 81*871f69f0Sivanaivanovska 82*871f69f0Sivanaivanovska std::string json = teardownProfiler(); 83*871f69f0Sivanaivanovska ASSERT_TRUE(json.find(R"("name":"sync event")") != std::string::npos); 84*871f69f0Sivanaivanovska ASSERT_TRUE(json.find(R"("detail":"sync detail")") != std::string::npos); 85*871f69f0Sivanaivanovska ASSERT_TRUE(json.find(R"("name":"instant event")") != std::string::npos); 86*871f69f0Sivanaivanovska ASSERT_TRUE(json.find(R"("detail":"instant detail")") != std::string::npos); 87*871f69f0Sivanaivanovska } 88*871f69f0Sivanaivanovska 89*871f69f0Sivanaivanovska TEST(TimeProfiler, Instant_Not_Added_Smoke) { 90*871f69f0Sivanaivanovska setupProfiler(); 91*871f69f0Sivanaivanovska 92*871f69f0Sivanaivanovska timeTraceAddInstantEvent("instant event", [&] { return "instant detail"; }); 93*871f69f0Sivanaivanovska 94*871f69f0Sivanaivanovska std::string json = teardownProfiler(); 95*871f69f0Sivanaivanovska ASSERT_TRUE(json.find(R"("name":"instant event")") == std::string::npos); 96*871f69f0Sivanaivanovska ASSERT_TRUE(json.find(R"("detail":"instant detail")") == std::string::npos); 97*871f69f0Sivanaivanovska } 98*871f69f0Sivanaivanovska 997061a3f3Smbs } // namespace 100