-
Notifications
You must be signed in to change notification settings - Fork 46
[WIP] Add JMH benchmarks for conjure map deserialization #2747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
aldexis
wants to merge
5
commits into
develop
Choose a base branch
from
ald/jmh
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| apply plugin: 'me.champeau.jmh' | ||
| apply plugin: 'org.revapi.revapi-gradle-plugin' | ||
|
|
||
| jmh { | ||
| // Use profilers to collect additional data. Supported profilers: | ||
| // [cl, comp, gc, stack, perf, perfnorm, perfasm, xperf, xperfasm, hs_cl, hs_comp, hs_gc, hs_rt, hs_thr] | ||
| profilers = ['gc'] | ||
| } | ||
|
|
||
| dependencies { | ||
| jmh 'org.openjdk.jmh:jmh-core' | ||
|
|
||
| jmh 'com.fasterxml.jackson.core:jackson-annotations' | ||
| jmh 'com.fasterxml.jackson.core:jackson-databind' | ||
| jmh 'com.google.code.findbugs:jsr305' | ||
| jmh 'com.google.guava:guava' | ||
| jmh 'com.palantir.conjure.java.runtime:conjure-java-jackson-serialization' | ||
| jmh 'com.palantir.safe-logging:preconditions' | ||
|
|
||
| jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess' | ||
| } |
297 changes: 297 additions & 0 deletions
297
conjure-jmh/src/jmh/java/com/palantir/conjure/ConjureBenchmarks.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,297 @@ | ||
| /* | ||
| * (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.palantir.conjure; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonSetter; | ||
| import com.fasterxml.jackson.annotation.Nulls; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.errorprone.annotations.CheckReturnValue; | ||
| import com.palantir.conjure.java.serialization.ObjectMappers; | ||
| import com.palantir.logsafe.Preconditions; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Collections; | ||
| import java.util.Iterator; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
| import java.util.concurrent.TimeUnit; | ||
| import javax.annotation.Nullable; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.profile.GCProfiler; | ||
| import org.openjdk.jmh.runner.Runner; | ||
| import org.openjdk.jmh.runner.options.Options; | ||
| import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
|
||
| @State(Scope.Benchmark) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
| @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | ||
| @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) | ||
| @Fork(1) | ||
| @SuppressWarnings({"NullAway", "designforextension", "checkstyle:RegexpSinglelineJava", "checkstyle:VisibilityModifier" | ||
| }) | ||
| public class ConjureBenchmarks { | ||
|
|
||
| private static final ObjectMapper mapper = ObjectMappers.newClientJsonMapper(); | ||
|
|
||
| @Setup | ||
| public void before() { | ||
| // Ensure we have enough retained capacity to avoid measuring resizing costs. | ||
| // Each operation takes at least ~100-200ns, so 10 million ensures we have enough for all benchmark iterations. | ||
| MemoryProfiler.ensureRetainedCapacity(10_000_000); | ||
| } | ||
|
|
||
| @SuppressWarnings("ImmutableEnumChecker") | ||
| public enum RawJson { | ||
| NO_MAP("{}"), | ||
| EMPTY(generateMap(0)), | ||
| SINGLETON(generateMap(1)), | ||
| THREE(generateMap(3)), | ||
| SIX(generateMap(6)), | ||
| TEN(generateMap(10)), | ||
| HUNDRED(generateMap(100)), | ||
| THOUSAND(generateMap(1000)), | ||
| ; | ||
|
|
||
| private static String generateMap(int count) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("{\"map\":{"); | ||
| for (int i = 1; i <= count; i++) { | ||
| sb.append("\"key").append(i).append("\":\"value").append(i).append("\""); | ||
| if (i < count) { | ||
| sb.append(","); | ||
| } | ||
| } | ||
| sb.append("}}"); | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| private final byte[] json; | ||
|
|
||
| RawJson(String jsonString) { | ||
| this.json = jsonString.getBytes(StandardCharsets.UTF_8); | ||
| } | ||
| } | ||
|
|
||
| public enum MapImplementation { | ||
| NORMAL(NormalMap.class), | ||
| SINGLETON(SingletonMap.class), | ||
| GUAVA_IMMUTABLE(GuavaImMap.class); | ||
|
|
||
| private final Class<?> clazz; | ||
|
|
||
| MapImplementation(Class<?> clazz) { | ||
| this.clazz = clazz; | ||
| } | ||
| } | ||
|
|
||
| @Param | ||
| public RawJson json; | ||
|
|
||
| @Param | ||
| public MapImplementation mapImpl; | ||
|
|
||
| @Benchmark | ||
| public void testAllocatingBenchmark() throws IOException { | ||
| MemoryProfiler.addRetained(mapper.readValue(json.json, mapImpl.clazz)); | ||
| } | ||
|
|
||
| @JsonDeserialize(builder = NormalMap.Builder.class) | ||
| public static final class NormalMap { | ||
| private final Map<String, String> map; | ||
|
|
||
| private NormalMap(Map<String, String> map) { | ||
| this.map = Collections.unmodifiableMap(map); | ||
| } | ||
|
|
||
| public Map<String, String> getMap() { | ||
| return map; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static final class Builder { | ||
| private boolean buildInvoked; | ||
|
|
||
| private Map<String, String> map = new LinkedHashMap<>(); | ||
|
|
||
| private Builder() {} | ||
|
|
||
| @JsonSetter(value = "map", nulls = Nulls.SKIP, contentNulls = Nulls.FAIL) | ||
| public Builder map(Map<String, String> newMap) { | ||
| checkNotBuilt(); | ||
| this.map = new LinkedHashMap<>(Preconditions.checkNotNull(newMap, "map cannot be null")); | ||
| return this; | ||
| } | ||
|
|
||
| @CheckReturnValue | ||
| public NormalMap build() { | ||
| checkNotBuilt(); | ||
| this.buildInvoked = true; | ||
| return new NormalMap(map); | ||
| } | ||
|
|
||
| private void checkNotBuilt() { | ||
| Preconditions.checkState(!buildInvoked, "Build has already been called"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static <K, V> Map<K, V> specialUnmodifiableMap(Map<K, V> in) { | ||
| if (in.isEmpty()) { | ||
| return Map.of(); | ||
| } | ||
| if (in.size() == 1) { | ||
| Iterator<Entry<K, V>> itr = in.entrySet().iterator(); | ||
| if (itr.hasNext()) { | ||
| Entry<K, V> entry = itr.next(); | ||
| if (!itr.hasNext()) { | ||
| return Map.of(entry.getKey(), entry.getValue()); | ||
| } | ||
| } | ||
| } | ||
| return Collections.unmodifiableMap(in); | ||
| } | ||
|
|
||
| @JsonDeserialize(builder = SingletonMap.Builder.class) | ||
| public static final class SingletonMap { | ||
| private final Map<String, String> map; | ||
|
|
||
| private SingletonMap(Map<String, String> map) { | ||
| this.map = specialUnmodifiableMap(map); | ||
| } | ||
|
|
||
| public Map<String, String> getMap() { | ||
| return map; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static final class Builder { | ||
| private boolean buildInvoked; | ||
|
|
||
| private Map<String, String> map = new LinkedHashMap<>(); | ||
|
|
||
| private Builder() {} | ||
|
|
||
| @JsonSetter(value = "map", nulls = Nulls.SKIP, contentNulls = Nulls.FAIL) | ||
| public Builder map(Map<String, String> newMap) { | ||
| checkNotBuilt(); | ||
| this.map = new LinkedHashMap<>(Preconditions.checkNotNull(newMap, "map cannot be null")); | ||
| return this; | ||
| } | ||
|
|
||
| @CheckReturnValue | ||
| public SingletonMap build() { | ||
| checkNotBuilt(); | ||
| this.buildInvoked = true; | ||
| return new SingletonMap(map); | ||
| } | ||
|
|
||
| private void checkNotBuilt() { | ||
| Preconditions.checkState(!buildInvoked, "Build has already been called"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @JsonDeserialize(builder = GuavaImMap.Builder.class) | ||
| @NullMarked | ||
| public static final class GuavaImMap { | ||
| private final Map<String, String> map; | ||
|
|
||
| private GuavaImMap(ImmutableMap<String, String> map) { | ||
| this.map = map; | ||
| } | ||
|
|
||
| public Map<String, String> getMap() { | ||
| return map; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static final class Builder { | ||
| private boolean buildInvoked; | ||
|
|
||
| @Nullable | ||
| private Object map; | ||
|
|
||
| private Builder() {} | ||
|
|
||
| @JsonSetter(value = "map", nulls = Nulls.SKIP, contentNulls = Nulls.FAIL) | ||
| @JsonDeserialize(as = ImmutableMap.class) | ||
| public Builder map(Map<String, String> newMap) { | ||
| checkNotBuilt(); | ||
| this.map = newMap instanceof ImmutableMap<String, String> im | ||
| ? im | ||
| : ImmutableMap.<String, String>builder().putAll(newMap); | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings({"rawtypes", "unchecked"}) | ||
| @CheckReturnValue | ||
| public GuavaImMap build() { | ||
| checkNotBuilt(); | ||
| this.buildInvoked = true; | ||
| ImmutableMap<String, String> finalMap; | ||
| if (map == null) { | ||
| finalMap = ImmutableMap.of(); | ||
| } else if (map instanceof ImmutableMap im) { | ||
| finalMap = im; | ||
| } else if (map instanceof ImmutableMap.Builder builder) { | ||
| finalMap = builder.buildOrThrow(); | ||
| } else { | ||
| throw new IllegalStateException("Unexpected map type: " + map.getClass()); | ||
| } | ||
| return new GuavaImMap(finalMap); | ||
| } | ||
|
|
||
| private void checkNotBuilt() { | ||
| Preconditions.checkState(!buildInvoked, "Build has already been called"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] _args) throws Exception { | ||
| Options opt = new OptionsBuilder() | ||
| .include(ConjureBenchmarks.class.getSimpleName()) | ||
| .addProfiler(GCProfiler.class) | ||
| .addProfiler(MemoryProfiler.class) | ||
| .build(); | ||
|
|
||
| new Runner(opt).run(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why, but I can't seem to run in IntelliJ with runtime = 25