Phalcon very slow using Phalcon\Http\Response();

半城伤御伤魂 提交于 2020-01-14 04:39:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!