php - Phalcon very slow when I use Http\Response::send -
i built micro application, works fine. performance tests shows frustrating results. application slow :
ab -n 1000 -c 50 http://myproject.net/
normal results:
concurrency level: 100 time taken 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 concurrent requests) transfer rate: 9.72 [kbytes/sec] received
after comment $response->send(); test results becomes better :
concurrency level: 100 time taken 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 concurrent requests) transfer rate: 20.36 [kbytes/sec] received
in index file defined response object shared service :
$di->setshared(system::response, new app\http\response());
app\http\response class extends \phalcon\http\response allow me add headers required in application
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'); } }
all code wrapped in try-catch statement in index file. tried make error in order throw exception, if the exception thrown zephir test shows fast results, while if thrown our code testshows same slow results.
when use -k modifier of 'ab' results becomes better :
ab -k -n 1000 -c 50 http://myproject.net/
do have view component disabled? why sending response, when sent in other parts of code?
in 1 of projects, have api module. api module has disable views globally:
app/modules/api/module.php
public function registerservices($di) { // .... $di->getview()->disable(); }
after sure no kind of view related service build here. in api controllers namespace have base them
app/modules/api/controllers/controllerbase.php
<?php namespace application\api\controllers; use \phalcon\mvc\controller; class controllerbase extends controller { /** * captures method result , tries make json response out of it. * * @param \phalcon\mvc\dispatcher $dispatcher * @return \phalcon\http\response */ protected function afterexecuteroute($dispatcher) { $content = $dispatcher->getreturnedvalue(); // logic 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(); } }
so return actions arrays proper results.
why return response? because ommiting lot of logic @ first place. send action itself doing overload, best way to:
// previous code frame $this->response->setjsoncontent($frame); return $this->response; // no send() here } }
and in index.php:
echo $application->handle() // returning instance of response ->getcontent(); // getting content echo
please check out code, because not first time occurs problem phalcon slowing down because of send()
method. may lucky 1 find why if start change code. guess views generation.
Comments
Post a Comment