使PHP7发挥性能需要注意哪些设置

PHPzkbhj 发表了文章 • 0 个评论 • 1480 次浏览 • 2016-08-15 15:05 • 来自相关话题

让PHP7达到最高性能的几个建议:
 
1. Opcache

记得启用Zend Opcache,因为PHP7即使不启用Opcache速度也比PHP-5.6启用了Opcache快,所以之前测试时期就发生了有人一直没有启用Opcache的事情。启用Opcache非常简单,在php.ini配置文件中加入:
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1"
2. 使用新的编译器

使用新一点的编译器,推荐GCC 4.8以上,因为只有GCC 4.8以上PHP才会开启Global Register for opline and execute_data支持,这个会带来5%左右的性能提升(Wordpres的QPS角度衡量)

其实GCC 4.8以前的版本也支持,但是我们发现它支持的有Bug,所以必须是4.8以上的版本才会开启这个特性。
 
3. HugePage

我之前的文章也介绍过: 让你的PHP7更快之Hugepage ,首先在系统中开启HugePages,然后开启Opcache的huge_code_pages。

以我的CentOS 6.5为例,通过:
$sudo sysctl vm.nr_hugepages=512分配512个预留的大页内存:
$ cat /proc/meminfo | grep Huge
AnonHugePages: 106496 kB
HugePages_Total: 512
HugePages_Free: 504
HugePages_Rsvd: 27
HugePages_Surp: 0
Hugepagesize: 2048 kB然后在php.ini中加入:
opcache.huge_code_pages=1
这样一来,PHP会把自身的text段,以及内存分配中的huge都采用大内存页来保存,减少TLB miss,从而提高性能。

4. Opcache file cache

开启Opcache File Cache(实验性),通过开启这个,我们可以让Opcache把opcode缓存缓存到外部文件中,对于一些脚本,会有很明显的性能提升。
在php.ini中加入:
opcache.file_cache=/tmp
这样PHP就会在/tmp目录下Cache一些Opcode的二进制导出文件,可以跨PHP生命周期存在。

5. PGO

我之前的文章: 让你的PHP7更快(GCC PGO) 也介绍过,如果你的PHP是专门为一个项目服务,比如只是为你的Wordpress,或者drupal,或者其他什么,那么你就可以尝试通过PGO,来提升PHP,专门为你的这个项目提高性能。

具体的,以wordpress 4.1为优化场景。首先在编译PHP的时候首先:
$ make prof-gen
然后用你的项目训练PHP,比如对于Wordpress:
$ sapi/cgi/php-cgi -T 100 /home/huixinchen/local/www/htdocs/wordpress/index.php >/dev/null
也就是让php-cgi跑100遍wordpress的首页,从而生成一些在这个过程中的profile信息。

最后:
$ make prof-clean
$ make prof-use这个时候你编译得到的PHP7,就是为你的项目量身打造的最高性能的编译版本。 查看全部
让PHP7达到最高性能的几个建议:
 
1. Opcache

记得启用Zend Opcache,因为PHP7即使不启用Opcache速度也比PHP-5.6启用了Opcache快,所以之前测试时期就发生了有人一直没有启用Opcache的事情。启用Opcache非常简单,在php.ini配置文件中加入:
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1"

2. 使用新的编译器

使用新一点的编译器,推荐GCC 4.8以上,因为只有GCC 4.8以上PHP才会开启Global Register for opline and execute_data支持,这个会带来5%左右的性能提升(Wordpres的QPS角度衡量)

其实GCC 4.8以前的版本也支持,但是我们发现它支持的有Bug,所以必须是4.8以上的版本才会开启这个特性。
 
3. HugePage

我之前的文章也介绍过: 让你的PHP7更快之Hugepage ,首先在系统中开启HugePages,然后开启Opcache的huge_code_pages。

以我的CentOS 6.5为例,通过:
$sudo sysctl vm.nr_hugepages=512
分配512个预留的大页内存:
$ cat /proc/meminfo  | grep Huge
AnonHugePages: 106496 kB
HugePages_Total: 512
HugePages_Free: 504
HugePages_Rsvd: 27
HugePages_Surp: 0
Hugepagesize: 2048 kB
然后在php.ini中加入:
opcache.huge_code_pages=1

这样一来,PHP会把自身的text段,以及内存分配中的huge都采用大内存页来保存,减少TLB miss,从而提高性能。

4. Opcache file cache

开启Opcache File Cache(实验性),通过开启这个,我们可以让Opcache把opcode缓存缓存到外部文件中,对于一些脚本,会有很明显的性能提升。
在php.ini中加入:
opcache.file_cache=/tmp

这样PHP就会在/tmp目录下Cache一些Opcode的二进制导出文件,可以跨PHP生命周期存在。

5. PGO

我之前的文章: 让你的PHP7更快(GCC PGO) 也介绍过,如果你的PHP是专门为一个项目服务,比如只是为你的Wordpress,或者drupal,或者其他什么,那么你就可以尝试通过PGO,来提升PHP,专门为你的这个项目提高性能。

具体的,以wordpress 4.1为优化场景。首先在编译PHP的时候首先:
$ make prof-gen

然后用你的项目训练PHP,比如对于Wordpress:
$ sapi/cgi/php-cgi -T 100 /home/huixinchen/local/www/htdocs/wordpress/index.php >/dev/null

也就是让php-cgi跑100遍wordpress的首页,从而生成一些在这个过程中的profile信息。

最后:
$ make prof-clean
$ make prof-use
这个时候你编译得到的PHP7,就是为你的项目量身打造的最高性能的编译版本。

thinkPHP如何实现复杂的SQL查询条件拼接?

回复

thinkPHPzkbhj 回复了问题 • 1 人关注 • 1 个回复 • 5790 次浏览 • 2016-08-15 15:01 • 来自相关话题

thinkPHP如何获取最后执行的sql语句?

回复

thinkPHPzkbhj 回复了问题 • 1 人关注 • 1 个回复 • 4355 次浏览 • 2016-08-15 14:53 • 来自相关话题

JavaScript如何获取屏幕的高度和宽度?

回复

前端开发zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 2378 次浏览 • 2016-08-15 14:39 • 来自相关话题

Jquery使用Ajax获取后台返回的Json数据处理

前端开发zkbhj 发表了文章 • 0 个评论 • 2223 次浏览 • 2016-08-15 14:35 • 来自相关话题

<script type="text/javascript">
$(function () {
$.ajax({
url: 'jsondata.ashx',
type: 'GET',
dataType: 'json',
timeout: 1000,
cache: false,
beforeSend: LoadFunction, //加载执行方法
error: erryFunction, //错误执行方法
success: succFunction //成功执行方法
})
function LoadFunction() {
$("#list").html('加载中...');
}
function erryFunction() {
alert("error");
}
function succFunction(tt) {
$("#list").html('');

//eval将字符串转成对象数组
//var json = { "id": "10086", "uname": "zhangsan", "email": "zhangsan@qq.com" };
//json = eval(json);
//alert("===json:id=" + json.id + ",uname=" + json.uname + ",email=" + json.email);

var json = eval(tt); //数组
$.each(json, function (index, item) {
//循环获取数据
var name = json[index].Name;
var idnumber = json[index].IdNumber;
var sex = json[index].Sex;
$("#list").html($("#list").html() + "<br>" + name + " - " + idnumber + " - " + sex + "<br/>");
});
}
});
</script> 查看全部
<script type="text/javascript">  
$(function () {
$.ajax({
url: 'jsondata.ashx',
type: 'GET',
dataType: 'json',
timeout: 1000,
cache: false,
beforeSend: LoadFunction, //加载执行方法
error: erryFunction, //错误执行方法
success: succFunction //成功执行方法
})
function LoadFunction() {
$("#list").html('加载中...');
}
function erryFunction() {
alert("error");
}
function succFunction(tt) {
$("#list").html('');

//eval将字符串转成对象数组
//var json = { "id": "10086", "uname": "zhangsan", "email": "zhangsan@qq.com" };
//json = eval(json);
//alert("===json:id=" + json.id + ",uname=" + json.uname + ",email=" + json.email);

var json = eval(tt); //数组
$.each(json, function (index, item) {
//循环获取数据
var name = json[index].Name;
var idnumber = json[index].IdNumber;
var sex = json[index].Sex;
$("#list").html($("#list").html() + "<br>" + name + " - " + idnumber + " - " + sex + "<br/>");
});
}
});
</script>

