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
buildMem()27 std::unique_ptr<SymbolIndex> buildMem() {
28 return loadIndex(IndexFilename, /*UseDex=*/false);
29 }
30
buildDex()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.
extractQueriesFromLogs()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 llvm::json::Path::Root Root("FuzzyFindRequest");
60 if (!fromJSON(Item, Request, Root)) {
61 llvm::errs() << llvm::toString(Root.getError()) << "\n";
62 Root.printErrorContext(Item, llvm::errs());
63 exit(1);
64 }
65 Requests.push_back(Request);
66 }
67 return Requests;
68 }
69
MemQueries(benchmark::State & State)70 static void MemQueries(benchmark::State &State) {
71 const auto Mem = buildMem();
72 const auto Requests = extractQueriesFromLogs();
73 for (auto _ : State)
74 for (const auto &Request : Requests)
75 Mem->fuzzyFind(Request, [](const Symbol &S) {});
76 }
77 BENCHMARK(MemQueries);
78
DexQueries(benchmark::State & State)79 static void DexQueries(benchmark::State &State) {
80 const auto Dex = buildDex();
81 const auto Requests = extractQueriesFromLogs();
82 for (auto _ : State)
83 for (const auto &Request : Requests)
84 Dex->fuzzyFind(Request, [](const Symbol &S) {});
85 }
86 BENCHMARK(DexQueries);
87
DexBuild(benchmark::State & State)88 static void DexBuild(benchmark::State &State) {
89 for (auto _ : State)
90 buildDex();
91 }
92 BENCHMARK(DexBuild);
93
94 } // namespace
95 } // namespace clangd
96 } // namespace clang
97
98 // FIXME(kbobyrev): Add index building time benchmarks.
99 // FIXME(kbobyrev): Add memory consumption "benchmarks" by manually measuring
100 // in-memory index size and reporting it as time.
101 // FIXME(kbobyrev): Create a logger wrapper to suppress debugging info printer.
main(int argc,char * argv[])102 int main(int argc, char *argv[]) {
103 if (argc < 3) {
104 llvm::errs() << "Usage: " << argv[0]
105 << " global-symbol-index.yaml requests.json "
106 "BENCHMARK_OPTIONS...\n";
107 return -1;
108 }
109 IndexFilename = argv[1];
110 RequestsFilename = argv[2];
111 // Trim first two arguments of the benchmark invocation and pretend no
112 // arguments were passed in the first place.
113 argv[2] = argv[0];
114 argv += 2;
115 argc -= 2;
116 ::benchmark::Initialize(&argc, argv);
117 ::benchmark::RunSpecifiedBenchmarks();
118 }
119