|
| 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 | + |
| 17 | +package io.micrometer.benchmark.core; |
| 18 | + |
| 19 | +import io.micrometer.common.KeyValues; |
| 20 | +import io.micrometer.observation.Observation; |
| 21 | +import io.micrometer.observation.ObservationRegistry; |
| 22 | +import org.openjdk.jmh.annotations.*; |
| 23 | +import org.openjdk.jmh.runner.Runner; |
| 24 | +import org.openjdk.jmh.runner.RunnerException; |
| 25 | +import org.openjdk.jmh.runner.options.Options; |
| 26 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 27 | + |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
| 30 | +@State(Scope.Benchmark) |
| 31 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 32 | +@Threads(4) |
| 33 | +public class ObservationKeyValuesBenchmark { |
| 34 | + |
| 35 | + private static final KeyValues KEY_VALUES = KeyValues.of("key1", "value1", "key2", "value2", "key3", "value3", |
| 36 | + "key4", "value4", "key5", "value5"); |
| 37 | + |
| 38 | + private final ObservationRegistry registry = ObservationRegistry.create(); |
| 39 | + |
| 40 | + private final Observation.Context context = new TestContext(); |
| 41 | + |
| 42 | + private final Observation observation = Observation.createNotStarted("jmh", () -> context, registry); |
| 43 | + |
| 44 | + @Benchmark |
| 45 | + @Group("contended") |
| 46 | + @GroupThreads(1) |
| 47 | + public Observation contendedWrite() { |
| 48 | + return write(); |
| 49 | + } |
| 50 | + |
| 51 | + @Benchmark |
| 52 | + @Group("contended") |
| 53 | + @GroupThreads(1) |
| 54 | + public KeyValues contendedRead() { |
| 55 | + return read(); |
| 56 | + } |
| 57 | + |
| 58 | + @Benchmark |
| 59 | + @Threads(1) |
| 60 | + public Observation uncontendedWrite() { |
| 61 | + return write(); |
| 62 | + } |
| 63 | + |
| 64 | + @Benchmark |
| 65 | + @Threads(1) |
| 66 | + public KeyValues uncontendedRead() { |
| 67 | + return read(); |
| 68 | + } |
| 69 | + |
| 70 | + private Observation write() { |
| 71 | + return observation.lowCardinalityKeyValues(KEY_VALUES); |
| 72 | + } |
| 73 | + |
| 74 | + private KeyValues read() { |
| 75 | + return observation.getContext().getLowCardinalityKeyValues(); |
| 76 | + } |
| 77 | + |
| 78 | + public static void main(String[] args) throws RunnerException { |
| 79 | + Options options = new OptionsBuilder().include(ObservationKeyValuesBenchmark.class.getSimpleName()) |
| 80 | + .warmupIterations(3) |
| 81 | + .measurementIterations(5) |
| 82 | + .mode(Mode.SampleTime) |
| 83 | + .forks(1) |
| 84 | + .build(); |
| 85 | + |
| 86 | + new Runner(options).run(); |
| 87 | + } |
| 88 | + |
| 89 | + static class TestContext extends Observation.Context { |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments