1 //===--- DecisionForestBenchmark.cpp ------------*- C++ -*-===//
3 // Benchmark for code completion ranking latency.
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
10 // ninja DecisionForestBenchmark && \
11 // tools/clang/tools/extra/clangd/benchmarks/CompletionModel/DecisionForestBenchmark
12 //===----------------------------------------------------------------------===//
14 #include "CompletionModel.h"
15 #include "benchmark/benchmark.h"
16 #include "llvm/ADT/StringRef.h"
23 std::vector
<Example
> generateRandomDataset(int NumExamples
) {
24 auto FlipCoin
= [&](double Probability
) {
25 return rand() % 1000 <= Probability
* 1000;
27 auto RandInt
= [&](int Max
) { return rand() % Max
; };
28 auto RandFloat
= [&](float Max
= 1.0) {
29 return rand() % 1000 / 1000.0 * Max
;
32 std::vector
<Example
> Examples
;
33 Examples
.reserve(NumExamples
);
34 for (int I
= 0; I
< NumExamples
; ++I
) {
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.
48 E
.setSemaFileProximityScore(RandFloat(1)); // Float in range [0,1].
49 E
.setSymbolScopeDistanceCost(
50 FlipCoin(0.1) ? 999999 // Sometimes scope distance is not available.
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
);
64 void runDecisionForestPrediction(const std::vector
<Example
> Examples
) {
65 for (const Example
&E
: Examples
)
69 static void decisionForestPredict(benchmark::State
&State
) {
71 for (auto _
: State
) {
73 const std::vector
<Example
> Examples
= generateRandomDataset(1000000);
75 runDecisionForestPrediction(Examples
);
78 BENCHMARK(decisionForestPredict
);