Why is Spring MVC json serialization 10x slower than calling jackson manually? Why is Spring MVC json serialization 10x slower than calling jackson manually? spring spring

Why is Spring MVC json serialization 10x slower than calling jackson manually?


The only idea that I have is that Spring's default ObjectMapper is configured a bit differently than the one you use in your benchmark. Like the comments mention, you'd probably see a bit of overhead if you let Spring handle the mapping automatically but it shouldn't have more than a few percent's worth of impact.

To be sure that the comparison is fair, add this bean definition to your configuration:

@Bean@PrimaryObjectMapper objectMapper() {    return new ObjectMapper(new JsonFactory());}

and replace ObjectMapper mapper = new ObjectMapper(new JsonFactory()); with an autowired field:

@AutowiredObjectMapper mapper;

and see if the benchmarks return the same value.

EDIT

I wanted to verify this for myselt so I wrote a JMeter plan and executed each endpoint exactly 5kk times, with a 1-minute warm-up period. The results were as expected, no major differences between the approaches:

Label,# Samples,Average,Min,Max,Std. Dev.,Error %,Throughput,KB/sec,Avg. BytesAuto Request,5000000,2,0,108,5.88,0.00%,15577.3,3088.08,203.0Manual Request,5000000,2,0,149,5.99,0.00%,15660.2,2813.94,184.0

The important thing to note is the throughput difference - auto's 15577.3 vs. manual's 15660.2.

Here's my JMeter test plan, if you'd like to test it yourself, I was running on port 8081. If I find the time, I'll try another benchmarking framework, perhaps Gatling.