1 //===-- TestTracer.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 #include "TestTracer.h"
9 #include "support/Trace.h"
10 #include "llvm/ADT/StringRef.h"
11 #include <mutex>
12 
13 namespace clang {
14 namespace clangd {
15 namespace trace {
16 
record(const Metric & Metric,double Value,llvm::StringRef Label)17 void TestTracer::record(const Metric &Metric, double Value,
18                         llvm::StringRef Label) {
19   std::lock_guard<std::mutex> Lock(Mu);
20   Measurements[Metric.Name][Label].push_back(Value);
21 }
22 
takeMetric(llvm::StringRef Metric,llvm::StringRef Label)23 std::vector<double> TestTracer::takeMetric(llvm::StringRef Metric,
24                                            llvm::StringRef Label) {
25   std::lock_guard<std::mutex> Lock(Mu);
26   auto LabelsIt = Measurements.find(Metric);
27   if (LabelsIt == Measurements.end())
28     return {};
29   auto &Labels = LabelsIt->getValue();
30   auto ValuesIt = Labels.find(Label);
31   if (ValuesIt == Labels.end())
32     return {};
33   auto Res = std::move(ValuesIt->getValue());
34   ValuesIt->getValue().clear();
35   return Res;
36 }
37 } // namespace trace
38 } // namespace clangd
39 } // namespace clang
40