- (一)简介 PEAR除了可以对输出的内容进行缓存处理外,还可以将对某个函数的调用结果缓存起来。这是个很有趣的功能,如果你的程序要频繁使用到某个函数,而且调用的结果相同的话,我建议你不妨试试,特别是当这个函数运行起来比较慢的时候。
- (二)是否需要加速?
- (三)如何加速?
-<1> 测试
◆ 服务器负载测试 ApacheBench
◆ 脚本执行速度测试 PEAR:: Benchmark
-<2> 加速
◆ 代码优化
◆ 压缩输出 Gzip
◆ 内容缓存输出 PEAR Content cache
◆ 函数缓存输出 PEAR Function cache
◆ 加速工具软件 APC、Turck MMCache、PHPA、Zend Performance Suite
- (四)总结
-c number_of_simultaneous_requests \
http://your_web_server/your_php_app.php
Server Hostname: localhost
Server Port: 80
Document Path: /myapp.php
Document Length: 1311 bytes
Concurrency Level: 50
Time taken for tests: 8.794 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 1754000 bytes
HTML transferred: 1311000 bytes
Requests per second: 113.71
Transfer rate: 199.45 kb/s received
Connection Times (ms)
min avg max
Connect: 0 0 5
Processing: 111 427 550
Total: 111 427 555
<?php
require_once('Benchmark/Iterate.php');
$benchmark = new Benchmark_Iterate();
$benchmark->run(10, 'myFunction','test');
$result = $benchmark->get();
echo "
"; print_r($result); echo "
";
exit;
function myFunction($var) {
// do something
echo 'Hello ';
}
?>
Array(
[1] => 0.000427
[2] => 0.000079
[3] => 0.000072
[4] => 0.000071
[5] => 0.000076
[6] => 0.000070
[7] => 0.000073
[8] => 0.000070
[9] => 0.000074
[10] => 0.000072
[mean] => 0.000108
[iterations] => 10
)
<?php
require_once 'Benchmark/Timer.php';
$timer = new Benchmark_Timer();
$timer->start();
$timer->setMarker('start_myFunction');
for($i=0; $i<10; $i++){
myFunction($argument);
}
$timer->setMarker('end_myFunction');
$timer->stop();
$profiling = $timer->getProfiling();
echo '
Time elapsed: ' .
$timer->timeElapsed('start_myFunction','end_myFunction') .'
';
echo '
'; print_r($profiling); echo '
';
exit;
function myFunction($var) {
static $counter = 0;
// do something
echo $counter++ . ' ';
}
?>
Array
(
[0] => Array
(
[name] => Start
[time] => 1085730111.27175200
[diff] => -
[total] => 1085730111.271752
)
[1] => Array
(
[name] => start_myFunction
[time] => 1085730111.27203800
[diff] => 0.000286
[total] => 1085730111.272038
)
[2] => Array
(
[name] => end_myFunction
[time] => 1085730111.27263200
[diff] => 0.000594
[total] => 1085730111.272632
)
[3] => Array
(
[name] => Stop
[time] => 1085730111.27271800
[diff] => 0.000086
[total] => 1085730111.272718
)
)
define('MAX',100);
if(ereg('gzip',$_SERVER['HTTP_ACCEPT_ENCODING']))
{
//浏览器支持gzip,将内容压缩并缓冲输出
ob_start("ob_gzhandler");
$output = '';
for($i=0;$i<=MAX;$i++)
{
$output .= "This is line $i
";
}
echo "浏览器支持gzip压缩输出
";
echo $output;
}
else
{
//浏览器不支持,直接输出
for($i=0;$i<=MAX;$i++)
{
$output .= "This is line $i
";
}
echo "浏览器不支持gzip压缩输出
";
echo $output;
}
?>
使用gzip压缩生成的网页的HTTP头信息与一般的网页相比中会多出这样的信息:
echo "这是内容。<P>";
echo "当前时间是" . date('M-d-Y H:i:s A', time()) . "<BR>";
?>
require_once 'Cache/Output.php';
//设置缓存目录,必须是可写的
$cacheDir = './pear_cache';
$cache = new Cache_Output('file',array('cache_dir' => $cacheDir));
//如果nocache变量为空,使用缓存中的内容
//如果想获得最新的内容,就要赋值给nocache变量
if (empty($_REQUEST['nocache']))
{
// 建立一个独一的cache标识
// 请求+Cookie信息
$cache_id = $cache->generateID(array('url' => $_REQUEST,'post' =>$_POST,'cookies' => $HTTP_COOKIE_VARS));
}
else
{
//想获得最新的内容,ID为空
$cache_id = null;
}
//看cache ID对应的缓存内容是否可用
if ($content = $cache->start($cache_id))
{
//缓存已存在,直接输出,并结束脚本
echo $content;
exit();
}
// 缓存中不存在该内容,生成新内容并写入缓存
echo "这是内容。<P>";
echo "当前时间是" . date('M-d-Y H:i:s A', time()) . "<BR>";
// 把内容写入缓存
echo $cache->end();
?>
{ echo $content;
exit();
}
require_once 'Cache/Function.php';
$cacheDir = './pear_cache/';
$cache = new Cache_Function('file',array('cache_dir' => $cacheDir));
$arr = array('苹果', '梨','西瓜');
$cache->call('slowFunction', $arr);
echo '<BR>';
$arr = array('苹果', '梨','西瓜');
slowFunction($arr);
function slowFunction($arr = null)
{
echo "一个执行起来很慢的函数 :( <br>";
echo "当前时间是 " . date('M-d-Y H:i:s A', time()) . '<br>';
foreach ($arr as $fruit)
{
echo "我吃了一个 $fruit <br>";
}
}
?>
当前时间是 Jul-28-2004 17:15:57 PM
我吃了一个 苹果
我吃了一个 梨
我吃了一个 西瓜
一个执行起来很慢的函数 :(
当前时间是 Jul-28-2004 17:17:55 PM
我吃了一个 苹果
我吃了一个 梨
我吃了一个 西瓜
http://turck-mmcache.sourceforge.net/
require_once('Benchmark/Iterate.php');
define('MAX_RUN',100);
$data = array(1, 2, 3, 4, 5);
doBenchmark('v1', $data);
doBenchmark('v2', $data);
doBenchmark('v3', $data);
function doBenchmark($functionName = null, $arr = null)
{
reset($arr);
$benchmark = new Benchmark_Iterate;
$benchmark->run(MAX_RUN, $functionName, $arr);
$result = $benchmark->get();
echo '<br>';
printf("%s ran %d times where average exec time %.5f ms",$functionName,$result['iterations'],$result['mean'] * 1000);
}
function v1($myArray = null) {
// 效率很差的循环
for ($i =0; $i < sizeof($myArray); $i++)
{
echo '<!--' . $myArray[$i] . ' --> ';
}
}
function v2($myArray = null) {
// 效率略有提高
$max = sizeof($myArray);
for ($i =0; $i < $max ; $i++)
{
echo '<!--' . $myArray[$i] . ' --> ';
}
}
function v3($myArray = null){
//最佳效率
echo "<!--", implode(" --> <!--", $myArray), " --> ";
}
?>
v2 ran 100 times where average exec time 0.15500 ms
v3 ran 100 times where average exec time 0.09100 ms
<?php
if(ereg('gzip',$_SERVER['HTTP_ACCEPT_ENCODING'])) {
//浏览器支持
} else {
//浏览器不支持,输出其它内容
}
?> 接下来我们对上面这个PHP程序进行扩展,使用ob_start(ob_gzhandler)来将网页内容压缩,存入缓冲并发送给支持gzip的浏览器,浏览器会自动将压缩后的内容解压,显示。
