问题
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
}
}
Whenever I use new Phalcon\Http\Response();
Phalcon becomes very slow. For example testing it with:
ab -c 50 -n 100 ...
Request/sec: 10
If I use a blank Controller I get
Request/sec: 1000+
The route is:
<?php
//file: app/config/routes.php
$router = new \Phalcon\Mvc\Router();
$router->add("/:controller/:action", array("controller" => "test", "action" => "index"));
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
We experienced the same behavior on a Macbook pro with osx 10.10.1
The problem is when I call the send()
method on the response:
$this->response->send(); // slows down everything
Note
As suggested by @Phate01 I tried return $response;
instead of $response->send();
and it is still very slow
回答1:
I went to this page: http://docs.phalconphp.com/en/latest/reference/response.html and they say that
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;
}
}
?>
As you can see it return directly the Response
object.
I guess that you should call the ->send()
outside the controller, and also adding some headers would help
来源:https://stackoverflow.com/questions/28982613/phalcon-very-slow-using-phalcon-http-response