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