我在使用CakePHP 3构建的API中有一些非常简单的东西。它是一个ping端点,只返回一个简单的小响应
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);
}
当我使用以下使用CakePHP扩展解析JSON的代码时,我得到以下基准。
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
基准:
public function ping(){
$message = 'Thanks for saying hello. Everything looks good so far.';
echo json_encode(['success'=>true, 'message'=> $message]);
exit(0);
}
我删除扩展名解析并执行以下代码...
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
基准:
public class W221 {
public static void main(String[] args) {
while (JPL.test()) {
Scanner kb = new Scanner (System.in);
int num = kb.nextInt();
int num2 = kb.nextInt();
int value1;
int value2;
if(num <= 15 || num2 <= 15){
value1 = 15 - num;
value2 = 15 - num2;
if(value1>value2){
if(value1 > 0){
System.out.println(" " + num2);
}
}
else if(value1<value2){
if(value2 > 0){
System.out.println(" " + num);
}
}
}
else{
System.out.println(0);
}
}
}
}
每秒1.2次请求比每秒589次请求微不足道。怎么会这样?我做错了什么吗?我在10秒内使用了10个并发请求的apache基准测试。我不能继续使用Cake 3,如果它这么慢......我最好在这一点上使用Phalcon,CI,Slim或者自己动手。请告诉我,我做错了什么......
答案 0 :(得分:3)
如果它的速度很慢,我无法继续使用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']);
});
这个控制器:
<?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);
}
}
这个控制器:
<?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);
}
}
调试关闭时和:
> grep debug config/app.php
'debug' => false,
无延期的响应时间为ms:
-> 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."}
基准测试结果:
-> 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)
使用路由器扩展解析结果不会明显变慢:
-> 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."}
基准测试结果:
-> 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)
这些结果中的绝对数字并不那么重要 - 但缺乏显着差异。这里的结论是问题不是一般的,它在某种程度上与问题中的安装有关。
由于它不可重复,因此无法说明为什么请求在您的方案中很慢,因此您需要深入挖掘。
简单的第一步是使用任何具有相同逻辑的url来渲染html并查看调试计时器瀑布:
(“查看渲染开始”的所有内容与请求/extension.json
)
如果任何特定的计时器很重要 - 请查看该代码正在执行的操作,并在必要时为调用的代码添加一些更精细的计时器。
如果这对xdebug和profile单个http请求没有帮助; webgrind是一种可视化xdebug配置文件信息的低成本方法。使用xdebug profiling作为任何php dev工具箱中的标准工具 - 如果你不熟悉使用xdebug配置文件 - 现在是时候开始:)。
答案 1 :(得分:0)
请确认您的调试已关闭:debug=>false
。
这是一个很晚的回复,但是我遇到了同样的问题。最终我的调试打开了,这使得JSON响应能够很好地显示缩进。这对于开发很有用,但出于明显的原因却不能用于生产。
修复了调试级别的json响应后,再次变得敏捷。
将此作为答案,以防万一有人犯同样的错误。