Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 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/SymbolYAML.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; |
Kirill Bobyrev | 60be1f5 | 2018-09-13 14:21:50 +0000 | [diff] [blame] | 22 | const char *RequestsFilename; |
Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 23 | |
| 24 | namespace clang { |
| 25 | namespace clangd { |
| 26 | namespace { |
| 27 | |
Kirill Bobyrev | 60be1f5 | 2018-09-13 14:21:50 +0000 | [diff] [blame] | 28 | std::unique_ptr<SymbolIndex> buildMem() { |
| 29 | return loadIndex(IndexFilename, {}, false); |
Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Kirill Bobyrev | 60be1f5 | 2018-09-13 14:21:50 +0000 | [diff] [blame] | 32 | std::unique_ptr<SymbolIndex> buildDex() { |
| 33 | return loadIndex(IndexFilename, {}, true); |
Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Kirill Bobyrev | 60be1f5 | 2018-09-13 14:21:50 +0000 | [diff] [blame] | 36 | // Reads JSON array of serialized FuzzyFindRequest's from user-provided file. |
| 37 | std::vector<FuzzyFindRequest> extractQueriesFromLogs() { |
| 38 | std::ifstream InputStream(RequestsFilename); |
Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 39 | std::string Log((std::istreambuf_iterator<char>(InputStream)), |
| 40 | std::istreambuf_iterator<char>()); |
Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 41 | |
Kirill Bobyrev | 60be1f5 | 2018-09-13 14:21:50 +0000 | [diff] [blame] | 42 | std::vector<FuzzyFindRequest> Requests; |
| 43 | auto JSONArray = llvm::json::parse(Log); |
Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 44 | |
Kirill Bobyrev | 60be1f5 | 2018-09-13 14:21:50 +0000 | [diff] [blame] | 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); |
Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 50 | } |
Kirill Bobyrev | 60be1f5 | 2018-09-13 14:21:50 +0000 | [diff] [blame] | 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; |
Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 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] |
Kirill Bobyrev | 60be1f5 | 2018-09-13 14:21:50 +0000 | [diff] [blame] | 98 | << " global-symbol-index.yaml requests.json " |
Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 99 | "BENCHMARK_OPTIONS...\n"; |
| 100 | return -1; |
| 101 | } |
| 102 | IndexFilename = argv[1]; |
Kirill Bobyrev | 60be1f5 | 2018-09-13 14:21:50 +0000 | [diff] [blame] | 103 | RequestsFilename = argv[2]; |
Kirill Bobyrev | 2fcdf76 | 2018-09-14 12:21:09 +0000 | [diff] [blame^] | 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; |
Kirill Bobyrev | 73c201d | 2018-09-12 07:49:44 +0000 | [diff] [blame] | 109 | ::benchmark::Initialize(&argc, argv); |
| 110 | ::benchmark::RunSpecifiedBenchmarks(); |
| 111 | } |