当我使用Http \ Response :: send时,Phalcon非常慢

时间:2016-01-27 15:25:37

标签: php phalcon

我构建了一个Micro应用程序,一切正常。但性能测试显示令人沮丧的结果。应用程序非常慢:

ab -n 1000 -c 50 http://myproject.net/

正常结果:

Concurrency Level:      100
Time taken for tests:   58.083 seconds
Complete requests:      1000
Failed requests:        0
Non-2xx responses:      1000
Total transferred:      578000 bytes
HTML transferred:       189000 bytes
Requests per second:    17.22 [#/sec] (mean)
Time per request:       5808.272 [ms] (mean)
Time per request:       58.083 [ms] (mean, across all concurrent requests)
Transfer rate:          9.72 [Kbytes/sec] received

在我评论 $ response-> send(); 后,测试结果变得更好:

Concurrency Level:      100
Time taken for tests:   7.960 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      166000 bytes
HTML transferred:       0 bytes
Requests per second:    125.62 [#/sec] (mean)
Time per request:       796.048 [ms] (mean)
Time per request:       7.960 [ms] (mean, across all concurrent requests)
Transfer rate:          20.36 [Kbytes/sec] received

在索引文件中,我将响应对象定义为共享服务:

$di->setShared(System::RESPONSE, new App\Http\Response());

App \ Http \ Response 是一个类扩展\ Phalcon \ Http \ Response,允许我在我的应用程序中添加一些标题

namespace App\Http;

class Response extends \Phalcon\Http\Response
{
    public function getDefaultErrorMessages()
    {
        return $this->defaultErrorMessages;
    }

    public function setDefaultErrorMessages($messages)
    {
        $this->defaultErrorMessages = $messages;
    }

    public function setErrors(array $messages)
    {
       $this->validationErrors = $messages;
    }

    public function setErrorContent(\Exception $e, $developerInfo = false)
    {
        $errorCode = $e->getCode();
        $statusCode = 500;
        $message = 'Unspecified error';

        $error = [];
        if ($this->validationErrors) {
            $error['errors'] = $this->validationErrors;
        }


        $this->setJsonContent(['error' => $error]);
        $this->setStatusCode($statusCode);
    }


    public function setJsonContent($content, $jsonOptions = 0, $depth = 512)
    {
        parent::setJsonContent($content, $jsonOptions, $depth);
        $this->setHeader('Access-Control-Allow-Origin', '*');
        $this->setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
        $this->setHeader(
            'Access-Control-Allow-Headers',
            'Content-Type, X-Requested-With, X-Authorization'
        );

        $this->setContentType('application/json');
    }
}

我的所有代码都包含在索引文件中的try-catch语句中。 我试图发出错误以便抛出异常,如果Zephir抛出异常测试显示快速结果,而如果它被我们的代码testshows引发同样缓慢的结果。

当我使用'ab'的-k修饰符时,结果会变得更好:

ab -k -n 1000 -c 50 http://myproject.net/

1 个答案:

答案 0 :(得分:2)

您是否禁用了View组件?当你在代码的其他部分发送响应时,为什么要发送响应?

在我的一个项目中,我有一个Api模块。此API模块全局禁用视图:

应用程序/模块/ API / Module.php

    public function registerServices($di)
    {
        // ....
        $di->getView()->disable();
    }

之后我确信这里不会构建任何与视图相关的服务。在Api控制器命名空间中,我有他们的基础

应用程序/模块/ API /控制器/ controllerBase.php

<?php

namespace Application\Api\Controllers;
use \Phalcon\Mvc\Controller;

class ControllerBase extends Controller {

    /**
     * Captures method result and tries to make a JSON response out of it.
     * 
     * @param \Phalcon\Mvc\Dispatcher $dispatcher
     * @return \Phalcon\Http\Response
     */
    protected function afterExecuteRoute($dispatcher) {
        $content = $dispatcher->getReturnedValue();

        // some logic for prematurely generated content (debugs/warnings)
        // ...

        if(is_object($content)) {
            if(is_callable(array($content, 'toArray'))) {
                $content = $content->toArray();
            } else {
                $content = (array) $content;
            }
        }


        $this->response->setContentType('application/json', 'UTF-8');

        $this->response->setJsonContent($frame);

        return $this->response->send();
    }
}

所以我从我的行动中返回的是具有适当结果的数组。

为什么要回复?因为你是ommiting a lot of logic的第一名。另外send action itself正在做一些重载,所以最好的方法是:

        // from previous code frame
        $this->response->setJsonContent($frame);

        return $this->response; // no send() here
    }
}

index.php

echo $application->handle() // returning instance of Response
         ->getContent(); // getting content to echo

请查看您的代码,因为由于send()方法,Phalcon第一次出现问题,因此不会出现问题。如果您开始更改代码,您可能会很幸运。我的猜测是视图生成。