clang-tools  12.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 "TestTracer.h"
10 #include "support/Context.h"
11 #include "support/Trace.h"
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/Threading.h"
18 #include "llvm/Support/YAMLParser.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 
22 namespace clang {
23 namespace clangd {
24 namespace {
25 
26 using testing::_;
27 using testing::ElementsAre;
28 using testing::SizeIs;
29 using testing::StartsWith;
30 
31 MATCHER_P(StringNode, Val, "") {
32  if (arg->getType() != llvm::yaml::Node::NK_Scalar) {
33  *result_listener << "is a " << arg->getVerbatimTag();
34  return false;
35  }
36  llvm::SmallString<32> S;
37  return Val == static_cast<llvm::yaml::ScalarNode *>(arg)->getValue(S);
38 }
39 
40 // Checks that N is a Mapping (JS object) with the expected scalar properties.
41 // The object must have all the Expected properties, but may have others.
42 bool VerifyObject(llvm::yaml::Node &N,
43  std::map<std::string, std::string> Expected) {
44  auto *M = llvm::dyn_cast<llvm::yaml::MappingNode>(&N);
45  if (!M) {
46  ADD_FAILURE() << "Not an object";
47  return false;
48  }
49  bool Match = true;
50  llvm::SmallString<32> Tmp;
51  for (auto &Prop : *M) {
52  auto *K = llvm::dyn_cast_or_null<llvm::yaml::ScalarNode>(Prop.getKey());
53  if (!K)
54  continue;
55  std::string KS = K->getValue(Tmp).str();
56  auto I = Expected.find(KS);
57  if (I == Expected.end())
58  continue; // Ignore properties with no assertion.
59 
60  auto *V = llvm::dyn_cast_or_null<llvm::yaml::ScalarNode>(Prop.getValue());
61  if (!V) {
62  ADD_FAILURE() << KS << " is not a string";
63  Match = false;
64  }
65  std::string VS = V->getValue(Tmp).str();
66  if (VS != I->second) {
67  ADD_FAILURE() << KS << " expected " << I->second << " but actual " << VS;
68  Match = false;
69  }
70  Expected.erase(I);
71  }
72  for (const auto &P : Expected) {
73  ADD_FAILURE() << P.first << " missing, expected " << P.second;
74  Match = false;
75  }
76  return Match;
77 }
78 
79 TEST(TraceTest, SmokeTest) {
80  // Capture some events.
81  std::string JSON;
82  {
83  llvm::raw_string_ostream OS(JSON);
84  auto JSONTracer = trace::createJSONTracer(OS);
85  trace::Session Session(*JSONTracer);
86  {
87  trace::Span Tracer("A");
88  trace::log("B");
89  }
90  }
91 
92  // Get the root JSON object using the YAML parser.
93  llvm::SourceMgr SM;
94  llvm::yaml::Stream Stream(JSON, SM);
95  auto Doc = Stream.begin();
96  ASSERT_NE(Doc, Stream.end());
97  auto *Root = llvm::dyn_cast_or_null<llvm::yaml::MappingNode>(Doc->getRoot());
98  ASSERT_NE(Root, nullptr) << "Root should be an object";
99 
100  // Check whether we expect thread name events on this platform.
101  llvm::SmallString<32> ThreadName;
102  get_thread_name(ThreadName);
103  bool ThreadsHaveNames = !ThreadName.empty();
104 
105  // We expect in order:
106  // displayTimeUnit: "ns"
107  // traceEvents: [process name, thread name, start span, log, end span]
108  // (The order doesn't matter, but the YAML parser is awkward to use otherwise)
109  auto Prop = Root->begin();
110  ASSERT_NE(Prop, Root->end()) << "Expected displayTimeUnit property";
111  ASSERT_THAT(Prop->getKey(), StringNode("displayTimeUnit"));
112  EXPECT_THAT(Prop->getValue(), StringNode("ns"));
113  ASSERT_NE(++Prop, Root->end()) << "Expected traceEvents property";
114  EXPECT_THAT(Prop->getKey(), StringNode("traceEvents"));
115  auto *Events =
116  llvm::dyn_cast_or_null<llvm::yaml::SequenceNode>(Prop->getValue());
117  ASSERT_NE(Events, nullptr) << "traceEvents should be an array";
118  auto Event = Events->begin();
119  ASSERT_NE(Event, Events->end()) << "Expected process name";
120  EXPECT_TRUE(VerifyObject(*Event, {{"ph", "M"}, {"name", "process_name"}}));
121  if (ThreadsHaveNames) {
122  ASSERT_NE(++Event, Events->end()) << "Expected thread name";
123  EXPECT_TRUE(VerifyObject(*Event, {{"ph", "M"}, {"name", "thread_name"}}));
124  }
125  ASSERT_NE(++Event, Events->end()) << "Expected log message";
126  EXPECT_TRUE(VerifyObject(*Event, {{"ph", "i"}, {"name", "Log"}}));
127  ASSERT_NE(++Event, Events->end()) << "Expected span end";
128  EXPECT_TRUE(VerifyObject(*Event, {{"ph", "X"}, {"name", "A"}}));
129  ASSERT_EQ(++Event, Events->end());
130  ASSERT_EQ(++Prop, Root->end());
131 }
132 
133 TEST(MetricsTracer, LatencyTest) {
134  trace::TestTracer Tracer;
135  constexpr llvm::StringLiteral MetricName = "span_latency";
136  constexpr llvm::StringLiteral OpName = "op_name";
137  {
138  // A span should record latencies to span_latency by default.
139  trace::Span SpanWithLat(OpName);
140  EXPECT_THAT(Tracer.takeMetric(MetricName, OpName), SizeIs(0));
141  }
142  EXPECT_THAT(Tracer.takeMetric(MetricName, OpName), SizeIs(1));
143 }
144 
145 class CSVMetricsTracerTest : public ::testing::Test {
146 protected:
147  CSVMetricsTracerTest()
148  : OS(Output), Tracer(trace::createCSVMetricTracer(OS)), Session(*Tracer) {
149  }
150  trace::Metric Dist = {"dist", trace::Metric::Distribution, "lbl"};
151  trace::Metric Counter = {"cnt", trace::Metric::Counter};
152 
153  std::vector<std::string> outputLines() {
154  // Deliberately don't flush output stream, the tracer should do that.
155  // This is important when clangd crashes.
156  llvm::SmallVector<llvm::StringRef, 4> Lines;
157  llvm::StringRef(Output).split(Lines, "\r\n");
158  return {Lines.begin(), Lines.end()};
159  }
160 
161  std::string Output;
162  llvm::raw_string_ostream OS;
163  std::unique_ptr<trace::EventTracer> Tracer;
165 };
166 
167 TEST_F(CSVMetricsTracerTest, RecordsValues) {
168  Dist.record(1, "x");
169  Counter.record(1, "");
170  Dist.record(2, "y");
171 
172  ASSERT_THAT(outputLines(),
173  ElementsAre("Kind,Metric,Label,Value,Timestamp",
174  StartsWith("d,dist,x,1.000000e+00,"),
175  StartsWith("c,cnt,,1.000000e+00,"),
176  StartsWith("d,dist,y,2.000000e+00,"), ""));
177 }
178 
179 TEST_F(CSVMetricsTracerTest, Escaping) {
180  Dist.record(1, ",");
181  Dist.record(1, "a\"b");
182  Dist.record(1, "a\nb");
183 
184  EXPECT_THAT(outputLines(), ElementsAre(_, StartsWith(R"(d,dist,",",1)"),
185  StartsWith(R"(d,dist,"a""b",1)"),
186  StartsWith("d,dist,\"a\nb\",1"), ""));
187 }
188 
189 } // namespace
190 } // namespace clangd
191 } // namespace clang
trace::Metric Counter
Definition: TraceTests.cpp:151
MATCHER_P(Named, N, "")
trace::Metric Dist
Definition: TraceTests.cpp:150
llvm::SourceMgr * SourceMgr
trace::Session Session
Definition: TraceTests.cpp:164
A distribution of values with a meaningful mean and count.
Definition: Trace.h:52
std::string Output
Definition: TraceTests.cpp:161
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
An aggregate number whose rate of change over time is meaningful.
Definition: Trace.h:46
TEST(BackgroundQueueTest, Priority)
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:162
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void log(const llvm::Twine &Message)
Records a single instant event, associated with the current thread.
Definition: Trace.cpp:274
std::unique_ptr< trace::EventTracer > Tracer
Definition: TraceTests.cpp:163
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:265
std::vector< const char * > Expected
std::unique_ptr< EventTracer > createCSVMetricTracer(llvm::raw_ostream &OS)
Create an instance of EventTracer that outputs metric measurements as CSV.
Definition: Trace.cpp:270
unsigned Lines