您的位置:首页 > 其它

设计模式系列(一)factory模式

2009-06-05 23:28 387 查看
1 php性能优化初探
我已经遇到性能问题了!

可能用到性能问题
1 php语法使用不当
2 用了其不擅长的事情
3 连接了不给力的服务
4 php自身的短板
5 我也不知道的问题
有靠谱的解决办法了吗????

2 php性能问题简析
占比例小于 50%,不要局限优化php。
解决方向:
1 php语言级的性能优化。 就是先查代码
2 周边问题优化,周边服务的优化。
3 php语言自身分析、优化,底层的C语言的优化。
3 php性能优化
压力测试工具简介
Apache Benchmark(ab)
简介:ab是由apache提供的压力测试软件,安装apache服务器时会自带该压缩软件
如何使用:
./ab -n100 -c10 http://www.baidu.com/ 这样才好用
./ab -n100 -c100 http://www.baidu.com -n 请求数
-c 并发数
url 目标压测地址
使用:
ab -h 出现帮助文档

可以使用结果分析:
Server Software: BWS/1.1 // 服务器版本 BWS是百度自己开发的服务器
Server Hostername: www.baidu.com
Server Port: 80

Document Path /
Document Length: 79648 bytes

Concurrency level: 10
Time taken for test: 0.984 seconds
Complete requests: 100
Failed requests 77
(Connect:0,Receive:0,Length:77,Exceptions:0)
Write errors: 0
Total transferred: 8123212bytes
*Requests per second 101.65[#/sec](mean)
Time per request: 98.379[ms](mean)
*Time per request: 9.838[ms] (mean,across all concurrent requests)
Transfer rate: 8063.49[Kbytes/sec] received

Connection Times(ms)
min mean[+/-sd] median max
2 21 10.6 23 41
Connect:

实际返回的:注意位置 bin 下面才可以的
[root@iZ25yhxt5uvZ bin]# ./ab -n600 -c300 http://www.tcd120.cn/ This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.tcd120.cn (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Finished 600 requests

Server Software: Apache/2.4.10
Server Hostname: www.tcd120.cn
Server Port: 80

Document Path: /
Document Length: 0 bytes

Concurrency Level: 300
Time taken for tests: 9.680 seconds
Complete requests: 600
Failed requests: 0
Write errors: 0
Non-2xx responses: 600
Total transferred: 269400 bytes
HTML transferred: 0 bytes
Requests per second: 61.98 [#/sec] (mean)
Time per request: 4840.010 [ms] (mean)
Time per request: 16.133 [ms] (mean, across all concurrent requests)
Transfer rate: 27.18 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 56 59 3.6 58 73
Processing: 92 3592 1501.0 4676 5032
Waiting: 92 3592 1501.0 4676 5032
Total: 148 3651 1501.7 4733 5089

Percentage of the requests served within a certain time (ms)
50% 4733
66% 4781
75% 4795
80% 4807
90% 4827
95% 4851
98% 4890
99% 4921
100% 5089 (longest request)

4 php语言级性能优化
优化点:少写代码,多用php自身能力
自写的性能低,冗余高
多掌握内置函数,很高大上啊

代码运行流程:*.php---> scanner[zend]--->exprs--->parser--->opcodes--->exec--->output;

内置函数的性能测试:内置函数也有好坏之分。用好的内置函数

尽量少用魔法函数。

禁止使用错误抑制符号,在 opcode 前增加 忽略,执行完成后

查看全部的 Opcode 扩展 vld 扩展安装,产生的额外开销

合理使用内存:内存回收机制,控制脚本使用量。unset() 即使释放内存,但是有注销不掉的情况
尽量少用正则表达式。 其回溯开销较大。
避免在循环内做计算。循环内尽量不用动态计算。
减少密集型业务:不适合密集型运算场景,大数据量不好的表现。php 语言由C语言书写的

适合衔接 Webserver与后端服务,UI呈现
务必使用单引号 字符串 做键值,否则会产生额外的开销。非引号,默认为常量。

5 php周边问题优化
linux 环境:可以独占服务器,其硬盘将影响其IO读写。数据库也是基于文件系统的。磁盘的性能快慢
C语言开发php扩展。
网络连接、内存情况
减少文件类操作。
减少Php发起网络请求,对方接口的不确定因素,网络稳定性
设置超时时间:连接超时、200ms读超时800ms、写超时500ms。
将串行机制并行化。
a curl_multi_*(),实现并行化
b swoole 扩展,实现并行化请求
压缩php的数据的好坏。 client 快, cpu 会提供额外开销
缓存复用。估计,需要做缓存,不建议。
重叠时间窗口思想。串行改并行。是否任务强依赖的开发。重叠时间窗口,这样减少总的请求

旁路方案:

6 性能分析:
工具: xhporf 开始 使用自己找
ab 压力测试 vid opcode代码分析
性能极致优化。
opcode cache: php 扩展 APC

PECL : APC 扩展, APC 缓存 代码 2012年就停止开发了。靠谱的缓存类扩展

鸟哥 跟 vac 写的。

高频代码 用 php 扩展执行。
HHVM 模版渲染 ,facebook 拓展出来的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: