Jump to content
新域网络技术论坛
Sign in to follow this  
Jamers

laravel/codeigniter/zpf简单测试

Recommended Posts

为了更直观的了解框架运行速度,特做了此次测试,为了公平起见,测试在同一台计算机上,各类请求分别请求10次,目前分三种情况,一是直接输出Hello World文本,二是使用默认模板输出相同内容,三是输出JSON串,其实一和三基本属于同一层面上的东西。先看测试代码。为了公平起见,未使用任何第三方代码。

<?php
/**
 * Created by PhpStorm.
 * User: Jamers
 * Date: 2019-06-27
 * Time: 10:18
 * File: speedtest.php
 */

set_time_limit(0);

$acts = [
    'text',
    'template',
    'json',
];

$urls = [
    'laravel' => 'http://localhost:8080/laravel/public/index.php/test/',
    'zpf' => 'http://localhost:8080/Test/zpf/index.php?a=',
    'ci' => 'http://localhost:8080/Test/ci/index.php/Test/',
];

$loop = 10;

$ret = [];

for ($i = $loop; $i > 0; $i--) {
    foreach ($acts as $act) {
        foreach ($urls as $k => $v) {
            $url = $v . $act;
            $s = microtime(true) * 1000;
            $data = getRequest($url);
            $time = round(microtime(true) * 1000 - $s, 4);
            if (!isset($ret[$act][$k])) {
                $ret[$act][$k] = ['total' => 0, 'max' => $time, 'min' => $time, 'avg' => 0, 'lost' => 0,];
            }
            $ret[$act][$k]['total'] += $time;
            if ($ret[$act][$k]['max'] < $time) {
                $ret[$act][$k]['max'] = $time;
            }
            if ($ret[$act][$k]['min'] > $time) {
                $ret[$act][$k]['min'] = $time;
            }
            if ($ret[$act][$k]['avg'] == 0) {
                $ret[$act][$k]['avg'] = $time;
            } else {
                $ret[$act][$k]['avg'] = round(($time + $ret[$act][$k]['avg'])/2, 4);
            }
            if (!$data) {
                $ret[$act][$k]['lost'] ++;
            }
        }
    }
}
$result = var_export($ret, true);
file_put_contents('times.txt', $result);
echo $result;

function getRequest($url, $header = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    if ($header) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $res = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    if ($info['http_code']!=200) {
        $res = '';
    }
    return $res;
}

 

框架方面,均使用默认配置,有修改的地方列出来:

Laravel 5.8.26,今天使用composer create-project --prefer-dist laravel/laravel laravel安装:

routes\web.php 添加以下路由:

Route::get('test/{act}', function (App\Http\Controllers\TestController $obj, $act) {
    return $obj->$act();
});

app\Http\Controllers\TestController.php

<?php
/**
 * Created by PhpStorm.
 * User: Jamers
 * Date: 2019-06-27
 * Time: 10:00
 * File: TestController.php
 */

namespace App\Http\Controllers;

class TestController extends Controller
{
    public function text()
    {
        exit('Hello World');
    }

    public function template()
    {
        $data = [
            'name' => 'World',
            'list' => [
                'Google' => 'https://www.google.com',
                'Baidu' => 'https://www.baidu.com',
                'Sohu' => 'https://www.sohu.com',
                'Zomew' => '',
            ],
        ];
        return view('demo', $data);
    }

    public function json()
    {
        $data = [
            'name' => 'World',
            'list' => [
                'Google' => 'https://www.google.com',
                'Baidu' => 'https://www.baidu.com',
                'Sohu' => 'https://www.sohu.com',
                'Zomew' => '',
            ],
        ];
        header('Content-type: application/json');
        exit(json_encode($data));
    }
}

resources\views\demo.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello {{ $name }}</title>
</head>
<body>
@if ($list && is_array($list))
    @foreach ($list as $k => $v)
        @if (trim($v))
            <a href="{{ $v }}" target="_blank">{{ $k }}</a><br>
        @else
            {{ $k }}<br>
        @endif
    @endforeach
@else
    no data
@endif
</body>
</html>

 

 

zpf几乎是原生代码,因为没有默认控制器只能新建入口文件,https://github.com/zomew/zpf

因为使用了Twig模板引擎,需要在git clone完成后,composer update一下。

新建index.php

<?php
/**
 * Created by PhpStorm.
 * User: Jamers
 * Date: 2019-06-27
 * Time: 9:32
 * File: index.php
 */

require_once '__INIT.php';

$act = \ZF\Common::input('a');

switch ($act) {
    case 'text':
        exit('Hello World');
        break;
    case 'template':
        $data = [
            'name' => 'World',
            'list' => [
                'Google' => 'https://www.google.com',
                'Baidu' => 'https://www.baidu.com',
                'Sohu' => 'https://www.sohu.com',
                'Zomew' => '',
            ],
        ];
        $twig = new \ZF\Twig();
        echo $twig->render('demo.twig', $data);
        break;
    case 'json':
        $data = [
            'name' => 'World',
            'list' => [
                'Google' => 'https://www.google.com',
                'Baidu' => 'https://www.baidu.com',
                'Sohu' => 'https://www.sohu.com',
                'Zomew' => '',
            ],
        ];
        header('Content-type: application/json');
        exit(json_encode($data));
        break;
    default:
        exit('Access deny');
        break;
}

