快捷搜索:  汽车  科技

php 比java 的开发效率高在哪(Java和PHP在网站服务器端性能对比测试)

php 比java 的开发效率高在哪(Java和PHP在网站服务器端性能对比测试)Java:yum update yum install httpd yum install php firewall-cmd --permanent --add-service=http firewall-cmd --reload systemctl restart httpd 安装完成以后,CPU内存占用情况:两个虚拟机均为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虚拟机:

为了真实体验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[] doTest(int cs){ String rtxt=""; String btxt="abcdefghigklmn"; String[] ss=new String[cs]; for(int i=0;i<cs;i )ss[i]=btxt i; try{ for(int i=0;i<cs;i ){ ss[i]=ss[i] " " MD5(ss[i]); } } catch(Exception ex){ //rtxt=rtxt "ERR" ex.toString(); } return ss; } %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Test with Java</title> </head> <body> <% 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("<input type='text' value='" cs "' id='sl'><input type='button' value='开始' onclick='location=\"?cs=\" Document.getElementById(\"sl\").value;'><br/>"); out.println("用时" (endTime-startTime) "毫秒<hr/>"); out.println(String.join("<br/>" otxt)); %> </body> </html>

PHP源代码:

<?php $btxt='abcdefghigklmn'; $t1=microtime(true); $otxt=""; $sl=empty($_GET['sl'])?100:intval($_GET['sl']); $s=''; $txt=array(); for($i=0;$i<$sl;$i )$txt[]=$btxt.$i; for($i=0;$i<$sl;$i )$txt[$i]=$txt[$i].' '.MD5($txt[$i]); $t2=microtime(true); $t3=intval(($t2-$t1)*1000); ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Test witth PHP</title> </head> <body> <?php echo '<input type="text" value="'.$sl.'" id="sl"><input type="button" value="开始" onclick="location=\'?sl=\' document.getElementById(\'sl\').value;">'; echo '<br/>耗时:'.$t3.'毫秒 共完成'.$sl.'条<hr/>'; //echo $otxt; echo join("<br/>" $txt); ?> </body> </html>

物理服务器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 比java 的开发效率高在哪(Java和PHP在网站服务器端性能对比测试)(1)

Java服务器,含操作系统共占用内存810M

PHP:

php 比java 的开发效率高在哪(Java和PHP在网站服务器端性能对比测试)(2)

PHP服务器,含操作系统共占用内存260M

10次运算测试结果对比:

Java 平均耗时22.2毫秒,最大24毫秒,最小20毫秒

PHP 平均耗时14.6毫秒,最大17毫秒,最小12毫秒

php 比java 的开发效率高在哪(Java和PHP在网站服务器端性能对比测试)(3)

可以看出,PHP运算的耗时明显低于Java

php 比java 的开发效率高在哪(Java和PHP在网站服务器端性能对比测试)(4)

php 比java 的开发效率高在哪(Java和PHP在网站服务器端性能对比测试)(5)

接下来看看这二者之间的压力测试:

首先看看java的表现:

[root@bogon ~]# ab -n 1000 -c 100 http://testjava.****.com/my/mytest.jsp?cs=10000 This is ApacheBench Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss Zeus Technology Ltd http://www.zeustech.net/ Licensed to The Apache Software Foundation http://www.apache.org/ 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 http://testphp.****.com/my/mytest.php?sl=10000 This is ApacheBench Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss Zeus Technology Ltd http://www.zeustech.net/ Licensed to The Apache Software Foundation http://www.apache.org/ 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胜在生态,有很多轮子可用,而且可以编译、可以常驻内存,要说并发和性能优于其它语言,的确值得思考。

也许是我的程序设计有问题,期待各位大神批评指正,可私信提供测试平台。

猜您喜欢: