xref: /llvm-project/clang-tools-extra/clangd/unittests/support/TestTracer.cpp (revision e64f99c51a8e4476981ffdc9632b57b9fd3b5286)
1*e64f99c5SKadir Cetinkaya //===-- TestTracer.cpp - Tracing unit tests ---------------------*- C++ -*-===//
2*e64f99c5SKadir Cetinkaya //
3*e64f99c5SKadir Cetinkaya // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e64f99c5SKadir Cetinkaya // See https://llvm.org/LICENSE.txt for license information.
5*e64f99c5SKadir Cetinkaya // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e64f99c5SKadir Cetinkaya //
7*e64f99c5SKadir Cetinkaya //===----------------------------------------------------------------------===//
8*e64f99c5SKadir Cetinkaya #include "TestTracer.h"
9*e64f99c5SKadir Cetinkaya #include "support/Trace.h"
10*e64f99c5SKadir Cetinkaya #include "llvm/ADT/StringRef.h"
11*e64f99c5SKadir Cetinkaya #include <mutex>
12*e64f99c5SKadir Cetinkaya 
13*e64f99c5SKadir Cetinkaya namespace clang {
14*e64f99c5SKadir Cetinkaya namespace clangd {
15*e64f99c5SKadir Cetinkaya namespace trace {
16*e64f99c5SKadir Cetinkaya 
record(const Metric & Metric,double Value,llvm::StringRef Label)17*e64f99c5SKadir Cetinkaya void TestTracer::record(const Metric &Metric, double Value,
18*e64f99c5SKadir Cetinkaya                         llvm::StringRef Label) {
19*e64f99c5SKadir Cetinkaya   std::lock_guard<std::mutex> Lock(Mu);
20*e64f99c5SKadir Cetinkaya   Measurements[Metric.Name][Label].push_back(Value);
21*e64f99c5SKadir Cetinkaya }
22*e64f99c5SKadir Cetinkaya 
takeMetric(llvm::StringRef Metric,llvm::StringRef Label)23*e64f99c5SKadir Cetinkaya std::vector<double> TestTracer::takeMetric(llvm::StringRef Metric,
24*e64f99c5SKadir Cetinkaya                                            llvm::StringRef Label) {
25*e64f99c5SKadir Cetinkaya   std::lock_guard<std::mutex> Lock(Mu);
26*e64f99c5SKadir Cetinkaya   auto LabelsIt = Measurements.find(Metric);
27*e64f99c5SKadir Cetinkaya   if (LabelsIt == Measurements.end())
28*e64f99c5SKadir Cetinkaya     return {};
29*e64f99c5SKadir Cetinkaya   auto &Labels = LabelsIt->getValue();
30*e64f99c5SKadir Cetinkaya   auto ValuesIt = Labels.find(Label);
31*e64f99c5SKadir Cetinkaya   if (ValuesIt == Labels.end())
32*e64f99c5SKadir Cetinkaya     return {};
33*e64f99c5SKadir Cetinkaya   auto Res = std::move(ValuesIt->getValue());
34*e64f99c5SKadir Cetinkaya   ValuesIt->getValue().clear();
35*e64f99c5SKadir Cetinkaya   return Res;
36*e64f99c5SKadir Cetinkaya }
37*e64f99c5SKadir Cetinkaya } // namespace trace
38*e64f99c5SKadir Cetinkaya } // namespace clangd
39*e64f99c5SKadir Cetinkaya } // namespace clang
40