本书未发布

创建Web服务器

为了更好的阅读体验,将文档从 Swoole 官方文档 搬运过来。内容比较多,我会慢慢搬运过来,也欢迎大家以提交改进的方式一起帮助我。

创建Web服务器

程序代码 http_server.php

$http = new swoole_http_server("0.0.0.0", 9501);

$http->on('request', function ($request, $response) {
    var_dump($request->get, $request->post);
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

Http 服务器只需要关注请求响应即可,所以只需要监听一个 onRequest 事件。当有新的 Http 请求进入就会触发此事件。事件回调函数有2个参数,一个是 $request对象,包含了请求的相关信息,如 GET/POST请求的数据。

另外一个是 response 对象,对 request 的响应可以通过操作 response 对象来完成。$response->end() 方法表示输出一段 HTML 内容,并结束此请求。

  • 0.0.0.0 表示监听所有 IP 地址,一台服务器可能同时有多个 IP,如 127.0.0.1 本地回环 IP、192.168.1.100 局域网 IP、210.127.20.2 外网IP,这里也可以单独指定监听一个 IP

  • 9501 监听的端口,如果被占用程序会抛出致命错误,中断执行。

    启动服务

    php http_server.php

  • 可以打开浏览器,访问http://127.0.0.1:9501查看程序的结果。

  • 也可以使用apache ab工具对服务器进行压力测试

Chrome 请求两次问题

使用 Chrome 浏览器访问服务器,会产生额外的一次请求,/favicon.ico,可以在代码中响应 404 错误。

$http->on('request', function ($request, $response) {
    if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
        return $response->end();
    }
    var_dump($request->get, $request->post);
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

URL 路由

应用程序可以根据$request->server['request_uri']实现路由。如:http://127.0.0.1:9501/test/index/?a=1,代码中可以这样实现URL路由

$http->on('request', function ($request, $response) {
    list($controller, $action) = explode('/', trim($request->server['request_uri'], '/'));
    //根据 $controller, $action 映射到不同的控制器类和方法
    (new $contoller)->$action($request, $response);
});

测试结果

下面为对应的测试数据对比结果 * nginx c module 代码如下: location /opt{ 测试 nginx c module echo "hello word"; }

测试结果如下:

ab -c 300 -n 100000 127.0.0.1:8080/opt This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests

Server Software: openresty/1.13.6.2 Server Hostname: 127.0.0.1 Server Port: 8080

Document Path: /opt Document Length: 11 bytes

Concurrency Level: 300 Time taken for tests: 5.021 seconds Complete requests: 100000 Failed requests: 0 Write errors: 0 Total transferred: 14000000 bytes HTML transferred: 1100000 bytes Requests per second: 19914.99 [#/sec] (mean) Time per request: 15.064 [ms] (mean) Time per request: 0.050 [ms] (mean, across all concurrent requests) Transfer rate: 2722.75 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 0 4 1.3 4 7 Processing: 2 11 1.5 11 16 Waiting: 0 8 0.7 8 10 Total: 6 15 1.2 15 19

Percentage of the requests served within a certain time (ms) 50% 15 66% 15 75% 16 80% 16 90% 17 95% 17 98% 17 99% 18 100% 19 (longest request) * mixphp1.1 版本的测试结果 ab -c 300 -n 100000 127.0.0.1:9501/ This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests

Server Software: swoole-http-server Server Hostname: 127.0.0.1 Server Port: 9501

Document Path: / Document Length: 13 bytes

Concurrency Level: 300 Time taken for tests: 8.759 seconds Complete requests: 100000 Failed requests: 0 Write errors: 0 Total transferred: 17600000 bytes HTML transferred: 1300000 bytes Requests per second: 11417.05 [#/sec] (mean) Time per request: 26.276 [ms] (mean) Time per request: 0.088 [ms] (mean, across all concurrent requests) Transfer rate: 1962.31 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 0 4 1.7 4 25 Processing: 6 22 3.4 22 70 Waiting: 0 18 2.6 18 63 Total: 6 26 4.0 26 94

Percentage of the requests served within a certain time (ms) 50% 26 66% 27 75% 28 80% 29 90% 30 95% 32 98% 35 99% 39 100% 94 (longest request) * 对应的openresty http框架测试结果 ab -c 300 -n 100000 127.0.0.1:88/ This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests

Server Software: openresty/1.13.6.2 Server Hostname: 127.0.0.1 Server Port: 88

Document Path: / Document Length: 14 bytes

Concurrency Level: 300 Time taken for tests: 10.265 seconds Complete requests: 100000 Failed requests: 0 Write errors: 0 Total transferred: 20600000 bytes HTML transferred: 1400000 bytes Requests per second: 9741.83 [#/sec] (mean) Time per request: 30.795 [ms] (mean) Time per request: 0.103 [ms] (mean, across all concurrent requests) Transfer rate: 1959.78 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 0 3 1.4 3 15 Processing: 6 28 8.4 27 53 Waiting: 0 25 9.0 23 52 Total: 6 31 7.4 30 54

Percentage of the requests served within a certain time (ms) 50% 30 66% 34 75% 37 80% 37 90% 41 95% 44 98% 46 99% 48 100% 54 (longest request)

对应的nginx drizze mysql 模块直接访问数据库压力测试结果 ab -c 300 -n 10000 127.0.0.1:8080/cat/22 This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient) Completed 1000 requests Completed 2000 requests Completed 3000 requests Completed 4000 requests Completed 5000 requests Completed 6000 requests Completed 7000 requests Completed 8000 requests Completed 9000 requests Completed 10000 requests Finished 10000 requests

Server Software: openresty/1.13.6.2 Server Hostname: 127.0.0.1 Server Port: 8080

Document Path: /cat/22 Document Length: 1114 bytes

Concurrency Level: 300 Time taken for tests: 2.434 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 12880000 bytes HTML transferred: 11140000 bytes Requests per second: 4108.21 [#/sec] (mean) Time per request: 73.025 [ms] (mean) Time per request: 0.243 [ms] (mean, across all concurrent requests) Transfer rate: 5167.35 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 0 4 4.1 4 53 Processing: 6 68 6.2 69 144 Waiting: 1 65 5.6 65 137 Total: 6 73 7.4 73 145

Percentage of the requests served within a certain time (ms) 50% 73 66% 73 75% 73 80% 73 90% 74 95% 74 98% 75 99% 116 100% 145 (longest request)

swoole_http 服务器 对比测试结果

ab -c 300 -n 100000 127.0.0.1:9509/ This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient) Completed 10000 requests Completed 20000 requests Completed 30000 requests Completed 40000 requests Completed 50000 requests Completed 60000 requests Completed 70000 requests Completed 80000 requests Completed 90000 requests Completed 100000 requests Finished 100000 requests

Server Software: swoole-http-server Server Hostname: 127.0.0.1 Server Port: 9509

Document Path: / Document Length: 11 bytes

Concurrency Level: 300 Time taken for tests: 5.290 seconds Complete requests: 100000 Failed requests: 0 Write errors: 0 Total transferred: 15900000 bytes HTML transferred: 1100000 bytes Requests per second: 18905.00 [#/sec] (mean) Time per request: 15.869 [ms] (mean) Time per request: 0.053 [ms] (mean, across all concurrent requests) Transfer rate: 2935.44 [Kbytes/sec] received

Connection Times (ms) min mean[+/-sd] median max Connect: 0 4 1.4 4 9 Processing: 3 12 1.5 12 21 Waiting: 0 8 0.6 8 15 Total: 6 16 0.9 16 24

Percentage of the requests served within a certain time (ms) 50% 16 66% 16 75% 16 80% 16 90% 17 95% 17 98% 17 99% 18 100% 24 (longest request)

本文章首发在 LearnKu.com 网站上。
上一篇 下一篇
讨论数量: 0
发起讨论 只看当前版本


暂无话题~