Skip to content

Commit d54127d

Browse files
authored
Add startup benchmark (#762)
1 parent c4012eb commit d54127d

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

benchmarks/build.gradle

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ dependencies {
2222
}
2323

2424
jmh {
25-
includes = ["io.micronaut.serde.JacksonBenchmark"]
25+
includes = ["io.micronaut.serde.StartupBenchmark"]
26+
fork = 10
27+
iterations = 1
28+
warmupIterations = 0
2629
duplicateClassesStrategy = DuplicatesStrategy.EXCLUDE
2730
}
2831

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package io.micronaut.serde;
2+
3+
import io.micronaut.context.ApplicationContext;
4+
import io.micronaut.core.type.Argument;
5+
import io.micronaut.jackson.databind.JacksonDatabindMapper;
6+
import io.micronaut.serde.data.SimpleBean;
7+
import io.micronaut.serde.jackson.JacksonJsonMapper;
8+
import org.openjdk.jmh.annotations.Benchmark;
9+
import org.openjdk.jmh.annotations.BenchmarkMode;
10+
import org.openjdk.jmh.annotations.Mode;
11+
import org.openjdk.jmh.annotations.Param;
12+
import org.openjdk.jmh.annotations.Scope;
13+
import org.openjdk.jmh.annotations.Setup;
14+
import org.openjdk.jmh.annotations.State;
15+
import org.openjdk.jmh.annotations.TearDown;
16+
17+
public class StartupBenchmark {
18+
19+
@Benchmark
20+
@BenchmarkMode(Mode.SingleShotTime)
21+
public Object fullStartupAndGetMapper(Holder1 holder) {
22+
try (ApplicationContext ctx = ApplicationContext.run()) {
23+
if (holder.stack == Stack.SERDE_JACKSON) {
24+
return ctx.getBean(JacksonJsonMapper.class);
25+
} else if (holder.stack == Stack.JACKSON_DATABIND) {
26+
return ctx.getBean(JacksonDatabindMapper.class);
27+
}
28+
throw new IllegalStateException();
29+
}
30+
}
31+
32+
@Benchmark
33+
@BenchmarkMode(Mode.SingleShotTime)
34+
public Object fullStartupAndGetMapperReadBean(Holder1 holder) throws Exception {
35+
try (ApplicationContext ctx = ApplicationContext.run()) {
36+
if (holder.stack == Stack.SERDE_JACKSON) {
37+
return ctx.getBean(JacksonJsonMapper.class).readValue("{}", Argument.of(SimpleBean.class));
38+
} else if (holder.stack == Stack.JACKSON_DATABIND) {
39+
return ctx.getBean(JacksonDatabindMapper.class).readValue("{}", Argument.of(SimpleBean.class));
40+
}
41+
throw new IllegalStateException();
42+
}
43+
}
44+
45+
@Benchmark
46+
@BenchmarkMode(Mode.SingleShotTime)
47+
public Object getMapper(Holder2 holder) {
48+
if (holder.stack == Stack.SERDE_JACKSON) {
49+
return holder.ctx.getBean(JacksonJsonMapper.class);
50+
} else if (holder.stack == Stack.JACKSON_DATABIND) {
51+
return holder.ctx.getBean(JacksonDatabindMapper.class);
52+
}
53+
throw new IllegalStateException();
54+
}
55+
56+
@Benchmark
57+
@BenchmarkMode(Mode.SingleShotTime)
58+
public Object getMapperReadBean(Holder2 holder) throws Exception {
59+
if (holder.stack == Stack.SERDE_JACKSON) {
60+
return holder.ctx.getBean(JacksonJsonMapper.class).readValue("{}", Argument.of(SimpleBean.class));
61+
} else if (holder.stack == Stack.JACKSON_DATABIND) {
62+
return holder.ctx.getBean(JacksonDatabindMapper.class).readValue("{}", Argument.of(SimpleBean.class));
63+
}
64+
throw new IllegalStateException();
65+
}
66+
67+
public static void main(String[] args) {
68+
try (ApplicationContext ctx = ApplicationContext.run()) {
69+
ctx.getBean(JacksonJsonMapper.class);
70+
}
71+
}
72+
73+
74+
@State(Scope.Thread)
75+
public static class Holder1 {
76+
@Param({
77+
"SERDE_JACKSON",
78+
"JACKSON_DATABIND"
79+
80+
})
81+
Stack stack = Stack.SERDE_JACKSON;
82+
83+
}
84+
85+
@State(Scope.Thread)
86+
public static class Holder2 {
87+
@Param({
88+
"SERDE_JACKSON",
89+
"JACKSON_DATABIND"
90+
})
91+
Stack stack = Stack.SERDE_JACKSON;
92+
93+
ApplicationContext ctx;
94+
95+
@Setup
96+
public void setUp() {
97+
ctx = ApplicationContext.run();
98+
}
99+
100+
@TearDown
101+
public void tearDown() {
102+
ctx.close();
103+
}
104+
}
105+
106+
public enum Stack {
107+
SERDE_JACKSON,
108+
JACKSON_DATABIND,
109+
}
110+
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.micronaut.serde.data;
2+
3+
import io.micronaut.serde.annotation.Serdeable;
4+
5+
@Serdeable
6+
public class SimpleBean {
7+
8+
private Long id;
9+
private String name;
10+
11+
public Long getId() {
12+
return id;
13+
}
14+
15+
public void setId(Long id) {
16+
this.id = id;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
}

0 commit comments

Comments
 (0)