Phalcon使用Phalcon \ Http \ Response()非常慢. [英] Phalcon very slow using Phalcon\Http\Response();
问题描述
我在使用Phalcon时遇到一个非常奇怪的问题.
每当我在控制器内使用Response
时,框架就会变得非常慢.
这是我的简单控制器:
I am experiencing a very strange issue with Phalcon.
Whenever I use Response
inside a controller the framework becomes very slow.
Here is my simple controller:
<?php
// file: app/controllers/TestController.php
use Phalcon\Mvc\View;
class TestController extends ControllerBase
{
private $response;
public function initialize()
{
$this->view->setRenderLevel(View::LEVEL_NO_RENDER);
$this->response = new Phalcon\Http\Response();
$this->response->setStatusCode(200, "OK");
}
public function indexAction()
{
$this->response->setContent("phalcon")->send(); // very slow
}
}
每当我使用new Phalcon\Http\Response();
时,Phalcon就会变得非常缓慢.例如,使用以下命令进行测试:
Whenever I use new Phalcon\Http\Response();
Phalcon becomes very slow. For example testing it with:
ab -c 50 -n 100 ...
请求/秒: 10
如果我使用空白控制器,则会得到
If I use a blank Controller I get
请求/秒: 1000 +
路线为:
<?php
//file: app/config/routes.php
$router = new \Phalcon\Mvc\Router();
$router->add("/:controller/:action", array("controller" => "test", "action" => "index"));
我在AWS上对其进行了测试:
I tested it on AWS:
c4.large
PHP 5.5.9-1ubuntu4.6 (cli) (built: Feb 13 2015 19:17:11)
2 cpu - 3.75gb ram
Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.6
我们在使用osx 10.10.1的Macbook pro上遇到了相同的行为
We experienced the same behavior on a Macbook pro with osx 10.10.1
问题是当我在响应上调用send()
方法时:
The problem is when I call the send()
method on the response:
$this->response->send(); // slows down everything
注意
如@ Phate01所建议,我尝试使用return $response;
而不是$response->send();
,但它仍然很慢
Note
As suggested by @Phate01 I tried return $response;
instead of $response->send();
and it is still very slow
推荐答案
我转到了此页面: http://docs.phalconphp.com/en/latest/reference/response.html 他们说
I went to this page: http://docs.phalconphp.com/en/latest/reference/response.html and they say that
如果您使用完整的MVC堆栈,则无需手动创建响应.但是,如果您需要直接通过控制器的操作返回响应,请遵循以下示例:
If you are using the full MVC stack there is no need to create responses manually. However, if you need to return a response directly from a controller’s action follow this example:
<?php
class FeedController extends Phalcon\Mvc\Controller
{
public function getAction()
{
// Getting a response instance
$response = new \Phalcon\Http\Response();
$feed = //.. load here the feed
//Set the content of the response
$response->setContent($feed->asString());
//Return the response
return $response;
}
}
?>
如您所见,它直接返回Response
对象.
As you can see it return directly the Response
object.
我想您应该在控制器外部调用->send()
,并且添加一些标头会有所帮助
I guess that you should call the ->send()
outside the controller, and also adding some headers would help
这篇关于Phalcon使用Phalcon \ Http \ Response()非常慢.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!