将普通 spring boot 项目改造为 spring native 项目非常简单. 首先, 在 gradle 中加入并启用org.springframework.experimental.aot 插件, 如果使用的 spring native 插件版本还未发布到中央仓库, 需手动添加 spring 仓库, 一个典型的 spring native 项目的插件配置可能是这样的(注意, spring boot 版本必须与 spring native 插件相符否则可能不兼容, 详见文档):
$ for i in {1..5}; do curl -w '%{time_total}\n' -o /dev/null -s localhost:8080/ping; done
0.157708
0.009744
0.007514
0.009595
0.007608
$ for i in {1..5}; do curl -w '%{time_total}\n' -o /dev/null -s localhost:8080/ping; done
0.003939
0.000716
0.000850
0.000726
0.000763
在 native image 编译后, 首次访问速度提升了四十倍, 其后的访问提升了十倍!
模拟一千个用户一共访问十万次:
$ ab -n 100000 -c 1000 http://localhost:8080/ping
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /ping
Document Length: 10 bytes
Concurrency Level: 1000
Time taken for tests: 4.103 seconds
Complete requests: 100000
Failed requests: 0
Total transferred: 8900000 bytes
HTML transferred: 1000000 bytes
Requests per second: 24370.69 [#/sec] (mean)
Time per request: 41.033 [ms] (mean)
Time per request: 0.041 [ms] (mean, across all concurrent requests)
Transfer rate: 2118.16 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 19 4.3 19 37
Processing: 3 21 4.6 21 43
Waiting: 1 13 4.4 13 40
Total: 23 41 2.6 40 75
Percentage of the requests served within a certain time (ms)
50% 40
66% 41
75% 41
80% 42
90% 44
95% 46
98% 48
99% 49
100% 75 (longest request)
$ ab -n 100000 -c 1000 http://localhost:8080/ping
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /ping
Document Length: 10 bytes
Concurrency Level: 1000
Time taken for tests: 3.996 seconds
Complete requests: 100000
Failed requests: 0
Total transferred: 8900000 bytes
HTML transferred: 1000000 bytes
Requests per second: 25024.10 [#/sec] (mean)
Time per request: 39.961 [ms] (mean)
Time per request: 0.040 [ms] (mean, across all concurrent requests)
Transfer rate: 2174.95 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 19 2.4 19 34
Processing: 7 21 3.5 20 44
Waiting: 1 15 3.5 15 36
Total: 22 40 3.0 39 65
Percentage of the requests served within a certain time (ms)
50% 39
66% 39
75% 40
80% 40
90% 44
95% 47
98% 49
99% 51
100% 65 (longest request)