资讯专栏INFORMATION COLUMN

代码性能-php(一)

GeekQiaQia / 1467人阅读

摘要:本文关于路由性能。性能测试其中,请求次数,并发次数。主要用于测试相应速度,并发性相应等。主要测试服务器压力性能,并发性访问,攻击性能等。性能优化性能测试对于文件包含测试,采用命令,文件内容为。下一节,关于调试工具,与性能测试小草

  

新博客地址:http://homeway.me/

写了几天python.tornado,换下口味,看看教程,回顾下PHP。

本文关于php路由 && php性能。

php性能测试
0X01.Apache Benchmark

ab [options] [full url]

  

ab -n 100 -c 100 http://homeway.me/

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking homeway.me (be patient).....done


Server Software:
Server Hostname:        homeway.me
Server Port:            80

Document Path:          /
Document Length:        0 bytes

Concurrency Level:      100
Time taken for tests:   3.464 seconds
Complete requests:      100
Failed requests:        99

(Connect: 0, Receive: 0, Length: 99, Exceptions: 0)
Write errors:           0
Non-2xx responses:      1
Total transferred:      1781365 bytes
HTML transferred:       1749568 bytes
Requests per second:    28.87 [#/sec] (mean)
Time per request:       3463.798 [ms] (mean)
Time per request:       34.638 [ms] (mean, across all concurrent requests)
Transfer rate:          502.23 [Kbytes/sec] received

Connection Times (ms)
          min  mean[+/-sd] median   max
Connect:      290 1454 179.6   1435    1610
Processing:   288 1091 404.5    988    2018
Waiting:      284  395  69.0    426     558
Total:        578 2545 475.4   2507    3462

Percentage of the requests served within a certain time (ms)
    50%   2507
    66%   2615
    75%   2737
    80%   2743
    90%   3448
    95%   3452
    98%   3457
    99%   3462
    100%   3462 (longest request)

其中,-n 请求次数, -c 并发次数。

0

主要用于测试相应速度,并发性相应等。

0x02.Siege
Information

siege主要测试服务器压力性能,并发性访问,dos攻击性能等。

Install

mac下安装 brew install siege

centos源码安装

wget http://download.joedog.org/siege/siege-3.0.7.tar.gz
tar -zvxf siege-3.0.7.tar.gz
cd siege-3.0.7
./configure
make && make install
Use
  

siege -c 300 -r 100 -f url.txt

说明:-c是并发量,-r是重复次数。url.txt就是一个文本文件,每行都是一个url,它会从里面随机访问的。

  

siege -c 200 -r 10 http://homeway.me/

Lifting the server siege...      done.

Transactions:                321 hits
Availability:             100.00 %
Elapsed time:               5.06 secs
Data transferred:           1.53 MB
Response time:              1.70 secs
Transaction rate:          63.44 trans/sec
Throughput:             0.30 MB/sec
Concurrency:              107.99
Successful transactions:         321
Failed transactions:               0
Longest transaction:            3.85
Shortest transaction:           0.70
php性能优化
0x01.require && require_once && include 性能测试

对于文件包含测试,采用ab命令,php文件内容为。

  

require_once.php

require_once ("test/test/test/test1.php");
require_once ("test/test/test/test2.php");
require_once ("test/test/test/test3.php");
require_once ("test/test/test/test4.php");
require_once ("test/test/test/test5.php");
require_once ("test/test/test/test6.php");
require_once ("test/test/test/test7.php");
require_once ("test/test/test/test8.php");
require_once ("test/test/test/test9.php");
require_once ("test/test/test/test10.php");
  

ab -n 10000 -t 10 http://127.0.0.1/require_once.php

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 5000 requests
Completed 10000 requests
Finished 10406 requests

Server Software:        Apache/2.4.9
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /require_once.php
Document Length:        0 bytes

Concurrency Level:      1
Time taken for tests:   10.001 seconds
Complete requests:      10406
Failed requests:        0
Write errors:           0
Total transferred:      2414192 bytes
HTML transferred:       0 bytes
Requests per second:    1040.50 [#/sec] (mean)
Time per request:       0.961 [ms] (mean)
Time per request:       0.961 [ms] (mean, across all concurrent requests)
Transfer rate:          235.74 [Kbytes/sec] received

Connection Times (ms)
          min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     1    1   0.1      1       5
Waiting:        0    1   0.1      1       5
Total:          1    1   0.2      1       5

Percentage of the requests served within a certain time (ms)
    50%      1
    66%      1
    75%      1
    80%      1
    90%      1
    95%      1
    98%      1
    99%      1
    100%      5 (longest request)

另一个

  

require.php

require ("test/test/test/test1.php");
require ("test/test/test/test2.php");
require ("test/test/test/test3.php");
require ("test/test/test/test4.php");
require ("test/test/test/test5.php");
require ("test/test/test/test6.php");
require ("test/test/test/test7.php");
require ("test/test/test/test8.php");
require ("test/test/test/test9.php");
require ("test/test/test/test10.php");  
  

ab -n 10000 -t 10 http://127.0.0.1/require.php

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 5000 requests
Finished 9571 requests

Server Software:        Apache/2.4.9
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /require.php
Document Length:        0 bytes

Concurrency Level:      1
Time taken for tests:   10.000 seconds
Complete requests:      9571
Failed requests:        0
Write errors:           0
Total transferred:      2220472 bytes   
HTML transferred:       0 bytes
Requests per second:    957.09 [#/sec] (mean)
Time per request:       1.045 [ms] (mean)
Time per request:       1.045 [ms] (mean, across all concurrent requests)
Transfer rate:          216.84 [Kbytes/sec] received

Connection Times (ms)
          min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       4
Processing:     1    1   0.6      1      25
Waiting:        1    1   0.5      1      25
Total:          1    1   0.6      1      25

Percentage of the requests served within a certain time (ms)
    50%      1
    66%      1
    75%      1
    80%      1
    90%      1
    95%      1
    98%      2
    99%      4
    100%     25 (longest request)

用require相比requre_once,链接总时间一个10.000,一个10.001,差不多,但是,Connection Times就明显变大了。

详细差别可以看这个: http://www.jb51.net/article/13968.htm



php路由设计
0x01. 路由实现

常见的重定向有.htaccess(apache),nginx修改conf配置文件,要么就读取URI重写URI。

前面两类是用配置文件解决,后面一类是用代码模型来解决。

关于路由模型,我还是比较喜欢CI的MVC思想。

0x02. .htaccess

windows不支持点开头的文件名,在linux里创建后下载到windows,我是直接在sublime里面创建这个文件。

.htaccess教程很多啦,也没有必要专门去学习。

下面是一个htaccess生成工具。

http://www.htaccesseditor.com/sc.shtml

下面是一个.htaccess转成nginx下conf配置的工具。

http://winginx.com/en/htaccess

0x03. 读取URI

比较常见的是类似CI的 /index.php/class/function 型

教程也是有的。

http://www.cnblogs.com/fredshare/archive/2012/09/17/2688944.html

今天在github上看到另一个,自主式获取。可以自由定义url,缺点就是,不能指定有需要时再加载php文件。

明天先研究下代码,然后修改下,优化下。

https://github.com/anandkunal/ToroPHP


下一节,

关于php调试工具,xdebug

sql cache 与 nosql cache性能测试


-by小草

2014-09-04

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/20827.html

相关文章

  • PHP应用性能优化指南

    摘要:怎样才算是高性能的应用性能和速度不是一对同义词。红线表示针对速度进行了优化的脚本,蓝线是可扩展性优先的脚本。将任何这些功能置于循环中可能会导致性能问题。完整的代码检测评估虽然可能很耗时,但它可以为你提供有关应用程序性能的深入信息。 showImg(https://segmentfault.com/img/bVNxDn?w=900&h=500);程序员都喜欢最新的PHP 7,因为它使PH...

    EddieChan 评论0 收藏0
  • PHP 性能分析与实验——性能的宏观分析

    摘要:本文就改变性能分析的角度,并通过实例来分析出的性能方面需要注意和改进的点。如下是作为解释性语言的执行过程。这里分别启用和做实验。 此前,阅读过了很多关于 PHP 性能分析的文章,不过写的都是一条一条的规则,而且,这些规则并没有上下文,也没有明确的实验来体现出这些规则的优势,同时讨论的也侧重于一些语法要点。本文就改变PHP 性能分析的角度,并通过实例来分析出 PHP 的性能方面需要注意和...

    ZHAO_ 评论0 收藏0
  • PHP 性能分析与实验——性能的宏观分析

    摘要:本文就改变性能分析的角度,并通过实例来分析出的性能方面需要注意和改进的点。如下是作为解释性语言的执行过程。这里分别启用和做实验。 此前,阅读过了很多关于 PHP 性能分析的文章,不过写的都是一条一条的规则,而且,这些规则并没有上下文,也没有明确的实验来体现出这些规则的优势,同时讨论的也侧重于一些语法要点。本文就改变PHP 性能分析的角度,并通过实例来分析出 PHP 的性能方面需要注意和...

    Ilikewhite 评论0 收藏0
  • php性能怎么优化?php性能优化及安全策略

    摘要:性能问题一般不会超过占整个项目性能的,一般在。内置函数的性能优劣。几乎与在函数中调用局部变量的速度相当。递增一个全局变量要比递增一个局部变量慢倍。类似的方法调用所花费的时间接近于次的局部变量递增操作。 php性能怎么优化?性能是网站运行是否良好的关键因素, 网站的性能与效率影响着公司的运营成本及长远发展,编写出高质高效的代码是我们每个开发人员必备的素质,也是我们良好...

    番茄西红柿 评论0 收藏2637
  • PHP性能优化方法总结

    摘要:内置函数的性能优劣。产生额外开销的错误抑制符号,最好别用不管是性能优化和项目的健壮性等方面。在方法中递增局部变量,速度是最快的。类似的方法调用所花费的时间接近于次的局部变量递增操作。 什么情况之下,会遇到PHP性能问题?1:PHP语法使用不恰当。2:使用PHP语言做了它不擅长的事情。3:使用PHP语言连接的服务不给力。4:PHP自身的短板(PHP自身做不了的事情)。5:我们也不知道的问...

    gekylin 评论0 收藏0
  • PHP 性能分析第篇: Xhprof & Xhgui 介绍

    摘要:注这是我们应用性能分析系列的第一篇,阅读第二篇可深入了解,第三篇则关注于性能调优实践。性能分析的行为也会影响应用性能。主动被动性能分析主动分析器在开发过程中使用,由开发人员启用。它对性能的影响最小,同时收集足够的信息用于诊断性能问题。 注:这是我们 PHP 应用性能分析系列的第一篇,阅读第二篇可深入了解 xhgui,第三篇则关注于性能调优实践。 什么是性能分析? 性能分析是衡量应用程...

    RdouTyping 评论0 收藏0

发表评论

0条评论

GeekQiaQia

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<