在我们平时的php开发中,一个大的项目经过长时间的积累以后你会发现性能越来越慢,而性能到底消耗在了什么地方,常常是一个令人头疼的问题,function a()调用了多少次,function b()又消耗了多少时间,我们到底怎么查找是哪个蛀虫拉慢了我们的程序运行速度呢?在这里给大家介绍一款工具xdebug,相信很多人已经听说过了,希望借助这个工具我们可以起到简单分析php程序性能瓶颈的问题。
A)假设1,假设用户目录在/home/ad
B)假设2,假设php目录在/home/ad/php
1、xdebug简介与安装
Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况。
1)下载xdebug
xdebug的官方下载地址为:http://xdebug.org/download.php
最新版本为:Xdebug 2.1.0
2)xdebug的安装
1
2
3
4
5
6
7
8
cd /home/ad
wget http://xdebug.org/files/xdebug-2.1.0.tgz
tar -zxvf xdebug-2.1.0.tgz
cd xdebug-2.1.0
/home/ad/php/bin/phpize
./configure --enable-xdebug --with-php-config=/home/ad/php/bin/php-config
make
make install
安装完以后会提示你扩展安装到了哪个目录,类似 /home/ad/php/lib/php/extensions/no-debug-non-zts-20060613/
假设你的php.ini放在 /home/ad/php/lib/php.ini
加上
1
2
3
4
5
6
7
8
9
[xdebug]
zend_extension = "/home/ad/php/lib/php/extensions/no-debug-non-zts-20060613/xdebug.so"
xdebug.auto_trace = on
xdebug.auto_profile = on
xdebug.collect_params = on
xdebug.collect_return = on
xdebug.profiler_enable = on
xdebug.trace_output_dir = "/home/ad/xdebug_log"
xdebug.profiler_output_dir = "/home/ad/xdebug_log"
重启apache
去/home/ad/xdebug_log下看看是不是日志已经出来了
2、xdebug参数简介
zend_extension 加载xdebug扩展
xdebug.auto_trace 自动打开打开函数调用监测
xdebug.auto_profile 自动打开性能监测
xdebug.trace_output_dir 设定函数调用监测信息的输出文件的路径。
xdebug.profiler_output_dir 设定效能监测信息输出文件的路径。
xdebug.collect_params 打开收集“函数参数”的功能。将函数调用的参数值列入函数过程调用的监测信息中。
xdebug.collect_return 打开收集“函数返回值”的功能。将函数的返回值列入函数过程调用的监测信息中。
3、示例程序与日志收集
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
function a()
{
echo "aaa";
}
function b()
{
a();
sleep(1);
a();
sleep(1);
a();
}
b();
?>
4、日志分析工具wincachegrind
http://sourceforge.net/projects/wincachegrind/
不用安装直接双击就可以打开了
我们用它打开刚才收集的日志cachegrind.out.***
前端开发中的性能那点事(二)巧用curl 并发减少后端访问时间
前言:
在我们平时的程序中难免出现同时访问几个接口的情况,平时我们用curl进行访问的时候,一般都是单个、顺序访问,假如有3个接口,每个接口耗时500毫秒那么我们三个接口就要花费1500毫秒了,这个问题太头疼了严重影响了页面访问速度,有没有可能并发访问来提高速度呢?今天就简单的说一下,利用curl并发来提高页面访问速度,
希望大家多指导。
1、老的curl访问方式以及耗时统计
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
function curl_fetch($url, $timeout=3){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno>0) {
$data = false;
}
curl_close($ch);
return $data;
}
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$url_arr=array(
"taobao"=>"http://www.taobao.com",
"sohu"=>"http://www.sohu.com",
"sina"=>"http://www.sina.com.cn",
);
$time_start = microtime_float();
$data=array();
foreach ($url_arr as $key=>$val)
{
$data[$key]=curl_fetch($val);
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "耗时:{$time}";
?>
耗时:0.614秒
2、curl并发访问方式以及耗时统计
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57