1 //===--- DecisionForestBenchmark.cpp ------------*- C++ -*-===// 2 // 3 // Benchmark for code completion ranking latency. 4 // 5 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 6 // See https://llvm.org/LICENSE.txt for license information. 7 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 8 // 9 // Usage: 10 // ninja DecisionForestBenchmark && \ 11 // tools/clang/tools/extra/clangd/benchmarks/CompletionModel/DecisionForestBenchmark 12 //===----------------------------------------------------------------------===// 13 14 #include "CompletionModel.h" 15 #include "benchmark/benchmark.h" 16 #include "llvm/ADT/StringRef.h" 17 18 #include <random> 19 20 namespace clang { 21 namespace clangd { 22 namespace { generateRandomDataset(int NumExamples)23std::vector<Example> generateRandomDataset(int NumExamples) { 24 auto FlipCoin = [&](float Probability) { 25 return rand() % 1000 <= Probability * 1000; 26 }; 27 auto RandInt = [&](int Max) { return rand() % Max; }; 28 auto RandFloat = [&](float Max = 1.0) { 29 return rand() % 1000 / 1000.0 * Max; 30 }; 31 32 std::vector<Example> Examples; 33 Examples.reserve(NumExamples); 34 for (int I = 0; I < NumExamples; ++I) { 35 Example E; 36 E.setIsDeprecated(FlipCoin(0.1)); // Boolean. 37 E.setIsReservedName(FlipCoin(0.1)); // Boolean. 38 E.setIsImplementationDetail(FlipCoin(0.3)); // Boolean. 39 E.setNumReferences(RandInt(10000)); // Can be large integer. 40 E.setSymbolCategory(RandInt(10)); // 10 Symbol Category. 41 42 E.setIsNameInContext(FlipCoin(0.5)); // Boolean. 43 E.setIsForbidden(FlipCoin(0.1)); // Boolean. 44 E.setIsInBaseClass(FlipCoin(0.3)); // Boolean. 45 E.setFileProximityDistance( 46 FlipCoin(0.1) ? 999999 // Sometimes file distance is not available. 47 : RandInt(20)); 48 E.setSemaFileProximityScore(RandFloat(1)); // Float in range [0,1]. 49 E.setSymbolScopeDistance( 50 FlipCoin(0.1) ? 999999 // Sometimes scope distance is not available. 51 : RandInt(20)); 52 E.setSemaSaysInScope(FlipCoin(0.5)); // Boolean. 53 E.setScope(RandInt(4)); // 4 Scopes. 54 E.setContextKind(RandInt(32)); // 32 Context kinds. 55 E.setIsInstanceMember(FlipCoin(0.5)); // Boolean. 56 E.setHadContextType(FlipCoin(0.6)); // Boolean. 57 E.setHadSymbolType(FlipCoin(0.6)); // Boolean. 58 E.setTypeMatchesPreferred(FlipCoin(0.5)); // Boolean. 59 E.setFilterLength(RandInt(15)); 60 Examples.push_back(E); 61 } 62 return Examples; 63 } 64 runDecisionForestPrediciton(const std::vector<Example> Examples)65void runDecisionForestPrediciton(const std::vector<Example> Examples) { 66 for (const Example &E : Examples) 67 Evaluate(E); 68 } 69 decisionForestPredict(benchmark::State & State)70static void decisionForestPredict(benchmark::State &State) { 71 srand(0); 72 for (auto _ : State) { 73 State.PauseTiming(); 74 const std::vector<Example> Examples = generateRandomDataset(1000000); 75 State.ResumeTiming(); 76 runDecisionForestPrediciton(Examples); 77 } 78 } 79 BENCHMARK(decisionForestPredict); 80 81 } // namespace 82 } // namespace clangd 83 } // namespace clang 84 85 BENCHMARK_MAIN(); 86