Skip to content

Commit

Permalink
Add startup benchmark (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Feb 16, 2024
1 parent c4012eb commit d54127d
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
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;
}
}

0 comments on commit d54127d

Please sign in to comment.