blob: dc868f8d2677e46282b6458526b281e40f31c543 [file] [log] [blame]
Kirill Bobyrev73c201d2018-09-12 07:49:44 +00001//===--- 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
21const char *IndexFilename;
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000022const char *RequestsFilename;
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000023
24namespace clang {
25namespace clangd {
26namespace {
27
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000028std::unique_ptr<SymbolIndex> buildMem() {
29 return loadIndex(IndexFilename, {}, false);
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000030}
31
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000032std::unique_ptr<SymbolIndex> buildDex() {
33 return loadIndex(IndexFilename, {}, true);
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000034}
35
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000036// Reads JSON array of serialized FuzzyFindRequest's from user-provided file.
37std::vector<FuzzyFindRequest> extractQueriesFromLogs() {
38 std::ifstream InputStream(RequestsFilename);
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000039 std::string Log((std::istreambuf_iterator<char>(InputStream)),
40 std::istreambuf_iterator<char>());
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000041
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000042 std::vector<FuzzyFindRequest> Requests;
43 auto JSONArray = llvm::json::parse(Log);
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000044
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000045 // 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 Bobyrev73c201d2018-09-12 07:49:44 +000050 }
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000051 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 Bobyrev73c201d2018-09-12 07:49:44 +000067}
68
69static 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}
76BENCHMARK(MemQueries);
77
78static 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}
85BENCHMARK(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.
95int main(int argc, char *argv[]) {
96 if (argc < 3) {
97 llvm::errs() << "Usage: " << argv[0]
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000098 << " global-symbol-index.yaml requests.json "
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000099 "BENCHMARK_OPTIONS...\n";
100 return -1;
101 }
102 IndexFilename = argv[1];
Kirill Bobyrev60be1f52018-09-13 14:21:50 +0000103 RequestsFilename = argv[2];
Kirill Bobyrev2fcdf762018-09-14 12:21:09 +0000104 // 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 Bobyrev73c201d2018-09-12 07:49:44 +0000109 ::benchmark::Initialize(&argc, argv);
110 ::benchmark::RunSpecifiedBenchmarks();
111}