|
| 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 | +} |
0 commit comments