From 73b53cd5d972cb6049d3e3f89fbca2591ae0d1d1 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Tue, 7 Jul 2020 07:40:18 +0300 Subject: [PATCH] Split FPM and Swoole modes --- CONTRIBUTING.md | 2 +- app/app.php | 10 +- composer.json | 2 +- composer.lock | 20 ++-- public/index.php | 2 +- src/Appwrite/{Utopia => Swoole}/Files.php | 2 +- src/Appwrite/{Utopia => Swoole}/Request.php | 2 +- src/Appwrite/Swoole/Response.php | 104 ++++++++++++++++++++ src/Appwrite/Utopia/Response.php | 92 +---------------- tests/unit/{Utopia => Swoole}/FilesTest.php | 3 +- 10 files changed, 128 insertions(+), 111 deletions(-) rename src/Appwrite/{Utopia => Swoole}/Files.php (99%) rename src/Appwrite/{Utopia => Swoole}/Request.php (99%) create mode 100644 src/Appwrite/Swoole/Response.php rename tests/unit/{Utopia => Swoole}/FilesTest.php (98%) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c1604ce57..4b72b5628 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -185,7 +185,7 @@ You can use WRK Docker image to benchmark the server performance. Benchmarking i ``` ```bash -docker run --rm skandyla/wrk -t5 -c10 -d30 https://[APPWRITE_HOSTNAME_OR_IP] +docker run --rm skandyla/wrk -t3 -c100 -d30 https://[APPWRITE_HOSTNAME_OR_IP] ``` ## Code Maintenance diff --git a/app/app.php b/app/app.php index 1e7b1b234..5344fd47b 100644 --- a/app/app.php +++ b/app/app.php @@ -297,10 +297,12 @@ App::error(function ($error, $utopia, $request, $response, $layout, $project) { /** @var Utopia\View $layout */ /** @var Appwrite\Database\Document $project */ - var_dump(get_class($error)); - var_dump($error->getMessage()); - var_dump($error->getFile()); - var_dump($error->getLine()); + if(php_sapi_name() === 'cli') { + var_dump(get_class($error)); + var_dump($error->getMessage()); + var_dump($error->getFile()); + var_dump($error->getLine()); + } $version = App::getEnv('_APP_VERSION', 'UNKNOWN'); diff --git a/composer.json b/composer.json index f0a53c2a6..a5cb5b1c5 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "appwrite/php-clamav": "1.0.*", - "utopia-php/framework": "0.8.1", + "utopia-php/framework": "0.8.2", "utopia-php/abuse": "0.2.*", "utopia-php/audit": "0.3.*", "utopia-php/cache": "0.2.*", diff --git a/composer.lock b/composer.lock index 4466bd124..6c1383bd5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "12fa4cc8c24ef38a37424285d9be917a", + "content-hash": "c9c02c8344b98a983e16152163f8d91a", "packages": [ { "name": "appwrite/php-clamav", @@ -1596,16 +1596,16 @@ }, { "name": "utopia-php/framework", - "version": "0.8.1", + "version": "0.8.2", "source": { "type": "git", "url": "https://github.com/utopia-php/framework.git", - "reference": "79c4193e42120b3eefcda22a0254a6bfc257d6b6" + "reference": "7fbd96667df44f5afe30c8c48edc82a649b3c16b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/framework/zipball/79c4193e42120b3eefcda22a0254a6bfc257d6b6", - "reference": "79c4193e42120b3eefcda22a0254a6bfc257d6b6", + "url": "https://api.github.com/repos/utopia-php/framework/zipball/7fbd96667df44f5afe30c8c48edc82a649b3c16b", + "reference": "7fbd96667df44f5afe30c8c48edc82a649b3c16b", "shasum": "" }, "require": { @@ -1636,7 +1636,7 @@ "php", "upf" ], - "time": "2020-07-06T11:37:03+00:00" + "time": "2020-07-07T04:23:03+00:00" }, { "name": "utopia-php/locale", @@ -3340,12 +3340,12 @@ "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "d190c2e7f7a0475ab86f3185ef34f958d341c5b4" + "reference": "e063bab1a67f4ceea759cee20c10ed609d1f6abb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/d190c2e7f7a0475ab86f3185ef34f958d341c5b4", - "reference": "d190c2e7f7a0475ab86f3185ef34f958d341c5b4", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/e063bab1a67f4ceea759cee20c10ed609d1f6abb", + "reference": "e063bab1a67f4ceea759cee20c10ed609d1f6abb", "shasum": "" }, "require": { @@ -3397,7 +3397,7 @@ "keywords": [ "templating" ], - "time": "2020-07-05T13:32:43+00:00" + "time": "2020-07-06T13:35:12+00:00" }, { "name": "webmozart/assert", diff --git a/public/index.php b/public/index.php index a143994d9..9a42796e7 100644 --- a/public/index.php +++ b/public/index.php @@ -8,9 +8,9 @@ * ― Rick Cook, The Wizardry Compiled */ +use Appwrite\Utopia\Response; use Utopia\App; use Utopia\Request; -use Utopia\Response; error_reporting(0); ini_set('display_errors', 0); diff --git a/src/Appwrite/Utopia/Files.php b/src/Appwrite/Swoole/Files.php similarity index 99% rename from src/Appwrite/Utopia/Files.php rename to src/Appwrite/Swoole/Files.php index e7cb3dc7b..aedfd22e0 100644 --- a/src/Appwrite/Utopia/Files.php +++ b/src/Appwrite/Swoole/Files.php @@ -1,6 +1,6 @@ swoole = $response; + parent::__construct(\microtime(true)); + } + + /** + * Output response + * + * Generate HTTP response output including the response header (+cookies) and body and prints them. + * + * @param string $body + * @param int $exit exit code or don't exit if code is null + * + * @return self + */ + public function send(string $body = '', int $exit = null): void + { + if(!$this->disablePayload) { + $this->addHeader('X-Debug-Speed', microtime(true) - $this->startTime); + + $this + ->appendCookies() + ->appendHeaders() + ; + + $this->size = $this->size + mb_strlen(implode("\n", $this->headers)) + mb_strlen($body, '8bit'); + + $this->swoole->end($body); + + $this->disablePayload(); + } + } + + /** + * Append headers + * + * Iterating over response headers to generate them using native PHP header function. + * This method is also responsible for generating the response and content type headers. + * + * @return self + */ + protected function appendHeaders(): self + { + // Send status code header + $this->swoole->status($this->statusCode); + + // Send content type header + $this + ->addHeader('Content-Type', $this->contentType) + ; + + // Set application headers + foreach ($this->headers as $key => $value) { + $this->swoole->header($key, $value); + } + + return $this; + } + + /** + * Append cookies + * + * Iterating over response cookies to generate them using native PHP cookie function. + * + * @return self + */ + protected function appendCookies(): self + { + foreach ($this->cookies as $cookie) { + $this->swoole->cookie( + $cookie['name'], + $cookie['value'], + $cookie['expire'], + $cookie['path'], + $cookie['domain'], + $cookie['secure'], + $cookie['httponly'], + $cookie['samesite'], + ); + } + + return $this; + } +} \ No newline at end of file diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 4939fe3e0..414168ce9 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -15,7 +15,6 @@ use Appwrite\Utopia\Response\Model\Locale; use Appwrite\Utopia\Response\Model\Membership; use Appwrite\Utopia\Response\Model\MembershipList; use Utopia\Response as UtopiaResponse; -use Swoole\Http\Response as SwooleResponse; class Response extends UtopiaResponse { @@ -51,19 +50,11 @@ class Response extends UtopiaResponse const MODEL_MEMBERSHIP = 'membership'; const MODEL_MEMBERSHIP_LIST = 'membershipList'; - /** - * Swoole Response Object - * - * @var SwooleResponse - */ - protected $swoole = null; - /** * Response constructor. */ - public function __construct(SwooleResponse $response) + public function __construct(int $time = 0) { - $this->swoole = $response; $this ->setModel(new Error()) ->setModel(new ErrorDev()) @@ -76,7 +67,7 @@ class Response extends UtopiaResponse ->setModel(new MembershipList()) ; - $this->startTime = \microtime(true); + parent::__construct($time); } /** @@ -182,83 +173,4 @@ class Response extends UtopiaResponse ->send(yaml_emit($data, YAML_UTF8_ENCODING)) ; } - - /** - * Output response - * - * Generate HTTP response output including the response header (+cookies) and body and prints them. - * - * @param string $body - * @param int $exit exit code or don't exit if code is null - * - * @return self - */ - public function send(string $body = '', int $exit = null): void - { - if(!$this->disablePayload) { - $this->addHeader('X-Debug-Speed', microtime(true) - $this->startTime); - - $this - ->appendCookies() - ->appendHeaders() - ; - - $this->size = $this->size + mb_strlen(implode("\n", $this->headers)) + mb_strlen($body, '8bit'); - - $this->swoole->end($body); - - $this->disablePayload(); - } - } - - /** - * Append headers - * - * Iterating over response headers to generate them using native PHP header function. - * This method is also responsible for generating the response and content type headers. - * - * @return self - */ - protected function appendHeaders(): self - { - // Send status code header - $this->swoole->status($this->statusCode); - - // Send content type header - $this - ->addHeader('Content-Type', $this->contentType) - ; - - // Set application headers - foreach ($this->headers as $key => $value) { - $this->swoole->header($key, $value); - } - - return $this; - } - - /** - * Append cookies - * - * Iterating over response cookies to generate them using native PHP cookie function. - * - * @return self - */ - protected function appendCookies(): self - { - foreach ($this->cookies as $cookie) { - $this->swoole->cookie( - $cookie['name'], - $cookie['value'], - $cookie['expire'], - $cookie['path'], - $cookie['domain'], - $cookie['secure'], - $cookie['httponly'], - $cookie['samesite'], - ); - } - - return $this; - } } diff --git a/tests/unit/Utopia/FilesTest.php b/tests/unit/Swoole/FilesTest.php similarity index 98% rename from tests/unit/Utopia/FilesTest.php rename to tests/unit/Swoole/FilesTest.php index 6f3fe9f00..adcf4b4fc 100644 --- a/tests/unit/Utopia/FilesTest.php +++ b/tests/unit/Swoole/FilesTest.php @@ -2,8 +2,7 @@ namespace Appwrite\Tests; -use Appwrite\URL\URL; -use Appwrite\Utopia\Files; +use Appwrite\Swoole\Files; use PHPUnit\Framework\TestCase; class FilesTest extends TestCase