PHP静态,动态编译性能对比测试
1. 软件版本
Web服务器
nginx-0.7.17
php-5.2.6
eaccelerator-0.9.5.3
2. PHP编译方式
第一种为纯静态编译
编译参数如下
--with-config-file-path=/usr/local/webserver/php-static \
--enable-fastcgi \
--enable-fpm \
--enable-force-cgi-redirect \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--enable-gd-native-ttf \
--with-freetype-dir \
--with-mysql=/usr/local/mysql \
--with-mcrypt \
--with-mhash \
--with-curl \
--with-curlwrappers \
--with-zlib-dir \
--with-zlib \
--with-pear \
--with-openssl \
--without-pdo-sqlite \
--without-sqlite \
--disable-debug \
--disable-rpath \
--without-iconv \
--enable-bcmath \
--enable-pcntl \
--enable-mbstring \
--enable-exif \
--enable-zip \
--enable-ftp
第二种以及第三种将所有模块作为动态链接库
编译参数如下
--with-config-file-path=/usr/local/webserver/conf \
--enable-fastcgi \
--enable-fpm \
--with-fpm-conf=/usr/local/webserver/conf/php-fpm.conf \
--enable-force-cgi-redirect \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--enable-gd-native-ttf \
--with-freetype-dir \
--with-mysql=/usr/local/webserver/mysql \
--with-libxml-dir \
--with-curl \
--with-curlwrappers \
--with-zlib \
--with-pear \
--enable-mbstring \
--enable-exif \
--disable-debug \
--disable-rpath \
--without-pdo-sqlite \
--without-sqlite \
--without-iconv
3.测试方法
将bcmath模块作为测试对象,以下为测试脚本
$a = '1.234';
$b = '5';
$i=0;
while($i<1000)
{
echo bcadd($a, $i);
echo bcadd($a, $i, 4);
echo bcdiv($i, '6.55957', 3);
$i++;
}
?>
另一台服务器发起http_load对指定脚本进行获取.对比最终的fetch数
4.测试数据
全部模块为静态编译,加载eaccelerator
USER %CPU %MEM VSZ RSS TTY STAT START TIME
root 0 1.2 94740 3284 ? Ss 04:44 00:00
nobody 0.3 1.8 95232 4720 ? S 04:44 00:00
nobody 0.2 1.6 95128 4368 ? S 04:44 00:00
nobody 0.3 1.6 95128 4368 ? S 04:44 00:00
nobody 0.3 1.6 95128 4368 ? S 04:44 00:00
nobody 0.4 1.6 95128 4368 ? S 04:44 00:00
nobody 0.4 1.6 95128 4368 ? S 04:44 00:00
nobody 0.4 1.6 95128 4368 ? S 04:44 00:00
nobody 0.4 1.6 95128 4368 ? S 04:44 00:00
nobody 0.3 1.6 95128 4368 ? S 04:44 00:00
nobody 0.3 1.6 95128 4368 ? S 04:44 00:00
nobody 0.4 1.6 95128 4368 ? S 04:44 00:00
nobody 0.4 1.6 95128 4368 ? S 04:44 00:00
nobody 0.4 1.6 95128 4372 ? S 04:44 00:00
nobody 0.4 1.6 95128 4372 ? S 04:44 00:00
nobody 0.8 1.6 95128 4372 ? S 04:44 00:00
nobody 0.5 1.6 95128 4372 ? S 04:44 00:00
nobody 0.9 1.6 95128 4372 ? S 04:44 00:00
nobody 2.9 1.6 95128 4372 ? S 04:44 00:01
nobody 9.6 1.6 95128 4376 ? S 04:45 00:02
nobody 10.8 1.6 95128 4376 ? S 04:45 00:02
nobody 9.9 1.6 95128 4376 ? S 04:45 00:02
nobody 10.6 1.6 95128 4376 ? S 04:45 00:02
nobody 10.1 1.6 95128 4376 ? S 04:45 00:02
nobody 10 1.6 95128 4376 ? S 04:45 00:01
nobody 5.6 1.6 95128 4376 ? S 04:45 00:00
SUM 76.6 41.4 2473044 112916
速度测试结果
161.396 fetches/sec, 2.75422e+06 bytes/sec
166.377 fetches/sec, 2.83923e+06 bytes/sec
164.772 fetches/sec, 2.81184e+06 bytes/sec
164.692 fetches/sec, 2.81047e+06 bytes/sec
166.4 fetches/sec, 2.83962e+06 bytes/sec
只加载bcmath动态库以及eaccelerator
USER %CPU %MEM VSZ RSS TTY STAT START TIME
root 0 1.1 89944 3104 ? Ss 04:49 00:00
nobody 1.1 1.7 90396 4508 ? S 04:49 00:00
nobody 1.4 1.5 90336 4156 ? S 04:49 00:00
nobody 1.2 1.5 90336 4156 ? S 04:49 00:00
nobody 1.2 1.5 90336 4156 ? S 04:49 00:00
nobody 1 1.5 90336 4156 ? S 04:49 00:00
nobody 1.2 1.5 90336 4156 ? S 04:49 00:00
nobody 0.8 1.5 90336 4156 ? S 04:49 00:00
nobody 0.9 1.5 90336 4156 ? S 04:49 00:00
nobody 0.8 1.5 90336 4156 ? S 04:49 00:00
nobody 1.1 1.5 90336 4156 ? S 04:49 00:00
nobody 1 1.5 90336 4156 ? S 04:49 00:00
nobody 1 1.5 90336 4156 ? S 04:49 00:00
nobody 1.1 1.5 90336 4156 ? S 04:49 00:00
nobody 1 1.5 90336 4156 ? S 04:49 00:00
nobody 1.2 1.5 90336 4156 ? S 04:49 00:00
nobody 1.2 1.5 90336 4156 ? S 04:49 00:00
nobody 1.1 1.5 90336 4156 ? S 04:49 00:00
nobody 1 1.5 90336 4156 ? S 04:49 00:00
nobody 1.1 1.5 90336 4156 ? S 04:49 00:00
nobody 3.2 1.5 90336 4156 ? S 04:49 00:02
nobody 9.6 1.5 90336 4156 ? S 04:49 00:02
nobody 10.2 1.5 90336 4156 ? S 04:49 00:02
nobody 10.3 1.5 90336 4156 ? S 04:49 00:02
nobody 8.6 1.5 90336 4156 ? S 04:49 00:02
nobody 9.1 1.5 90336 4156 ? S 04:49 00:02
SUM 71.4 38.8 2348404 107356
速度测试结果
161.5 fetches/sec, 2.756e+06 bytes/sec
163.6 fetches/sec, 2.79183e+06 bytes/sec
162.5 fetches/sec, 2.77306e+06 bytes/sec
160.098 fetches/sec, 2.73208e+06 bytes/sec
162.592 fetches/sec, 2.77464e+06 bytes/sec
所有动态库全部加载
USER %CPU %MEM VSZ RSS TTY STAT START TIME
root 0 1.2 98460 3220 ? Ss 04:46 00:00
nobody 3 1.6 98848 4320 ? S 04:46 00:02
nobody 1.8 1.6 98848 4320 ? S 04:46 00:01
nobody 3 1.6 98848 4320 ? S 04:46 00:02
nobody 2.9 1.6 98848 4320 ? S 04:46 00:02
nobody 1.5 1.6 98848 4320 ? S 04:46 00:01
nobody 1.2 1.6 98848 4320 ? S 04:46 00:00
nobody 1.4 1.6 98848 4320 ? S 04:46 00:00
nobody 1.5 1.6 98848 4320 ? S 04:46 00:01
nobody 2 1.6 98848 4320 ? S 04:46 00:01
nobody 4 1.6 98848 4320 ? S 04:46 00:02
nobody 1.3 1.6 98848 4320 ? S 04:46 00:00
nobody 1.5 1.6 98848 4320 ? S 04:46 00:01
nobody 1.5 1.6 98848 4320 ? S 04:46 00:01
nobody 1.3 1.6 98848 4320 ? S 04:46 00:00
nobody 1.3 1.6 98848 4320 ? S 04:46 00:00
nobody 1.5 1.6 98848 4320 ? S 04:46 00:01
nobody 1.5 1.6 98848 4320 ? S 04:46 00:01
nobody 3.2 1.6 98848 4320 ? S 04:46 00:02
nobody 3.6 1.6 98848 4320 ? S 04:46 00:02
nobody 1.9 1.6 98848 4320 ? S 04:46 00:00
nobody 7.2 1.6 98848 4320 ? S 04:47 00:01
nobody 0 1.6 98848 4320 ? S 04:47 00:00
nobody 7.9 1.6 98848 4320 ? S 04:47 00:00
nobody 1.4 1.6 98848 4320 ? S 04:47 00:00
nobody 3.2 1.6 98848 4320 ? S 04:47 00:00
SUM 60.6 41.2 2569660 111220
速度测试结果
160.9 fetches/sec, 2.74576e+06 bytes/sec
164.6 fetches/sec, 2.8089e+06 bytes/sec
164.1 fetches/sec, 2.80036e+06 bytes/sec
164.2 fetches/sec, 2.80207e+06 bytes/sec
163.7 fetches/sec, 2.79354e+06 bytes/sec
同时我们测试phpinfo函数
对比结果如下
加载全部模块/静态编译
251.4 fetches/sec
257.799 fetches/sec
258.993 fetches/sec
257.798 fetches/sec
249.2 fetches/sec
只加载最小模块
290.199 fetches/sec
332.1 fetches/sec
332.5 fetches/sec
319.8 fetches/sec
332.8 fetches/sec
5.测试总结
使用静态方式和动态方式来加载模块对于php的执行效率并没有非常显著的影响
甚至原先认为在内存占用方面,动态编译由于模块只需要加载一份在真实内存中,内存占用会比较小的假象
在实际测试结果中也没有获得印证
两者的差异几乎可以忽略.而动态加载所获得的机动性和便利性.则大大高于静态编译.
而是否加载了不必要的模块.会略微的影响到内存占用,但是不会影响到其他函数或者模块的执行效率
更不会影响到PHP本身的执行效率
在最后一次测试中.由于phpinfo需要遍历所有的模块并取出对应的参数
所以全部加载的效率远低于只加载必要模块.
如果是服务器需要处理很多外部请求.建议将模块数量最小化.
如果是量并不大到一定程度.全部加载没有太大影响.

