kbobyrev updated this revision to Diff 163821.
kbobyrev edited the summary of this revision.
kbobyrev added a comment.

- Rebase on top of new code
- Simplify code structure and get rid of global state (except for two filenames 
coming from `main()`)

The only problem now is that the generated output contains log strings (e.g. 
"Built DexIndex with estimated memory usage 63445392 bytes.", see example in 
the Summary), I should find a way to deal with that (since it's rather 
annoying). It's also the case for the `dexplorer` tool.


https://reviews.llvm.org/D51090

Files:
  clang-tools-extra/clangd/CMakeLists.txt
  clang-tools-extra/clangd/benchmarks/CMakeLists.txt
  clang-tools-extra/clangd/benchmarks/IndexBenchmark.cpp

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 <fstream>
+#include <streambuf>
+#include <string>
+
+std::string IndexFilename;
+std::string LogFilename;
+
+std::unique_ptr<clang::clangd::SymbolIndex> buildMem() {
+  return clang::clangd::buildStaticIndex(IndexFilename, false);
+}
+
+std::unique_ptr<clang::clangd::SymbolIndex> buildDex() {
+  return clang::clangd::buildStaticIndex(IndexFilename, true);
+}
+
+std::vector<clang::clangd::FuzzyFindRequest> extractQueriesFromLogs() {
+  llvm::Regex RequestMatcher("fuzzyFind\\(\"([a-zA-Z]*)\", scopes=\\[(.*)\\]");
+  llvm::SmallVector<llvm::StringRef, 200> Matches;
+  std::ifstream InputStream(LogFilename);
+  std::string Log((std::istreambuf_iterator<char>(InputStream)),
+                  std::istreambuf_iterator<char>());
+  llvm::StringRef Temporary(Log);
+  llvm::SmallVector<llvm::StringRef, 200> Strings;
+  Temporary.split(Strings, '\n');
+
+  clang::clangd::FuzzyFindRequest R;
+  R.MaxCandidateCount = 100;
+
+  llvm::SmallVector<llvm::StringRef, 200> CommaSeparatedValues;
+
+  std::vector<clang::clangd::FuzzyFindRequest> 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<clang::clangd::FuzzyFindRequest> generateArtificialRequests() {
+  std::vector<clang::clangd::FuzzyFindRequest> 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();
+}
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/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()
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to