代码性能-php(一)

写了几天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<c<=250 & a<50000

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

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性能优化


Ox01.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思想。

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



本文出自 夏日小草,转载请注明出处:http://homeway.me/2014/07/04/code-performance-php/


-by小草

2014-07-04

Fork me on GitHub