公司的服务器上安装了两个php版本,版本一:php5.6.30,版本二:php7.2.25
现在我们使用命令行模式分别测试性能
版本一测试结果:
版本二测试结果:
此结果单纯只反映出php版本cpu性能差异,下面展示一下内部框架与Laravel的性能对比
使用ApacheBench来测试一下性能对比差异
内部框架php5.6下的测试结果
内部框架php7.2测试结果
laravel5.8版本7.2测试结果:
结果显而易见laravel重度框架性能较低,同时需要注意的是以上测试基于http测试,https由于采用了非对称式加密所以测试速度会慢。
内部框架php5.6,https测试结果
下面附上php cpu测试的代码,linux下切换到文件目录使用time php micro_bench.php 即可开启CPU性能测试
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/12/18
* Time: 16:20
*/
function hallo()
{
}
function simpleucall($n)
{
for ($i = 0; $i < $n; $i++)
hallo();
}
function simpleudcall($n)
{
for ($i = 0; $i < $n; $i++)
hallo2();
}
function hallo2()
{
}
function simpleicall($n)
{
for ($i = 0; $i < $n; $i++)
func\_num\_args();
}
class Foo
{
static $a = 0;
public $b = 0;
const TEST = 0;
static function read_static($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = self::$a;
}
}
static function write_static($n)
{
for ($i = 0; $i < $n; ++$i) {
self::$a = 0;
}
}
static function isset_static($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = isset(self::$a);
}
}
static function empty_static($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = empty(self::$a);
}
}
static function f()
{
}
static function call_static($n)
{
for ($i = 0; $i < $n; ++$i) {
self::f();
}
}
function read_prop($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = $this->b;
}
}
function write_prop($n)
{
for ($i = 0; $i < $n; ++$i) {
$this->b = 0;
}
}
function assign\_add\_prop($n)
{
for ($i = 0; $i < $n; ++$i) {
$this->b += 2;
}
}
function pre\_inc\_prop($n)
{
for ($i = 0; $i < $n; ++$i) {
++$this->b;
}
}
function pre\_dec\_prop($n)
{
for ($i = 0; $i < $n; ++$i) {
--$this->b;
}
}
function post\_inc\_prop($n)
{
for ($i = 0; $i < $n; ++$i) {
$this->b++;
}
}
function post\_dec\_prop($n)
{
for ($i = 0; $i < $n; ++$i) {
$this->b--;
}
}
function isset_prop($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = isset($this->b);
}
}
function empty_prop($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = empty($this->b);
}
}
function g()
{
}
function call($n)
{
for ($i = 0; $i < $n; ++$i) {
$this->g();
}
}
function read_const($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = $this::TEST;
}
}
}
function read_static($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = Foo::$a;
}
}
function write_static($n)
{
for ($i = 0; $i < $n; ++$i) {
Foo::$a = 0;
}
}
function isset_static($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = isset(Foo::$a);
}
}
function empty_static($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = empty(Foo::$a);
}
}
function call_static($n)
{
for ($i = 0; $i < $n; ++$i) {
Foo::f();
}
}
function create_object($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = new Foo();
}
}
define('TEST', null);
function read_const($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = TEST;
}
}
function read\_auto\_global($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = $_GET;
}
}
$g_var = 0;
function read\_global\_var($n)
{
for ($i = 0; $i < $n; ++$i) {
$x = $GLOBALS\['g_var'\];
}
}
function read_hash($n)
{
$hash = array('test' => 0);
for ($i = 0; $i < $n; ++$i) {
$x = $hash\['test'\];
}
}
function read\_str\_offset($n)
{
$str = "test";
for ($i = 0; $i < $n; ++$i) {
$x = $str\[1\];
}
}
function issetor($n)
{
$val = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for ($i = 0; $i < $n; ++$i) {
$x = $val ?: null;
}
}
function issetor2($n)
{
$f = false;
$j = 0;
for ($i = 0; $i < $n; ++$i) {
$x = $f ?: $j + 1;
}
}
function ternary($n)
{
$val = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$f = false;
for ($i = 0; $i < $n; ++$i) {
$x = $f ? null : $val;
}
}
function ternary2($n)
{
$f = false;
$j = 0;
for ($i = 0; $i < $n; ++$i) {
$x = $f ? $f : $j + 1;
}
}
/*****/
function empty_loop($n)
{
for ($i = 0; $i < $n; ++$i) {
}
}
function getmicrotime()
{
$t = gettimeofday();
return ($t\['sec'\] + $t\['usec'\] / 1000000);
}
function start_test()
{
ob_start();
return getmicrotime();
}
function end_test($start, $name, $overhead = null)
{
global $total;
global $last_time;
$end = getmicrotime();
ob\_end\_clean();
$last_time = $end - $start;
$total += $last_time;
$num = number\_format($last\_time, 3);
$pad = str_repeat(" ", 24 - strlen($name) - strlen($num));
if (is_null($overhead)) {
echo $name . $pad . $num . "n";
} else {
$num2 = number\_format($last\_time - $overhead, 3);
echo $name . $pad . $num . " " . $num2 . "n";
}
ob_start();
return getmicrotime();
}
function total()
{
global $total;
$pad = str_repeat("-", 24);
echo $pad . "n";
$num = number_format($total, 3);
$pad = str_repeat(" ", 24 - strlen("Total") - strlen($num));
echo "Total" . $pad . $num . "n";
}
const N = 5000000;
$t0 = $t = start_test();
empty_loop(N);
$t = end\_test($t, 'empty\_loop');
$overhead = $last_time;
simpleucall(N);
$t = end_test($t, 'func()', $overhead);
simpleudcall(N);
$t = end\_test($t, 'undef\_func()', $overhead);
simpleicall(N);
$t = end\_test($t, 'int\_func()', $overhead);
Foo::read_static(N);
$t = end_test($t, '$x = self::$x', $overhead);
Foo::write_static(N);
$t = end_test($t, 'self::$x = 0', $overhead);
Foo::isset_static(N);
$t = end_test($t, 'isset(self::$x)', $overhead);
Foo::empty_static(N);
$t = end_test($t, 'empty(self::$x)', $overhead);
read_static(N);
$t = end_test($t, '$x = Foo::$x', $overhead);
write_static(N);
$t = end_test($t, 'Foo::$x = 0', $overhead);
isset_static(N);
$t = end_test($t, 'isset(Foo::$x)', $overhead);
empty_static(N);
$t = end_test($t, 'empty(Foo::$x)', $overhead);
Foo::call_static(N);
$t = end_test($t, 'self::f()', $overhead);
call_static(N);
$t = end_test($t, 'Foo::f()', $overhead);
$x = new Foo();
$x->read_prop(N);
$t = end_test($t, '$x = $this->x', $overhead);
$x->write_prop(N);
$t = end_test($t, '$this->x = 0', $overhead);
$x->assign\_add\_prop(N);
$t = end_test($t, '$this->x += 2', $overhead);
$x->pre\_inc\_prop(N);
$t = end_test($t, '++$this->x', $overhead);
$x->pre\_dec\_prop(N);
$t = end_test($t, '--$this->x', $overhead);
$x->post\_inc\_prop(N);
$t = end_test($t, '$this->x++', $overhead);
$x->post\_dec\_prop(N);
$t = end_test($t, '$this->x--', $overhead);
$x->isset_prop(N);
$t = end_test($t, 'isset($this->x)', $overhead);
$x->empty_prop(N);
$t = end_test($t, 'empty($this->x)', $overhead);
$x->call(N);
$t = end_test($t, '$this->f()', $overhead);
$x->read_const(N);
$t = end_test($t, '$x = Foo::TEST', $overhead);
create_object(N);
$t = end_test($t, 'new Foo()', $overhead);
read_const(N);
$t = end_test($t, '$x = TEST', $overhead);
read\_auto\_global(N);
$t = end\_test($t, '$x = $\_GET', $overhead);
read\_global\_var(N);
$t = end_test($t, '$x = $GLOBALS\["v"\]', $overhead);
read_hash(N);
$t = end_test($t, '$x = $hash\["v"\]', $overhead);
read\_str\_offset(N);
$t = end_test($t, '$x = $str\[0\]', $overhead);
issetor(N);
$t = end_test($t, '$x = $a ?: null', $overhead);
issetor2(N);
$t = end_test($t, '$x = $f ?: tmp', $overhead);
ternary(N);
$t = end_test($t, '$x = $f ? $f : $a', $overhead);
ternary2(N);
$t = end_test($t, '$x = $f ? $f : tmp', $overhead);
total($t0, "Total");
来源:oschina
链接:https://my.oschina.net/u/4087795/blog/3213194