views\demo.twig

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello {{ name }}</title>
</head>
<body>
{% if list %}
    {% for k,v in list %}
        {% if v|trim %}
            <a href="{{ v }}" target="_blank">{{ k }}</a><br>
        {% else %}
            {{ k }}<br>
        {% endif %}
    {% endfor %}
{% else %}
    no data
{% endif %}
</body>
</html>

 

 

CodeIgniter 3.1.10

application\controllers\Test.php

<?php
/**
 * Created by PhpStorm.
 * User: Jamers
 * Date: 2019-06-27
 * Time: 9:07
 * File: Test.php
 */

defined('BASEPATH') or exit('No direct script access allowed');

class Test extends CI_Controller
{
    public function text()
    {
        exit('Hello World');
    }

    public function template()
    {
        $data = [
            'name' => 'World',
            'list' => [
                'Google' => 'https://www.google.com',
                'Baidu' => 'https://www.baidu.com',
                'Sohu' => 'https://www.sohu.com',
                'Zomew' => '',
            ],
        ];
        $this->load->view('demo', $data);
    }

    public function json()
    {
        $data = [
            'name' => 'World',
            'list' => [
                'Google' => 'https://www.google.com',
                'Baidu' => 'https://www.baidu.com',
                'Sohu' => 'https://www.sohu.com',
                'Zomew' => '',
            ],
        ];
        header('Content-type: application/json');
        exit(json_encode($data));
    }
}

application\views\demo.php

<?php
defined('BASEPATH') or exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello <?php echo $name; ?></title>
</head>
<body>
<?php
if (isset($list) && $list && is_array($list)) {
    foreach ($list as $k => $v) {
        if (trim($v)) {
            echo "<a href='{$v}' target='_blank'>{$k}</a><br>";
        } else {
            echo "{$k}<br>";
        }
    }
} else {
    echo 'no data';
}
?>
</body>
</html>

 

 

相关测试代码仅这些,然后看一下本地测试的结果:

array (
  'text' => 
  array (
    'laravel' => 
    array (
      'total' => 3435.4015,
      'max' => 359.5203,
      'min' => 329.5181,
      'avg' => 353.3861,
      'lost' => 0,
    ),
    'zpf' => 
    array (
      'total' => 2270.3377,
      'max' => 234.7439,
      'min' => 221.2659,
      'avg' => 228.5257,
      'lost' => 0,
    ),
    'ci' => 
    array (
      'total' => 2397.8734999999997,
      'max' => 251.7878,
      'min' => 221.7522,
      'avg' => 231.6582,
      'lost' => 0,
    ),
  ),
  'template' => 
  array (
    'laravel' => 
    array (
      'total' => 5673.1377,
      'max' => 584.2161,
      'min' => 546.803,
      'avg' => 572.1757,
      'lost' => 0,
    ),
    'zpf' => 
    array (
      'total' => 2369.9499,
      'max' => 254.4719,
      'min' => 220.6382,
      'avg' => 233.7834,
      'lost' => 0,
    ),
    'ci' => 
    array (
      'total' => 4424.9958,
      'max' => 464.0081,
      'min' => 431.9641,
      'avg' => 446.3534,
      'lost' => 0,
    ),
  ),
  'json' => 
  array (
    'laravel' => 
    array (
      'total' => 3397.3995,
      'max' => 349.1572,
      'min' => 328.8938,
      'avg' => 342.091,
      'lost' => 0,
    ),
    'zpf' => 
    array (
      'total' => 2289.3552999999997,
      'max' => 237.9939,
      'min' => 214.7542,
      'avg' => 232.6302,
      'lost' => 0,
    ),
    'ci' => 
    array (
      'total' => 2363.8577999999998,
      'max' => 248.4502,
      'min' => 222.6829,
      'avg' => 240.1061,
      'lost' => 0,
    ),
  ),
)

 

从结果上来看,

1. 简单的hello world,laravel由于加载的内容较多,需要的时间足足比其它框架高出来100多ms,CodeIgniter和zpf几乎平手,平均速度相差2-3ms。

2. 模板引擎,这样框架需要加载的东西更多了,laravel又垫底,平均速度比CI高了125ms,CI与zpf相比高了212ms,差距还是很明显的。

3. JSON,之前也说过json和测试1的纯文本几乎一致,laravel还是慢100ms,CI和zpf相比高了7ms。

 

总结:如果是相对轻型的应用使用zpf应该会更合适一些,扩展方面需要自行完善,laravel虽然在测试中均在最后,不过由于它的可扩展性较强,目前仍有很多项目选择。CodeIgniter效率上中规中矩,但是模板引擎对前端来说太不友好了,也只能适合一些小型的应用,当然可以换模板引擎,那样的话不如换个框架来用了。

以上纯属个人看法,如果测试过程有所疏漏,烦请告知,谢谢。

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×
×
  • Create New...