Skip to content

Instantly share code, notes, and snippets.

@RomanStone
Created May 8, 2019 13:31
Show Gist options
  • Save RomanStone/f1d6160ef4c0b62da640aac473429884 to your computer and use it in GitHub Desktop.
Save RomanStone/f1d6160ef4c0b62da640aac473429884 to your computer and use it in GitHub Desktop.
PHP Events Simple Hello World Server Example
<?php
$addr = '127.0.0.1';
$port = 10080;
$host = 'localhost';
function hello_world($args = array()) {
$OS = PHP_OS; //strtolower(substr(PHP_OS, 0, 3)) == 'win';
$phpversion = phpversion();
$resp = new stdClass();
$resp->http_status = array('code' => 200, 'msg' => 'OK');
$resp->http_headers = array('Content-Type' => 'text/html; charset=utf-8');
$resp->http_body = '<!DOCTYPE HTML>'
. '<html>'
. '<head>'
. '<title>Hello World</title>'
. '</head>'
. '<body>'
. '<h1>Hello World!</h1>'
. "<h4>PECL Events PHP/{$phpversion} Sockets/{$OS}</h4>"
. '<pre>' . print_r($args, true) . '</pre>'
. '</body>'
. '</html>';
$resp->http_headers['Content-Length'] = strlen($resp->http_body);
$resp->http_headers['Connection'] = 'close';
$resp->http_headers['Server'] = "Events PHP/{$phpversion}";
return $resp;
}
function _close_callback($conn = NULL) : void {
if ($conn) {
echo ".";
}
}
function _http_default($req = NULL, $arg = NULL) : void {
if ($req) {
$resp = hello_world(array(
'cmd' => $req->getCommand(),
'host' => $req->getHost(),
'path' => $req->getUri(),
'headers' => $req->getInputHeaders()
)
);
$conn = $req->getConnection();
$conn->setCloseCallback('_close_callback', NULL);
$bev = $req->getBufferEvent();
$bev->enable(Event::READ);
$buf = new EventBuffer();
$buf->add($resp->http_body);
foreach($resp->http_headers as $key => $val) {
$req->addHeader($key, $val, EventHttpRequest::OUTPUT_HEADER);
}
$req->sendReply($resp->http_status['code'], $resp->http_status['msg']);
$req->sendReplyChunk($buf);
$req->sendReplyEnd();
}
}
$base = new EventBase();
$http = new EventHttp($base);
$http->setDefaultCallback('_http_default', NULL);
if (defined('SIGINT')) {
$signal = Event::signal($base, SIGINT, function() use($base) {
echo "Caught SIGINT\n";
$base->stop();
});
$signal->add();
}
$http->bind($addr, $port);
$http->addServerAlias($host);
$http->setAllowedMethods(EventHttpRequest::CMD_GET
| EventHttpRequest::CMD_POST
//| EventHttpRequest::CMD_OPTIONS
);
$http->setTimeout(60);
$base->loop();
@RomanStone
Copy link
Author

RomanStone commented May 8, 2019

ab -c 10000 -n 10000 http://127.0.0.1:10080/

CPU: AMD Phenom II X4 (945) Windows x64 | 0.723 [ms] / Request (Avg.)


Server Software:        Events
Server Hostname:        127.0.0.1
Server Port:            10080

Document Path:          /
Document Length:        375 bytes

Concurrency Level:      10000
Time taken for tests:   7.233 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      5000000 bytes
HTML transferred:       3750000 bytes
Requests per second:    1382.47 [#/sec] (mean)
Time per request:       7233.413 [ms] (mean)
Time per request:       0.723 [ms] (mean, across all concurrent requests)
Transfer rate:          675.04 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       1
Processing:  2487 3688 881.7   3413    5605
Waiting:      459 2201 1324.6   1799    5146
Total:       2487 3688 881.7   3413    5606

Percentage of the requests served within a certain time (ms)
  50%   3413
  66%   3862
  75%   4380
  80%   4650
  90%   5146
  95%   5383
  98%   5518
  99%   5562
 100%   5606 (longest request)

PHP -V

PHP 7.1.7 (cli) ( NTS MSVC14 (Visual C++ 2015) x86 )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.7, Copyright (c) 1999-2017, by Zend Technologies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment