| 
 | 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.indices.resolution;  | 
 | 11 | + | 
 | 12 | +import org.elasticsearch.action.IndicesRequest;  | 
 | 13 | +import org.elasticsearch.action.support.IndicesOptions;  | 
 | 14 | +import org.elasticsearch.cluster.ClusterName;  | 
 | 15 | +import org.elasticsearch.cluster.ClusterState;  | 
 | 16 | +import org.elasticsearch.cluster.metadata.DataStream;  | 
 | 17 | +import org.elasticsearch.cluster.metadata.IndexMetadata;  | 
 | 18 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;  | 
 | 19 | +import org.elasticsearch.cluster.metadata.Metadata;  | 
 | 20 | +import org.elasticsearch.common.settings.Settings;  | 
 | 21 | +import org.elasticsearch.common.util.concurrent.ThreadContext;  | 
 | 22 | +import org.elasticsearch.index.Index;  | 
 | 23 | +import org.elasticsearch.index.IndexVersion;  | 
 | 24 | +import org.elasticsearch.indices.SystemIndices;  | 
 | 25 | +import org.openjdk.jmh.annotations.Benchmark;  | 
 | 26 | +import org.openjdk.jmh.annotations.BenchmarkMode;  | 
 | 27 | +import org.openjdk.jmh.annotations.Fork;  | 
 | 28 | +import org.openjdk.jmh.annotations.Mode;  | 
 | 29 | +import org.openjdk.jmh.annotations.OutputTimeUnit;  | 
 | 30 | +import org.openjdk.jmh.annotations.Param;  | 
 | 31 | +import org.openjdk.jmh.annotations.Scope;  | 
 | 32 | +import org.openjdk.jmh.annotations.Setup;  | 
 | 33 | +import org.openjdk.jmh.annotations.State;  | 
 | 34 | + | 
 | 35 | +import java.util.ArrayList;  | 
 | 36 | +import java.util.List;  | 
 | 37 | +import java.util.concurrent.TimeUnit;  | 
 | 38 | + | 
 | 39 | +@State(Scope.Benchmark)  | 
 | 40 | +@Fork(3)  | 
 | 41 | +@BenchmarkMode(Mode.AverageTime)  | 
 | 42 | +@OutputTimeUnit(TimeUnit.MILLISECONDS)  | 
 | 43 | +@SuppressWarnings("unused") // invoked by benchmarking framework  | 
 | 44 | +public class IndexNameExpressionResolverBenchmark {  | 
 | 45 | + | 
 | 46 | +    private static final String DATA_STREAM_PREFIX = "my-ds-";  | 
 | 47 | +    private static final String INDEX_PREFIX = "my-index-";  | 
 | 48 | + | 
 | 49 | +    @Param(  | 
 | 50 | +        {  | 
 | 51 | +            // # data streams | # indices  | 
 | 52 | +            "              1000|        100",  | 
 | 53 | +            "              5000|        500",  | 
 | 54 | +            "             10000|       1000" }  | 
 | 55 | +    )  | 
 | 56 | +    public String resourceMix = "100|10";  | 
 | 57 | + | 
 | 58 | +    @Setup  | 
 | 59 | +    public void setUp() {  | 
 | 60 | +        final String[] params = resourceMix.split("\\|");  | 
 | 61 | + | 
 | 62 | +        int numDataStreams = toInt(params[0]);  | 
 | 63 | +        int numIndices = toInt(params[1]);  | 
 | 64 | + | 
 | 65 | +        Metadata.Builder mb = Metadata.builder();  | 
 | 66 | +        String[] indices = new String[numIndices + numDataStreams * (numIndices + 1)];  | 
 | 67 | +        int position = 0;  | 
 | 68 | +        for (int i = 1; i <= numIndices; i++) {  | 
 | 69 | +            String indexName = INDEX_PREFIX + i;  | 
 | 70 | +            createIndexMetadata(indexName, mb);  | 
 | 71 | +            indices[position++] = indexName;  | 
 | 72 | +        }  | 
 | 73 | + | 
 | 74 | +        for (int i = 1; i <= numDataStreams; i++) {  | 
 | 75 | +            String dataStreamName = DATA_STREAM_PREFIX + i;  | 
 | 76 | +            List<Index> backingIndices = new ArrayList<>();  | 
 | 77 | +            for (int j = 1; j <= numIndices; j++) {  | 
 | 78 | +                String backingIndexName = DataStream.getDefaultBackingIndexName(dataStreamName, j);  | 
 | 79 | +                backingIndices.add(createIndexMetadata(backingIndexName, mb).getIndex());  | 
 | 80 | +                indices[position++] = backingIndexName;  | 
 | 81 | +            }  | 
 | 82 | +            indices[position++] = dataStreamName;  | 
 | 83 | +            mb.put(DataStream.builder(dataStreamName, backingIndices).build());  | 
 | 84 | +        }  | 
 | 85 | +        int mid = indices.length / 2;  | 
 | 86 | +        clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(mb).build();  | 
 | 87 | +        resolver = new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY), new SystemIndices(List.of()));  | 
 | 88 | +        indexListRequest = new Request(IndicesOptions.lenientExpandOpenHidden(), indices);  | 
 | 89 | +        starRequest = new Request(IndicesOptions.lenientExpandOpenHidden(), "*");  | 
 | 90 | +        String[] mixed = indices.clone();  | 
 | 91 | +        mixed[mid] = "my-*";  | 
 | 92 | +        mixedRequest = new Request(IndicesOptions.lenientExpandOpenHidden(), mixed);  | 
 | 93 | +    }  | 
 | 94 | + | 
 | 95 | +    private IndexMetadata createIndexMetadata(String indexName, Metadata.Builder mb) {  | 
 | 96 | +        IndexMetadata indexMetadata = IndexMetadata.builder(indexName)  | 
 | 97 | +            .settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()))  | 
 | 98 | +            .numberOfShards(1)  | 
 | 99 | +            .numberOfReplicas(0)  | 
 | 100 | +            .build();  | 
 | 101 | +        mb.put(indexMetadata, false);  | 
 | 102 | +        return indexMetadata;  | 
 | 103 | +    }  | 
 | 104 | + | 
 | 105 | +    private IndexNameExpressionResolver resolver;  | 
 | 106 | +    private ClusterState clusterState;  | 
 | 107 | +    private Request starRequest;  | 
 | 108 | +    private Request indexListRequest;  | 
 | 109 | +    private Request mixedRequest;  | 
 | 110 | + | 
 | 111 | +    @Benchmark  | 
 | 112 | +    public String[] resolveResourcesListToConcreteIndices() {  | 
 | 113 | +        return resolver.concreteIndexNames(clusterState, indexListRequest);  | 
 | 114 | +    }  | 
 | 115 | + | 
 | 116 | +    @Benchmark  | 
 | 117 | +    public String[] resolveAllStarToConcreteIndices() {  | 
 | 118 | +        return resolver.concreteIndexNames(clusterState, starRequest);  | 
 | 119 | +    }  | 
 | 120 | + | 
 | 121 | +    @Benchmark  | 
 | 122 | +    public String[] resolveMixedConcreteIndices() {  | 
 | 123 | +        return resolver.concreteIndexNames(clusterState, mixedRequest);  | 
 | 124 | +    }  | 
 | 125 | + | 
 | 126 | +    private int toInt(String v) {  | 
 | 127 | +        return Integer.parseInt(v.trim());  | 
 | 128 | +    }  | 
 | 129 | + | 
 | 130 | +    record Request(IndicesOptions indicesOptions, String... indices) implements IndicesRequest {  | 
 | 131 | + | 
 | 132 | +    }  | 
 | 133 | +}  | 
0 commit comments