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 提问
4 个回答
0

这个看版本的吧, 忘了是那篇文章看到的, go 1.9 下高并发会造成锁争抢, 锁的范围太大了, 后续的版本把锁的作用范围给细化了

0

这种测试是不科学的,这不叫性能测试
你的handler只有简单的输出页面,并没有实际处理逻辑,你试试同样复杂的计算逻辑,db操作,看看哪个快?
这个测试只能说明在I/O调度上go不如swoole

0

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

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

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

-1

https://wiki.swoole.com/wiki/...

IO模型
go语言使用单线程eventloop处理IO事件,多线程实现协程调度,执行用户层代码
swoole使用多线程eventloop处理IO事件,多进程执行用户层php代码
Go对与IO事件的处理是单线程的,无法利用多核,吞吐量稍弱于swoole
在实际的TCP/UDP 密集IO压测中,swoole表现要优于go

复制粘贴有点水了..
这是语言特性决定的,不能评价说性能好不好,各有各的优势.

撰写答案

推广链接