在上一篇Nginx负载均衡与RabbitMQ消息队列实践,我们实践了Nginx
负载均衡与RabbitMQ
消息队列的数据落盘,但是,你以为解决高并发的手段就结束了吗?
现在我们来了解一下Opcache:
对于PHP解释型语言,不产生机器码,而是产生中间码(中间码是不能直接执行,这个中间码只有解释器可以识别到,中间码要靠解析器来进行执行)
比如说PHP的解析器是Zend,PHP使用Zend引擎,中间码我们也称作为操作码(opcode),而Opcahce这样的缓存组件就是将PHP代码预编译生成的脚本文件 Opcode 缓存在共享内存中供以后反复使用,从而避免了从磁盘读取代码再次编译的时间消耗。同时,它还应用了一些代码优化模式,使得代码执行更快。从而加速PHP的执行。
现在我们将启用AB测试在100并发下的结果:
Concurrency Level: 100
Time taken for tests: 20.698 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 286000 bytes
HTML transferred: 13000 bytes
Requests per second: 48.31 [#/sec] (mean)
Time per request: 2069.847 [ms] (mean)
Time per request: 20.698 [ms] (mean, across all concurrent requests)
Transfer rate: 13.49 [Kbytes/sec] received
我们可以看到吞吐率是Requests per second: 48.31 [#/sec] (mean)
吞吐率是服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数。
现在我们来安装opcache
安装过程查看PHP官网Opcache
安装后再次执行
ab -c 100 -n 1000 http://192.168.2.101/log
Concurrency Level: 100
Time taken for tests: 7.816 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 286000 bytes
HTML transferred: 13000 bytes
Requests per second: 101.87 [#/sec] (mean)
Time per request: 781.610 [ms] (mean)
Time per request: 9.816 [ms] (mean, across all concurrent requests)
Transfer rate: 28.45 [Kbytes/sec] received
我们可以看到吞吐率已经从48提升到101,超过一倍的提升
接下来我们继续增加并发数;
ab -c 150 -n 2000 http://192.168.2.101/log
Concurrency Level: 150
Time taken for tests: 14.060 seconds
Complete requests: 2000
Failed requests: 0
Write errors: 0
Total transferred: 572000 bytes
HTML transferred: 26000 bytes
Requests per second: 120.74 [#/sec] (mean)
Time per request: 1054.496 [ms] (mean)
Time per request: 9.030 [ms] (mean, across all concurrent requests)
Transfer rate: 30.93 [Kbytes/sec] received
我们可以看到即使在150的并发下我们的吞吐率也可以维持在每秒120吞吐率左右,证明Opcache确实提升了我们系统的可用性