HTTPServer
HTTP\Server对HTTP协议的支持并不完整,建议作为应用服务器。并且在前端增加Nginx作为代理
<?php
// 监听9501端口,创建http服务
$http = new Swoole\Http\Server('127.0.0.1',9501);
// 接收到请求,设置返回数据
$http->on('request',function ($request,$response){
$response->end('<h1>Hello Swoole</h1>'.PHP_EOL);
});
// 开启服务
$http->start();
测试验证
#执行shell脚本
curl localhost:9501
############返回结果################
#<h1>Hello Swoole</h1>
###################################
性能测试
ab -c 200 -n 2000000 -k http://192.168.255.109:9501/
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.255.109 (be patient)
Completed 200000 requests
Completed 400000 requests
Completed 600000 requests
Completed 800000 requests
Completed 1000000 requests
Completed 1200000 requests
Completed 1400000 requests
Completed 1600000 requests
Completed 1800000 requests
Completed 2000000 requests
Finished 2000000 requests
Server Software: swoole-http-server
Server Hostname: 192.168.255.109
Server Port: 9501
Document Path: /
Document Length: 22 bytes
Concurrency Level: 200
Time taken for tests: 197.370 seconds
Complete requests: 2000000
Failed requests: 0
Keep-Alive requests: 2000000
Total transferred: 350000000 bytes
HTML transferred: 44000000 bytes
Requests per second: 10133.27 [#/sec] (mean)
Time per request: 19.737 [ms] (mean)
Time per request: 0.099 [ms] (mean, across all concurrent requests)
Transfer rate: 1731.76 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 2
Processing: 3 20 7.6 18 121
Waiting: 3 20 7.6 18 121
Total: 3 20 7.6 18 121
Percentage of the requests served within a certain time (ms)
50% 18
66% 21
75% 22
80% 23
90% 25
95% 28
98% 50
99% 57
100% 121 (longest request)
HTTPServer设置Cookie
<?php
// 监听9501端口,创建http服务
$http = new Swoole\Http\Server('0.0.0.0',9501);
// 接收到请求,设置返回数据
$http->on('request',function ($request,$response){
// 设置cookie
$response->cookie('php','phpcookie',$expire=180,$path="/");
$response->end("请求参数:".json_encode($request->get));
});
// 开启服务
$http->start();
验证Cookie
curl -v http://192.168.255.109:9501/?m=1&c=2
[3] 8612
[2] Done curl -v http://192.168.255.109:9501/?m=1
[work@p109 client]$ * About to connect() to 192.168.255.109 port 9501 (#0)
* Trying 192.168.255.109...
* Connected to 192.168.255.109 (192.168.255.109) port 9501 (#0)
> GET /?m=1 HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.255.109:9501
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: swoole-http-server
< Connection: keep-alive
< Content-Type: text/html
< Date: Sun, 12 Jan 2020 11:20:29 GMT
< Content-Length: 24
< Set-Cookie: php=phpcookie; expires=Thu, 01-Jan-1970 00:03:00 GMT; path=/
<
* Connection #0 to host 192.168.255.109 left intact
请求参数:{"m":"1"}