Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make SpringBoot start faster

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to make SpringBoot start faster" related knowledge, editor through the actual case to show you the operation process, the operation method is simple and fast, practical, I hope that this "how to make SpringBoot start faster" article can help you solve the problem.

I am using OpenJDK 11.

❯ java-- version

Openjdk 11.0.1 2018-10-16

OpenJDK Runtime Environment 18.9 (build 11.0.1 / 13)

OpenJDK 64-Bit Server VM 18.9 (build 11.0.1 / 13, mixed mode)

You can run the benchmark as follows. It may take some time to run, and all the tests will be performed next.

❯. / mvnw clean package

❯ (cd benchmarks/; java-jar target/benchmarks.jar)

1. FluxBaseline

I use SpringInitializr to create a project that contains only Reactive Web. Next, I will write a minimalist controller in WebMVC style.

@ SpringBootApplication

@ RestController

Public class DemoApplication {

@ GetMapping ("/")

Public String home () {

Return "Hello"

}

Public static void main (String [] args) {

SpringApplication.run (DemoApplication.class, args)

}

}

The version of ↓ Spring Boot is 2.1.0.RELEASE.

Org.springframework.boot

Spring-boot-starter-parent

2.1.0.RELEASE

The starting result of ↓ was 2.938 ±0.287 s/op.

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

Now, use this result as the baseline. Let's start here.

2. WebMVC

↓, I'm curious why use WebMVC instead of WebFlux? I tried. Maybe just to compare Tomcat and Netty?

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case02_Web ss 10 3.281 ±0.342 s/op

WebFlux is a little fast, isn't it?

3. Spring-context-indexer

Next, I tried spring-context-indexer and seemed to have created component index.

Org.springframework

Spring-context-indexer

True

↓, um... A little slow?

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case03_WithContextIndexer ss 10 3.063 ±0.102 s/op

↓ I checked the spring.components and found that only one component was included. Understand. I should try a larger project so that I can see the effect.

#

# Sun Nov 04 18:42:59 JST 2018

Com.example.DemoApplication=org.springframework.stereotype.Component

4. Lazy initialization

Lazy initialization attempted.

@ Configuration

Public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

@ Override

Public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory) throws BeansException {

For (String beanName: beanFactory.getBeanDefinitionNames ()) {

BeanFactory.getBeanDefinition (beanName) .setLazyInit (true)

}

}

}

↓ looks at the results and starts up a little faster.

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case04_WithLazyInit ss 10 2.844 ±0.129 s/op

5. NoVerify

Run the add-noverify option:

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case05_WithNoVerifyOption ss 10 2.582 ±0.060 s/op

The start-up is a little fast. I don't know why there is such a result, but I need to understand it carefully later.

6. TieredStopAtLevel

Run the add-XX:TieredStopAtLevel=1 option:

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case06_WithTieredStopAtLevel1Option ss 10 1.980 ±0.037 s/op

Well, it's much faster! Reduced by almost 2 seconds. I still don't know what this parameter means, but I need to know it carefully later.

7. Specify SpringConfigLocation parameters

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case07_WithSpringConfigLocationOption ss 10 3.026 ±0.139 s/op

Well, it's slowing down again.

8. Close JMX

Run the add-Dspring.jmx.enabled=false option:

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case08_WithJmxDisabledOption ss 10 2.877 ±0.097 s/op

It's getting a little fast.

9. Cancel Logback

From here on, I began to reduce the library. To begin, cancel Logback:

Org.springframework.boot

Spring-boot-starter-webflux

Spring-boot-starter-logging

Org.springframework.boot

Org.slf4j

Slf4j-jdk14

The results are as follows: Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case09_WithoutLogback ss 10 2.904 ±0.096 s/op

Um... Seems to have improved a little bit?

10. Cancel Jackson

And then there's Jackson.

Org.springframework.boot

Spring-boot-starter-webflux

Spring-boot-starter-json

Org.springframework.boot

The results are as follows:

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case10_WithoutJackson ss 10 2.789 ±0.093 s/op

It turned out to be a little faster.

11. Cancel HibernateValidator

Org.springframework.boot

Spring-boot-starter-webflux

Hibernate-validator

Org.hibernate.validator

The results are as follows:

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case11_WithoutHibernateValidator ss 10 2.857 ±0.084 s/op

It also has a little effect.

At this point, the library is no longer canceled.

12. AppCDS

AppCDS (Application Class Data Sharing) is an enterprise feature of Oracle JDK. OpenJDK 10 began to include this feature.

It looks like the AppCDS dump information is saved to a shared zip file, so the startup time is shorter.

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case12_WithAppCds ss 10 2.957 ±0.079 s/op

Um... It's not getting any faster... Then I read the relevant articles about CDS and found out why.

SpringBoot FatJAR is outside the scope of CDS management.

13. Flux using Thin Launcher

Well, I'm sorry, the "Exploded" benchmark is wrong. I've tried to use FatJAR, but CDS can't do that. So, I switched to Thin Launcher, so "Exploded" became "Thin Launche".

Before using CDS, I test the startup speed of packaging JAR files with Thin Launcher.

Org.springframework.boot

Spring-boot-maven-plugin

Org.springframework.boot.experimental

Spring-boot-thin-layout

1.0.15.RELEASE

Although I use Thin Launcher to package app, instead of using Thin Launcher to start the class, I use Main class to make it start as quickly as possible.

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case13_Exploded ss 10 2.476 ±0.091 s/op

Well, it's a little fast, isn't it?

14. Thin Launcher + CDS

Now, I'm going to use AppCDS.

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case14_ExplodedWithAppCds ss 10 1.535 ±0.036 s/op

oh! It's getting faster!

15. All operations are on.

In the end, I used all the operations.

one

two

3Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case15_AllApplied ss 10 0.801 ±0.037 s/op

Less than 1 second! (∩ '∀--) ∩

further more

In Dave's video, he mentions the "functional Bean definition" and tries to make it faster to use only Spring without SpringBoot,app. The truth needs to be further understood.

Results:

Benchmark Mode Cnt Score Error Units

MyBenchmark.case01_FluxBaseline ss 10 2.938 ±0.287 s/op

MyBenchmark.case02_Web ss 10 3.281 ±0.342 s/op

MyBenchmark.case03_WithContextIndexer ss 10 3.063 ±0.102 s/op

MyBenchmark.case04_WithLazyInit ss 10 2.844 ±0.129 s/op

MyBenchmark.case05_WithNoVerifyOption ss 10 2.582 ±0.060 s/op

MyBenchmark.case06_WithTieredStopAtLevel1Option ss 10 1.980 ±0.037 s/op

MyBenchmark.case07_WithSpringConfigLocationOption ss 10 3.026 ±0.139 s/op

MyBenchmark.case08_WithJmxDisabledOption ss 10 2.877 ±0.097 s/op

MyBenchmark.case09_WithoutLogback ss 10 2.904 ±0.096 s/op

MyBenchmark.case10_WithoutJackson ss 10 2.789 ±0.093 s/op

MyBenchmark.case11_WithoutHibernateValidator ss 10 2.857 ±0.084 s/op

MyBenchmark.case12_WithAppCds ss 10 2.957 ±0.079 s/op

MyBenchmark.case13_Exploded ss 10 2.476 ±0.091 s/op

MyBenchmark.case14_ExplodedWithAppCds ss 10 1.535 ±0.036 s/op

MyBenchmark.case15_AllApplied ss 10 0.801 ±0.037 s/op

This is the end of the content about "how to make SpringBoot start faster". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report