|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.benchmark.esql; |
| 11 | + |
| 12 | +import org.elasticsearch.common.logging.LogConfigurator; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.index.IndexMode; |
| 15 | +import org.elasticsearch.license.XPackLicenseState; |
| 16 | +import org.elasticsearch.xpack.esql.analysis.Analyzer; |
| 17 | +import org.elasticsearch.xpack.esql.analysis.AnalyzerContext; |
| 18 | +import org.elasticsearch.xpack.esql.analysis.EnrichResolution; |
| 19 | +import org.elasticsearch.xpack.esql.analysis.Verifier; |
| 20 | +import org.elasticsearch.xpack.esql.core.expression.FoldContext; |
| 21 | +import org.elasticsearch.xpack.esql.core.type.EsField; |
| 22 | +import org.elasticsearch.xpack.esql.core.util.DateUtils; |
| 23 | +import org.elasticsearch.xpack.esql.expression.function.EsqlFunctionRegistry; |
| 24 | +import org.elasticsearch.xpack.esql.index.EsIndex; |
| 25 | +import org.elasticsearch.xpack.esql.index.IndexResolution; |
| 26 | +import org.elasticsearch.xpack.esql.inference.InferenceResolution; |
| 27 | +import org.elasticsearch.xpack.esql.optimizer.LogicalOptimizerContext; |
| 28 | +import org.elasticsearch.xpack.esql.optimizer.LogicalPlanOptimizer; |
| 29 | +import org.elasticsearch.xpack.esql.parser.EsqlParser; |
| 30 | +import org.elasticsearch.xpack.esql.parser.QueryParams; |
| 31 | +import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; |
| 32 | +import org.elasticsearch.xpack.esql.plugin.EsqlPlugin; |
| 33 | +import org.elasticsearch.xpack.esql.plugin.QueryPragmas; |
| 34 | +import org.elasticsearch.xpack.esql.session.Configuration; |
| 35 | +import org.elasticsearch.xpack.esql.telemetry.Metrics; |
| 36 | +import org.elasticsearch.xpack.esql.telemetry.PlanTelemetry; |
| 37 | +import org.openjdk.jmh.annotations.Benchmark; |
| 38 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 39 | +import org.openjdk.jmh.annotations.Fork; |
| 40 | +import org.openjdk.jmh.annotations.Measurement; |
| 41 | +import org.openjdk.jmh.annotations.Mode; |
| 42 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 43 | +import org.openjdk.jmh.annotations.Scope; |
| 44 | +import org.openjdk.jmh.annotations.Setup; |
| 45 | +import org.openjdk.jmh.annotations.State; |
| 46 | +import org.openjdk.jmh.annotations.Warmup; |
| 47 | +import org.openjdk.jmh.infra.Blackhole; |
| 48 | + |
| 49 | +import java.util.LinkedHashMap; |
| 50 | +import java.util.Locale; |
| 51 | +import java.util.Map; |
| 52 | +import java.util.concurrent.TimeUnit; |
| 53 | + |
| 54 | +import static java.util.Collections.emptyMap; |
| 55 | +import static org.elasticsearch.xpack.esql.core.type.DataType.TEXT; |
| 56 | + |
| 57 | +@Fork(1) |
| 58 | +@Warmup(iterations = 5) |
| 59 | +@Measurement(iterations = 10) |
| 60 | +@BenchmarkMode(Mode.AverageTime) |
| 61 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 62 | +@State(Scope.Benchmark) |
| 63 | +public class QueryPlanningBenchmark { |
| 64 | + |
| 65 | + static { |
| 66 | + LogConfigurator.configureESLogging(); |
| 67 | + } |
| 68 | + |
| 69 | + private PlanTelemetry telemetry; |
| 70 | + private EsqlParser parser; |
| 71 | + private Analyzer analyzer; |
| 72 | + private LogicalPlanOptimizer optimizer; |
| 73 | + |
| 74 | + @Setup |
| 75 | + public void setup() { |
| 76 | + |
| 77 | + var config = new Configuration( |
| 78 | + DateUtils.UTC, |
| 79 | + Locale.US, |
| 80 | + null, |
| 81 | + null, |
| 82 | + new QueryPragmas(Settings.EMPTY), |
| 83 | + EsqlPlugin.QUERY_RESULT_TRUNCATION_MAX_SIZE.getDefault(Settings.EMPTY), |
| 84 | + EsqlPlugin.QUERY_RESULT_TRUNCATION_DEFAULT_SIZE.getDefault(Settings.EMPTY), |
| 85 | + "", |
| 86 | + false, |
| 87 | + Map.of(), |
| 88 | + System.nanoTime(), |
| 89 | + false |
| 90 | + ); |
| 91 | + |
| 92 | + var fields = 10_000; |
| 93 | + var mapping = LinkedHashMap.<String, EsField>newLinkedHashMap(fields); |
| 94 | + for (int i = 0; i < fields; i++) { |
| 95 | + mapping.put("field" + i, new EsField("field-" + i, TEXT, emptyMap(), true)); |
| 96 | + } |
| 97 | + |
| 98 | + var esIndex = new EsIndex("test", mapping, Map.of("test", IndexMode.STANDARD)); |
| 99 | + |
| 100 | + var functionRegistry = new EsqlFunctionRegistry(); |
| 101 | + |
| 102 | + telemetry = new PlanTelemetry(functionRegistry); |
| 103 | + parser = new EsqlParser(); |
| 104 | + analyzer = new Analyzer( |
| 105 | + new AnalyzerContext( |
| 106 | + config, |
| 107 | + functionRegistry, |
| 108 | + IndexResolution.valid(esIndex), |
| 109 | + Map.of(), |
| 110 | + new EnrichResolution(), |
| 111 | + InferenceResolution.EMPTY |
| 112 | + ), |
| 113 | + new Verifier(new Metrics(functionRegistry), new XPackLicenseState(() -> 0L)) |
| 114 | + ); |
| 115 | + optimizer = new LogicalPlanOptimizer(new LogicalOptimizerContext(config, FoldContext.small())); |
| 116 | + } |
| 117 | + |
| 118 | + private LogicalPlan plan(String query) { |
| 119 | + var parsed = parser.createStatement(query, new QueryParams(), telemetry); |
| 120 | + var analyzed = analyzer.analyze(parsed); |
| 121 | + var optimized = optimizer.optimize(analyzed); |
| 122 | + return optimized; |
| 123 | + } |
| 124 | + |
| 125 | + @Benchmark |
| 126 | + public void run(Blackhole blackhole) { |
| 127 | + blackhole.consume(plan("FROM test | LIMIT 10")); |
| 128 | + } |
| 129 | +} |
0 commit comments