常用的Linux服务器命令

服务器zkbhj 发表了文章 • 0 个评论 • 1433 次浏览 • 2016-08-12 19:50 • 来自相关话题

1、查看静态进程:ps aux(或-efl)。比如:ps aux 或查某一进程ps -elf | grep httpd
2、查看动态进程:top 默认3秒钟刷新一次。如下: 默认5秒刷新一次。top -d 53、清屏命令:clear
4、复制文件或目录cp -r /home/www/zkbhj2 /home/www/to/ 
5、PHP相关[root@KaiBoss_4_45 php-ext-trie-filter-master]# whereis php
php: /usr/sbin/php /usr/local/php
[root@KaiBoss_4_45 php-ext-trie-filter-master]# which php
/usr/local/php/bin/php
[root@KaiBoss_4_45 php-ext-trie-filter-master]# find / -name php-config
/php-7.1.11/scripts/php-config
/usr/local/php56/bin/php-config
/usr/local/php/bin/php-config
/lnmp/src/php-7.1.11/scripts/php-config

6、查看文件大小
[dev@wx_new_1_45_126 cli]$ du -sh *
4.0K apartmentQualification.sh
4.0K SingleactHandoutPrize.sh
4.0K yii
4.0K ziroomerHandoutPrize.sh
4.0K ziroomerQualification.sh 查看全部
1、查看静态进程:ps aux(或-efl)。比如:ps aux 或查某一进程
ps -elf | grep httpd

2、查看动态进程:top 默认3秒钟刷新一次。如下: 默认5秒刷新一次。
top -d 5
3、清屏命令:clear
4、复制文件或目录
cp -r /home/www/zkbhj2 /home/www/to/
 
5、PHP相关
[root@KaiBoss_4_45 php-ext-trie-filter-master]# whereis php
php: /usr/sbin/php /usr/local/php
[root@KaiBoss_4_45 php-ext-trie-filter-master]# which php
/usr/local/php/bin/php
[root@KaiBoss_4_45 php-ext-trie-filter-master]# find / -name php-config
/php-7.1.11/scripts/php-config
/usr/local/php56/bin/php-config
/usr/local/php/bin/php-config
/lnmp/src/php-7.1.11/scripts/php-config

6、查看文件大小
[dev@wx_new_1_45_126 cli]$ du -sh *
4.0K apartmentQualification.sh
4.0K SingleactHandoutPrize.sh
4.0K yii
4.0K ziroomerHandoutPrize.sh
4.0K ziroomerQualification.sh

什么是QPS?什么是TPS?有何异同?

回复

专业名词zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 2557 次浏览 • 2016-08-11 16:23 • 来自相关话题

如何在Yii Model的验证规则Rules中区分场景?

回复

Yii框架zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 2700 次浏览 • 2016-08-10 18:34 • 来自相关话题

Yii 2插入数据库方式有哪些?

回复

Yii框架zkbhj 回复了问题 • 1 人关注 • 1 个回复 • 4510 次浏览 • 2016-08-10 16:48 • 来自相关话题

Yii rules常用规则介绍

Yii框架zkbhj 发表了文章 • 0 个评论 • 1457 次浏览 • 2016-08-09 19:22 • 来自相关话题

