CentOS上使用Postman进行压力测试的步骤
在CentOS系统上,可通过以下步骤安装Postman:
Postman-linux-x64-版本号.tar.gz);tar -xvf Postman-linux-x64-版本号.tar.gz;Postman文件夹移动至/opt目录:sudo mv Postman /opt;sudo ln -s /opt/Postman/Postman /usr/local/bin/postman
安装完成后,终端输入postman即可启动应用。
// 示例:检查状态码是否为200
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
// 示例:检查响应时间是否小于1秒
pm.test("Response time is less than 1000ms", function () {
pm.expect(pm.response.responseTime).to.be.below(1000);
});
保存集合为JSON文件(如performance-test.json),后续可用于Runner或Newman。
Newman是Postman的命令行工具,可将压力测试集成至CI/CD流程(如Jenkins),实现自动化运行。
npm install -g newman
run-test.js):const newman = require('newman');
newman.run({
collection: '/path/to/performance-test.json', // 集合文件路径
reporters: 'cli', // 输出命令行报告
iterationCount: 100, // 迭代次数
concurrency: 10 // 并发数
}, function (err, summary) {
if (err) {
console.error('测试失败:', err);
} else {
console.log('测试完成:', summary);
}
});
node run-test.js,即可在命令行查看测试结果。Postman Runner本身不提供服务器资源监控功能,需结合CentOS系统自带工具:
1可查看每个CPU核心的使用情况);yum install epel-release && yum install htop):更直观的资源监控工具,支持排序和过滤;vmstat 1 5 # 每1秒刷新一次,共刷新5次
通过监控资源使用,可判断服务器是否成为性能瓶颈(如CPU占用过高、内存不足)。