Index: clang-tools-extra/clangd/CMakeLists.txt =================================================================== --- clang-tools-extra/clangd/CMakeLists.txt +++ clang-tools-extra/clangd/CMakeLists.txt @@ -74,3 +74,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,173 @@ +//===--- 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/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/Regex.h" +#include +#include +#include + +std::string IndexFilename; +std::string LogFilename; +std::string LLVMRootPath; + +std::unique_ptr buildMem() { + return clang::clangd::loadIndex(IndexFilename, {}, false); +} + +std::unique_ptr buildDex() { + return clang::clangd::loadIndex(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); + } + } + 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); + + llvm::SmallVector Path(begin(LLVMRootPath), end(LLVMRootPath)); + Request.ProximityPaths.push_back(std::string(Path.begin(), Path.end())); + llvm::sys::path::append(Path, "include"); + Request.ProximityPaths.push_back(std::string(Path.begin(), Path.end())); + llvm::sys::path::append(Path, "llvm"); + Request.ProximityPaths.push_back(std::string(Path.begin(), Path.end())); + llvm::sys::path::append(Path, "Support"); + Request.ProximityPaths.push_back(std::string(Path.begin(), Path.end())); + + Request.Query = "str"; + Request.Scopes = {"::", "llvm::", "llvm::sys::", "std::sys::path::"}; + Requests.push_back(Request); + + Request.Query = "file"; + Requests.push_back(Request); + + Request.Query = "path"; + 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 < 4) { + llvm::errs() << "Usage: " << argv[0] + << " global-symbol-index.yaml fuzzy-find-requests.log " + "/path/to/llvm BENCHMARK_OPTIONS...\n"; + return -1; + } + IndexFilename = argv[1]; + LogFilename = argv[2]; + LLVMRootPath = argv[3]; + // Trim first two arguments of the benchmark invocation. + argv += 3; + argc -= 3; + ::benchmark::Initialize(&argc, argv); + ::benchmark::RunSpecifiedBenchmarks(); +}