A sweep packet optimization
Sweep start packet belongs to optimize, optimization not run
1.1 components automatically scan problems caused by
Use
@SpringBootApplication
annotations, the package will traverse the following sub-categories, can affect performance.
By default, we will use @SpringBootApplication
annotations to automatically obtain configuration information for the application, but it will also bring some side effects to the application.
After using this annotation, triggers 自动配置
(auto-configuration) and 组件扫描
(component scanning), which with the use of @Configuration
, @EnableAutoConfiguration
and @ComponentScan
the role of three notes is the same. Doing so bring convenience to developers, there will be a threefold effect:
- The project will lead to longer startup time. When starting a large application, or to do a lot of integration tests start the application, the impact will be particularly pronounced.
- Loads some unwanted extra instance (beans).
- Increase CPU consumption.
For the above three cases, we can be removed @SpringBootApplication
and @ComponentScan
two component annotations to disable automatic scanning, then we need bean
to be explicitly configured on:
// 移除 @SpringBootApplication and @ComponentScan, 用 @EnableAutoConfiguration 来替代
// @SpringBootApplication
@ComponentScan(basePackages = "com.snow")
@EnableAutoConfiguration
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2 SpringBoot
JVM
Parameter Tuning
JVM
Tuning parameters, belonging to the overall optimization
The memory size of the server to set the parameters of the heap.
-Xms
: Set the initial size of the Java stack-Xmx
: Set the maximum heap size java
instance parameters-XX:+PrintGCDetails -Xmx32M -Xms1M
Local project Tuning:
External operation Tuning:
java -server -Xms32m -Xmx32m -jar springboot_v2.jar
3 Servlet
container becomesUndertow
Step 3.1
By default, Spring Boot using the embedded Tomcat as a Servlet container.
Web server can be switched to Undertow to improve application performance.
Undertow is a flexible, high-performance Web server Java development services including blocking NIO-based and non-blocking mechanism. Undertow is Red Hat's open source products, is Wildfly default Web server.
First, remove the Tomcat configuration dependent on information from inside:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加 Undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
Start, we found using Undertow
:
3.2 Test
Create thread groups
each request 10000
Creating HTTP request:
test:
Create an aggregate reports:
Test:
Click Run, view the test report
name of server | First run | Second run | Third run | average value |
---|---|---|---|---|
Tomcat | 4773 | 5194 | 5334.7 | 5100 |
Undertow | 6666 | 6373 | 6451 | 6496 |