clang-tools  10.0.0git
TraceTests.cpp
Go to the documentation of this file.
1 //===-- TraceTests.cpp - Tracing unit tests ---------------------*- 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 "Trace.h"
10 
11 #include "llvm/ADT/DenseMap.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/Support/SourceMgr.h"
14 #include "llvm/Support/Threading.h"
15 #include "llvm/Support/YAMLParser.h"
16 #include "gmock/gmock.h"
17 #include "gtest/gtest.h"
18 
19 namespace clang {
20 namespace clangd {
21 namespace {
22 
23 MATCHER_P(StringNode, Val, "") {
24  if (arg->getType() != llvm::yaml::Node::NK_Scalar) {
25  *result_listener << "is a " << arg->getVerbatimTag();
26  return false;
27  }
28  llvm::SmallString<32> S;
29  return Val == static_cast<llvm::yaml::ScalarNode *>(arg)->getValue(S);
30 }
31 
32 // Checks that N is a Mapping (JS object) with the expected scalar properties.
33 // The object must have all the Expected properties, but may have others.
34 bool VerifyObject(llvm::yaml::Node &N,
35  std::map<std::string, std::string> Expected) {
36  auto *M = llvm::dyn_cast<llvm::yaml::MappingNode>(&N);
37  if (!M) {
38  ADD_FAILURE() << "Not an object";
39  return false;
40  }
41  bool Match = true;
42  llvm::SmallString<32> Tmp;
43  for (auto &Prop : *M) {
44  auto *K = llvm::dyn_cast_or_null<llvm::yaml::ScalarNode>(Prop.getKey());
45  if (!K)
46  continue;
47  std::string KS = K->getValue(Tmp).str();
48  auto I = Expected.find(KS);
49  if (I == Expected.end())
50  continue; // Ignore properties with no assertion.
51 
52  auto *V = llvm::dyn_cast_or_null<llvm::yaml::ScalarNode>(Prop.getValue());
53  if (!V) {
54  ADD_FAILURE() << KS << " is not a string";
55  Match = false;
56  }
57  std::string VS = V->getValue(Tmp).str();
58  if (VS != I->second) {
59  ADD_FAILURE() << KS << " expected " << I->second << " but actual " << VS;
60  Match = false;
61  }
62  Expected.erase(I);
63  }
64  for (const auto &P : Expected) {
65  ADD_FAILURE() << P.first << " missing, expected " << P.second;
66  Match = false;
67  }
68  return Match;
69 }
70 
71 TEST(TraceTest, SmokeTest) {
72  // Capture some events.
73  std::string JSON;
74  {
75  llvm::raw_string_ostream OS(JSON);
76  auto JSONTracer = trace::createJSONTracer(OS);
77  trace::Session Session(*JSONTracer);
78  {
79  trace::Span Tracer("A");
80  trace::log("B");
81  }
82  }
83 
84  // Get the root JSON object using the YAML parser.
85  llvm::SourceMgr SM;
86  llvm::yaml::Stream Stream(JSON, SM);
87  auto Doc = Stream.begin();
88  ASSERT_NE(Doc, Stream.end());
89  auto *Root = llvm::dyn_cast_or_null<llvm::yaml::MappingNode>(Doc->getRoot());
90  ASSERT_NE(Root, nullptr) << "Root should be an object";
91 
92  // Check whether we expect thread name events on this platform.
93  llvm::SmallString<32> ThreadName;
94  get_thread_name(ThreadName);
95  bool ThreadsHaveNames = !ThreadName.empty();
96 
97  // We expect in order:
98  // displayTimeUnit: "ns"
99  // traceEvents: [process name, thread name, start span, log, end span]
100  // (The order doesn't matter, but the YAML parser is awkward to use otherwise)
101  auto Prop = Root->begin();
102  ASSERT_NE(Prop, Root->end()) << "Expected displayTimeUnit property";
103  ASSERT_THAT(Prop->getKey(), StringNode("displayTimeUnit"));
104  EXPECT_THAT(Prop->getValue(), StringNode("ns"));
105  ASSERT_NE(++Prop, Root->end()) << "Expected traceEvents property";
106  EXPECT_THAT(Prop->getKey(), StringNode("traceEvents"));
107  auto *Events =
108  llvm::dyn_cast_or_null<llvm::yaml::SequenceNode>(Prop->getValue());
109  ASSERT_NE(Events, nullptr) << "traceEvents should be an array";
110  auto Event = Events->begin();
111  ASSERT_NE(Event, Events->end()) << "Expected process name";
112  EXPECT_TRUE(VerifyObject(*Event, {{"ph", "M"}, {"name", "process_name"}}));
113  if (ThreadsHaveNames) {
114  ASSERT_NE(++Event, Events->end()) << "Expected thread name";
115  EXPECT_TRUE(VerifyObject(*Event, {{"ph", "M"}, {"name", "thread_name"}}));
116  }
117  ASSERT_NE(++Event, Events->end()) << "Expected log message";
118  EXPECT_TRUE(VerifyObject(*Event, {{"ph", "i"}, {"name", "Log"}}));
119  ASSERT_NE(++Event, Events->end()) << "Expected span end";
120  EXPECT_TRUE(VerifyObject(*Event, {{"ph", "X"}, {"name", "A"}}));
121  ASSERT_EQ(++Event, Events->end());
122  ASSERT_EQ(++Prop, Root->end());
123 }
124 
125 } // namespace
126 } // namespace clangd
127 } // namespace clang
MATCHER_P(Named, N, "")
TEST(BackgroundQueueTest, Priority)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void log(const llvm::Twine &Message)
Records a single instant event, associated with the current thread.
Definition: Trace.cpp:205
std::unique_ptr< EventTracer > createJSONTracer(llvm::raw_ostream &OS, bool Pretty)
Create an instance of EventTracer that produces an output in the Trace Event format supported by Chro...
Definition: Trace.cpp:200
std::vector< const char * > Expected