clang-tools  11.0.0
IndexBenchmark.cpp
Go to the documentation of this file.
1 //===--- IndexBenchmark.cpp - Clangd index benchmarks -----------*- 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 "../index/Serialization.h"
10 #include "../index/dex/Dex.h"
11 #include "benchmark/benchmark.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/Regex.h"
16 #include <fstream>
17 #include <streambuf>
18 #include <string>
19 
20 const char *IndexFilename;
21 const char *RequestsFilename;
22 
23 namespace clang {
24 namespace clangd {
25 namespace {
26 
27 std::unique_ptr<SymbolIndex> buildMem() {
28  return loadIndex(IndexFilename, /*UseDex=*/false);
29 }
30 
31 std::unique_ptr<SymbolIndex> buildDex() {
32  return loadIndex(IndexFilename, /*UseDex=*/true);
33 }
34 
35 // Reads JSON array of serialized FuzzyFindRequest's from user-provided file.
36 std::vector<FuzzyFindRequest> extractQueriesFromLogs() {
37  std::ifstream InputStream(RequestsFilename);
38  std::string Log((std::istreambuf_iterator<char>(InputStream)),
39  std::istreambuf_iterator<char>());
40 
41  std::vector<FuzzyFindRequest> Requests;
42  auto JSONArray = llvm::json::parse(Log);
43 
44  // Panic if the provided file couldn't be parsed.
45  if (!JSONArray) {
46  llvm::errs() << "Error when parsing JSON requests file: "
47  << llvm::toString(JSONArray.takeError());
48  exit(1);
49  }
50  if (!JSONArray->getAsArray()) {
51  llvm::errs() << "Error: top-level value is not a JSON array: " << Log
52  << '\n';
53  exit(1);
54  }
55 
56  for (const auto &Item : *JSONArray->getAsArray()) {
57  FuzzyFindRequest Request;
58  // Panic if the provided file couldn't be parsed.
59  if (!fromJSON(Item, Request)) {
60  llvm::errs() << "Error when deserializing request: " << Item << '\n';
61  exit(1);
62  }
63  Requests.push_back(Request);
64  }
65  return Requests;
66 }
67 
68 static void MemQueries(benchmark::State &State) {
69  const auto Mem = buildMem();
70  const auto Requests = extractQueriesFromLogs();
71  for (auto _ : State)
72  for (const auto &Request : Requests)
73  Mem->fuzzyFind(Request, [](const Symbol &S) {});
74 }
75 BENCHMARK(MemQueries);
76 
77 static void DexQueries(benchmark::State &State) {
78  const auto Dex = buildDex();
79  const auto Requests = extractQueriesFromLogs();
80  for (auto _ : State)
81  for (const auto &Request : Requests)
82  Dex->fuzzyFind(Request, [](const Symbol &S) {});
83 }
84 BENCHMARK(DexQueries);
85 
86 static void DexBuild(benchmark::State &State) {
87  for (auto _ : State)
88  buildDex();
89 }
90 BENCHMARK(DexBuild);
91 
92 } // namespace
93 } // namespace clangd
94 } // namespace clang
95 
96 // FIXME(kbobyrev): Add index building time benchmarks.
97 // FIXME(kbobyrev): Add memory consumption "benchmarks" by manually measuring
98 // in-memory index size and reporting it as time.
99 // FIXME(kbobyrev): Create a logger wrapper to suppress debugging info printer.
100 int main(int argc, char *argv[]) {
101  if (argc < 3) {
102  llvm::errs() << "Usage: " << argv[0]
103  << " global-symbol-index.yaml requests.json "
104  "BENCHMARK_OPTIONS...\n";
105  return -1;
106  }
107  IndexFilename = argv[1];
108  RequestsFilename = argv[2];
109  // Trim first two arguments of the benchmark invocation and pretend no
110  // arguments were passed in the first place.
111  argv[2] = argv[0];
112  argv += 2;
113  argc -= 2;
114  ::benchmark::Initialize(&argc, argv);
115  ::benchmark::RunSpecifiedBenchmarks();
116 }
main
int main(int argc, char *argv[])
Definition: IndexBenchmark.cpp:100
clang::clangd::loadIndex
std::unique_ptr< SymbolIndex > loadIndex(llvm::StringRef SymbolFilename, bool UseDex)
Definition: Serialization.cpp:668
clang::clangd::fromJSON
bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request)
Definition: Index.cpp:34
IndexFilename
const char * IndexFilename
Definition: IndexBenchmark.cpp:20
RequestsFilename
const char * RequestsFilename
Definition: IndexBenchmark.cpp:21
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::cppcoreguidelines::toString
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Definition: SpecialMemberFunctionsCheck.cpp:60