php-fpm,workerman,swoole,golang简单性能测试

颜文昌
2023-12-01

测试环境:

操作系统:CentOS Linux release 7.6.1810 (Core)  单核2G云服务器

PHP版本:PHP 7.4.21

golang版本:1.17

workerman:4.0.22(已安装event扩展)

swoole:4.7.0

测试工具:ab ,webbench

测试的脚本

测试脚本比较简单,产生1000个100-999的随机数,计算随机数的和然后输出

php

$sum = 0;
foreach(range(1,1000) as $i){
    $sum += mt_rand(100,1000);
}
echo "sum=$sum";

go

package main

import (
	"fmt"
	"math/rand"
	"net/http"
	"time"
)

func main() {

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		var sum int
		rand.Seed(time.Now().UnixNano())
		for index := 1; index <= 1000; index++ {
			sum += rand.Intn(900) + 100
		}

		w.Write([]byte(fmt.Sprintf("sum=%d", sum)))
	})

	http.ListenAndServe("127.0.0.1:8000", nil)

}

ab测试结果:

测试命令:ab -n 100000 -c 100 http://127.0.0.1:端口/

1,php-fpm

Concurrency Level:      100
Time taken for tests:   26.163 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      18900000 bytes
HTML transferred:       1000000 bytes
Requests per second:    3822.12 [#/sec] (mean)
Time per request:       26.163 [ms] (mean)
Time per request:       0.262 [ms] (mean, across all concurrent requests)
Transfer rate:          705.45 [Kbytes/sec] received

2,workerman(使用webman框架测试)

Concurrency Level:      100
Time taken for tests:   12.198 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      13100000 bytes
HTML transferred:       1000000 bytes
Requests per second:    8198.21 [#/sec] (mean)
Time per request:       12.198 [ms] (mean)
Time per request:       0.122 [ms] (mean, across all concurrent requests)
Transfer rate:          1048.79 [Kbytes/sec] received

3,swoole异步http server

Concurrency Level:      100
Time taken for tests:   11.454 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      15800000 bytes
HTML transferred:       1000000 bytes
Requests per second:    8730.33 [#/sec] (mean)
Time per request:       11.454 [ms] (mean)
Time per request:       0.115 [ms] (mean, across all concurrent requests)
Transfer rate:          1347.06 [Kbytes/sec] received

4,swoole协程http server

Concurrency Level:      100
Time taken for tests:   10.579 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      15800000 bytes
HTML transferred:       1000000 bytes
Requests per second:    9453.09 [#/sec] (mean)
Time per request:       10.579 [ms] (mean)
Time per request:       0.106 [ms] (mean, across all concurrent requests)
Transfer rate:          1458.58 [Kbytes/sec] received

5,golang http server

Concurrency Level:      100
Time taken for tests:   11.433 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Total transferred:      12700000 bytes
HTML transferred:       1000000 bytes
Requests per second:    8746.31 [#/sec] (mean)
Time per request:       11.433 [ms] (mean)
Time per request:       0.114 [ms] (mean, across all concurrent requests)
Transfer rate:          1084.75 [Kbytes/sec] received

rps比较:swoole协程http server > golang http server = swoole异步http server > workerman > php-fpm 

webbench测试结果

测试命令:webbench -c 100 -t 30 http://127.0.0.1/

1,php-fpm

Benchmarking: GET http://127.0.0.1/test.php
100 clients, running 30 sec.

Speed=210944 pages/min, 664473 bytes/sec.
Requests: 105472 susceed, 0 failed.

2,workerman(使用webman框架测试)

Benchmarking: GET http://127.0.0.1:8787/
100 clients, running 30 sec.

Speed=466696 pages/min, 1018952 bytes/sec.
Requests: 233348 susceed, 0 failed.

3,swoole异步http server

Benchmarking: GET http://127.0.0.1:9501/
100 clients, running 30 sec.

Speed=491948 pages/min, 1295463 bytes/sec.
Requests: 245974 susceed, 0 failed.

4,swoole协程http server


Benchmarking: GET http://127.0.0.1:9502/
100 clients, running 30 sec.

Speed=542284 pages/min, 1428014 bytes/sec.
Requests: 271142 susceed, 0 failed.

5,golang http server

Benchmarking: GET http://127.0.0.1:8000/
100 clients, running 30 sec.

Speed=498384 pages/min, 1054912 bytes/sec.
Requests: 249192 susceed, 0 failed.

webbench的测试结果和ab差不多

测试代码比较简单,没有进行文件IO操作,数据库操作等,结果仅供参考,workerman这种纯php框架能有这样的性能还是不错的

 类似资料: