|
| 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.xcontent; |
| 11 | + |
| 12 | +import org.elasticsearch.benchmark.index.mapper.MapperServiceFactory; |
| 13 | +import org.elasticsearch.common.UUIDs; |
| 14 | +import org.elasticsearch.common.bytes.BytesReference; |
| 15 | +import org.elasticsearch.common.logging.LogConfigurator; |
| 16 | +import org.elasticsearch.index.mapper.MapperService; |
| 17 | +import org.elasticsearch.index.mapper.SourceToParse; |
| 18 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 19 | +import org.elasticsearch.xcontent.XContentFactory; |
| 20 | +import org.elasticsearch.xcontent.XContentType; |
| 21 | +import org.openjdk.jmh.annotations.Benchmark; |
| 22 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 23 | +import org.openjdk.jmh.annotations.Fork; |
| 24 | +import org.openjdk.jmh.annotations.Level; |
| 25 | +import org.openjdk.jmh.annotations.Measurement; |
| 26 | +import org.openjdk.jmh.annotations.Mode; |
| 27 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 28 | +import org.openjdk.jmh.annotations.Param; |
| 29 | +import org.openjdk.jmh.annotations.Scope; |
| 30 | +import org.openjdk.jmh.annotations.Setup; |
| 31 | +import org.openjdk.jmh.annotations.State; |
| 32 | +import org.openjdk.jmh.annotations.Threads; |
| 33 | +import org.openjdk.jmh.annotations.Warmup; |
| 34 | +import org.openjdk.jmh.infra.Blackhole; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.Random; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | + |
| 40 | +/** |
| 41 | + * Benchmark to measure indexing performance of keyword fields. Used to measure performance impact of skipping |
| 42 | + * UTF-8 to UTF-16 conversion during document parsing. |
| 43 | + */ |
| 44 | +@BenchmarkMode(Mode.AverageTime) |
| 45 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 46 | +@State(Scope.Benchmark) |
| 47 | +@Fork(1) |
| 48 | +@Threads(1) |
| 49 | +@Warmup(iterations = 1) |
| 50 | +@Measurement(iterations = 5) |
| 51 | +public class OptimizedTextBenchmark { |
| 52 | + static { |
| 53 | + // For Elasticsearch900Lucene101Codec: |
| 54 | + LogConfigurator.loadLog4jPlugins(); |
| 55 | + LogConfigurator.configureESLogging(); |
| 56 | + LogConfigurator.setNodeName("test"); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Total number of documents to index. |
| 61 | + */ |
| 62 | + @Param("1048576") |
| 63 | + private int nDocs; |
| 64 | + |
| 65 | + private MapperService mapperService; |
| 66 | + private SourceToParse[] sources; |
| 67 | + |
| 68 | + private String randomValue(int length) { |
| 69 | + final String CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 70 | + Random random = new Random(); |
| 71 | + StringBuilder builder = new StringBuilder(length); |
| 72 | + for (int i = 0; i < length; i++) { |
| 73 | + builder.append(CHARS.charAt(random.nextInt(CHARS.length()))); |
| 74 | + } |
| 75 | + return builder.toString(); |
| 76 | + } |
| 77 | + |
| 78 | + @Setup(Level.Trial) |
| 79 | + public void setup() throws IOException { |
| 80 | + mapperService = MapperServiceFactory.create(""" |
| 81 | + { |
| 82 | + "_doc": { |
| 83 | + "dynamic": false, |
| 84 | + "properties": { |
| 85 | + "field": { |
| 86 | + "type": "keyword" |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + """); |
| 92 | + |
| 93 | + sources = new SourceToParse[nDocs]; |
| 94 | + for (int i = 0; i < nDocs; i++) { |
| 95 | + XContentBuilder b = XContentFactory.jsonBuilder(); |
| 96 | + b.startObject().field("field", randomValue(8)).endObject(); |
| 97 | + sources[i] = new SourceToParse(UUIDs.randomBase64UUID(), BytesReference.bytes(b), XContentType.JSON); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Benchmark |
| 102 | + public void indexDocuments(final Blackhole bh) { |
| 103 | + final var mapper = mapperService.documentMapper(); |
| 104 | + for (int i = 0; i < nDocs; i++) { |
| 105 | + bh.consume(mapper.parse(sources[i])); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments