generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #897 from micronaut-projects/thrash-test
Add type thrashing test
- Loading branch information
Showing
5 changed files
with
163 additions
and
8 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
60 changes: 60 additions & 0 deletions
60
benchmarks/src/jmh/java/io/micronaut/serde/ComboBenchmark.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,60 @@ | ||
package io.micronaut.serde; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.core.type.Argument; | ||
import io.micronaut.json.JsonMapper; | ||
import io.micronaut.serde.data.StringArrayField; | ||
import io.micronaut.serde.jackson.JacksonJsonMapper; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
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.TearDown; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.io.IOException; | ||
|
||
public class ComboBenchmark { | ||
|
||
@State(Scope.Thread) | ||
public static class Holder { | ||
@Param | ||
TestCase testCase; | ||
|
||
JsonMapper jsonMapper; | ||
ApplicationContext ctx; | ||
|
||
@Setup | ||
public void setUp() { | ||
ctx = ApplicationContext.run(); | ||
jsonMapper = ctx.getBean(JacksonJsonMapper.class).createSpecific(testCase.type); | ||
} | ||
|
||
@TearDown | ||
public void tearDown() { | ||
ctx.close(); | ||
} | ||
} | ||
|
||
public enum TestCase { | ||
STRING_ARRAY_FIELD(Argument.of(StringArrayField.class), new StringArrayField() {{ strs = new String[] { "foo", "bar" }; }}), | ||
STRING(Argument.of(String.class), "foo"); | ||
|
||
final Argument<?> type; | ||
final Object input; | ||
|
||
TestCase(Argument<?> type, Object input) { | ||
this.type = type; | ||
this.input = input; | ||
} | ||
} | ||
|
||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
@Benchmark | ||
public Object serde(Holder holder, Blackhole bh) throws IOException { | ||
byte[] bytes = holder.jsonMapper.writeValueAsBytes((Argument) holder.testCase.type, holder.testCase.input); | ||
bh.consume(bytes); | ||
return holder.jsonMapper.readValue(bytes, holder.testCase.type); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
benchmarks/src/typeCheckTest/java/io/micronaut/serde/TypeThrashingTest.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,61 @@ | ||
package io.micronaut.serde; | ||
|
||
import io.micronaut.test.typepollution.FocusListener; | ||
import io.micronaut.test.typepollution.ThresholdFocusListener; | ||
import io.micronaut.test.typepollution.TypePollutionTransformer; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.RunnerException; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class TypeThrashingTest { | ||
static final int THRESHOLD = 10_000; | ||
|
||
private ThresholdFocusListener focusListener; | ||
|
||
@BeforeAll | ||
static void setupAgent() { | ||
TypePollutionTransformer.install(net.bytebuddy.agent.ByteBuddyAgent.install()); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
focusListener = new ThresholdFocusListener(); | ||
FocusListener.setFocusListener(focusListener); | ||
} | ||
|
||
@AfterEach | ||
void verifyNoTypeThrashing() { | ||
FocusListener.setFocusListener(null); | ||
Assertions.assertTrue(focusListener.checkThresholds(THRESHOLD), "Threshold exceeded, check logs."); | ||
} | ||
|
||
@Test | ||
public void testFromJmh() throws RunnerException { | ||
Options opt = new OptionsBuilder() | ||
.include(Stream.of(ComboBenchmark.class) | ||
.map(Class::getName) | ||
.collect(Collectors.joining("|", "(", ")")) | ||
+ ".*") | ||
.warmupIterations(0) | ||
.measurementIterations(1) | ||
.mode(Mode.SingleShotTime) | ||
.timeUnit(TimeUnit.NANOSECONDS) | ||
.forks(0) | ||
.measurementBatchSize(THRESHOLD * 2) | ||
.shouldFailOnError(true) | ||
.build(); | ||
|
||
new Runner(opt).run(); | ||
} | ||
} |
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