我决定做一些你可以在你自己的服务器上运行的基准测试,而不是推测,以查看在你的情况下该问题的答案是什么。我还将包括我在计算机上获得的那些非常有趣的测试结果。
准备测试
首先,我做了什么以及任何人都可以重复它:
创建一些新目录并安装 http-server 模块 - 如果您已经有一个正在运行的服务器,则可以跳过这部分,但我将其包含在此处,以便任何人都可以重复这些测试:
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
运行基准测试
现在,在另一个终端中,也成为 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)
回答您的问题
这是我在一个非常繁忙的系统上得到的结果,可用 RAM 很少,因此您的里程可能会有所不同。但它同时服务于 10,000 个连接,因此您的问题的答案是:它可以处理很多请求,至少 10,000 个。我想知道您将能够在自己的服务器上实现什么 - 如果您得到一些有趣的结果,请发表评论。
结论
如果您使用http-server,那么您不必担心请求的复杂性,因为它们都会做同样的事情——从磁盘提供单个静态文件。唯一的区别是文件的大小,但提供更大的文件不应该是可能的并发连接数,而是传输数据所需的时间。
您应该在您自己实际提供的真实文件上进行这些测试,以便您可以看到您自己的具体案例的数字。
结果很有趣,因为它显示了使用 Node.js 编写的如此简单的服务器可以处理多少连接。用 Apache 试试。