forked from boskworks/bosk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Memoize compiled Jackson (de)serializers.
I'm not sure why I need to memoize these myself; I thought Jackson would do it. This reduces the benchmark score from about 7700us to 170us, a 45x improvement.
- Loading branch information
Showing
4 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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
107 changes: 107 additions & 0 deletions
107
bosk-jackson/src/test/java/works/bosk/jackson/JacksonRoundTripBenchmark.java
This file contains 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,107 @@ | ||
package works.bosk.jackson; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.TearDown; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import works.bosk.AbstractBoskTest; | ||
import works.bosk.AbstractRoundTripTest; | ||
import works.bosk.Bosk; | ||
import works.bosk.BoskDriver; | ||
import works.bosk.DriverStack; | ||
import works.bosk.Identifier; | ||
import works.bosk.Path; | ||
import works.bosk.Reference; | ||
import works.bosk.exceptions.InvalidTypeException; | ||
|
||
import static java.util.concurrent.TimeUnit.MICROSECONDS; | ||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
import static java.util.concurrent.TimeUnit.NANOSECONDS; | ||
import static org.openjdk.jmh.annotations.Mode.AverageTime; | ||
import static works.bosk.jackson.JacksonPluginConfiguration.defaultConfiguration; | ||
|
||
@Fork(0) | ||
@Warmup(iterations = 5, time = 1) | ||
@Measurement(iterations = 10, time = 1) | ||
@OutputTimeUnit(MICROSECONDS) | ||
public class JacksonRoundTripBenchmark extends AbstractRoundTripTest { | ||
|
||
@State(Scope.Benchmark) | ||
public static class BenchmarkState { | ||
private Bosk<TestRoot> bosk; | ||
private JacksonPlugin jacksonPlugin; | ||
private ObjectMapper mapper; | ||
private BoskDriver driver; | ||
private BoskDriver downstreamDriver; | ||
private Bosk<TestRoot>.ReadContext context; | ||
private Reference<TestRoot> rootRef; | ||
private Reference<TestEnum> ref5Segments; | ||
private TestRoot root1, root2; | ||
private ThreadLocal<TestRoot> threadLocalRoot; | ||
|
||
final Identifier parentID = Identifier.from("parent"); | ||
final Identifier child1ID = Identifier.from("child1"); | ||
|
||
@Setup(Level.Trial) | ||
public void setup() throws InvalidTypeException, JsonProcessingException { | ||
AtomicReference<BoskDriver> downstreamRef = new AtomicReference<>(); | ||
this.bosk = setUpBosk(DriverStack.of( | ||
jacksonRoundTripFactory(defaultConfiguration()), | ||
(b,d) -> { | ||
downstreamRef.set(d); | ||
return d; | ||
} | ||
)); | ||
this.driver = bosk.driver(); | ||
this.downstreamDriver = downstreamRef.get(); | ||
this.jacksonPlugin = new JacksonPlugin(); | ||
this.mapper = new ObjectMapper().registerModule(jacksonPlugin.moduleFor(bosk)); | ||
context = bosk.readContext(); | ||
rootRef = bosk.rootReference(); | ||
TestRoot localRoot = root1 = rootRef.value(); | ||
threadLocalRoot = ThreadLocal.withInitial(() -> localRoot); | ||
ref5Segments = bosk.rootReference().then(TestEnum.class, Path.of( | ||
TestRoot.Fields.entities, "parent", | ||
AbstractBoskTest.TestEntity.Fields.children, "child1", | ||
AbstractBoskTest.TestChild.Fields.testEnum | ||
)); | ||
|
||
// Make a separate identical state object, cloning via JSON | ||
String json = mapper.writerFor(rootRef.targetClass()).writeValueAsString(root1); | ||
root2 = mapper.readerFor(rootRef.targetClass()).readValue(json); | ||
} | ||
|
||
@TearDown(Level.Trial) | ||
public void closeReadContext() { | ||
context.close(); | ||
} | ||
|
||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(AverageTime) | ||
public void replacementOverhead(BenchmarkState state) { | ||
state.downstreamDriver.submitReplacement(state.rootRef, state.root2); | ||
state.downstreamDriver.submitReplacement(state.rootRef, state.root1); | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(AverageTime) | ||
public void replacement(BenchmarkState state) { | ||
state.driver.submitReplacement(state.rootRef, state.root2); | ||
state.driver.submitReplacement(state.rootRef, state.root1); | ||
} | ||
|
||
} |