preface

OpenJDK 11 is used.

Openjdk Runtime Environment 18.9 (build 11.0.1+13) OpenJDK 64-bit Server VM 18.9 (Build 11.0.1+13, Mixed mode)Copy the code
You can run the benchmark as follows. It may take some time to run, but all the tests will be performed below.

./mvnw clean package
(cd benchmarks/; java -jar target/benchmarks.jar)

Copy the code

FluxBaseline

Create a project using SpringInitializr, including Reactive Web only. Next, I’m going to write a minimalist Controller in the style of WebMVC.

@SpringBootApplication
@RestController
public class DemoApplication {
 @GetMapping("/")
 public String home() {
 return "Hello"; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Copy the code
The Spring Boot version is 2.1.0.release.

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > 2.1.0. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent>Copy the code
Startup result 2.938 ± 0.287s /op.

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /opCopy the code
Now, take this result as a baseline. Let’s start here.

WebMVC

I wonder why WebMVC instead of WebFlux? I gave it a try. Maybe just to compare Tomcat and Netty?

Benchmark Mode Cnt Score Error Units MyBenchmark. Case01_FluxBaseline SS 10 2.938 ± 0.287s /op MyBenchmark. Case02_Web SS 10 3.281 ± 0.342 s/opCopy the code
WebFlux is a bit faster, isn’t it?

spring-context-indexer

Next, I tried spring-Context-Indexer, which seems to create the Component index.

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context-indexer</artifactId>
 <optional>true</optional>
</dependency>

Copy the code
B: well… A little slow?

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op Mybenchmark.case03_withcontextindexer SS 10 3.063 ± 0.102 s/opCopy the code
I checked spring.com Ponents and found it contained only one component. To understand… I should have tried a bigger project to see the results.

#
#Sun Nov 04 18:42:59 JST 2018
com.example.DemoApplication=org.springframework.stereotype.Component

Copy the code

Lazy initialization

Lazy initialization was 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); }}}Copy the code
Looking at the results, it started up a little bit faster.

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op Mybenchmark.case04_withlazyinit SS 10 2.844 ± 0.129 s/opCopy the code

NoVerify

Run the -noverify option:

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op Mybenchmark.case05_withnoverifyoption SS 10 2.582 ± 0.060s /opCopy the code
Start up a little bit faster. I don’t know why there is this result, and I need to take a closer look later.

TieredStopAtLevel

Run the -xx :TieredStopAtLevel=1 option:

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op MyBenchmark. Case06_WithTieredStopAtLevel1Option ss 10 1.980 + / - 0.037 s/opCopy the code
Well, much faster! That’s almost two seconds less. I still don’t know what this parameter means, so I need to learn more about it later.

Specify the SpringConfigLocation parameter

Add – run Dspring. Config. Location = classpath: / application. The properties option:

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op MyBenchmark. Case07_WithSpringConfigLocationOption ss 10 3.026 + / - 0.139 s/opCopy the code
Well, it’s slowing down again.

Close the JMX

Run the -dspring.jmx. enabled=false option:

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op Mybenchmark.case08_withjmxdisabledoption SS 10 2.877 ± 0.097s /opCopy the code
It gets a little faster.

Cancel the Logback

From here, I start to reduce the library. To start, cancel Logback:

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-webflux</artifactId>
 <exclusions>
 <exclusion>
 <artifactId>spring-boot-starter-logging</artifactId>
 <groupId>org.springframework.boot</groupId>
 </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-jdk14</artifactId>
</dependency>

Copy the code
The results are as follows:

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op Mybenchmark.case09_withoutlogback SS 10 2.904 ± 0.096s /opCopy the code
B: well… Seems like a little bit of improvement?

Cancel the Jackson

Next up is Jackson

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-webflux</artifactId>
 <exclusions>
 <exclusion>
 <artifactId>spring-boot-starter-json</artifactId>
 <groupId>org.springframework.boot</groupId>
 </exclusion>
 </exclusions>
</dependency>

Copy the code
The results are as follows:

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op Mybenchmark.case10_withoutjackson SS 10 2.789 ± 0.093 s/opCopy the code
It’s a little bit faster.

Cancel HibernateValidator

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-webflux</artifactId>
 <exclusions>
 <exclusion>
 <artifactId>hibernate-validator</artifactId>
 <groupId>org.hibernate.validator</groupId>
 </exclusion>
 </exclusions>
</dependency>

Copy the code
The results are as follows:

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op MyBenchmark. Case11_WithoutHibernateValidator ss 10 2.857 + / - 0.084 s/opCopy the code
It worked a little.

At this point, I do not cancel the library anymore.

AppCDS

AppCDS (Application Class Data Sharing) is an enterprise version of the Oracle JDK. OpenJDK 10 is starting to include this functionality.

It looks like the AppCDS dump is stored in a shared compressed file, so the startup time is shorter.

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op Mybenchmark.case12_withappcds SS 10 2.957 ± 0.079 s/opCopy the code
B: well… It’s not getting faster… Then I read about CDS and found out why.

SpringBoot FatJAR is not covered by CDS.

Flux using Thin Launcher

Well, SORRY, ‘Exploded’ benchmark is wrong. I’ve tried FatJAR, but CDS doesn’t work that way. So I switched to using the Thin Launcher, so that ‘Exploded’ became ‘Thin Launche.’

Before using CDS, I tested the startup speed of a JAR file packaged with Thin Launcher.

<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot.experimental</groupId> The < artifactId > spring - the boot - thin - layout < / artifactId > < version > 1.0.15. RELEASE < / version > < / dependency > < / dependencies > </plugin> </plugins>Copy the code
Although I use Thin Launcher to package my app, INSTEAD of using the Thin Launcher class, I use the Main class to get it started as quickly as possible.

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op Mybenchmark.case13_exploded SS 10 2.476 ± 0.091 s/opCopy the code
Well, it’s a bit fast, isn’t it?

Thin Launcher + CDS

Now, I’m going to use AppCDS.

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op Mybenchmark.case14_explodedwithappcds SS 10 1.535 ± 0.036 s/opCopy the code
B: oh! It gets faster!

All operations

I ended up using everything.

Mybenchmark.case01_fluxbaseline SS 10 2.938 ± 0.287s /op Mybenchmark.case15_allapplied SS 10 0.801 ± 0.037 s/opCopy the code
Less than 1 second! Studying ´ ∀ `) studying

further

In Dave’s video, he talks about “functional Bean definitions,” trying to use only Spring instead of SpringBoot, and making apps faster. The reason for this need to be further understood.

Results:

Benchmark Mode Cnt Score Error Units MyBenchmark. Case01_FluxBaseline SS 10 2.938 ± 0.287s /op MyBenchmark. Case02_Web SS 10 3.281 ± 0.342s /op myBenchmark.case04_WithContextIndexer SS 10 3.063 ± 0.102s /op myBenchmark.case04_withLazyInit SS 10 2.844 ± 0.129s /op mybenchmark.Case05_WithNoverIfyOption SS 10 2.582 ± 0.060s /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.097s /op myBenchmark.Case09_withoutlogBack SS 10 2.904 ± 0.096s /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.036s /op myBenchmark.Case15_AllApplied SS 10 0.801 ± 0.037s /opCopy the code
It’s really interesting. Thank you!



The last

Welcome to pay attention to my public number [programmer chasing wind], the article will be updated in it, sorting out the data will be placed in it.