Skip to content

Commit 7028c35

Browse files
author
Anton Shabouta
committed
Added Swoole and AMPHP benchmarks
1 parent 630acb2 commit 7028c35

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1505
-625
lines changed

bench/monitoring.xml

-5
This file was deleted.

db/mysql/Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
FROM mysql:latest
22

3+
RUN mkdir -p \
4+
/var/lib/mysql \
5+
/var/run/mysqld && \
6+
chown mysql /var/lib/mysql && \
7+
chown mysql /var/run/mysqld
8+
9+
COPY mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
310
COPY seeds/app.sql /docker-entrypoint-initdb.d/app.sql

db/mysql/mysqld.cnf

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[mysqld]
2+
socket = /var/run/mysqld/mysqld.sock
3+
pid-file = /var/run/mysqld/mysqld.pid
4+
5+
default_authentication_plugin = caching_sha2_password
6+
back_log = 500
7+
max_connections = 3000
8+
max_allowed_packet = 64M
9+
10+
log-error = /var/lib/mysql/error.log
11+
general_log = 1
12+
general_log_file = /var/lib/mysql/general.log
13+
slow_query_log = 1
14+
slow_query_log_file = /var/lib/mysql/slow.log
15+
long_query_time = 1
16+
log_slow_admin_statements = 1
17+
log_queries_not_using_indexes = 1
18+
19+
interactive_timeout = 28800
20+
wait_timeout = 28800
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"require": {
3+
"amphp/http-server-router": "^1.0",
4+
"amphp/http-server": "^1.1",
5+
"amphp/mysql": "^1.1",
6+
"amphp/log": "^1.1"
7+
},
8+
"autoload": {
9+
"psr-4": {
10+
"App\\": "src/"
11+
}
12+
}
13+
}

