http服务器可以处理多少个客户端?

时间:2016-11-24 14:13:44

标签: node.js client-server httpserver

我构建了一个Web应用程序,Angular2作为客户端,NodeJS作为服务器。我想用npm的http-server应用程序提供服务而不需要任何配置,但我想知道它可以同时处理多少个客户端?

2 个答案:

答案 0 :(得分:12)

而不是猜测我决定制作一些基准,你可以在自己的服务器上运行,看看问题的答案在你的情况下 。我还将包括我在计算机上获得的那些测试的结果非常有趣。

准备考试

首先,我做了什么以及如何重复它:

创建一个新目录并安装http-server模块 - 如果您已经有一台正在运行的服务器,可以跳过此部分,但我将其包含在此处,以便任何人都可以重复这些测试:

mkdir httptest
cd httptest
npm install http-server

启动服务器

现在您必须启动服务器。我们将使用root来执行此操作,因为这样最容易增加打开文件的限制。

成为root以便以后能够增加打开文件限制:

sudo -s

现在以root身份:

ulimit -a 100000

现在以root身份运行服务器:

./node_modules/.bin/http-server

或运行它,但如果已安装http-server,则通常会运行。

您应该看到类似的内容:

Starting up http-server, serving ./
Available on:
  http://127.0.0.1:8080

运行基准

现在,在另一个终端中,也成为root用户:

sudo -s

您需要安装Apache的ab工具。在Ubuntu上,您可以使用以下命令安装它:

apt-get install apache2-utils

现在,仍以root身份,增加打开文件限制:

ulimit -n 100000

并以:

开始基准测试
ab -n 10000 -c 10000 -k http://localhost:8080/

这意味着要生成10,000个请求,其中10,000个(所有这些)同时生成。

测试结果

我得到的结果是:

# ab -n 10000 -c 10000 -k http://localhost:8080/
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        
Server Hostname:        localhost
Server Port:            8080

Document Path:          /
Document Length:        538 bytes

Concurrency Level:      10000
Time taken for tests:   17.247 seconds
Complete requests:      10000
Failed requests:        0
Keep-Alive requests:    0
Total transferred:      7860000 bytes
HTML transferred:       5380000 bytes
Requests per second:    579.82 [#/sec] (mean)
Time per request:       17246.722 [ms] (mean)
Time per request:       1.725 [ms] (mean, across all concurrent requests)
Transfer rate:          445.06 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  255 321.2    141    1000
Processing:   143 2588 1632.6   3073   16197
Waiting:      143 2588 1632.7   3073   16197
Total:        143 2843 1551.8   3236   17195

Percentage of the requests served within a certain time (ms)
  50%   3236
  66%   3386
  75%   3455
  80%   3497
  90%   3589
  95%   3636
  98%   3661
  99%   3866
 100%  17195 (longest request)

回答你的问题

这是我在一个非常繁忙的系统上得到的,只有很少的可用内存,所以你的里程可能会有所不同。但它同时提供了10,000个连接,所以你的问题的答案是:它可以处理很多请求,至少10,000个。我想知道你能在自己的服务器上实现什么 - 如果你得到一些有趣的结果,请评论。

结论

如果您使用http-server,那么您不必担心请求的复杂性,因为它们都会做同样的事情 - 从磁盘提供单个静态文件。唯一的区别是文件的大小,但服务较大的文件不应该是可能的并发连接数,而是传输数据所需的时间。

您应该在您自己真实服务的真实文件上进行这些测试,以便您可以查看自己特定案例的数字。

结果很有意思,因为它显示了使用Node编写的这样一个简单服务器可以处理的连接数。尝试使用Apache。

答案 1 :(得分:1)

最大吞吐量取决于您使用的硬件和请求的复杂性(cpu / io / eventloop块......)。

您可以使用一些http基准测试工具自行测量,或在此处找到一些示例:https://raygun.com/blog/2016/06/node-performance/

一些http基准测试工具: