Skip to content

Commit 826b8e6

Browse files
authored
[php] With Workerman without worker mode (#7798)
1 parent 316536c commit 826b8e6

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

frameworks/PHP/php/benchmark_config.json

+23
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,29 @@
183183
"notes": "",
184184
"versus": "php"
185185
},
186+
"workerman": {
187+
"json_url": "/json.php",
188+
"plaintext_url": "/plaintext.php",
189+
"db_url": "/dbraw.php",
190+
"query_url": "/dbquery.php?queries=",
191+
"fortune_url": "/fortune.php",
192+
"update_url": "/updateraw.php?queries=",
193+
"port": 8080,
194+
"approach": "Realistic",
195+
"classification": "Platform",
196+
"database": "MySQL",
197+
"framework": "PHP",
198+
"language": "PHP",
199+
"flavor": "PHP8",
200+
"orm": "Raw",
201+
"platform": "Workerman",
202+
"webserver": "none",
203+
"os": "Linux",
204+
"database_os": "Linux",
205+
"display_name": "php-workerman",
206+
"notes": "Workerman without worker mode",
207+
"versus": "php"
208+
},
186209
"eloquent": {
187210
"db_url": "/eloquent/db-eloquent.php",
188211
"query_url": "/eloquent/db-eloquent.php?queries=",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
opcache.enable=1
2+
opcache.enable_cli=1
3+
opcache.validate_timestamps=0
4+
opcache.save_comments=0
5+
opcache.enable_file_override=1
6+
opcache.memory_consumption=256
7+
opcache.interned_strings_buffer=16
8+
opcache.huge_code_pages=1
9+
10+
mysqlnd.collect_statistics = Off
11+
memory_limit = 512M
12+
13+
opcache.jit_buffer_size=128M
14+
opcache.jit=tracing
15+
16+
disable_functions=header,header_remove,headers_sent,http_response_code,setcookie,session_create_id,session_id,session_name,session_save_path,session_status,session_start,session_write_close,set_time_limit
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"joanhey/adapterman": "0.5"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
use Adapterman\Adapterman;
4+
use Workerman\Worker;
5+
use Workerman\Lib\Timer;
6+
7+
require_once __DIR__ . '/vendor/autoload.php';
8+
9+
Adapterman::init();
10+
// WebServer
11+
$web = new Worker("http://0.0.0.0:8080");
12+
$web->count = (int) shell_exec('nproc') * 4;
13+
$web->name = 'workerman';
14+
15+
define('WEBROOT', '/php/');
16+
17+
$web->onWorkerStart = static function () {
18+
Header::init();
19+
};
20+
21+
$web->onMessage = static function ($connection, $request) {
22+
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
23+
/* if ($path === '/') {
24+
$connection->send(exec_php_file(WEBROOT.'/index.php', $request));
25+
return;
26+
} */
27+
28+
$file = realpath(WEBROOT . $path);
29+
if (false === $file || !str_ends_with($file, '.php')) {
30+
http_response_code(404);
31+
$connection->send('<h3>404 Not Found</h3>');
32+
return;
33+
}
34+
// Security check!
35+
if (!str_starts_with($file, WEBROOT)) {
36+
http_response_code(400);
37+
$connection->send('<h3>400 Bad Request</h3>');
38+
return;
39+
}
40+
41+
header(Header::$date); // To pass the bench
42+
$connection->send(exec_php_file($file));
43+
};
44+
45+
function exec_php_file($file)
46+
{
47+
ob_start();
48+
// Try to include php file.
49+
try {
50+
include $file;
51+
} catch (\Exception $e) {
52+
echo $e;
53+
}
54+
return ob_get_clean();
55+
}
56+
57+
class Header
58+
{
59+
const NAME = 'Date: ';
60+
61+
/**
62+
* Date header
63+
*
64+
* @var string
65+
*/
66+
public static $date;
67+
68+
public static function init(): void
69+
{
70+
self::$date = self::NAME . gmdate('D, d M Y H:i:s').' GMT';
71+
Timer::add(1, static function() {
72+
self::$date = self::NAME . gmdate('D, d M Y H:i:s').' GMT';
73+
});
74+
}
75+
}
76+
77+
Worker::runAll();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM ubuntu:22.04
2+
3+
ARG DEBIAN_FRONTEND=noninteractive
4+
5+
RUN apt-get update -yqq && apt-get install -yqq software-properties-common > /dev/null
6+
RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php > /dev/null && \
7+
apt-get update -yqq > /dev/null && apt-get upgrade -yqq
8+
9+
RUN apt-get install -yqq git unzip \
10+
php8.2 php8.2-common php8.2-cli php8.2-fpm php8.2-mysql > /dev/null
11+
12+
RUN apt-get install -y php-pear php8.2-dev libevent-dev > /dev/null
13+
RUN pecl install event-3.0.8 > /dev/null && echo "extension=event.so" > /etc/php/8.2/cli/conf.d/event.ini
14+
15+
COPY deploy/workerman/cli-php.ini /etc/php/8.2/cli/php.ini
16+
17+
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
18+
19+
ADD ./ /php
20+
WORKDIR /php
21+
22+
COPY deploy/workerman/composer.json ./
23+
RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet
24+
25+
COPY deploy/workerman/start.php ./
26+
27+
RUN chmod -R 777 /php
28+
29+
EXPOSE 8080
30+
31+
CMD php start.php start

0 commit comments

Comments
 (0)