blob: 26a70de8ee515541c45797c34098937cee51d2d5 [file] [log] [blame]
Kirill Bobyrev73c201d2018-09-12 07:49:44 +00001//===--- IndexBenchmark.cpp - Clangd index benchmarks -----------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Kirill Bobyrev73c201d2018-09-12 07:49:44 +00006//
7//===----------------------------------------------------------------------===//
8
Sam McCall02d600d2018-09-25 18:06:43 +00009#include "../index/Serialization.h"
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000010#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
20const char *IndexFilename;
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000021const char *RequestsFilename;
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000022
23namespace clang {
24namespace clangd {
25namespace {
26
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000027std::unique_ptr<SymbolIndex> buildMem() {
Ilya Biryukovd60c2892018-11-26 15:58:29 +000028 return loadIndex(IndexFilename, /*UseDex=*/false);
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000029}
30
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000031std::unique_ptr<SymbolIndex> buildDex() {
Ilya Biryukovd60c2892018-11-26 15:58:29 +000032 return loadIndex(IndexFilename, /*UseDex=*/true);
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000033}
34
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000035// Reads JSON array of serialized FuzzyFindRequest's from user-provided file.
36std::vector<FuzzyFindRequest> extractQueriesFromLogs() {
37 std::ifstream InputStream(RequestsFilename);
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000038 std::string Log((std::istreambuf_iterator<char>(InputStream)),
39 std::istreambuf_iterator<char>());
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000040
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000041 std::vector<FuzzyFindRequest> Requests;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000042 auto JSONArray = llvm::json::parse(Log);
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000043
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000044 // Panic if the provided file couldn't be parsed.
45 if (!JSONArray) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000046 llvm::errs() << "Error when parsing JSON requests file: "
47 << llvm::toString(JSONArray.takeError());
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000048 exit(1);
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000049 }
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000050 if (!JSONArray->getAsArray()) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000051 llvm::errs() << "Error: top-level value is not a JSON array: " << Log
52 << '\n';
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000053 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 if (!fromJSON(Item, Request)) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000060 llvm::errs() << "Error when deserializing request: " << Item << '\n';
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000061 exit(1);
62 }
63 Requests.push_back(Request);
64 }
65 return Requests;
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000066}
67
68static void MemQueries(benchmark::State &State) {
69 const auto Mem = buildMem();
70 const auto Requests = extractQueriesFromLogs();
71 for (auto _ : State)
72 for (const auto &Request : Requests)
73 Mem->fuzzyFind(Request, [](const Symbol &S) {});
74}
75BENCHMARK(MemQueries);
76
77static void DexQueries(benchmark::State &State) {
78 const auto Dex = buildDex();
79 const auto Requests = extractQueriesFromLogs();
80 for (auto _ : State)
81 for (const auto &Request : Requests)
82 Dex->fuzzyFind(Request, [](const Symbol &S) {});
83}
84BENCHMARK(DexQueries);
85
Sam McCall735ab462020-05-14 02:43:27 +020086static void DexBuild(benchmark::State &State) {
87 for (auto _ : State)
88 buildDex();
89}
90BENCHMARK(DexBuild);
91
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000092} // namespace
93} // namespace clangd
94} // namespace clang
95
96// FIXME(kbobyrev): Add index building time benchmarks.
97// FIXME(kbobyrev): Add memory consumption "benchmarks" by manually measuring
98// in-memory index size and reporting it as time.
99// FIXME(kbobyrev): Create a logger wrapper to suppress debugging info printer.
100int main(int argc, char *argv[]) {
101 if (argc < 3) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000102 llvm::errs() << "Usage: " << argv[0]
103 << " global-symbol-index.yaml requests.json "
104 "BENCHMARK_OPTIONS...\n";
Kirill Bobyrev73c201d2018-09-12 07:49:44 +0000105 return -1;
106 }
107 IndexFilename = argv[1];
Kirill Bobyrev60be1f52018-09-13 14:21:50 +0000108 RequestsFilename = argv[2];
Kirill Bobyrev2fcdf762018-09-14 12:21:09 +0000109 // Trim first two arguments of the benchmark invocation and pretend no
110 // arguments were passed in the first place.
111 argv[2] = argv[0];
112 argv += 2;
113 argc -= 2;
Kirill Bobyrev73c201d2018-09-12 07:49:44 +0000114 ::benchmark::Initialize(&argc, argv);
115 ::benchmark::RunSpecifiedBenchmarks();
116}