CentOS环境下使用Postman进行性能测试的步骤
在CentOS上使用Postman前,需先完成安装与环境配置:
.tar.gz格式),解压至/opt目录,并创建符号链接以便终端直接调用。例如:sudo tar -xzf Postman-linux-x64-*.tar.gz -C /opt
sudo ln -s /opt/Postman/Postman /usr/local/bin/postman
/etc/sysctl.conf,添加以下参数:net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.ip_local_port_range = "1024 65535"
net.core.somaxconn = 1024
执行sudo sysctl -p使配置生效。性能测试需基于明确的API请求集合:
Content-Type)、Body(如JSON数据)等信息。api-performance-tests.json),便于后续通过Runner或Newman复用。通过Postman的Runner功能(图形界面)设置性能测试参数:
通过Tests选项卡编写JavaScript脚本,验证响应结果的正确性与性能指标:
pm.test("Status code is 200", function() {
pm.response.to.have.status(200);
});
pm.test("Response contains expected data", function() {
const jsonData = pm.response.json();
pm.expect(jsonData.data.user.name).to.eql("John Doe");
});
pm.response.timings获取响应时间(如responseTime),用于后续分析。测试完成后,Postman会生成详细的报告,包含以下关键性能指标:
Postman Runner仅提供请求级别的性能数据,如需了解服务器资源使用情况(如CPU、内存、磁盘IO),需结合CentOS自带工具:
对于自动化性能测试需求,可使用Newman(Postman的命令行工具):
npm install -g newman
run-performance.js):调用Newman运行Postman集合,设置超时、间隔等参数。示例:const newman = require('newman');
newman.run({
collection: '/path/to/api-performance-tests.json',
options: {
timeout: 5000, // 请求超时时间(ms)
interval: 1000, // 请求间隔(ms)
reporters: 'cli' // 命令行输出报告
}
}, function(err, summary) {
if (err) throw err;
console.log(summary);
});
node run-performance.js,结果将输出到命令行。注意事项: