php7 怎么比 Java 还快?

2019-09-23 11:22:47 +08:00
 zjsxwc

有点反认知,分别用 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 快

PHP 结果耗时 0.125 秒
$ 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)
Java 结果耗时 0.498 秒
$ 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)
16589 次点击
所在节点    程序员
134 条回复
askfilm
2019-09-23 18:30:14 +08:00
大家聊性能好嗨啊, 然而我业务还没写完呢 ~
zjsxwc
2019-09-23 18:34:15 +08:00
@notreami #43 原文:“2019 年了,能活下来的,性能、周边、背书、易用都不会太差。2019 年了,考验的是某个领域的杀手锏工具,比如 java 的大数据领域、python 的 AI 领域、golang 的运维工具领域、js 的前端领域。而 php 在最近 10 年,好像没啥特别领域突破。”

回复:

php 好处在于讨好程序员领域吧,我写代码别的语言都不想用,只想写 php,因为可以无脑 php array 与 string 莽过去,array 可以当数组,可以当 map,可以当 object 用; string 可以当 char,可以当[]byte,可以当 intger,可以当 stream ;

别的语言各种类型转换写起来比较烦躁。
shansing
2019-09-23 18:38:00 +08:00
@auciou2 我找到的公开资料不符合你的感觉噢:
https://www.phoronix.com/scan.php?page=news_item&px=PHP-7.4-RC1-Released
https://www.zimuel.it/blog/benchmarking_PHP74
有提升但不并算很大。
autogen
2019-09-23 18:47:18 +08:00
// 来跟 C 比一下速度?
printf("Content-type: image/png\n\n");
write(stdout, base64_decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWP4////fwAJ+wP9CNHoHgAAAABJRU5ErkJggg=="));
auciou2
2019-09-23 18:50:22 +08:00
@shansing 是的,那个公开资料我之前看过。实际的测试数据,和那个资料是不一样的。我的测试数据,是循环 100 万次时间太短,所以循环 1000 万次。
这 2 个月在开发一键 LAMP/LNMP,顺便把这些 PHP 的性能测试了一下。本来我一直用 PHP 5.3、5.5,速度也很快。

测试结果,PHP 7.4 的执行速度约是 PHP 7.3 的 1.8 倍~ 2 倍; PHP 7.4 的执行速度约是 PHP 5.3 的 10 倍; PHP 7.4 的执行速度约是 PHP 5.5 的 5 倍多,PHP 7.0、7.1、7.2、7.3 的速度差异不大,这 4 个版本当时没有具体记录,大概最多是 20%-40%的差别。
但是,PHP 7.3 到 PHP 7.4 这样大的提升,当时我十分吃惊。所以我的项目正在转向 PHP 7.4,也正在把 PHP 5 的程序都慢慢改写为 PHP 7,已经写了 2 个月。

一个月前当时的一点测试记录:
环境为 Ubuntu、Debian+Apache,执行 1000 万次的循环程序,测试结果如:

PHP 执行时间:(单位:微秒)
PHP 7.4 0.045616865158081 (Ubuntu 16+PPA)
PHP 7.0 0.099370956420898 (Debian 9) (PHP 7.0 ~ 7.3 变化不太大)
PHP 5.3 0.52340078353882 (Ubuntu 12)
PHP 5.5.9 0.25087404251099 (Ubuntu 14)

在高并发时可能会有很大的差别。PHP 的瓶颈主要是在网络线路,MySQL,或者数据库的设计。
zjsxwc
2019-09-23 18:54:45 +08:00
@autogen #64 原文:“// 来跟 C 比一下速度? printf("Content-type: image/png\n\n");write(stdout, base64_decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWP4////fwAJ+wP9CNHoHgAAAABJRU5ErkJggg=="));”
回复:



cgi 吗?没试过,我上面有 c++的 web 例子,不过已经可以得出总结,在 web 服务端开发领域所有语言基本都能撑满到目前 io 极限,在我的电脑里都是 0.5 秒处理完 9000 并发的。


目前也就是在 cpu 计算密集领域这些语言性能还会有要求。
auciou2
2019-09-23 19:02:06 +08:00
@shansing 总之,大家转到 PHP 7.4,会有不少惊喜。
PHP 7 有些语法和 PHP 5 不一样,比 PHP 5 严格。所以 PHP 编程者应该尽快抛弃 PHP 5 的开发,并不是因为性能的原因,因为性能只有 PHP 7.4 的 PHP 5.3,速度也很快。PHP 的瓶颈主要是网络线路。

转向 PHP 7 开发的原因是语法、巨大的工作量的原因,原来在 PHP 5 下的很多程序,到了 PHP 7 就会报错。所以一步到位写 PHP 7 的程序,可以省掉很大的工作量。PHP 5 程序改为 PHP 7,需要对所有的模块重新逐一测试,估计占到原始开发量的工作量的 10%-15%左右,工作量非常巨大。
auciou2
2019-09-23 19:02:44 +08:00
修改:因为性能只有 PHP 7.4 的 1/10 的 PHP 5.3,速度也很快。
lscho
2019-09-23 19:23:35 +08:00
不是,为什么有人觉得 php 内置函数慢?
cloudzhou
2019-09-23 19:41:10 +08:00
@zhuzeitou 还是你骚

我的优化逻辑上是通的,因为减少一次 if 判读以及求余计算
我猜测和编译器优化有关,比如进行内联等,continue 应该是直接 go to 到 for {} 入口

基本上来说,这些测评的作用是很有限的,在非常单一的计算测试中,考验的只是局部的某点优化
agdhole
2019-09-23 19:48:42 +08:00
看着快,实际上没人会直接拿来这么用
把业务堆上去,PHP 只能往后稍稍
rrfeng
2019-09-23 19:53:42 +08:00
这种测试没意义啊,你用 netty 起个 socket 直接返回字符试试,更快。
godoway
2019-09-23 20:27:29 +08:00
Java 上 vertx 试试,或者看这里现成的结果。
https://www.techempower.com/benchmarks/#section=data-r18&hw=ph&test=db
zhuzeitou
2019-09-23 20:32:59 +08:00
@cloudzhou 嗯,感觉和跳转的优化有关,后面改着改着以为自己在玩 Human Resource Machine……
areless
2019-09-23 20:40:46 +08:00
目前在编程语言上遇到瓶颈的公司是没有的。阿里、新浪、facebook 也不是因为语言性能问题换掉 PHP 的,而是大厂的阵营问题,套路,发布会,秀,娱乐圈精髓要带进来,网红文化,敏捷开发,概念化,生态化反 =___=
romisanic
2019-09-23 20:42:01 +08:00
性能测试都要预热的啊。。。。
des
2019-09-23 20:51:28 +08:00
web worker 不是多进程么?
多线程不是这个么?
https://nodejs.org/dist/latest-v12.x/docs/api/worker_threads.html
zy445566
2019-09-23 21:05:24 +08:00
@Hanggi
@raincode
@photon006
@gimp
@ourleven
这个用 node 去比没有意义,这种压测加 base64 就变成 CPU 密集运算了,会有一定会对其它请求有柱塞。这个反而多线程会占优势。要是 node 读写文件混打 sql 查询,node 肯定秒了其它语言
GoLand
2019-09-23 22:08:08 +08:00
areless
2019-09-23 22:30:44 +08:00
第 30 万个质数
PHP 用 gmp_nextprime 函数能在 10 秒内。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/603272

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX