Skip to content
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

Add startup benchmark #762

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion benchmarks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ dependencies {
}

jmh {
includes = ["io.micronaut.serde.JacksonBenchmark"]
includes = ["io.micronaut.serde.StartupBenchmark"]
fork = 10
iterations = 1
warmupIterations = 0
duplicateClassesStrategy = DuplicatesStrategy.EXCLUDE
}

Expand Down
111 changes: 111 additions & 0 deletions benchmarks/src/jmh/java/io/micronaut/serde/StartupBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.micronaut.serde;

import io.micronaut.context.ApplicationContext;
import io.micronaut.core.type.Argument;
import io.micronaut.jackson.databind.JacksonDatabindMapper;
import io.micronaut.serde.data.SimpleBean;
import io.micronaut.serde.jackson.JacksonJsonMapper;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
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;

public class StartupBenchmark {

@Benchmark
@BenchmarkMode(Mode.SingleShotTime)
public Object fullStartupAndGetMapper(Holder1 holder) {
try (ApplicationContext ctx = ApplicationContext.run()) {
if (holder.stack == Stack.SERDE_JACKSON) {
return ctx.getBean(JacksonJsonMapper.class);
} else if (holder.stack == Stack.JACKSON_DATABIND) {
return ctx.getBean(JacksonDatabindMapper.class);
}
throw new IllegalStateException();
}
}

@Benchmark
@BenchmarkMode(Mode.SingleShotTime)
public Object fullStartupAndGetMapperReadBean(Holder1 holder) throws Exception {
try (ApplicationContext ctx = ApplicationContext.run()) {
if (holder.stack == Stack.SERDE_JACKSON) {
return ctx.getBean(JacksonJsonMapper.class).readValue("{}", Argument.of(SimpleBean.class));
} else if (holder.stack == Stack.JACKSON_DATABIND) {
return ctx.getBean(JacksonDatabindMapper.class).readValue("{}", Argument.of(SimpleBean.class));
}
throw new IllegalStateException();
}
}

@Benchmark
@BenchmarkMode(Mode.SingleShotTime)
public Object getMapper(Holder2 holder) {
if (holder.stack == Stack.SERDE_JACKSON) {
return holder.ctx.getBean(JacksonJsonMapper.class);
} else if (holder.stack == Stack.JACKSON_DATABIND) {
return holder.ctx.getBean(JacksonDatabindMapper.class);
}
throw new IllegalStateException();
}

@Benchmark
@BenchmarkMode(Mode.SingleShotTime)
public Object getMapperReadBean(Holder2 holder) throws Exception {
if (holder.stack == Stack.SERDE_JACKSON) {
return holder.ctx.getBean(JacksonJsonMapper.class).readValue("{}", Argument.of(SimpleBean.class));
} else if (holder.stack == Stack.JACKSON_DATABIND) {
return holder.ctx.getBean(JacksonDatabindMapper.class).readValue("{}", Argument.of(SimpleBean.class));
}
throw new IllegalStateException();
}

public static void main(String[] args) {
try (ApplicationContext ctx = ApplicationContext.run()) {
ctx.getBean(JacksonJsonMapper.class);
}
}


@State(Scope.Thread)
public static class Holder1 {
@Param({
"SERDE_JACKSON",
"JACKSON_DATABIND"

})
Stack stack = Stack.SERDE_JACKSON;

}

@State(Scope.Thread)
public static class Holder2 {
@Param({
"SERDE_JACKSON",
"JACKSON_DATABIND"
})
Stack stack = Stack.SERDE_JACKSON;

ApplicationContext ctx;

@Setup
public void setUp() {
ctx = ApplicationContext.run();
}

@TearDown
public void tearDown() {
ctx.close();
}
}

public enum Stack {
SERDE_JACKSON,
JACKSON_DATABIND,
}

}
26 changes: 26 additions & 0 deletions benchmarks/src/jmh/java/io/micronaut/serde/data/SimpleBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.micronaut.serde.data;

import io.micronaut.serde.annotation.Serdeable;

@Serdeable
public class SimpleBean {

private Long id;
private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Loading