cni*_*ini 1 cakephp cakephp-3.0
我用 CakePHP 3 构建的 API 中有一些非常简单的东西。它是一个 ping 端点,只返回一个简单的小响应
{"success":true,"message":"Thanks for saying hello. Everything looks good so far."}
Run Code Online (Sandbox Code Playgroud)
当我使用以下使用 CakePHPs 扩展解析 JSON 的代码时,我得到以下基准。
public function ping(){
$message = 'Thanks for saying hello. Everything looks good so far.';
$this->set('message', $message);
$this->set('success', true);
$this->set('_serialize', true);
}
Run Code Online (Sandbox Code Playgroud)
基准:
Server Software: Apache/2.4.18
Server Hostname: app.local
Server Port: 80
Document Path: /publisher/v3.0/.json
Document Length: 83 bytes
Concurrency Level: 10
Time taken for tests: 10.034 seconds
Complete requests: 12
Failed requests: 0
Total transferred: 4760 bytes
HTML transferred: 1660 bytes
Requests per second: 1.20 [#/sec] (mean)
Time per request: 8361.339 [ms] (mean)
Time per request: 836.134 [ms] (mean, across all concurrent requests)
Transfer rate: 0.46 [Kbytes/sec] received
Run Code Online (Sandbox Code Playgroud)
我删除扩展解析并执行以下代码......
public function ping(){
$message = 'Thanks for saying hello. Everything looks good so far.';
echo json_encode(['success'=>true, 'message'=> $message]);
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
基准:
Server Software: Apache/2.4.18
Server Hostname: app.local
Server Port: 80
Document Path: /publisher/v3.0/
Document Length: 83 bytes
Concurrency Level: 10
Time taken for tests: 10.000 seconds
Complete requests: 5895
Failed requests: 0
Total transferred: 1733130 bytes
HTML transferred: 489285 bytes
Requests per second: 589.49 [#/sec] (mean)
Time per request: 16.964 [ms] (mean)
Time per request: 1.696 [ms] (mean, across all concurrent requests)
Transfer rate: 169.25 [Kbytes/sec] received
Run Code Online (Sandbox Code Playgroud)
与每秒 589 个请求相比,每秒 1.2 个请求简直微不足道。怎么会这样?我做错了什么吗?我使用 apache 基准测试,在 10 秒内处理了 10 个并发请求。如果它这么慢,我就无法继续使用 Cake 3...我最好使用 Phalcon、CI、Slim,或者在这一点上自己推出。请告诉我我做错了什么...
如果 Cake 3 这么慢,我就无法继续使用
任何头脑正常的人都不会使用一个会导致对琐碎响应的响应时间极慢的框架。应用程序有问题。
有了这组路线:
<?php
use Cake\Routing\Router;
Router::scope('/extension', function ($routes) {
$routes->extensions(['json']);
$routes->connect('/', ['controller' => 'RequestHandler', 'action' => 'ping']);
});
Router::scope('/noextension', function ($routes) {
$routes->connect('/', ['controller' => 'NoRequestHandler', 'action' => 'ping']);
});
Run Code Online (Sandbox Code Playgroud)
该控制器:
<?php
namespace App\Controller;
use App\Controller\AppController;
class RequestHandlerController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
public function ping()
{
$message = 'Thanks for saying hello. Everything looks good so far.';
$this->set('message', $message);
$this->set('success', true);
$this->set('_serialize', true);
}
}
Run Code Online (Sandbox Code Playgroud)
还有这个控制器:
<?php
namespace App\Controller;
use App\Controller\AppController;
class NoRequestHandlerController extends AppController
{
public function ping()
{
$this->viewClass = 'Cake\View\JsonView';
$message = 'Thanks for saying hello. Everything looks good so far.';
$this->set('message', $message);
$this->set('success', true);
$this->set('_serialize', true);
}
}
Run Code Online (Sandbox Code Playgroud)
并关闭调试:
> grep debug config/app.php
'debug' => false,
Run Code Online (Sandbox Code Playgroud)
无扩展的响应时间为几毫秒:
-> curl -i http://cakephp.dev/noextension # Demonstrate exactly what's being benchmarked
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Sat, 06 Aug 2016 09:31:53 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
{"success":true,"message":"Thanks for saying hello. Everything looks good so far."}
Run Code Online (Sandbox Code Playgroud)
基准测试结果:
-> ab -c 10 -t 10 http://cakephp.dev/noextension
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 cakephp.dev (be patient)
Finished 1719 requests
Server Software: nginx/1.8.0
Server Hostname: cakephp.dev
Server Port: 80
Document Path: /noextension
Document Length: 83 bytes
Concurrency Level: 10
Time taken for tests: 10.002 seconds
Complete requests: 1719
Failed requests: 0
Write errors: 0
Total transferred: 428031 bytes
HTML transferred: 142677 bytes
Requests per second: 171.86 [#/sec] (mean)
Time per request: 58.186 [ms] (mean)
Time per request: 5.819 [ms] (mean, across all concurrent requests)
Transfer rate: 41.79 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.9 0 41
Processing: 10 58 20.0 57 224
Waiting: 10 58 20.0 57 223
Total: 10 58 20.2 57 224
Percentage of the requests served within a certain time (ms)
50% 57
66% 62
75% 67
80% 70
90% 81
95% 94
98% 112
99% 122
100% 224 (longest request)
Run Code Online (Sandbox Code Playgroud)
使用路由器扩展解析结果并没有明显变慢:
-> curl -i http://cakephp.dev/extension.json
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Sat, 06 Aug 2016 09:33:25 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
{"success":true,"message":"Thanks for saying hello. Everything looks good so far."}
Run Code Online (Sandbox Code Playgroud)
基准测试结果:
-> ab -c 10 -t 10 http://cakephp.dev/extension.json
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 cakephp.dev (be patient)
Finished 1672 requests
Server Software: nginx/1.8.0
Server Hostname: cakephp.dev
Server Port: 80
Document Path: /extension.json
Document Length: 83 bytes
Concurrency Level: 10
Time taken for tests: 10.012 seconds
Complete requests: 1672
Failed requests: 0
Write errors: 0
Total transferred: 416328 bytes
HTML transferred: 138776 bytes
Requests per second: 166.99 [#/sec] (mean)
Time per request: 59.882 [ms] (mean)
Time per request: 5.988 [ms] (mean, across all concurrent requests)
Transfer rate: 40.61 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 8
Processing: 10 60 21.8 58 278
Waiting: 10 59 21.8 58 278
Total: 10 60 21.9 58 278
Percentage of the requests served within a certain time (ms)
50% 58
66% 63
75% 67
80% 71
90% 80
95% 94
98% 111
99% 134
100% 278 (longest request)
Run Code Online (Sandbox Code Playgroud)
这些结果中的绝对数字并不那么重要——但缺乏显着差异。这里的结论是,问题不是一般性的,它在某种程度上特定于问题中的安装。
不可能说出为什么请求在您的场景中很慢,因为它不可重现,因此您需要更深入地挖掘。
简单的第一步是使用具有与渲染 html 相同逻辑的任何 url 并查看调试计时器瀑布:
(“View Render start”之前的所有内容都与请求相同/extension.json
)
如果任何特定的计时器很重要 - 查看该代码正在做什么,并在必要时向被调用的代码添加一些更细粒度的计时器。
如果这没有帮助使用 xdebug 并分析单个 http 请求;webgrind是一种可视化 xdebug 配置文件信息的省力方法。使用 xdebug 分析无疑是任何 php 开发人员工具带中的标准工具 - 如果您不熟悉使用 xdebug 配置文件 - 现在是开始的时候了:)。