forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvalBenchmark.java
262 lines (247 loc) · 11.2 KB
/
EvalBenchmark.java
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.benchmark.compute.operator;
import org.elasticsearch.compute.data.Block;
import org.elasticsearch.compute.data.BooleanBlock;
import org.elasticsearch.compute.data.BooleanVector;
import org.elasticsearch.compute.data.IntBlock;
import org.elasticsearch.compute.data.LongBlock;
import org.elasticsearch.compute.data.LongVector;
import org.elasticsearch.compute.data.Page;
import org.elasticsearch.compute.operator.EvalOperator;
import org.elasticsearch.compute.operator.Operator;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.xpack.esql.expression.function.scalar.date.DateTrunc;
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Abs;
import org.elasticsearch.xpack.esql.expression.function.scalar.multivalue.MvMin;
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.Equals;
import org.elasticsearch.xpack.esql.planner.EvalMapper;
import org.elasticsearch.xpack.esql.planner.Layout;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
import org.elasticsearch.xpack.ql.expression.FieldAttribute;
import org.elasticsearch.xpack.ql.expression.Literal;
import org.elasticsearch.xpack.ql.expression.predicate.operator.arithmetic.Add;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataTypes;
import org.elasticsearch.xpack.ql.type.EsField;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
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.State;
import org.openjdk.jmh.annotations.Warmup;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Warmup(iterations = 5)
@Measurement(iterations = 7)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
@Fork(1)
public class EvalBenchmark {
private static final int BLOCK_LENGTH = 8 * 1024;
static {
// Smoke test all the expected values and force loading subclasses more like prod
try {
for (String operation : EvalBenchmark.class.getField("operation").getAnnotationsByType(Param.class)[0].value()) {
run(operation);
}
} catch (NoSuchFieldException e) {
throw new AssertionError();
}
}
@Param({ "abs", "add", "date_trunc", "equal_to_const", "long_equal_to_long", "long_equal_to_int", "mv_min", "mv_min_ascending" })
public String operation;
private static Operator operator(String operation) {
return new EvalOperator(evaluator(operation));
}
private static EvalOperator.ExpressionEvaluator evaluator(String operation) {
return switch (operation) {
case "abs" -> {
FieldAttribute longField = longField();
yield EvalMapper.toEvaluator(new Abs(Source.EMPTY, longField), layout(longField)).get();
}
case "add" -> {
FieldAttribute longField = longField();
yield EvalMapper.toEvaluator(
new Add(Source.EMPTY, longField, new Literal(Source.EMPTY, 1L, DataTypes.LONG)),
layout(longField)
).get();
}
case "date_trunc" -> {
FieldAttribute timestamp = new FieldAttribute(
Source.EMPTY,
"timestamp",
new EsField("timestamp", DataTypes.DATETIME, Map.of(), true)
);
yield EvalMapper.toEvaluator(
new DateTrunc(Source.EMPTY, timestamp, new Literal(Source.EMPTY, Duration.ofHours(24), EsqlDataTypes.TIME_DURATION)),
layout(timestamp)
).get();
}
case "equal_to_const" -> {
FieldAttribute longField = longField();
yield EvalMapper.toEvaluator(
new Equals(Source.EMPTY, longField, new Literal(Source.EMPTY, 100_000L, DataTypes.LONG)),
layout(longField)
).get();
}
case "long_equal_to_long" -> {
FieldAttribute lhs = longField();
FieldAttribute rhs = longField();
yield EvalMapper.toEvaluator(new Equals(Source.EMPTY, lhs, rhs), layout(lhs, rhs)).get();
}
case "long_equal_to_int" -> {
FieldAttribute lhs = longField();
FieldAttribute rhs = intField();
yield EvalMapper.toEvaluator(new Equals(Source.EMPTY, lhs, rhs), layout(lhs, rhs)).get();
}
case "mv_min", "mv_min_ascending" -> {
FieldAttribute longField = longField();
yield EvalMapper.toEvaluator(new MvMin(Source.EMPTY, longField), layout(longField)).get();
}
default -> throw new UnsupportedOperationException();
};
}
private static FieldAttribute longField() {
return new FieldAttribute(Source.EMPTY, "long", new EsField("long", DataTypes.LONG, Map.of(), true));
}
private static FieldAttribute intField() {
return new FieldAttribute(Source.EMPTY, "int", new EsField("int", DataTypes.INTEGER, Map.of(), true));
}
private static Layout layout(FieldAttribute... fields) {
Layout.Builder layout = new Layout.Builder();
for (FieldAttribute field : fields) {
layout.appendChannel(field.id());
}
return layout.build();
}
private static void checkExpected(String operation, Page actual) {
switch (operation) {
case "abs" -> {
LongVector v = actual.<LongBlock>getBlock(1).asVector();
for (int i = 0; i < BLOCK_LENGTH; i++) {
if (v.getLong(i) != i * 100_000) {
throw new AssertionError("[" + operation + "] expected [" + (i * 100_000) + "] but was [" + v.getLong(i) + "]");
}
}
}
case "add" -> {
LongVector v = actual.<LongBlock>getBlock(1).asVector();
for (int i = 0; i < BLOCK_LENGTH; i++) {
if (v.getLong(i) != i * 100_000 + 1) {
throw new AssertionError("[" + operation + "] expected [" + (i * 100_000 + 1) + "] but was [" + v.getLong(i) + "]");
}
}
}
case "date_trunc" -> {
LongVector v = actual.<LongBlock>getBlock(1).asVector();
long oneDay = TimeValue.timeValueHours(24).millis();
for (int i = 0; i < BLOCK_LENGTH; i++) {
long expected = i * 100_000;
expected -= expected % oneDay;
if (v.getLong(i) != expected) {
throw new AssertionError("[" + operation + "] expected [" + expected + "] but was [" + v.getLong(i) + "]");
}
}
}
case "equal_to_const" -> {
BooleanVector v = actual.<BooleanBlock>getBlock(1).asVector();
for (int i = 0; i < BLOCK_LENGTH; i++) {
if (v.getBoolean(i) != (i == 1)) {
throw new AssertionError("[" + operation + "] expected [" + (i == 1) + "] but was [" + v.getBoolean(i) + "]");
}
}
}
case "long_equal_to_long", "long_equal_to_int" -> {
BooleanVector v = actual.<BooleanBlock>getBlock(2).asVector();
for (int i = 0; i < BLOCK_LENGTH; i++) {
if (v.getBoolean(i) != true) {
throw new AssertionError("[" + operation + "] expected [" + (i == 1) + "] but was [" + v.getBoolean(i) + "]");
}
}
}
case "mv_min", "mv_min_ascending" -> {
LongVector v = actual.<LongBlock>getBlock(1).asVector();
for (int i = 0; i < BLOCK_LENGTH; i++) {
if (v.getLong(i) != i) {
throw new AssertionError("[" + operation + "] expected [" + i + "] but was [" + v.getLong(i) + "]");
}
}
}
default -> throw new UnsupportedOperationException();
}
}
private static Page page(String operation) {
return switch (operation) {
case "abs", "add", "date_trunc", "equal_to_const" -> {
var builder = LongBlock.newBlockBuilder(BLOCK_LENGTH);
for (int i = 0; i < BLOCK_LENGTH; i++) {
builder.appendLong(i * 100_000);
}
yield new Page(builder.build());
}
case "long_equal_to_long" -> {
var lhs = LongBlock.newBlockBuilder(BLOCK_LENGTH);
var rhs = LongBlock.newBlockBuilder(BLOCK_LENGTH);
for (int i = 0; i < BLOCK_LENGTH; i++) {
lhs.appendLong(i * 100_000);
rhs.appendLong(i * 100_000);
}
yield new Page(lhs.build(), rhs.build());
}
case "long_equal_to_int" -> {
var lhs = LongBlock.newBlockBuilder(BLOCK_LENGTH);
var rhs = IntBlock.newBlockBuilder(BLOCK_LENGTH);
for (int i = 0; i < BLOCK_LENGTH; i++) {
lhs.appendLong(i * 100_000);
rhs.appendInt(i * 100_000);
}
yield new Page(lhs.build(), rhs.build());
}
case "mv_min", "mv_min_ascending" -> {
var builder = LongBlock.newBlockBuilder(BLOCK_LENGTH);
if (operation.endsWith("ascending")) {
builder.mvOrdering(Block.MvOrdering.ASCENDING);
}
for (int i = 0; i < BLOCK_LENGTH; i++) {
builder.beginPositionEntry();
builder.appendLong(i);
builder.appendLong(i + 1);
builder.appendLong(i + 2);
builder.endPositionEntry();
}
yield new Page(builder.build());
}
default -> throw new UnsupportedOperationException();
};
}
@Benchmark
@OperationsPerInvocation(1024 * BLOCK_LENGTH)
public void run() {
run(operation);
}
private static void run(String operation) {
try (Operator operator = operator(operation)) {
Page page = page(operation);
Page output = null;
for (int i = 0; i < 1024; i++) {
operator.addInput(page);
output = operator.getOutput();
}
// We only check the last one
checkExpected(operation, output);
}
}
}