|
| 1 | +/* |
| 2 | + * Copyright 2024 VMware, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micrometer.benchmark.core; |
| 17 | + |
| 18 | +import io.micrometer.common.KeyValue; |
| 19 | +import io.micrometer.common.KeyValues; |
| 20 | +import org.openjdk.jmh.annotations.*; |
| 21 | +import org.openjdk.jmh.runner.Runner; |
| 22 | +import org.openjdk.jmh.runner.RunnerException; |
| 23 | +import org.openjdk.jmh.runner.options.Options; |
| 24 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 25 | + |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +@Fork(1) |
| 29 | +@Measurement(iterations = 2) |
| 30 | +@Warmup(iterations = 2) |
| 31 | +@BenchmarkMode(Mode.AverageTime) |
| 32 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 33 | +public class KeyValuesBenchmark { |
| 34 | + |
| 35 | + static final KeyValue[] orderedKeyValuesSet10 = new KeyValue[] { KeyValue.of("key0", "value"), |
| 36 | + KeyValue.of("key1", "value"), KeyValue.of("key2", "value"), KeyValue.of("key3", "value"), |
| 37 | + KeyValue.of("key4", "value"), KeyValue.of("key5", "value"), KeyValue.of("key6", "value"), |
| 38 | + KeyValue.of("key7", "value"), KeyValue.of("key8", "value"), KeyValue.of("key9", "value") }; |
| 39 | + |
| 40 | + static final KeyValue[] orderedKeyValuesSet4 = new KeyValue[] { KeyValue.of("key0", "value"), |
| 41 | + KeyValue.of("key1", "value"), KeyValue.of("key2", "value"), KeyValue.of("key3", "value"), }; |
| 42 | + |
| 43 | + static final KeyValue[] orderedKeyValuesSet2 = new KeyValue[] { KeyValue.of("key0", "value"), |
| 44 | + KeyValue.of("key1", "value"), }; |
| 45 | + |
| 46 | + static final KeyValue[] unorderedKeyValuesSet10 = new KeyValue[] { KeyValue.of("key1", "value"), |
| 47 | + KeyValue.of("key2", "value"), KeyValue.of("key3", "value"), KeyValue.of("key4", "value"), |
| 48 | + KeyValue.of("key5", "value"), KeyValue.of("key6", "value"), KeyValue.of("key7", "value"), |
| 49 | + KeyValue.of("key8", "value"), KeyValue.of("key9", "value"), KeyValue.of("key0", "value") }; |
| 50 | + |
| 51 | + static final KeyValue[] unorderedKeyValuesSet4 = new KeyValue[] { KeyValue.of("key1", "value"), |
| 52 | + KeyValue.of("key2", "value"), KeyValue.of("key3", "value"), KeyValue.of("key0", "value"), }; |
| 53 | + |
| 54 | + static final KeyValue[] unorderedKeyValuesSet2 = new KeyValue[] { KeyValue.of("key1", "value"), |
| 55 | + KeyValue.of("key0", "value") }; |
| 56 | + |
| 57 | + @Benchmark |
| 58 | + public KeyValues KeyValuesOfOrderedKeyValuesSet10() { |
| 59 | + return KeyValues.of(orderedKeyValuesSet10); |
| 60 | + } |
| 61 | + |
| 62 | + @Benchmark |
| 63 | + public KeyValues KeyValuesOfOrderedKeyValuesSet4() { |
| 64 | + return KeyValues.of(orderedKeyValuesSet4); |
| 65 | + } |
| 66 | + |
| 67 | + @Benchmark |
| 68 | + public KeyValues KeyValuesOfOrderedKeyValuesSet2() { |
| 69 | + return KeyValues.of(orderedKeyValuesSet2); |
| 70 | + } |
| 71 | + |
| 72 | + @Benchmark |
| 73 | + public KeyValues KeyValuesOfUnorderedKeyValuesSet10() { |
| 74 | + return KeyValues.of(unorderedKeyValuesSet10); |
| 75 | + } |
| 76 | + |
| 77 | + @Benchmark |
| 78 | + public KeyValues KeyValuesOfUnorderedKeyValuesSet4() { |
| 79 | + return KeyValues.of(unorderedKeyValuesSet4); |
| 80 | + } |
| 81 | + |
| 82 | + @Benchmark |
| 83 | + public KeyValues KeyValuesOfUnorderedKeyValuesSet2() { |
| 84 | + return KeyValues.of(unorderedKeyValuesSet2); |
| 85 | + } |
| 86 | + |
| 87 | + @Benchmark |
| 88 | + public KeyValues of() { |
| 89 | + return KeyValues.of("key", "value", "key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5"); |
| 90 | + } |
| 91 | + |
| 92 | + @Benchmark |
| 93 | + public KeyValues dotAnd() { |
| 94 | + return KeyValues.of("key", "value").and("key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5"); |
| 95 | + } |
| 96 | + |
| 97 | + public static void main(String[] args) throws RunnerException { |
| 98 | + Options opt = new OptionsBuilder().include(KeyValuesBenchmark.class.getSimpleName()).build(); |
| 99 | + new Runner(opt).run(); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments