|
15 | 15 | */ |
16 | 16 | package io.micrometer.benchmark.core; |
17 | 17 |
|
| 18 | +import io.micrometer.core.instrument.Tag; |
18 | 19 | import io.micrometer.core.instrument.Tags; |
19 | 20 | import org.openjdk.jmh.annotations.*; |
| 21 | +import org.openjdk.jmh.profile.GCProfiler; |
20 | 22 | import org.openjdk.jmh.runner.Runner; |
21 | 23 | import org.openjdk.jmh.runner.RunnerException; |
22 | 24 | import org.openjdk.jmh.runner.options.Options; |
23 | 25 | import org.openjdk.jmh.runner.options.OptionsBuilder; |
24 | 26 |
|
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
25 | 30 | import java.util.concurrent.TimeUnit; |
26 | 31 |
|
27 | | -@Fork(1) |
28 | | -@Measurement(iterations = 2) |
29 | | -@Warmup(iterations = 2) |
30 | | -@BenchmarkMode(Mode.AverageTime) |
31 | | -@OutputTimeUnit(TimeUnit.NANOSECONDS) |
32 | 32 | public class TagsBenchmark { |
33 | 33 |
|
34 | | - @Benchmark |
35 | | - public Tags of() { |
36 | | - return Tags.of("key", "value", "key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5"); |
37 | | - } |
| 34 | + @Fork(1) |
| 35 | + @Measurement(iterations = 2) |
| 36 | + @Warmup(iterations = 2) |
| 37 | + @BenchmarkMode(Mode.AverageTime) |
| 38 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 39 | + public static class TagsOfBenchmark { |
| 40 | + |
| 41 | + @Benchmark |
| 42 | + public Tags ofStringVarargs() { |
| 43 | + return Tags.of("key", "value", "key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5"); |
| 44 | + } |
| 45 | + |
| 46 | + @Benchmark |
| 47 | + public Tags ofTagVarargs() { |
| 48 | + return Tags.of(Tag.of("key", "value"), Tag.of("key2", "value2"), Tag.of("key3", "value3"), |
| 49 | + Tag.of("key4", "value4"), Tag.of("key5", "value5")); |
| 50 | + } |
| 51 | + |
| 52 | + @Benchmark |
| 53 | + public Tags ofTags() { |
| 54 | + return Tags |
| 55 | + .of(Tags.of("key", "value", "key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5")); |
| 56 | + } |
| 57 | + |
| 58 | + @Benchmark |
| 59 | + public Tags ofArrayList() { |
| 60 | + List<Tag> tags = new ArrayList<>(5); |
| 61 | + tags.add(Tag.of("key", "value")); |
| 62 | + tags.add(Tag.of("key2", "value2")); |
| 63 | + tags.add(Tag.of("key3", "value3")); |
| 64 | + tags.add(Tag.of("key4", "value4")); |
| 65 | + tags.add(Tag.of("key5", "value5")); |
| 66 | + return Tags.of(tags); |
| 67 | + } |
| 68 | + |
| 69 | + @Benchmark |
| 70 | + public Tags ofEmptyCollection() { |
| 71 | + return Tags.of(Collections.emptyList()); |
| 72 | + } |
| 73 | + |
| 74 | + @Benchmark |
| 75 | + public Tags ofEmptyTags() { |
| 76 | + return Tags.of(Tags.empty()); |
| 77 | + } |
| 78 | + |
| 79 | + public static void main(String[] args) throws RunnerException { |
| 80 | + Options opt = new OptionsBuilder().include(TagsOfBenchmark.class.getSimpleName()) |
| 81 | + .addProfiler(GCProfiler.class) |
| 82 | + .build(); |
| 83 | + new Runner(opt).run(); |
| 84 | + } |
38 | 85 |
|
39 | | - @Benchmark |
40 | | - public Tags dotAnd() { |
41 | | - return Tags.of("key", "value").and("key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5"); |
42 | 86 | } |
43 | 87 |
|
44 | | - public static void main(String[] args) throws RunnerException { |
45 | | - Options opt = new OptionsBuilder().include(TagsBenchmark.class.getSimpleName()).build(); |
46 | | - new Runner(opt).run(); |
| 88 | + @Fork(1) |
| 89 | + @Measurement(iterations = 3) |
| 90 | + @Warmup(iterations = 5) |
| 91 | + @BenchmarkMode(Mode.AverageTime) |
| 92 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 93 | + public static class TagsAndBenchmark { |
| 94 | + |
| 95 | + @Benchmark |
| 96 | + public Tags andVarargsString() { // allocating more; 360 B/op vs 336 on 1.13.14 |
| 97 | + return Tags.of("key", "value").and("key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5"); |
| 98 | + } |
| 99 | + |
| 100 | + @Benchmark |
| 101 | + public Tags andTagVarargs() { |
| 102 | + return Tags.of("key", "value") |
| 103 | + .and(Tag.of("key2", "value2"), Tag.of("key3", "value3"), Tag.of("key4", "value4"), |
| 104 | + Tag.of("key5", "value5")); |
| 105 | + } |
| 106 | + |
| 107 | + @Benchmark |
| 108 | + public Tags andTags() { // allocating more; 360 B/op vs 336 on 1.13.14 |
| 109 | + return Tags.of("key", "value") |
| 110 | + .and(Tags.of("key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5")); |
| 111 | + } |
| 112 | + |
| 113 | + @Benchmark |
| 114 | + public Tags andArrayList() { |
| 115 | + List<Tag> tags = new ArrayList<>(4); |
| 116 | + tags.add(Tag.of("key2", "value2")); |
| 117 | + tags.add(Tag.of("key3", "value3")); |
| 118 | + tags.add(Tag.of("key4", "value4")); |
| 119 | + tags.add(Tag.of("key5", "value5")); |
| 120 | + return Tags.of("key", "value").and(tags); |
| 121 | + } |
| 122 | + |
| 123 | + @Benchmark |
| 124 | + public Tags andEmptyCollection() { |
| 125 | + return Tags.of("key", "value").and(Collections.emptyList()); |
| 126 | + } |
| 127 | + |
| 128 | + @Benchmark |
| 129 | + public Tags andEmptyTags() { |
| 130 | + return Tags.of("key", "value").and(Tags.empty()); |
| 131 | + } |
| 132 | + |
| 133 | + public static void main(String[] args) throws RunnerException { |
| 134 | + Options opt = new OptionsBuilder().include(TagsAndBenchmark.class.getSimpleName()) |
| 135 | + .addProfiler(GCProfiler.class) |
| 136 | + .build(); |
| 137 | + new Runner(opt).run(); |
| 138 | + } |
| 139 | + |
47 | 140 | } |
48 | 141 |
|
49 | 142 | } |
0 commit comments