blob: 237655dcc9bc4d32a9d8e580035a497d4056de37 [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.
Sam McCallfa69b602020-09-24 01:14:12 +020059 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());
Kirill Bobyrev60be1f52018-09-13 14:21:50 +000063 exit(1);
64 }
65 Requests.push_back(Request);
66 }
67 return Requests;
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000068}
69
70static 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}
77BENCHMARK(MemQueries);
78
79static 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}
86BENCHMARK(DexQueries);
87
Sam McCall735ab462020-05-14 02:43:27 +020088static void DexBuild(benchmark::State &State) {
89 for (auto _ : State)
90 buildDex();
91}
92BENCHMARK(DexBuild);
93
Kirill Bobyrev73c201d2018-09-12 07:49:44 +000094} // 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.
102int main(int argc, char *argv[]) {
103 if (argc < 3) {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000104 llvm::errs() << "Usage: " << argv[0]
105 << " global-symbol-index.yaml requests.json "
106 "BENCHMARK_OPTIONS...\n";
Kirill Bobyrev73c201d2018-09-12 07:49:44 +0000107 return -1;
108 }
109 IndexFilename = argv[1];
Kirill Bobyrev60be1f52018-09-13 14:21:50 +0000110 RequestsFilename = argv[2];
Kirill Bobyrev2fcdf762018-09-14 12:21:09 +0000111 // 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;
Kirill Bobyrev73c201d2018-09-12 07:49:44 +0000116 ::benchmark::Initialize(&argc, argv);
117 ::benchmark::RunSpecifiedBenchmarks();
118}