Skip to content

Commit 1d498f6

Browse files
committed
Apply performance improvement from Tags to KeyValues
See gh-4959. Resolves gh-5140
1 parent 6d846a0 commit 1d498f6

File tree

3 files changed

+280
-30
lines changed

3 files changed

+280
-30
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.KeyValues;
19+
import org.openjdk.jmh.annotations.*;
20+
import org.openjdk.jmh.runner.Runner;
21+
import org.openjdk.jmh.runner.RunnerException;
22+
import org.openjdk.jmh.runner.options.Options;
23+
import org.openjdk.jmh.runner.options.OptionsBuilder;
24+
25+
import java.util.concurrent.TimeUnit;
26+
27+
@Fork(1)
28+
@Measurement(iterations = 2)
29+
@Warmup(iterations = 2)
30+
@BenchmarkMode(Mode.AverageTime)
31+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
32+
@State(Scope.Thread)
33+
public class KeyValuesMergeBenchmark {
34+
35+
static final KeyValues left = KeyValues.of("key", "value", "key2", "value2", "key6", "value6", "key7", "value7",
36+
"key8", "value8", "keyA", "valueA", "keyC", "valueC", "keyE", "valueE", "keyF", "valueF", "keyG", "valueG",
37+
"keyG", "valueG", "keyG", "valueG", "keyH", "valueH");
38+
39+
static final KeyValues right = KeyValues.of("key", "value", "key1", "value1", "key2", "value2", "key3", "value3",
40+
"key4", "value4", "key5", "value5", "keyA", "valueA", "keyB", "valueB", "keyD", "valueD");
41+
42+
@Benchmark
43+
public KeyValues mergeKeyValues() {
44+
return left.and(right);
45+
}
46+
47+
public static void main(String[] args) throws RunnerException {
48+
Options opt = new OptionsBuilder().include(KeyValuesMergeBenchmark.class.getSimpleName()).build();
49+
new Runner(opt).run();
50+
}
51+
52+
}

0 commit comments

Comments
 (0)