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 <string> |
17 | |
18 | const char *IndexFilename; |
19 | const char *RequestsFilename; |
20 | |
21 | namespace clang { |
22 | namespace clangd { |
23 | namespace { |
24 | |
25 | std::unique_ptr<SymbolIndex> buildMem() { |
26 | return loadIndex(Filename: IndexFilename, Origin: clang::clangd::SymbolOrigin::Static, |
27 | /*UseDex=*/false); |
28 | } |
29 | |
30 | std::unique_ptr<SymbolIndex> buildDex() { |
31 | return loadIndex(Filename: IndexFilename, Origin: clang::clangd::SymbolOrigin::Static, |
32 | /*UseDex=*/true); |
33 | } |
34 | |
35 | // Reads JSON array of serialized FuzzyFindRequest's from user-provided file. |
36 | std::vector<FuzzyFindRequest> () { |
37 | |
38 | auto Buffer = llvm::MemoryBuffer::getFile(Filename: RequestsFilename); |
39 | if (!Buffer) { |
40 | llvm::errs() << "Error cannot open JSON request file:" << RequestsFilename |
41 | << ": " << Buffer.getError().message() << "\n" ; |
42 | exit(status: 1); |
43 | } |
44 | |
45 | StringRef Log = Buffer.get()->getBuffer(); |
46 | |
47 | std::vector<FuzzyFindRequest> Requests; |
48 | auto JSONArray = llvm::json::parse(JSON: Log); |
49 | |
50 | // Panic if the provided file couldn't be parsed. |
51 | if (!JSONArray) { |
52 | llvm::errs() << "Error when parsing JSON requests file: " |
53 | << llvm::toString(E: JSONArray.takeError()); |
54 | exit(status: 1); |
55 | } |
56 | if (!JSONArray->getAsArray()) { |
57 | llvm::errs() << "Error: top-level value is not a JSON array: " << Log |
58 | << '\n'; |
59 | exit(status: 1); |
60 | } |
61 | |
62 | for (const auto &Item : *JSONArray->getAsArray()) { |
63 | FuzzyFindRequest Request; |
64 | // Panic if the provided file couldn't be parsed. |
65 | llvm::json::Path::Root Root("FuzzyFindRequest" ); |
66 | if (!fromJSON(Value: Item, Request, Root)) { |
67 | llvm::errs() << llvm::toString(E: Root.getError()) << "\n" ; |
68 | Root.printErrorContext(Item, llvm::errs()); |
69 | exit(status: 1); |
70 | } |
71 | Requests.push_back(x: Request); |
72 | } |
73 | return Requests; |
74 | } |
75 | |
76 | static void memQueries(benchmark::State &State) { |
77 | const auto Mem = buildMem(); |
78 | const auto Requests = extractQueriesFromLogs(); |
79 | for (auto _ : State) |
80 | for (const auto &Request : Requests) |
81 | Mem->fuzzyFind(Req: Request, Callback: [](const Symbol &S) {}); |
82 | } |
83 | BENCHMARK(memQueries); |
84 | |
85 | static void dexQueries(benchmark::State &State) { |
86 | const auto Dex = buildDex(); |
87 | const auto Requests = extractQueriesFromLogs(); |
88 | for (auto _ : State) |
89 | for (const auto &Request : Requests) |
90 | Dex->fuzzyFind(Req: Request, Callback: [](const Symbol &S) {}); |
91 | } |
92 | BENCHMARK(dexQueries); |
93 | |
94 | static void dexBuild(benchmark::State &State) { |
95 | for (auto _ : State) |
96 | buildDex(); |
97 | } |
98 | BENCHMARK(dexBuild); |
99 | |
100 | } // namespace |
101 | } // namespace clangd |
102 | } // namespace clang |
103 | |
104 | // FIXME(kbobyrev): Add index building time benchmarks. |
105 | // FIXME(kbobyrev): Add memory consumption "benchmarks" by manually measuring |
106 | // in-memory index size and reporting it as time. |
107 | // FIXME(kbobyrev): Create a logger wrapper to suppress debugging info printer. |
108 | int main(int argc, char *argv[]) { |
109 | if (argc < 3) { |
110 | llvm::errs() << "Usage: " << argv[0] |
111 | << " global-symbol-index.dex requests.json " |
112 | "BENCHMARK_OPTIONS...\n" ; |
113 | return -1; |
114 | } |
115 | IndexFilename = argv[1]; |
116 | RequestsFilename = argv[2]; |
117 | // Trim first two arguments of the benchmark invocation and pretend no |
118 | // arguments were passed in the first place. |
119 | argv[2] = argv[0]; |
120 | argv += 2; |
121 | argc -= 2; |
122 | ::benchmark::Initialize(argc: &argc, argv); |
123 | ::benchmark::RunSpecifiedBenchmarks(); |
124 | } |
125 | |