Webserver性能如何

0

用Workerman的GW快2年了,感觉挺不错的,现在有个项目想webServer,但是webServer的简介“WorkerMan自带了一个简单的Web服务器,同样也是基于Worker实现的。文件位置在Workerman/WebServer.php。这个WebServer开发的目的主要是为了方便运行一些简单的Web程序”让我有点迷惑。。。意思是说它只能用来写写小网站,不能承受大任。。。。是这个意思?

已邀请:

walkor

赞同来自: blogdaren weed yepo

总体来说workerman的tcp worker 性能 > workerman的http worker性 webserver,因为webserver每个请求要从磁盘读取文件并解析,会有一些磁盘io损耗。但是worekrman的webserver的性能仍好于nginx+php-fpm组合。
以下是我的mac pro笔记本跑的http worker 和 webserver的helloworld性能测试,php版本7.1.16,你可以参考下。
 
1、http worker 9W+QPS
代码


<?php
require_once __DIR__ . '/../Workerman/Autoloader.php';
use Workerman\Worker;
$worker = new Worker('http://0.0.0.0:1234');
$worker->count = 7;
$worker->onMessage = function($connection, $data) {
$connection->send('hello');
};
Worker::runAll(); 

压测结果 QPS 9.2W


ab -n 100000 -c100 -k  http://127.0.0.1:1234/
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
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: workerman/3.5.15
Server Hostname: 127.0.0.1
Server Port: 1234

Document Path: /
Document Length: 5 bytes

Concurrency Level: 100
Time taken for tests: 1.084 seconds
Complete requests: 100000
Failed requests: 0
Keep-Alive requests: 100000
Total transferred: 13200000 bytes
HTML transferred: 500000 bytes
Requests per second: 92280.29 (mean)
Time per request: 1.084 (mean)
Time per request: 0.011 (mean, across all concurrent requests)
Transfer rate: 11895.51 received

Connection Times (ms)
min mean median max
Connect: 0 0 0.1 0 5
Processing: 0 1 0.3 1 5
Waiting: 0 1 0.3 1 4
Total: 0 1 0.4 1 6

2、webserver 3W+QPS
代码


<?php
require_once __DIR__ . '/../Workerman/Autoloader.php';
use Workerman\Worker;
use Workerman\WebServer;
$worker = new WebServer('http://0.0.0.0:1235');
$worker->count = 7;
$worker->addRoot('localhost', __DIR__ . '/Web');
Worker::runAll();

test.php内容


<?php
echo 'hello world';

结果 3.4W QPS


ab -n 100000 -c100 -k  http://127.0.0.1:1235/test.php
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
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: workerman/3.5.15
Server Hostname: 127.0.0.1
Server Port: 1235

Document Path: /test.php
Document Length: 11 bytes

Concurrency Level: 100
Time taken for tests: 2.895 seconds
Complete requests: 100000
Failed requests: 0
Keep-Alive requests: 100000
Total transferred: 13900000 bytes
HTML transferred: 1100000 bytes
Requests per second: 34542.24 (mean)
Time per request: 2.895 (mean)
Time per request: 0.029 (mean, across all concurrent requests)
Transfer rate: 4688.84 received

Connection Times (ms)
min mean median max
Connect: 0 0 0.1 0 5
Processing: 0 3 1.4 3 11
Waiting: 0 3 1.4 3 11
Total: 0 3 1.4 3 11

总结:
 
虽然压测性能不代表最终实际项目性能,但是也能看出来workerman这块不会成为瓶颈,即使是每次都从磁盘加载文件的webserver性能也足够好。
 
但是实际上大部分项目不需要这么高的性能,绝大多数项目QPS有几百就很不错了,nginx+php-fpm这里不会成为瓶颈,万一瓶颈加台机器就好,当然你说数据库是瓶颈那是另外的讨论话题了。
 
workerman的webserver性能虽然足够好,但是毕竟是一个简单的web服务器,有以下缺点:
1、和nginx+php-fpm 或者 apache比起来功能上会欠缺,比如没有nginx/apache那么多的配置项。
2、大部分php框架都是基于php-fpm这种模式开发的,不能完全不支持php cli模式,这使得传统框架项目一般无法直接运行在workerman上,要运行需要大量的改造,成本很高。
 
所以新http项目想尝试下高性能可以考虑workerman的http worker或者workerman的webserver,但是老项目还是用php-fpm或者apache较好。
 
最后说下workerman的 http worker和webserver区别和应用场景:
http worker不会每个请求从磁盘加载文件,程序已经常驻内存,所有请求是纯内存计算,性能超高,非常适合做一些不需要渲染页面的http接口,或者微服务接口。
webserver每个请求回去磁盘加载一次文件并解析,被加载的文件没有常驻内存,有磁盘读取开销,性能也很好,适合需要渲染页面的项目。
 
 

要回复问题请先登录注册