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 { |
23 | std::vector<Example> generateRandomDataset(int NumExamples) { |
24 | auto FlipCoin = [&](double 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 | E.setNumNameInContext(RandInt(20)); // 0 to ContextWords->size(). |
42 | E.setFractionNameInContext(RandFloat(1.0)); // Float in range [0,1]. |
43 | E.setIsNameInContext(FlipCoin(0.5)); // Boolean. |
44 | E.setIsInBaseClass(FlipCoin(0.3)); // Boolean. |
45 | E.setFileProximityDistanceCost( |
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.setSymbolScopeDistanceCost( |
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(36)); // 36 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 | Examples.push_back(E); |
60 | } |
61 | return Examples; |
62 | } |
63 | |
64 | void runDecisionForestPrediction(const std::vector<Example> Examples) { |
65 | for (const Example &E : Examples) |
66 | Evaluate(E); |
67 | } |
68 | |
69 | static void decisionForestPredict(benchmark::State &State) { |
70 | srand(seed: 0); |
71 | for (auto _ : State) { |
72 | State.PauseTiming(); |
73 | const std::vector<Example> Examples = generateRandomDataset(1000000); |
74 | State.ResumeTiming(); |
75 | runDecisionForestPrediction(Examples); |
76 | } |
77 | } |
78 | BENCHMARK(decisionForestPredict); |
79 | |
80 | } // namespace |
81 | } // namespace clangd |
82 | } // namespace clang |
83 | |
84 | BENCHMARK_MAIN(); |
85 | |