我使用 Angular2 作为客户端和 NodeJS 作为服务器构建了一个 Web 应用程序。我希望使用 npm 的 http-server
应用程序来提供它,而无需任何配置,但我想知道它能同时处理多少个客户端?
我使用 Angular2 作为客户端和 NodeJS 作为服务器构建了一个 Web 应用程序。我希望使用 npm 的 http-server
应用程序来提供它,而无需任何配置,但我想知道它能同时处理多少个客户端?
我决定进行一些基准测试,以便你可以在自己的服务器上运行并查看在你的情况下该问题的答案,而不是进行猜测。我还将包括我在计算机上得到的这些测试结果,它们非常有趣。
首先,我做了什么,以及任何人如何重复操作:
创建一个新目录并安装http-server
模块-如果您已经有一个运行中的服务器,则可以跳过此部分,但我在这里包含它,以便任何人都可以重复这些测试:
mkdir httptest
cd httptest
npm install http-server
现在您需要启动服务器。我们将使用根用户来启动,因为这样最容易增加打开文件限制。
成为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
现在,在另一个终端中,也要以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个请求,全部(100%)同时发送。
我得到的结果是:
# 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)
以下是我在一个非常繁忙且可用RAM非常少的系统上得到的结果,因此可能会有所不同。但是它同时服务于10,000个连接,所以你的问题的答案是:它可以处理很多请求,至少10,000个。 我想知道您在自己的服务器上能够实现什么-如果您获得了一些有趣的结果,请留言。
如果您使用http-server
,则不必担心请求的复杂性,因为所有请求都将执行相同的操作-从磁盘提供单个静态文件。唯一的区别在于文件的大小,但提供更大的文件不应该在可能的并发连接数中,而应该考虑传输数据所需的时间。
您应该对您实际提供的真实文件进行测试,以便您可以看到您自己特定情况的数字。
结果很有趣,因为它显示出使用Node编写的这样一个简单服务器可以处理多少连接。尝试在Apache中进行同样的测试。