为了真实体验 Java 和PHP在网站服务器端的真实表现,设置了一个相同的任务,完成10000次 MD5 运算,并显示出来,先放源代码。
Java源代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import=" java .security.MessageDigest" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%!
String MD5(String data) throws Exception {
java.security.MessageDigest md = MessageDigest. getInstance ("MD5");
byte [] array = md.digest(data.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
}
String[] do Test (int cs){
String rtxt="";
String btxt="abcdefghigklmn";
String[] ss=new String[cs];
for(int i=0;i
charset ="UTF-8">
Test with Java
<%
int cs=100;
if( request .getParameter("cs")!=null)cs=Integer.parseInt(request.getParameter("cs"));
if(cs<1)cs=100;
long startTime = System.currentTimeMillis();
String[] otxt= doTest(cs);
long endTime=System.currentTimeMillis();
out.println("
");
out.println("用时"+(endTime-startTime)+"毫秒
");
out.println(String.join("
",otxt));
%>
PHP源代码:
Test witth PHP
';
echo '
耗时:'.$t3.'毫秒,共完成'.$sl.'条
';
//echo $otxt;
echo join("
",$txt);
?>
物理 服务器CPU 型号:Intel(R) Xeon(R) Gold 5218 CPU @ 2.30GHz
两个 虚拟机 均为4核CPU,4G内存,原生Centos7。
Java虚拟机 :
yum update
yum install java-1.8.0-openjdk
yum install tomcat
firewall -cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
systemctl start tomcat
PHP虚拟机:
yum update
yum install httpd
yum install php
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
systemctl restart httpd
安装完成以后,CPU内存占用情况:
Java:
PHP:
10次运算测试结果对比:
Java 平均耗时22.2毫秒,最大24毫秒,最小20毫秒
PHP 平均耗时14.6毫秒,最大17毫秒,最小12毫秒
接下来看看这二者之间的压力测试:
首先看看java的表现:
[ root @bogon ~]# ab -n 1000 -c 100
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd,
Licensed to The Apache Software Foundation,
Benchmarking testjava.****.com (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: nginx/1.20.1
Server Hostname: testjava.****.com
Server Port: 80
Document Path: /my/mytest.jsp?cs=10000
Document Length: 559191 bytes
Concurrency Level: 100
Time taken for tests: 9.625 seconds
Complete requests: 1000
Failed requests: 859
(Connect: 0, Receive: 0, Length: 859, Exceptions: 0)
Write errors: 0
Total transferred: 559405189 bytes
HTML transferred: 559192189 bytes
Requests per second: 103.90 [#/sec] (mean)
Time per request: 962.453 [ms] (mean)
Time per request: 9.625 [ms] (mean, across all concurrent requests)
Transfer rate: 56760.62 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 1.8 0 18
Processing: 27 937 673.3 816 4208
Waiting: 20 906 672.6 789 4178
Total: 28 938 673.4 817 4211
Percentage of the requests served within a certain time (ms)
50% 817
66% 1126
75% 1341
80% 1471
90% 1814
95% 2195
98% 2754
99% 3047
100% 4211 (longest request)
再看看PHP的表现:
[root@bogon ~]# ab -n 1000 -c 100
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd,
Licensed to The Apache Software Foundation,
Benchmarking testphp.****.com (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: nginx/1.20.1
Server Hostname: testphp.****.com
Server Port: 80
Document Path: /my/mytest.php?sl=10000
Document Length: 559191 bytes
Concurrency Level: 100
Time taken for tests: 3.138 seconds
Complete requests: 1000
Failed requests: 828
(Connect: 0, Receive: 0, Length: 828, Exceptions: 0)
Write errors: 0
Total transferred: 559353172 bytes
HTML transferred: 559190172 bytes
Requests per second: 318.65 [#/sec] (mean)
Time per request: 313.826 [ms] (mean)
Time per request: 3.138 [ms] (mean, across all concurrent requests)
Transfer rate: 174059.30 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 1.9 0 13
Processing: 50 299 87.0 273 676
Waiting: 23 277 83.8 255 614
Total: 54 300 87.1 274 678
Percentage of the requests served within a certain time (ms)
50% 274
66% 282
75% 293
80% 303
90% 437
95% 520
98% 585
99% 621
100% 678 (longest request)
可以看出,PHP的并发性能也优于Java。
有人说因为是小型项目,所以PHP优于Java,要知道大型项目也是由一个一个的子模块组合起来的,简单的小模块性能弱,最终的大模块也好不到哪里去,很多政府部门网页打开慢的原因,大多由Java完成的,但是,Java胜在生态,有很多轮子可用,而且可以编译、可以常驻内存,要说并发和性能优于其它语言,的确值得思考。
也许是我的程序设计有问题,期待各位大神批评指正,可私信提供测试平台。
海报
0 条评论
186
相关文章
本站已关闭游客评论,请登录或者注册后再评论吧~