forked from opensearch-project/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnifiedFunctionBenchmark.java
More file actions
128 lines (110 loc) · 4.06 KB
/
Copy pathUnifiedFunctionBenchmark.java
File metadata and controls
128 lines (110 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.sql.api;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.opensearch.sql.api.function.UnifiedFunction;
import org.opensearch.sql.api.function.UnifiedFunctionRepository;
/**
* JMH benchmark for measuring {@link UnifiedFunction} performance. Tests one representative
* function per PPL category (json, math, conditional, collection, string).
*
* <p>Benchmarks:
*
* <ul>
* <li>{@link #loadFunction()}: Measures function loading from repository
* <li>{@link #evalFunction(Blackhole)}: Measures function evaluation (1000 calls per invocation)
* </ul>
*/
@Warmup(iterations = 2, time = 1)
@Measurement(iterations = 5, time = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@Fork(value = 1)
public class UnifiedFunctionBenchmark extends UnifiedQueryTestBase {
/** Number of function evaluations per benchmark invocation. */
private static final int OPS = 1000;
/** Benchmark specification selecting which function to test. */
@Param public BenchmarkSpec benchmarkSpec;
/** Repository for loading unified functions. */
private UnifiedFunctionRepository repository;
/** Pre-loaded function instance for evaluation benchmarks. */
private UnifiedFunction function;
/** Input arguments for function evaluation. */
private List<Object> inputs;
@Setup(Level.Trial)
public void setUpBenchmark() {
super.setUp();
repository = new UnifiedFunctionRepository(context);
function = benchmarkSpec.loadFunction(repository);
inputs = benchmarkSpec.getInputs();
}
@TearDown(Level.Trial)
public void tearDownBenchmark() throws Exception {
super.tearDown();
}
/** Benchmarks function loading from repository. */
@Benchmark
public UnifiedFunction loadFunction() {
return benchmarkSpec.loadFunction(repository);
}
/** Benchmarks function evaluation with pre-loaded function and inputs. */
@Benchmark
@OperationsPerInvocation(OPS)
public void evalFunction(Blackhole bh) {
for (int i = 0; i < OPS; i++) {
bh.consume(function.eval(inputs));
}
}
/** Enum defining benchmark test cases - one representative function per PPL category. */
@RequiredArgsConstructor
public enum BenchmarkSpec {
JSON_EXTRACT(
inputTypes("VARCHAR", "VARCHAR"),
sampleInputs("{\"name\":\"test\",\"value\":42}", "$.name")),
COALESCE(
inputTypes("VARCHAR", "VARCHAR", "VARCHAR"),
sampleInputs(null, "first_value", "default_value")),
MVFIND(
inputTypes("ARRAY", "VARCHAR"),
sampleInputs(List.of("debug", "error", "warn", "info"), "err.*")),
REX_EXTRACT(
inputTypes("VARCHAR", "VARCHAR", "INTEGER"),
sampleInputs("192.168.1.1 - GET /api", "(\\d+)", 1));
private final List<String> inputTypes;
@Getter private final List<Object> inputs;
UnifiedFunction loadFunction(UnifiedFunctionRepository repository) {
return repository
.loadFunction(name())
.map(desc -> desc.getBuilder().build(inputTypes))
.orElseThrow();
}
private static List<String> inputTypes(String... types) {
return List.of(types);
}
private static List<Object> sampleInputs(Object... args) {
return Arrays.asList(args);
}
}
}