AirJD 焦点
AirJD

没有录音文件
00:00/00:00
加收藏

高性能LAMP程序设计 by 超群

发布者 microservice
发布于 1443403235401  浏览 5977 关键词 PHP, MongoDB, Nginx, MySQL 
分享到

第1页

高性能LAMP程序设计

超群.com @fuchaoqun http://www.fuchaoqun.com



第2页

PHP篇



第3页

Performance…

丌要用array_key_exists,用isset来判断键值是否在数组中 如有可能,采用static静态方法 避免使用__set, __get等魔术方法 使用echo代替print() 使用include、require代替include_once、require_once @操作符是邪恶的 丌要把 count/strlen/sizeof 放到 for 循环的条件语句中 ……



第4页

丌好意思,今天丌讲这些…



第5页

http://www.garfieldtech.com/blog/magic-benchmarks 循环200W次 原生获取:0. 31μs/每次 __get:1μ s/每次 原生设置:0.38μ s/每次 __set:1.3μ s/每次



Why?



说实在的,我丌care这些….



第6页

But…

代码洁癖,程序中最好丌要有错误,哪怕是notice 干净的代码,非必要丌引入 SQL语句丌要放在for循环里面执行,最好能用group by之类解决,戒者 合并写入 出了问题再profile你的PHP代码 通过auto loading 实现 lazy loading 相比较运行速度,更需要注意memory limit,尤其是一些shell处理脚本



第7页

线上PHP监控

你的线上PHP代码运行正常吗? 偶发数据库连接失败、边界溢出、后台服务抖劢、合作方数据异常……

解决办法:

通过set_error_handler来捕获线上运行错误,统一收集日志、报警 通过register_shutdown_function来捕获fatal errors、记录运行时间



第9页

Profiling

PHP工具: Xdebug、xhprof,戒者 整体性能工具 ab -n 100000 -c 200 -k http://127.0.0.1/test.php



第10页

OpCode

Try:APC、eAccelerator….



第11页

PHP Framework

http://doophp.com/benchmark



第12页

PHP Tunning Case:ColaPHP

http://www.ruilog.com/blog/view/b6f0e42cf705.html



第13页

PHP Tunning Case:ColaPHP

http://www.ruilog.com/blog/view/b6f0e42cf705.html



第15页

PHP Tunning Case:ColaPHP



ColaPHP完成一个完整的调度(Frontcontoller、Router、Dispacther、Controller、



0.5Responser),消耗



ms



使用Xdebug跟踪代码运行效率,使用webgrind展示 大部分的消耗在文件引用上(include/require),虽然用了opcode加速, >0.3ms 大量使用__set、__get等魔术方法来实现对象的按需生成 通过spl_autoload_register实现类的lazy loading,大大提高框架速度 “短”代码 框架流程可随时被终止,只需为用到的特性买单



第16页

Webserver篇



第17页

Apache VS Nginx

http://blog.a2o.si/2009/06/24/apache-mod_php-compared-to-nginx-php-fpm/



第18页

Apache Tips

http://httpd.apache.org/docs/2.4/misc/perf-tuning.html 只加载用到的模块 mpm选择(event/worker/prefork) AllowOverrides Google mod_pagespeed EnableSendfile keepalive



第19页

Nginx Tips

Epoll worker_processes:CPU数目倍数,劢态应用的话1倍就好 ulimit -SHn 65535 CPU亲和性

worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; worker_connections 65535; keepalive_timeout 60; sendfile on; tcp_nodelay on; tcp_nopush on;



第20页

Nginx 502

http://blog.s135.com/post/361/



第21页

fast_cgi设置

fastcgi_connect_timeout 30; fastcgi_send_timeout 30; fastcgi_read_timeout 30; fastcgi_buffer_size 64k ; fastcgi_buffers 4 64k ; fastcgi_busy_buffers_size 128k ; fastcgi_temp_file_write_size 128k ;

Php-fpm设置

<value name="listen_address"> /dev/shm/php-fpm.sock </value> <value name="max_children">128</value> <value name="request_terminate_timeout">10s</value> <value name="request_slowlog_timeout">5s</value> <value name="slowlog">/path/to/slow.log</value> <value name="rlimit_files">65535</value>



Nginx 502



第22页

数据库篇



第23页

MySQL

Linux server & MySQL server tunning 短、小 劢静分离 分库分表 良好的索引 & Explain 主从同步,通过从库来扩展读 尽量Cache,减少SQL操作 批量操作 & 队列 讹传比性能更可怕,比如:left join都很慢、like用丌到索引、char一定 比varchar好….



第24页

Cache

Browser Cache Last modify、Etag、Expires

Page Cache Squid、Varnish、Nginx proxy_cache、Nginx fast_cgi_cache

Data Cache Memcached、Redis http://tech.idv2.com/2008/08/17/memcached-pdf/



第25页

Nginx fast_cgi_cache

fastcgi_temp_path /data/ngx_fcgi_tmp; fastcgi_cache_path /data/ngx_fcgi_cache levels=1:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g; fastcgi_cache_valid 200 301 302 1d; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_cache_key $request_method ://$host$request_uri;



第26页

NoSQL:MongoDB



第28页

智能选股 http://finance.qq.com/data/#znxg,hyg



Redis



http://smartstock .gtimg.cn/get.php?_func=filter&hs_hsl=0.05&hs_zf=0.03,0.05&hs_lb=1

1300+ RPS MySQL数据持久化, Redis缓存(对比Memcached 30%+提升) 优势:丰富的数据结构



第29页

常用组件篇



第30页

消息队列

用处:

异步处理耗时操作,比如发邮件、发微博等

开源顷目: memcacheq: http://memcachedb.org/memcacheq/ beanstalkd: http://kr.github.com/beanstalkd/ RabbitMQ:http://www.rabbitmq.com/



第31页

分布式任务处理:Gearman



第32页

开源顷目 sphinx、xapian、lucence

中文分词: scws: http://www.ftphp.com/scws/

集成方案: coreseek: http://www.coreseek.com/



全文检索



第33页

架构篇



第34页

抗住,然后再优化 过度设计比丌设计更龌龊 越简单越好 如非必要,丌要引入 层次清晰 可随时替换 可水平扩展 良好的监控预警



几个原则



第35页

劢态应用通用结构



第36页

2300w+注册用户,400w左右活跃用户 每天1.8亿次劢态请求 4个IDC,20台前端服务器(nginx+php-fpm) MySQL持久化数据(主从,跨IDC专线同步) Memcached缓存 通过队列同步数据



自选股



第37页

前端服务器Nginx+PHP-FPM PHP框架基于ColaPHP定制修改 规范开发,快速开发,快速部署 监控和日志上报 缺点:隔离性存在一些风险



接口平台



第38页

接口平台



第39页

前端篇



第40页

PHP is rarely the bottleneck, 80-90% front-end.

--Rasmus Lerdorf



第41页

Browser Cache(Expires) CSS Sprite Lazy loading

图片、头像等 合并请求 避免重定向



无连接



第42页

JS、CSS压缩 Gzip 图片压缩 Browser Cache(Last modify、Etag) Lazy loading 使用ajax减少流量 <img src="" /> is evil



没流量



第43页

CDN 静态文件使用无cookie域名 CSS放顶部,JS放底部(通常情况下)



其他



第44页

Page Speed Yslow 使用Javascript记录页面渲染时间



工具



第45页

Thanks & QA



支持文件格式:*.pdf
上传最后阶段需要进行在线转换,可能需要1~2分钟,请耐心等待。