1

swoole的代码

<?php
$http = new swoole_http_server("127.0.0.1", 9501);

$http->on("start", function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$http->on("request", function ($request, $response) {
    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});

$http->start();

golang的代码

package main
import (
    "io"
    "net/http"
)
func main() {
    http.HandleFunc("/", handle)
    http.ListenAndServe("0.0.0.0:10056", nil)
}
func handle(rw http.ResponseWriter, r *http.Request) {
    rw.Header().Set("Content-Type", "text/html")
    io.WriteString(rw, "<h3>http server</h3>")
}

都在我机器上测试的

ab -n 30000 -c 100 "http://127.0.0.1:10056/"

swoole结果

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

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      100
Time taken for tests:   1.910 seconds
Complete requests:      30000
Failed requests:        0
Total transferred:      4830000 bytes
HTML transferred:       360000 bytes
Requests per second:    15708.78 [#/sec] (mean)
Time per request:       6.366 [ms] (mean)
Time per request:       0.064 [ms] (mean, across all concurrent requests)
Transfer rate:          2469.84 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    3   0.5      3       8
Processing:     1    4   0.9      3      18
Waiting:        0    3   0.9      2      16
Total:          2    6   0.9      6      21
WARNING: The median and mean for the processing time are not within a normal deviation
        These results are probably not that reliable.
WARNING: The median and mean for the waiting time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%      6
  66%      6
  75%      7
  80%      7
  90%      7
  95%      7
  98%      8
  99%      9
 100%     21 (longest request)

golang结果

Server Software:        
Server Hostname:        127.0.0.1
Server Port:            10056

Document Path:          /
Document Length:        20 bytes

Concurrency Level:      100
Time taken for tests:   4.364 seconds
Complete requests:      30000
Failed requests:        0
Total transferred:      3630000 bytes
HTML transferred:       600000 bytes
Requests per second:    6873.89 [#/sec] (mean)
Time per request:       14.548 [ms] (mean)
Time per request:       0.145 [ms] (mean, across all concurrent requests)
Transfer rate:          812.25 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    6   4.6      5      54
Processing:     0    9   5.8      7      63
Waiting:        0    7   4.4      6      50
Total:          0   14   8.7     12     101

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     14
  75%     15
  80%     16
  90%     20
  95%     28
  98%     47
  99%     59
 100%    101 (longest request)
xxfaxy 1.3k
2018-10-29 提问

查看全部 5 个回答

0

Swoole是多线程事件轮询IO。Golang是线程事件轮询。理论上,Swoole可以同时接受更多的连接。

但,Swoole是执行的PHP代码,通过Worker进程执行用户代码,通过Task进程执行异步逻辑,通过管道或消息队列通信。

实际上,执行效率是远远低于golang的。尤其是大量没有优化的PHP代码。再加上进程间通信效率不一定就比加锁的channel更快。

推广链接