Skip to content

Commit 416b522

Browse files
committed
Add benchmarks
1 parent 6ae82a4 commit 416b522

File tree

2 files changed

+110
-17
lines changed

2 files changed

+110
-17
lines changed

benchmarks/benchmarks-core/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ plugins {
1212

1313
dependencies {
1414
jmh project(':micrometer-core')
15-
// jmh 'io.micrometer:micrometer-core:1.13.0-M2'
15+
// jmh 'io.micrometer:micrometer-core:1.13.14'
1616
jmh project(':micrometer-registry-prometheus')
17-
// jmh 'io.micrometer:micrometer-registry-prometheus:1.13.0-M2'
17+
// jmh 'io.micrometer:micrometer-registry-prometheus:1.13.14'
1818

1919
jmh libs.dropwizardMetricsCore5
2020
jmh libs.prometheusMetrics

benchmarks/benchmarks-core/src/jmh/java/io/micrometer/benchmark/core/TagsBenchmark.java

Lines changed: 108 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,128 @@
1515
*/
1616
package io.micrometer.benchmark.core;
1717

18+
import io.micrometer.core.instrument.Tag;
1819
import io.micrometer.core.instrument.Tags;
1920
import org.openjdk.jmh.annotations.*;
21+
import org.openjdk.jmh.profile.GCProfiler;
2022
import org.openjdk.jmh.runner.Runner;
2123
import org.openjdk.jmh.runner.RunnerException;
2224
import org.openjdk.jmh.runner.options.Options;
2325
import org.openjdk.jmh.runner.options.OptionsBuilder;
2426

27+
import java.util.ArrayList;
28+
import java.util.Collections;
29+
import java.util.List;
2530
import java.util.concurrent.TimeUnit;
2631

27-
@Fork(1)
28-
@Measurement(iterations = 2)
29-
@Warmup(iterations = 2)
30-
@BenchmarkMode(Mode.AverageTime)
31-
@OutputTimeUnit(TimeUnit.NANOSECONDS)
3232
public class TagsBenchmark {
3333

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+
}
3885

39-
@Benchmark
40-
public Tags dotAnd() {
41-
return Tags.of("key", "value").and("key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5");
4286
}
4387

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+
47140
}
48141

49142
}

0 commit comments

Comments
 (0)