11#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/StringRef.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"
24using testing::ElementsAre;
26using testing::StartsWith;
29 if (arg->getType() != llvm::yaml::Node::NK_Scalar) {
30 *result_listener <<
"is a " << arg->getVerbatimTag();
33 llvm::SmallString<32> S;
34 return Val ==
static_cast<llvm::yaml::ScalarNode *
>(arg)->getValue(S);
39bool verifyObject(llvm::yaml::Node &N,
40 std::map<std::string, std::string>
Expected) {
41 auto *
M = llvm::dyn_cast<llvm::yaml::MappingNode>(&N);
43 ADD_FAILURE() <<
"Not an object";
47 llvm::SmallString<32> Tmp;
48 for (
auto &Prop : *
M) {
49 auto *
K = llvm::dyn_cast_or_null<llvm::yaml::ScalarNode>(Prop.getKey());
52 std::string KS =
K->getValue(Tmp).str();
57 auto *V = llvm::dyn_cast_or_null<llvm::yaml::ScalarNode>(Prop.getValue());
59 ADD_FAILURE() << KS <<
" is not a string";
63 std::string VS = V->getValue(Tmp).str();
64 if (VS != I->second) {
65 ADD_FAILURE() << KS <<
" expected " << I->second <<
" but actual " << VS;
71 ADD_FAILURE() << P.first <<
" missing, expected " << P.second;
77TEST(TraceTest, SmokeTest) {
81 llvm::raw_string_ostream
OS(JSON);
83 trace::Session
Session(*JSONTracer);
85 trace::Span Tracer(
"A");
92 llvm::yaml::Stream Stream(JSON, SM);
93 auto Doc = Stream.begin();
94 ASSERT_NE(Doc, Stream.end());
95 auto *
Root = llvm::dyn_cast_or_null<llvm::yaml::MappingNode>(Doc->getRoot());
96 ASSERT_NE(
Root,
nullptr) <<
"Root should be an object";
99 llvm::SmallString<32> ThreadName;
100 get_thread_name(ThreadName);
101 bool ThreadsHaveNames = !ThreadName.empty();
107 auto Prop =
Root->begin();
108 ASSERT_NE(Prop,
Root->end()) <<
"Expected displayTimeUnit property";
109 ASSERT_THAT(Prop->getKey(), stringNode(
"displayTimeUnit"));
110 EXPECT_THAT(Prop->getValue(), stringNode(
"ns"));
111 ASSERT_NE(++Prop,
Root->end()) <<
"Expected traceEvents property";
112 EXPECT_THAT(Prop->getKey(), stringNode(
"traceEvents"));
114 llvm::dyn_cast_or_null<llvm::yaml::SequenceNode>(Prop->getValue());
115 ASSERT_NE(Events,
nullptr) <<
"traceEvents should be an array";
116 auto Event = Events->begin();
117 ASSERT_NE(Event, Events->end()) <<
"Expected process name";
118 EXPECT_TRUE(verifyObject(*Event, {{
"ph",
"M"}, {
"name",
"process_name"}}));
119 if (ThreadsHaveNames) {
120 ASSERT_NE(++Event, Events->end()) <<
"Expected thread name";
121 EXPECT_TRUE(verifyObject(*Event, {{
"ph",
"M"}, {
"name",
"thread_name"}}));
123 ASSERT_NE(++Event, Events->end()) <<
"Expected log message";
124 EXPECT_TRUE(verifyObject(*Event, {{
"ph",
"i"}, {
"name",
"Log"}}));
125 ASSERT_NE(++Event, Events->end()) <<
"Expected span end";
126 EXPECT_TRUE(verifyObject(*Event, {{
"ph",
"X"}, {
"name",
"A"}}));
127 ASSERT_EQ(++Event, Events->end());
128 ASSERT_EQ(++Prop,
Root->end());
131TEST(MetricsTracer, LatencyTest) {
132 trace::TestTracer Tracer;
133 constexpr llvm::StringLiteral MetricName =
"span_latency";
134 constexpr llvm::StringLiteral OpName =
"op_name";
137 trace::Span SpanWithLat(OpName);
138 EXPECT_THAT(Tracer.takeMetric(MetricName, OpName), SizeIs(0));
140 EXPECT_THAT(Tracer.takeMetric(MetricName, OpName), SizeIs(1));
143class CSVMetricsTracerTest :
public ::testing::Test {
145 CSVMetricsTracerTest()
151 std::vector<std::string> outputLines() {
154 llvm::SmallVector<llvm::StringRef>
Lines;
155 llvm::StringRef(Output).split(
Lines,
"\r\n");
160 llvm::raw_string_ostream
OS;
161 std::unique_ptr<trace::EventTracer> Tracer;
165TEST_F(CSVMetricsTracerTest, RecordsValues) {
170 ASSERT_THAT(outputLines(),
171 ElementsAre(
"Kind,Metric,Label,Value,Timestamp",
172 StartsWith(
"d,dist,x,1.000000e+00,"),
173 StartsWith(
"c,cnt,,1.000000e+00,"),
174 StartsWith(
"d,dist,y,2.000000e+00,"),
""));
177TEST_F(CSVMetricsTracerTest, Escaping) {
182 EXPECT_THAT(outputLines(), ElementsAre(_, StartsWith(R
"(d,dist,",",1)"),
183 StartsWith(R"(d,dist,"a""b",1)"),
184 StartsWith("d,dist,\"a\nb\",1"),
""));
187TEST_F(CSVMetricsTracerTest, IgnoresArgs) {
188 trace::Span Tracer(
"Foo");
189 EXPECT_EQ(
nullptr, Tracer.Args);
std::vector< const char * > Expected
const google::protobuf::Message & M
llvm::raw_string_ostream OS
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...
std::unique_ptr< EventTracer > createCSVMetricTracer(llvm::raw_ostream &OS)
Create an instance of EventTracer that outputs metric measurements as CSV.
void log(const llvm::Twine &Message)
Records a single instant event, associated with the current thread.
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
TEST(BackgroundQueueTest, Priority)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
@ Counter
An aggregate number whose rate of change over time is meaningful.
@ Distribution
A distribution of values with a meaningful mean and count.
void record(double Value, llvm::StringRef Label="") const
Records a measurement for this metric to active tracer.