|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.benchmark.search.aggregations.bucket.terms; |
| 21 | + |
| 22 | +import org.apache.lucene.util.BytesRef; |
| 23 | +import org.elasticsearch.common.io.stream.DelayableWriteable; |
| 24 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 25 | +import org.elasticsearch.search.DocValueFormat; |
| 26 | +import org.elasticsearch.search.aggregations.BucketOrder; |
| 27 | +import org.elasticsearch.search.aggregations.InternalAggregation; |
| 28 | +import org.elasticsearch.search.aggregations.InternalAggregations; |
| 29 | +import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; |
| 30 | +import org.openjdk.jmh.annotations.Benchmark; |
| 31 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 32 | +import org.openjdk.jmh.annotations.Fork; |
| 33 | +import org.openjdk.jmh.annotations.Measurement; |
| 34 | +import org.openjdk.jmh.annotations.Mode; |
| 35 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 36 | +import org.openjdk.jmh.annotations.Param; |
| 37 | +import org.openjdk.jmh.annotations.Scope; |
| 38 | +import org.openjdk.jmh.annotations.Setup; |
| 39 | +import org.openjdk.jmh.annotations.State; |
| 40 | +import org.openjdk.jmh.annotations.Warmup; |
| 41 | + |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.List; |
| 44 | +import java.util.concurrent.TimeUnit; |
| 45 | + |
| 46 | +@Fork(2) |
| 47 | +@Warmup(iterations = 10) |
| 48 | +@Measurement(iterations = 5) |
| 49 | +@BenchmarkMode(Mode.AverageTime) |
| 50 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 51 | +@State(Scope.Benchmark) |
| 52 | +public class StringTermsSerializationBenchmark { |
| 53 | + private static final NamedWriteableRegistry REGISTRY = new NamedWriteableRegistry( |
| 54 | + List.of(new NamedWriteableRegistry.Entry(InternalAggregation.class, StringTerms.NAME, StringTerms::new)) |
| 55 | + ); |
| 56 | + @Param(value = { "1000" }) |
| 57 | + private int buckets; |
| 58 | + |
| 59 | + private DelayableWriteable<InternalAggregations> results; |
| 60 | + |
| 61 | + @Setup |
| 62 | + public void initResults() { |
| 63 | + results = DelayableWriteable.referencing(InternalAggregations.from(List.of(newTerms(true)))); |
| 64 | + } |
| 65 | + |
| 66 | + private StringTerms newTerms(boolean withNested) { |
| 67 | + List<StringTerms.Bucket> resultBuckets = new ArrayList<>(buckets); |
| 68 | + for (int i = 0; i < buckets; i++) { |
| 69 | + InternalAggregations inner = withNested ? InternalAggregations.from(List.of(newTerms(false))) : InternalAggregations.EMPTY; |
| 70 | + resultBuckets.add(new StringTerms.Bucket(new BytesRef("test" + i), i, inner, false, 0, DocValueFormat.RAW)); |
| 71 | + } |
| 72 | + return new StringTerms( |
| 73 | + "test", |
| 74 | + BucketOrder.key(true), |
| 75 | + BucketOrder.key(true), |
| 76 | + buckets, |
| 77 | + 1, |
| 78 | + null, |
| 79 | + DocValueFormat.RAW, |
| 80 | + buckets, |
| 81 | + false, |
| 82 | + 100000, |
| 83 | + resultBuckets, |
| 84 | + 0 |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + @Benchmark |
| 89 | + public DelayableWriteable<InternalAggregations> serialize() { |
| 90 | + return results.asSerialized(InternalAggregations::readFrom, REGISTRY); |
| 91 | + } |
| 92 | +} |
0 commit comments