引用:
loadtest
是一个 Node.js 包,用于进行简单的负载测试。loadtest
可以模拟多个用户并发地对您的应用发送请求,以测试应用的性能和可扩展性。loadtest
配置参数- 并发用户数量 (
-c
参数) - 要发送的总请求数量 (
-n
参数) - 请求的 URL
- ……
- 并发用户数量 (
- 模拟 100 个并发用户对某个 http://www.baidu.com 发送总共 1000 次请求:
loadtest -c 100 -n 1000 http://www.baidu.com
这种工具在进行性能基准测试、负载测试或简单的压力测试时非常有用,尤其是当您想要了解应用在高并发场景下的表现时。
loadtest -c 100 -n 1000 http://www.baidu.com
[Mon Aug 14 2023 14:06:35 GMT+0800 (中国标准时间)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO Target URL: http://www.baidu.com
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO Max requests: 1000
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO Concurrency level: 100
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO Agent: none
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO Completed requests: 1000
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO Total errors: 0
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO Total time: 1.6719576 s
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO Requests per second: 598
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO Mean latency: 104.8 ms
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO Percentage of the requests served within a certain time
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO 50% 69 ms
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO 90% 171 ms
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO 95% 315 ms
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO 99% 624 ms
[Mon Aug 14 2023 14:06:37 GMT+0800 (中国标准时间)] INFO 100% 1054 ms (longest request)
loadtest -c 100 -n 1000 http://www.taobao.com
[Mon Aug 14 2023 14:07:12 GMT+0800 (中国标准时间)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO Target URL: http://www.taobao.com
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO Max requests: 1000
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO Concurrency level: 100
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO Agent: none
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO Completed requests: 1000
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO Total errors: 0
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO Total time: 2.0451327 s
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO Requests per second: 489
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO Mean latency: 106.7 ms
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO Percentage of the requests served within a certain time
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO 50% 81 ms
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO 90% 141 ms
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO 95% 327 ms
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO 99% 536 ms
[Mon Aug 14 2023 14:07:14 GMT+0800 (中国标准时间)] INFO 100% 1080 ms (longest request)
测试 http/2.0 vs. http/1.1
const express = require('express');
const http = require('http');
const spdy = require('spdy');
const fs = require('fs');
const path = require('path');
const app1 = express();
const app2 = express();
// Mock resources
const mockData = 'A'.repeat(1000); // 1 KB of data
app1.get('/resource', (req, res) => res.send(mockData));
app2.get('/resource', (req, res) => res.send(mockData));
// HTTP/1.1 Server
http.createServer(app1).listen(8080);
// HTTP/2 Server with the same SSL/TLS setup
const options = {
key: fs.readFileSync(path.join(__dirname, './server.key')),
cert: fs.readFileSync(path.join(__dirname, './server.crt'))
};
spdy.createServer(options, app2).listen(8443);
PS C:\Users\AINUC\Desktop\23.08> loadtest -c 100 -n 1000 http://localhost:8080/resource
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Target URL: http://localhost:8080/resource
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Max requests: 1000
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Concurrency level: 100
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Agent: none
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Completed requests: 1000
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Total errors: 0
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Total time: 0.5290842 s
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Requests per second: 1890
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Mean latency: 48.9 ms
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO Percentage of the requests served within a certain time
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO 50% 48 ms
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO 90% 58 ms
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO 95% 60 ms
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO 99% 71 ms
[Mon Aug 14 2023 13:57:49 GMT+0800 (中国标准时间)] INFO 100% 73 ms (longest request)
PS C:\Users\AINUC\Desktop\23.08> loadtest -c 100 -n 1000 https://localhost:8443/resource --insecure
(node:9456) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
(Use `node --trace-warnings ...` to show where the warning was created)
[Mon Aug 14 2023 14:01:08 GMT+0800 (中国标准时间)] INFO Requests: 0 (0%), requests per second: 0, mean latency: 0 ms
[Mon Aug 14 2023 14:01:13 GMT+0800 (中国标准时间)] INFO Requests: 923 (92%), requests per second: 185, mean latency: 518.9 ms
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO Target URL: https://localhost:8443/resource
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO Max requests: 1000
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO Concurrency level: 100
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO Agent: none
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO Completed requests: 1000
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO Total errors: 0
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO Total time: 5.281356799999999 s
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO Requests per second: 189
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO Mean latency: 512.5 ms
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO Percentage of the requests served within a certain time
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO 50% 546 ms
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO 90% 572 ms
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO 95% 586 ms
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO 99% 683 ms
[Mon Aug 14 2023 14:01:14 GMT+0800 (中国标准时间)] INFO 100% 684 ms (longest request)
你已经为HTTP/1.1和HTTP/2协议的两个Express应用服务器运行了性能测试。这里的重点是对比这两个协议在相同负载条件下的性能。让我们先了解下这两次测试的结果:
- HTTP/1.1 Server (端口8080)
- Total time: 所有请求完成所需的时间为0.5290842秒。
- Requests per second: 平均每秒的请求次数为1890。
- Mean latency: 所有请求的平均延迟为48.9毫秒。
- HTTP/2 Server (端口8443)
- Total time: 所有请求完成所需的时间为5.281356799999999秒。
- Requests per second: 平均每秒的请求次数为189。
- Mean latency: 所有请求的平均延迟为512.5毫秒。
这些结果告诉我们:
- HTTP/1.1服务器的性能明显优于HTTP/2服务器。在相同的请求总数和并发水平下,HTTP/1.1的平均响应时间和每秒请求数远远超过HTTP/2。
- HTTP/2的平均响应时间(延迟)是HTTP/1.1的大约10倍,而每秒请求处理量则是其的大约1/10。
在实际场景中,HTTP/2通常比HTTP/1.1表现得更好,特别是在需要多个请求来获取页面资源(如图片、样式表和脚本)的情况下,因为HTTP/2支持多路复用,允许多个请求和响应同时在同一连接上进行。然而,在这种简单的性能测试中,您可能会看到不同的结果,这可能是由于以下几个原因:
- SSL/TLS开销:HTTP/2在此测试中使用了SSL/TLS,而HTTP/1.1则没有。SSL/TLS的握手和加密可能导致了额外的延迟。
- 服务器配置:可能需要对HTTP/2服务器进行额外的调优或配置以获得更好的性能。
- 测试工具:
loadtest
可能没有针对HTTP/2进行特定的优化。 - 单一资源的简单请求:由于测试只请求了一个资源,所以HTTP/2的多路复用等优势没有被充分利用。
总的来说,这个测试表明,在这种特定的场景和配置下,HTTP/1.1的性能超过了HTTP/2。但在实际的复杂Web应用中,HTTP/2可能会提供更好的性能。如果您想进一步测试HTTP/2的性能,建议使用更复杂的页面和资源,以及针对HTTP/2优化的测试工具。