一台http服务器可以处理多less个客户端?

我用Angular2作为客户端和NodeJS作为服务器构build了一个Web应用程序。 我想在没有任何configuration的情况下使用npm的http-server应用程序来服务它,但是我想知道它可以同时处理多less个客户端?

而不是推测,我决定做一些你可以在自己的服务器上运行的基准testing,看看你的情况下 ,这个问题的答案是什么。 我还将包括我在电脑上得到的testing结果,这些testing非常有趣。

准备testing

首先,我做了什么以及任何人都可以重复它:

创build一个新的目录并安装http-server模块 – 如果你已经有一个正在运行的服务器,这个部分可以跳过,但是我把它包含在这里,这样任何人都可以重复这些testing:

 mkdir httptest cd httptest npm install http-server 

启动服务器

现在你将不得不启动服务器。 我们会用root来做,因为这样做会增加打开的文件限制是最容易的。

以后成为root会增加打开的文件限制:

 sudo -s 

现在作为根:

 ulimit -a 100000 

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

 ./node_modules/.bin/http-server 

或者运行它,但是如果你已经安装了http-server你通常会运行它。

你应该看到像这样的东西:

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

运行基准

现在,在另一个terminal,也成为根源:

 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) 

回答你的问题

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

结论

如果您使用http-server那么您不必担心请求的复杂性,因为所有这些都会执行相同的操作 – 从磁盘提供单个静态文件。 唯一的区别是文件的大小,但服务更大的文件不应该在可能的并发连接数量,而是传输数据所需的时间。

你应该在自己真实的文件上做这些testing,以便你可以看到你自己的具体情况的数字。

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

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

您可以使用一些http基准工具自己衡量它,或者在这里find一些示例: https : //raygun.com/blog/2016/06/node-performance/

一些http基准工具:

  • ab : http : //httpd.apache.org/docs/2.4/programs/ab.html
  • wrk : https : //github.com/wg/wrk