summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/benchmarks/IndexBenchmark.cpp
blob: 44af02023db9cdddd462512ba1075fb3ab9e64d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//===--- IndexBenchmark.cpp - Clangd index benchmarks -----------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "../index/SymbolYAML.h"
#include "../index/dex/Dex.h"
#include "benchmark/benchmark.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
#include <fstream>
#include <streambuf>
#include <string>

const char *IndexFilename;
const char *LogFilename;

namespace clang {
namespace clangd {
namespace {

std::unique_ptr<clang::clangd::SymbolIndex> buildMem() {
  return clang::clangd::loadIndex(IndexFilename, {}, false);
}

std::unique_ptr<clang::clangd::SymbolIndex> buildDex() {
  return clang::clangd::loadIndex(IndexFilename, {}, true);
}

// This function processes user-provided Log file with fuzzy find requests in
// the following format:
//
// fuzzyFind("UnqualifiedName", scopes=["clang::", "clang::clangd::"])
//
// It constructs vector of FuzzyFindRequests which is later used for the
// benchmarks.
std::vector<clang::clangd::FuzzyFindRequest> extractQueriesFromLogs() {
  llvm::Regex RequestMatcher("fuzzyFind\\(\"([a-zA-Z]*)\", scopes=\\[(.*)\\]");
  llvm::SmallVector<llvm::StringRef, 200> Matches;
  std::ifstream InputStream(LogFilename);
  std::string Log((std::istreambuf_iterator<char>(InputStream)),
                  std::istreambuf_iterator<char>());
  llvm::StringRef Temporary(Log);
  llvm::SmallVector<llvm::StringRef, 200> Strings;
  Temporary.split(Strings, '\n');

  clang::clangd::FuzzyFindRequest R;
  R.MaxCandidateCount = 100;

  llvm::SmallVector<llvm::StringRef, 200> CommaSeparatedValues;

  std::vector<clang::clangd::FuzzyFindRequest> RealRequests;
  for (auto Line : Strings) {
    if (RequestMatcher.match(Line, &Matches)) {
      R.Query = Matches[1];
      CommaSeparatedValues.clear();
      Line.split(CommaSeparatedValues, ',');
      R.Scopes.clear();
      for (auto C : CommaSeparatedValues) {
        R.Scopes.push_back(C);
      }
      RealRequests.push_back(R);
    }
  }
  return RealRequests;
}

static void MemQueries(benchmark::State &State) {
  const auto Mem = buildMem();
  const auto Requests = extractQueriesFromLogs();
  for (auto _ : State)
    for (const auto &Request : Requests)
      Mem->fuzzyFind(Request, [](const Symbol &S) {});
}
BENCHMARK(MemQueries);

static void DexQueries(benchmark::State &State) {
  const auto Dex = buildDex();
  const auto Requests = extractQueriesFromLogs();
  for (auto _ : State)
    for (const auto &Request : Requests)
      Dex->fuzzyFind(Request, [](const Symbol &S) {});
}
BENCHMARK(DexQueries);

} // namespace
} // namespace clangd
} // namespace clang

// FIXME(kbobyrev): Add index building time benchmarks.
// FIXME(kbobyrev): Add memory consumption "benchmarks" by manually measuring
// in-memory index size and reporting it as time.
// FIXME(kbobyrev): Create a logger wrapper to suppress debugging info printer.
int main(int argc, char *argv[]) {
  if (argc < 3) {
    llvm::errs() << "Usage: " << argv[0]
                 << " global-symbol-index.yaml fuzzy-find-requests.log "
                    "BENCHMARK_OPTIONS...\n";
    return -1;
  }
  IndexFilename = argv[1];
  LogFilename = argv[2];
  // Trim first two arguments of the benchmark invocation.
  argv += 3;
  argc -= 3;
  ::benchmark::Initialize(&argc, argv);
  ::benchmark::RunSpecifiedBenchmarks();
}
OpenPOWER on IntegriCloud