|
| 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.common; |
| 11 | + |
| 12 | +import org.elasticsearch.TransportVersion; |
| 13 | +import org.elasticsearch.cluster.ClusterModule; |
| 14 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 15 | +import org.elasticsearch.common.compress.CompressedXContent; |
| 16 | +import org.elasticsearch.common.logging.LogConfigurator; |
| 17 | +import org.elasticsearch.common.settings.IndexScopedSettings; |
| 18 | +import org.elasticsearch.common.settings.Setting; |
| 19 | +import org.elasticsearch.common.settings.Settings; |
| 20 | +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; |
| 21 | +import org.elasticsearch.index.IndexSettings; |
| 22 | +import org.elasticsearch.index.IndexVersion; |
| 23 | +import org.elasticsearch.index.analysis.IndexAnalyzers; |
| 24 | +import org.elasticsearch.index.cache.bitset.BitsetFilterCache; |
| 25 | +import org.elasticsearch.index.mapper.MapperMetrics; |
| 26 | +import org.elasticsearch.index.mapper.MapperRegistry; |
| 27 | +import org.elasticsearch.index.mapper.MapperService; |
| 28 | +import org.elasticsearch.index.mapper.ProvidedIdFieldMapper; |
| 29 | +import org.elasticsearch.index.similarity.SimilarityService; |
| 30 | +import org.elasticsearch.indices.IndicesModule; |
| 31 | +import org.elasticsearch.script.Script; |
| 32 | +import org.elasticsearch.script.ScriptCompiler; |
| 33 | +import org.elasticsearch.script.ScriptContext; |
| 34 | +import org.elasticsearch.xcontent.NamedXContentRegistry; |
| 35 | +import org.elasticsearch.xcontent.XContentParserConfiguration; |
| 36 | +import org.elasticsearch.xpack.logsdb.LogsDBPlugin; |
| 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.Param; |
| 44 | +import org.openjdk.jmh.annotations.Scope; |
| 45 | +import org.openjdk.jmh.annotations.Setup; |
| 46 | +import org.openjdk.jmh.annotations.State; |
| 47 | +import org.openjdk.jmh.annotations.Warmup; |
| 48 | + |
| 49 | +import java.io.IOException; |
| 50 | +import java.util.ArrayList; |
| 51 | +import java.util.HashSet; |
| 52 | +import java.util.List; |
| 53 | +import java.util.Map; |
| 54 | +import java.util.Random; |
| 55 | +import java.util.Set; |
| 56 | +import java.util.concurrent.TimeUnit; |
| 57 | + |
| 58 | +@Fork(value = 1) |
| 59 | +@Warmup(iterations = 2) |
| 60 | +@Measurement(iterations = 5) |
| 61 | +@BenchmarkMode(Mode.AverageTime) |
| 62 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 63 | +@State(Scope.Benchmark) |
| 64 | +public class MappingParsingBenchmark { |
| 65 | + static { |
| 66 | + // For Elasticsearch900Lucene101Codec: |
| 67 | + LogConfigurator.loadLog4jPlugins(); |
| 68 | + LogConfigurator.configureESLogging(); |
| 69 | + LogConfigurator.setNodeName("test"); |
| 70 | + } |
| 71 | + |
| 72 | + private static final String MAPPING = """ |
| 73 | + { |
| 74 | + "_doc": { |
| 75 | + "dynamic": false, |
| 76 | + "properties": { |
| 77 | + "@timestamp": { |
| 78 | + "type": "date" |
| 79 | + }, |
| 80 | + "host": { |
| 81 | + "type": "object", |
| 82 | + "properties": { |
| 83 | + "name": { |
| 84 | + "type": "keyword" |
| 85 | + } |
| 86 | + } |
| 87 | + }, |
| 88 | + "message": { |
| 89 | + "type": "pattern_text" |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + \s"""; |
| 95 | + |
| 96 | + @Param("1024") |
| 97 | + private int numIndices; |
| 98 | + |
| 99 | + private List<MapperService> mapperServices; |
| 100 | + private CompressedXContent compressedMapping; |
| 101 | + |
| 102 | + private Random random = new Random(); |
| 103 | + private static final String CHARS = "abcdefghijklmnopqrstuvwxyz1234567890"; |
| 104 | + |
| 105 | + private String randomIndexName() { |
| 106 | + StringBuilder b = new StringBuilder(); |
| 107 | + for (int i = 0; i < 10; i++) { |
| 108 | + b.append(CHARS.charAt(random.nextInt(CHARS.length()))); |
| 109 | + } |
| 110 | + return b.toString(); |
| 111 | + } |
| 112 | + |
| 113 | + @Setup |
| 114 | + public void setUp() throws IOException { |
| 115 | + Settings settings = Settings.builder() |
| 116 | + .put("index.number_of_replicas", 0) |
| 117 | + .put("index.number_of_shards", 1) |
| 118 | + .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) |
| 119 | + .put("index.mode", "logsdb") |
| 120 | + .put("index.logsdb.sort_on_host_name", true) |
| 121 | + .put("index.logsdb.sort_on_message_template", true) |
| 122 | + .build(); |
| 123 | + |
| 124 | + LogsDBPlugin logsDBPlugin = new LogsDBPlugin(settings); |
| 125 | + |
| 126 | + Set<Setting<?>> definedSettings = new HashSet<>(IndexScopedSettings.BUILT_IN_INDEX_SETTINGS); |
| 127 | + definedSettings.addAll(logsDBPlugin.getSettings().stream().filter(Setting::hasIndexScope).toList()); |
| 128 | + IndexScopedSettings indexScopedSettings = new IndexScopedSettings(Settings.EMPTY, definedSettings); |
| 129 | + |
| 130 | + mapperServices = new ArrayList<>(numIndices); |
| 131 | + for (int i = 0; i < numIndices; i++) { |
| 132 | + IndexMetadata meta = IndexMetadata.builder(randomIndexName()).settings(settings).build(); |
| 133 | + IndexSettings indexSettings = new IndexSettings(meta, settings, indexScopedSettings); |
| 134 | + MapperRegistry mapperRegistry = new IndicesModule(List.of(logsDBPlugin)).getMapperRegistry(); |
| 135 | + SimilarityService similarityService = new SimilarityService(indexSettings, null, Map.of()); |
| 136 | + BitsetFilterCache bitsetFilterCache = new BitsetFilterCache(indexSettings, BitsetFilterCache.Listener.NOOP); |
| 137 | + MapperService mapperService = new MapperService( |
| 138 | + () -> TransportVersion.current(), |
| 139 | + indexSettings, |
| 140 | + IndexAnalyzers.of(Map.of()), |
| 141 | + XContentParserConfiguration.EMPTY.withRegistry(new NamedXContentRegistry(ClusterModule.getNamedXWriteables())) |
| 142 | + .withDeprecationHandler(LoggingDeprecationHandler.INSTANCE), |
| 143 | + similarityService, |
| 144 | + mapperRegistry, |
| 145 | + () -> { |
| 146 | + throw new UnsupportedOperationException(); |
| 147 | + }, |
| 148 | + new ProvidedIdFieldMapper(() -> true), |
| 149 | + new ScriptCompiler() { |
| 150 | + @Override |
| 151 | + public <T> T compile(Script script, ScriptContext<T> scriptContext) { |
| 152 | + throw new UnsupportedOperationException(); |
| 153 | + } |
| 154 | + }, |
| 155 | + bitsetFilterCache::getBitSetProducer, |
| 156 | + MapperMetrics.NOOP |
| 157 | + ); |
| 158 | + |
| 159 | + mapperServices.add(mapperService); |
| 160 | + } |
| 161 | + |
| 162 | + compressedMapping = new CompressedXContent(MAPPING); |
| 163 | + } |
| 164 | + |
| 165 | + @Benchmark |
| 166 | + public void mappingParsingBenchmark() { |
| 167 | + for (MapperService service : mapperServices) { |
| 168 | + service.merge("_doc", compressedMapping, MapperService.MergeReason.MAPPING_UPDATE); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments