• require和require_once经济性能对比


    require和require_once都是PHP函数,开发人员可以使用它们在某个特定的脚本中导入外部PHP文件。你可以根据应用程序的复杂度调用一次或若干次require_once/require。使用require(而不是require_once)可以提高应用程序的性能

    测试前准备:

    创建4个空的类来模拟主脚本要使用的外部PHP文件脚本。

    ClassA.php:

    <?php
    
    class ClassA
    {
    
    }

    ClassB.php:

    <?php
    
    class ClassB
    {
    
    }

    ClassC.php:

    <?php
    
    class ClassC
    {
    
    }

    ClassD.php:

    <?php
    
    class ClassD
    {
    
    }

    使用require_once导入外部文件:

    index.php:

    <?php
    
    require_once './ClassA.php';
    require_once './ClassB.php';
    require_once './ClassC.php';
    require_once './ClassD.php';
    
    echo "Only testing require_once";

    重启服务器,使用ab压力测试工具模拟10000个请求,同一时间有5个并发请求。

    ab.exe -c 5 -n 10000 http://www.demo.com/optimization/index.php

    测试结果:

    D:phpStudyApachein>ab.exe -c 5 -n 10000 http://www.demo.com/optimization/index.php
    This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/
    
    Benchmarking www.demo.com (be patient)
    Completed 1000 requests
    Completed 2000 requests
    Completed 3000 requests
    Completed 4000 requests
    Completed 5000 requests
    Completed 6000 requests
    Completed 7000 requests
    Completed 8000 requests
    Completed 9000 requests
    Completed 10000 requests
    Finished 10000 requests
    
    
    Server Software:        Apache/2.4.23
    Server Hostname:        www.demo.com
    Server Port:            80
    
    Document Path:          /optimization/index.php
    Document Length:        25 bytes
    
    Concurrency Level:      5
    Time taken for tests:   17.446 seconds
    Complete requests:      10000
    Failed requests:        0
    Total transferred:      2280000 bytes
    HTML transferred:       250000 bytes
    Requests per second:    573.20 [#/sec] (mean)
    Time per request:       8.723 [ms] (mean)
    Time per request:       1.745 [ms] (mean, across all concurrent requests)
    Transfer rate:          127.63 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.4      0       2
    Processing:     2    8  90.4      4    2503
    Waiting:        1    7  90.5      3    2503
    Total:          2    8  90.4      4    2504
    
    Percentage of the requests served within a certain time (ms)
      50%      4
      66%      5
      75%      5
      80%      5
      90%      5
      95%      5
      98%      6
      99%      6
     100%   2504 (longest request)

    使用ab工具测试require_once,可以看到相应时间是8.723ms,该脚本每秒支持573.20个请求。

    使用require导入外部文件:

    index_1.php:

    <?php
    
    require './ClassA.php';
    require './ClassB.php';
    require './ClassC.php';
    require './ClassD.php';
    
    echo "Only testing require";

    重启服务器,使用ab压力测试工具模拟10000个请求,同一时间有5个并发请求。

    ab.exe -c 5 -n 10000 http://www.demo.com/optimization/index_1.php

    测试结果:

    D:phpStudyApachein>ab.exe -c 5 -n 10000 http://www.demo.com/optimization/index_1.php
    This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/
    
    Benchmarking www.demo.com (be patient)
    Completed 1000 requests
    Completed 2000 requests
    Completed 3000 requests
    Completed 4000 requests
    Completed 5000 requests
    Completed 6000 requests
    Completed 7000 requests
    Completed 8000 requests
    Completed 9000 requests
    Completed 10000 requests
    Finished 10000 requests
    
    
    Server Software:        Apache/2.4.23
    Server Hostname:        www.demo.com
    Server Port:            80
    
    Document Path:          /optimization/index_1.php
    Document Length:        20 bytes
    
    Concurrency Level:      5
    Time taken for tests:   17.207 seconds
    Complete requests:      10000
    Failed requests:        0
    Total transferred:      2230000 bytes
    HTML transferred:       200000 bytes
    Requests per second:    581.16 [#/sec] (mean)
    Time per request:       8.604 [ms] (mean)
    Time per request:       1.721 [ms] (mean, across all concurrent requests)
    Transfer rate:          126.56 [Kbytes/sec] received
    
    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.4      0      21
    Processing:     1    8  86.5      4    2320
    Waiting:        1    7  86.5      3    2320
    Total:          1    8  86.5      4    2320
    
    Percentage of the requests served within a certain time (ms)
      50%      4
      66%      5
      75%      5
      80%      5
      90%      5
      95%      6
      98%      7
      99%      7
     100%   2320 (longest request)
    
    D:phpStudyApachein>ab.exe -c 5 -n 10000 http://www.demo.com/optimization/index_1.php
    This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
    Licensed to The Apache Software Foundation, http://www.apache.org/
    
    Benchmarking www.demo.com (be patient)
    Completed 1000 requests
    Completed 2000 requests
    Completed 3000 requests
    Completed 4000 requests
    Completed 5000 requests
    Completed 6000 requests
    Completed 7000 requests
    Completed 8000 requests
    Completed 9000 requests
    Completed 10000 requests
    Finished 10000 requests

    使用ab工具测试require,可以看到相应时间是8.604ms,该脚本每秒支持581.16个请求。

  • 相关阅读:
    python实现单线程多任务非阻塞TCP服务端
    sudo命令
    ajax 未加载出数据时,显示loding,数据显示后,隐藏loading
    ie7 解决圆角,阴影问题
    学习JavaScript你必须掌握的8大知识点!
    es6 个人笔记
    原生js,实现单选框
    原生js开发简单复选框,点击变颜色,再点击取消颜色,三种解决方法
    es6对象转数组
    JS正则截取两个字符串之间及字符串前后内容的方法
  • 原文地址:https://www.cnblogs.com/zhouguowei/p/9444085.html
Copyright © 2020-2023  润新知