有点反认知,分别用 PHP 与 Java Spring Boot 写了个返回 1 个像素点图片的接口,结果 php 比 java 快。
PHP 代码
<?php
//xpng.php
header("Content-type: image/png");
echo base64_decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWP4////fwAJ+wP9CNHoHgAAAABJRU5ErkJggg==");
Java 代码
//XController.java
package com.abtest;
import java.util.Base64;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class XController {
@RequestMapping("/x.png")
public ResponseEntity<byte[]> xpng() {
String onepointpngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWP4////fwAJ+wP9CNHoHgAAAABJRU5ErkJggg==";
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Content-type", "image/png");
byte[] decodedValue = Base64.getDecoder().decode(onepointpngBase64);
return new ResponseEntity<byte[]>(decodedValue, responseHeaders, HttpStatus.OK);
}
}
Server 分别 SpringBoot Jar 包自带的 Apache Tomcat/9.0.24
PHP 就是 PHP 7.0.33-0+deb9u1 (cli) 自带的低性能调试用 server (使用php -S localhost:7767 -t .
运行)
AB 结果反直觉,PHP 居然比 Java 快
$ ab -n 1000 -c 1000 "http://localhost:7767/xpng.php"
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
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 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:
Server Hostname: localhost
Server Port: 7767
Document Path: /xpng.php
Document Length: 70 bytes
Concurrency Level: 1000
Time taken for tests: 0.125 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 190000 bytes
HTML transferred: 70000 bytes
Requests per second: 8011.99 [#/sec] (mean)
Time per request: 124.813 [ms] (mean)
Time per request: 0.125 [ms] (mean, across all concurrent requests)
Transfer rate: 1486.60 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 5.5 8 14
Processing: 5 22 20.1 13 69
Waiting: 4 22 20.2 12 69
Total: 8 29 21.3 19 71
Percentage of the requests served within a certain time (ms)
50% 19
66% 24
75% 37
80% 62
90% 68
95% 70
98% 70
99% 70
100% 71 (longest request)
$ ab -n 1000 -c 1000 "http://localhost:8080/x.png"
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
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 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /x.png
Document Length: 70 bytes
Concurrency Level: 1000
Time taken for tests: 0.498 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 188000 bytes
HTML transferred: 70000 bytes
Requests per second: 2007.85 [#/sec] (mean)
Time per request: 498.046 [ms] (mean)
Time per request: 0.498 [ms] (mean, across all concurrent requests)
Transfer rate: 368.63 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 3.2 0 11
Processing: 5 73 63.3 48 237
Waiting: 3 64 56.7 41 194
Total: 5 74 66.0 48 247
Percentage of the requests served within a certain time (ms)
50% 48
66% 63
75% 76
80% 88
90% 216
95% 225
98% 229
99% 234
100% 247 (longest request)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.