platform/amphp-cluster/app/server.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
require __DIR__ . '/vendor/autoload.php';
4+
5+
use Amp\Http\Server\RequestHandler\CallableRequestHandler;
6+
use Amp\Http\Server\Router;
7+
use Amp\Http\Server\Server;
8+
use Amp\Http\Server\Request;
9+
use Amp\Http\Server\Response;
10+
use Amp\Http\Status;
11+
use Amp\Socket;
12+
use App\Controller;
13+
use App\Repository;
14+
use Psr\Log\NullLogger;
15+
16+
Amp\Loop::run(function () {
17+
$repository = new Repository(\getenv('DATABASE'));
18+
$controller = new Controller($repository);
19+
20+
$sockets = [
21+
Socket\listen("0.0.0.0:9000"),
22+
Socket\listen("[::]:9000"),
23+
];
24+
25+
$router = new Router;
26+
$router->addRoute('GET', '/', new CallableRequestHandler(function (Request $request) use ($controller) {
27+
$result = yield $controller($request);
28+
29+
return new Response(Status::OK, ['content-type' => 'application/json'], \json_encode($result));
30+
}));
31+
32+
$server = new Server($sockets, $router, new NullLogger());
33+
34+
yield $server->start();
35+
36+
Amp\Loop::onSignal(\SIGINT, function (string $watcherId) use ($server) {
37+
Amp\Loop::cancel($watcherId);
38+
39+
yield $server->stop();
40+
});
41+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
use Amp\Http\Server\Request;
8+
use Amp\Promise;
9+
use function Amp\call;
10+
11+
class Controller
12+
{
13+
/**
14+
* @var Repository
15+
*/
16+
private $repository;
17+
18+
/**
19+
* @param Repository $repository
20+
*/
21+
public function __construct(Repository $repository)
22+
{
23+
$this->repository = $repository;
24+
}
25+
26+
/**
27+
* @param Request $request
28+
*
29+
* @return array
30+
*/
31+
public function __invoke(Request $request): Promise
32+
{
33+
return call(function () use ($request) {
34+
\parse_str($request->getUri()->getQuery(), $query);
35+
36+
$items = yield $this->repository->paginate($query['page'] ?? \random_int(1, 199), $query['limit'] ?? 50);
37+
38+
return \array_map(function (array $item) {
39+
return [
40+
'id' => (int) $item['id'],
41+
'name' => \sprintf('%s %s', $item['first_name'], $item['last_name']),
42+
];
43+
}, $items);
44+
});
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
use Amp\Mysql\ConnectionConfig;
8+
use Amp\Mysql\ResultSet;
9+
use Amp\Mysql\Statement;
10+
use Amp\Promise;
11+
use Amp\Sql\Pool;
12+
use Psr\Log\LoggerInterface;
13+
use function Amp\call;
14+
use function Amp\Mysql\pool;
15+
16+
class Repository
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $dsn;
22+
23+
/**
24+
* @var Pool
25+
*/
26+
private $pool;
27+
28+
/**
29+
* @var bool
30+
*/
31+
private $connected = false;
32+
33+
/**
34+
* @param string $dsn
35+
*/
36+
public function __construct(string $dsn)
37+
{
38+
$this->dsn = $dsn;
39+
}
40+
41+
/**
42+
* @return void
43+
*/
44+
public function connect(): void
45+
{
46+
if ($this->connected) {
47+
return;
48+
}
49+
50+
$this->pool = pool(ConnectionConfig::fromString($this->dsn));
51+
52+
$this->connected = true;
53+
}
54+
55+
/**
56+
* @param int $page
57+
* @param int $limit
58+
*
59+
* @return Promise<array>
60+
*/
61+
public function paginate(int $page, int $limit): Promise
62+
{
63+
$this->connect();
64+
65+
return call(function () use ($page, $limit) {
66+
$page = $page > 1 ? $page : 1;
67+
$offset = ($page - 1) * $limit;
68+
69+
/** @var ResultSet $result */
70+
$result = yield $this->pool->execute('SELECT * FROM users LIMIT :limit OFFSET :offset;', [
71+
'limit' => $limit,
72+
'offset' => $offset,
73+
]);
74+
75+
$rows = [];
76+
77+
while (yield $result->advance()) {
78+
$rows[] = $result->getCurrent();
79+
}
80+
81+
return $rows;
82+
});
83+
}
84+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
version: '3'
2+
services:
3+
nginx:
4+
build: ./../../http/nginx
5+
container_name: nginx
6+
ports:
7+
- 80:80
8+
volumes:
9+
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
10+
- ./app:/var/www/html:ro
11+
depends_on:
12+
- php
13+
ulimits:
14+
nproc: 819200
15+
nofile: 819200
16+
mysql:
17+
build: ./../../db/mysql
18+
container_name: mysql
19+
command: --default-authentication-plugin=mysql_native_password
20+
environment:
21+
MYSQL_DATABASE: app
22+
MYSQL_ROOT_PASSWORD: root
23+
php:
24+
build: ./php
25+
container_name: php
26+
command: ["php", "/var/www/html/server.php"]
27+
volumes:
28+
- ./app:/var/www/html:ro
29+
depends_on:
30+
- mysql
31+
environment:
32+
TIMEZONE: Europe/Moscow
33+
DATABASE: "host=mysql user=root password=root db=app"
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server {
2+
listen 80 default_server;
3+
root /var/www/html/public;
4+
5+
resolver 127.0.0.11 valid=5s;
6+
set $upstream http://php:9000;
7+
8+
location / {
9+
proxy_pass $upstream;
10+
}
11+
}

platform/amphp-cluster/php/Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM php:7.3-cli
2+
3+
RUN apt-get update \
4+
&& apt-get -y install libevent-dev libssl-dev \
5+
&& docker-php-ext-install pcntl \
6+
&& docker-php-ext-install sockets \
7+
&& pecl install event \
8+
&& docker-php-ext-enable event
9+
10+
RUN usermod -u 1000 www-data
11+
12+
COPY php.ini /usr/local/etc/php/php.ini

platform/amphp-cluster/php/php.ini

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
date.timezone = ${TIMEZONE}
2+
short_open_tag = Off
3+
log_errors = On
4+
error_reporting = E_ALL
5+
display_errors = Off
6+
error_log = /proc/self/fd/2
7+
memory_limit = 512M
8+
9+
; Optimizations for Symfony, as documented on http://symfony.com/doc/current/performance.html
10+
opcache.enable=1
11+
opcache.enable_cli=1
12+
; maximum memory that OPcache can use to store compiled PHP files
13+
opcache.memory_consumption=256
14+
; maximum number of files that can be stored in the cache
15+
opcache.max_accelerated_files=20000
16+
opcache.validate_timestamps=0
17+
; maximum memory allocated to store the results
18+
realpath_cache_size=4096K
19+
; save the results for 10 minutes (600 seconds)
20+
realpath_cache_ttl=600
21+
22+
zend.assertions = -1

platform/amphp/app/composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"require": {
3-
"amphp/http-server-router": "^1.0",
43
"amphp/http-server": "^1.1",
54
"amphp/mysql": "^1.1",
65
"amphp/log": "^1.1"

0 commit comments

Comments
 (0)