public function rules()
{
return array(
//必须填写
array('email, username, password,agree,verifyPassword,verifyCode', 'required'),
//检查用户名是否重复
array('email','unique','message'=>'用户名已占用'),
//用户输入最大的字符限制
array('email, username', 'length', 'max'=>64),
//限制用户最小长度和最大长度
array('username', 'length', 'max'=>7, 'min'=>2, 'tooLong'=>'用户名请输入长度为4-14个字符', 'tooShort'=>'用户名请输入长度为2-7个字'),
//限制密码最小长度和最大长度
array('password', 'length', 'max'=>22, 'min'=>6, 'tooLong'=>'密码请输入长度为6-22位字符', 'tooShort'=>'密码请输入长度为6-22位字符'),
//判断用户输入的是否是邮件
array('email','email','message'=>'邮箱格式错误'),
//检查用户输入的密码是否是一样的
array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message'=>'请再输入确认密码'),
//检查用户是否同意协议条款
array('agree', 'required', 'requiredValue'=>true,'message'=>'请确认是否同意隐私权协议条款'),
//判断是否是日期格式
array('created', 'date', 'format'=>'yyyy/MM/dd/ HH:mm:ss'),
//判断是否包含输入的字符
array('superuser', 'in', 'range' => array(0, 1)),
//正则验证器:
array('name','match','pattern'=>'/^[a-z0-9\-_]+$/'),
//数字验证器:
array('id', 'numerical', 'min'=>1, 'max'=>10, 'integerOnly'=>true),
//类型验证 integer,float,string,array,date,time,datetime
array('created', 'type', 'datetime'),
//文件验证:
array('filename', 'file', 'allowEmpty'=>true, 'types'=>'zip, rar, xls, pdf, ppt','tooLarge'=>'图片不要超过800K'),
array('url',
'file', //定义为file类型
'allowEmpty'=>true,
'types'=>'jpg,png,gif,doc,docx,pdf,xls,xlsx,zip,rar,ppt,pptx', //上传文件的类型
'maxSize'=>1024*1024*10, //上传大小限制,注意不是php.ini中的上传文件大小
'tooLarge'=>'文件大于10M,上传失败!请上传小于10M的文件!'
),
} );

$news= new news('search'); //search关联规则 查看全部
public function rules()
{
return array(
//必须填写
array('email, username, password,agree,verifyPassword,verifyCode', 'required'),
//检查用户名是否重复
array('email','unique','message'=>'用户名已占用'),
//用户输入最大的字符限制
array('email, username', 'length', 'max'=>64),
//限制用户最小长度和最大长度
array('username', 'length', 'max'=>7, 'min'=>2, 'tooLong'=>'用户名请输入长度为4-14个字符', 'tooShort'=>'用户名请输入长度为2-7个字'),
//限制密码最小长度和最大长度
array('password', 'length', 'max'=>22, 'min'=>6, 'tooLong'=>'密码请输入长度为6-22位字符', 'tooShort'=>'密码请输入长度为6-22位字符'),
//判断用户输入的是否是邮件
array('email','email','message'=>'邮箱格式错误'),
//检查用户输入的密码是否是一样的
array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message'=>'请再输入确认密码'),
//检查用户是否同意协议条款
array('agree', 'required', 'requiredValue'=>true,'message'=>'请确认是否同意隐私权协议条款'),
//判断是否是日期格式
array('created', 'date', 'format'=>'yyyy/MM/dd/ HH:mm:ss'),
//判断是否包含输入的字符
array('superuser', 'in', 'range' => array(0, 1)),
//正则验证器:
array('name','match','pattern'=>'/^[a-z0-9\-_]+$/'),
//数字验证器:
array('id', 'numerical', 'min'=>1, 'max'=>10, 'integerOnly'=>true),
//类型验证 integer,float,string,array,date,time,datetime
array('created', 'type', 'datetime'),
//文件验证:
array('filename', 'file', 'allowEmpty'=>true, 'types'=>'zip, rar, xls, pdf, ppt','tooLarge'=>'图片不要超过800K'),
array('url',
'file', //定义为file类型
'allowEmpty'=>true,
'types'=>'jpg,png,gif,doc,docx,pdf,xls,xlsx,zip,rar,ppt,pptx', //上传文件的类型
'maxSize'=>1024*1024*10, //上传大小限制,注意不是php.ini中的上传文件大小
'tooLarge'=>'文件大于10M,上传失败!请上传小于10M的文件!'
),
} );

$news= new news('search'); //search关联规则