Index: clang-tools-extra/clangd/CMakeLists.txt =================================================================== --- clang-tools-extra/clangd/CMakeLists.txt +++ clang-tools-extra/clangd/CMakeLists.txt @@ -72,3 +72,7 @@ endif() add_subdirectory(tool) add_subdirectory(global-symbol-builder) + +if (LLVM_INCLUDE_BENCHMARKS) + add_subdirectory(benchmarks) +endif() Index: clang-tools-extra/clangd/benchmarks/CMakeLists.txt =================================================================== --- /dev/null +++ clang-tools-extra/clangd/benchmarks/CMakeLists.txt @@ -0,0 +1,9 @@ +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) + +add_benchmark(IndexBenchmark IndexBenchmark.cpp) + +target_link_libraries(IndexBenchmark + PRIVATE + clangDaemon + LLVMSupport + ) Index: clang-tools-extra/clangd/benchmarks/IndexBenchmark.cpp =================================================================== --- /dev/null +++ clang-tools-extra/clangd/benchmarks/IndexBenchmark.cpp @@ -0,0 +1,150 @@ +//===--- DexBenchmark.cpp - DexIndex benchmarks -----------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "../index/SymbolYAML.h" +#include "../index/dex/DexIndex.h" +#include "benchmark/benchmark.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Regex.h" +#include +#include +#include + +std::string IndexFilename; +std::string LogFilename; + +std::unique_ptr buildMem() { + return clang::clangd::buildStaticIndex(IndexFilename, false); +} + +std::unique_ptr buildDex() { + return clang::clangd::buildStaticIndex(IndexFilename, true); +} + +std::vector extractQueriesFromLogs() { + llvm::Regex RequestMatcher("fuzzyFind\\(\"([a-zA-Z]*)\", scopes=\\[(.*)\\]"); + llvm::SmallVector Matches; + std::ifstream InputStream(LogFilename); + std::string Log((std::istreambuf_iterator(InputStream)), + std::istreambuf_iterator()); + llvm::StringRef Temporary(Log); + llvm::SmallVector Strings; + Temporary.split(Strings, '\n'); + + clang::clangd::FuzzyFindRequest R; + R.MaxCandidateCount = 100; + + llvm::SmallVector CommaSeparatedValues; + + std::vector RealRequests; + for (auto Line : Strings) { + if (RequestMatcher.match(Line, &Matches)) { + R.Query = Matches[1]; + CommaSeparatedValues.clear(); + Line.split(CommaSeparatedValues, ','); + R.Scopes.clear(); + for (auto C : CommaSeparatedValues) { + R.Scopes.push_back(C); + } + RealRequests.push_back(R); + } + } + llvm::outs() << "Collected " << RealRequests.size() << " requests.\n"; + return RealRequests; +} + +std::vector generateArtificialRequests() { + std::vector Requests; + // FXIME(kbobyrev): Add more requests. + clang::clangd::FuzzyFindRequest Request; + Request.MaxCandidateCount = 100; + Requests.push_back(Request); + Request.Scopes = {"::"}; + Requests.push_back(Request); + Request.Scopes = {"::", "llvm::", "clang::", "clangd::"}; + Requests.push_back(Request); + Request.Scopes = {"::", "clang::", "llvm::", "std::"}; + Request.Query = "TUDec"; + Requests.push_back(Request); + Request.Query = "non-existent symbol"; + Requests.push_back(Request); + return Requests; +} + +namespace clang { +namespace clangd { +namespace dex { + +static void BuildMem(benchmark::State &State) { + for (auto _ : State) + buildMem(); +} +BENCHMARK(BuildMem); + +static void MemArtificialQueries(benchmark::State &State) { + const auto Mem = buildMem(); + const auto Requests = generateArtificialRequests(); + for (auto _ : State) + for (const auto &Request : Requests) + Mem->fuzzyFind(Request, [](const Symbol &S) {}); +} +BENCHMARK(MemArtificialQueries); + +static void MemRealQueries(benchmark::State &State) { + const auto Mem = buildMem(); + const auto Requests = extractQueriesFromLogs(); + for (auto _ : State) + for (const auto &Request : Requests) + Mem->fuzzyFind(Request, [](const Symbol &S) {}); +} +BENCHMARK(MemRealQueries); + +static void BuildDex(benchmark::State &State) { + for (auto _ : State) + buildDex(); +} +BENCHMARK(BuildDex); + +static void DexArtificialQueries(benchmark::State &State) { + const auto Dex = buildDex(); + const auto Requests = generateArtificialRequests(); + for (auto _ : State) + for (const auto &Request : Requests) + Dex->fuzzyFind(Request, [](const Symbol &S) {}); +} +BENCHMARK(DexArtificialQueries); + +static void DexRealQueries(benchmark::State &State) { + const auto Dex = buildDex(); + const auto Requests = extractQueriesFromLogs(); + for (auto _ : State) + for (const auto &Request : Requests) + Dex->fuzzyFind(Request, [](const Symbol &S) {}); +} +BENCHMARK(DexRealQueries); + +} // namespace dex +} // namespace clangd +} // namespace clang + +int main(int argc, char *argv[]) { + if (argc < 3) { + llvm::errs() << "Usage: " << argv[0] + << " global-symbol-index.yaml fuzzy-find-requests.log " + "BENCHMARK_OPTIONS...\n"; + return -1; + } + IndexFilename = argv[1]; + LogFilename = argv[2]; + // Trim first two arguments of the benchmark invocation. + argv += 2; + argc -= 2; + ::benchmark::Initialize(&argc, argv); + ::benchmark::RunSpecifiedBenchmarks(); +}