如何快速调度 PTS 的百万并发能力
发布时间:2022-01-04 点击数:436
作者:灵苒
业界常见的压测软件 JMeter 和 PTS
Cloud Native
目前 JMeter 是性能压测领域应用最广泛的开源软件。
对于场景简单,要求测试并发量不高的情况下,JMeter 本地测试就能满足需求。但随着互联网用户的增加,对系统承载更大并发的需求日渐提升,而单台 JMeter 施压机的施压能力有一定上限,所以需要使用多台施压机,以提高 JMeter 的施压能力,这就要使用到 JMeter 的分布式施压功能。
但 JMeter 的分布式压测前置准备较多,需要注意以下几点:
- 施压机的防火墙已关闭或打开了正确的端口。为 RMI 设置了 SSL 或禁用了它。
- 所有施压机都在同一个子网上。如果使用 192.xxx或10.xxx IP 地址,则服务器位于同一子网中。
- 所有施压机上使用相同版本的 JMeter 和 Java。
- 所有施压机都已经拷贝了切分好的 CSV 数据文件、依赖 jar 包等。
- 已配置好监控数据的收集。
由此可见 JMeter 的分布式压测需要自己协调各资源,前置准备比较麻烦,对实施压测的人员来说压测效率低。
PTS 的 JMeter 压测极大的简化了 JMeter 分布式压测流程,同时也降低了压测过程中对施压机的维护成本。使用 PTS 的 JMeter 压测,用户只需要在控制台配置需要使用的机器数,无须用户提前准备多台已安装相同 Java 和 JMeter 版本的施压机。同时无须用户根据施压机数量去切分 CSV 参数文件;压测结束后,PTS 会将监控数据汇总产生一个详细的压测报告供用户查阅。
相比于直接在命令行执行 JMeter 脚本来说,PTS 使用更加方便,可按需提供海量的施压能力,并且能提供简洁直观的监控和报告。
如何发起 PTS 的 JMeter 压测
Cloud Native
和所有压测的核心步骤一样,使用 PTS 的 JMeter 压测,也主要集中在创建场景、压测场景和查看报告三个步骤中。
1、创建场景:PTS 的 JMeter 压测以场景为核心,压测对象为一个场景,场景中包括JMeter(原生)脚本、JMeter 依赖(一系列依赖 jar 包和一系列 properties 配置)、及一些压测配置(PTS 压测的配置,例如公网/VPC 压测、并发量、引擎数量、压测时长等)。
2、压测场景:对场景的操作分为两方面,一是对场景配置的增删改查,二是对场景的压测和调试。
3、生成报告:每次对场景压测都会生成一个压测任务,同时生成一个报告,其中包括压测的关键指标,如 TPS、RT、成功率等,可辅助用户排查系统性能瓶颈。此外,PTS 默认将报告保存 30 天,可以随时查看历史报告,并且提供导出 PDF 格式的报告。
所以,为了方便用户便捷调度 PTS 百万并发的能力,PTS 开通了 JMeter 的 OpenAPI,提供了如下几类压测的核心功能:编辑场景、调试场景、压测场景、查看运行时数据、查看报告。
附录:
具体步骤如下:
1 引入 pom 依赖
<!--创建PTS场景需要的实体类,如果只使用JMeter压测则不需要引入--><dependency> <groupId>com.aliyun</groupId> <artifactId>pts-api-entity</artifactId> <version>1.0.1</version></dependency><!--PTS Java SDK依赖。--><dependency> <groupId>com.aliyun</groupId> <artifactId>pts20201020</artifactId> <version>1.8.10</version></dependency><!--阿里云核心库。--><dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.2</version></dependency>
2 复制下列代码
import com.aliyun.pts20201020.Client;import com.aliyun.pts20201020.models.*;import com.aliyun.teaopenapi.models.Config; import java.util.ArrayList;import java.util.List;import java.util.Map; public class StartingDemo { public static void main(String[] args) throws Exception { Client client = getClient(); // 创建场景 String sceneId = createScene(client); // 启动场景 String reportId = startTesting(client, sceneId); // 最多等待次数 int count = 0; // 查询是否已生成报告 while (!hasReport(client, reportId) && count++ < 20) { // 若报告还未生成,则等待(30s)一段时间再查询 // 根据压测时间酌情等待 Thread.sleep(30 * 1000); } // 查看报告 getJMeterReport(client, reportId); } private static boolean hasReport(Client client, String reportId) throws Exception { ListJMeterReportsRequest request = new ListJMeterReportsRequest(); // 分页设置 request.setPageNumber(1); request.setPageSize(1); // 查询条件设置 request.setReportId(reportId); ListJMeterReportsResponse response = client.listJMeterReports(request); return response.getBody().getReports().size() > 0; } private static void getJMeterReport(Client client, String reportId) throws Exception { // 查看机器日志 GetJMeterLogsResponse getJMeterLogsResponse = getJMeterLogs(client, reportId); List<Map<String, ?>> logs = getJMeterLogsResponse.getBody().getLogs(); // 查看采样器聚合数据 GetJMeterSampleMetricsResponse getJMeterSampleMetrics = getJMeterSampleMetrics(client, reportId); List<String> sampleMetricList = getJMeterSampleMetrics.getBody().getSampleMetricList(); // 查看采样日志 GetJMeterSamplingLogsResponse getJMeterSamplingLogs = getJMeterSamplingLogs(client, reportId); List<String> sampleResults = getJMeterSamplingLogs.getBody().getSampleResults(); } private static GetJMeterSamplingLogsResponse getJMeterSamplingLogs(Client client, String reportId) throws Exception { GetJMeterSamplingLogsRequest request = new GetJMeterSamplingLogsRequest(); // 分页设置 request.setPageNumber(1); request.setPageSize(10); // 条件设置 request.setReportId(reportId); GetJMeterSamplingLogsResponse response = client.getJMeterSamplingLogs(request); return response; } private static GetJMeterSampleMetricsResponse getJMeterSampleMetrics(Client client, String reportId) throws Exception { GetJMeterSampleMetricsRequest request = new GetJMeterSampleMetricsRequest(); // 设置报告id request.setReportId(reportId); GetJMeterSampleMetricsResponse response = client.getJMeterSampleMetrics(request); return response; } private static GetJMeterLogsResponse getJMeterLogs(Client client, String reportId) throws Exception { GetJMeterLogsRequest request = new GetJMeterLogsRequest(); // 分页设置 request.setPageNumber(1); request.setPageSize(10); // 查询的压测引擎索引 request.setReportId(reportId); GetJMeterLogsResponse response = client.getJMeterLogs(request); return response; } private static String startTesting(Client client, String sceneId) throws Exception { StartTestingJMeterSceneResponse startTestingSceneResponse = startTestingScene(client, sceneId); String reportId = startTestingSceneResponse.getBody().getReportId(); return reportId; } private static StartTestingJMeterSceneResponse startTestingScene(Client client, String sceneId) throws Exception { StartTestingJMeterSceneRequest request = new StartTestingJMeterSceneRequest(); request.setSceneId(sceneId); StartTestingJMeterSceneResponse response = client.startTestingJMeterScene(request); return response; } private static String createScene(Client client) throws Exception { SaveOpenJMeterSceneRequest request = new SaveOpenJMeterSceneRequest(); // 定义场景 SaveOpenJMeterSceneRequest.SaveOpenJMeterSceneRequestOpenJMeterScene scene = new SaveOpenJMeterSceneRequest.SaveOpenJMeterSceneRequestOpenJMeterScene(); // 设置场景名 scene.setSceneName("test"); // 设置文件列表,包括JMeter脚本、JMeter压测依赖jar包、配置额度数据文件等 List<SaveOpenJMeterSceneRequest.SaveOpenJMeterSceneRequestOpenJMeterSceneFileList> fileList = new ArrayList<SaveOpenJMeterSceneRequest.SaveOpenJMeterSceneRequestOpenJMeterSceneFileList>(); // 设置文件的属性 需要设置文件的名称和文件公网可访问的oss地址 SaveOpenJMeterSceneRequest.SaveOpenJMeterSceneRequestOpenJMeterSceneFileList testFile = new SaveOpenJMeterSceneRequest.SaveOpenJMeterSceneRequestOpenJMeterSceneFileList(); testFile.setFileName("baidu.jmx"); testFile.setFileOssAddress("https://pts-openapi-test.oss-cn-shanghai.aliyuncs.com/baidu.jmx"); fileList.add(testFile); scene.setFileList(fileList); // 设置场景并发,可设置为100万 scene.setConcurrency(1000000); // 设置引擎数量 说明:一台引擎最多能发500并发,最少1并发所以此处能设置的引擎数为[2,1000],另外引擎数量越多消耗vum越快 scene.setAgentCount(2000); // 设置压测持续时间 60s scene.setDuration(60); // 设置测试文件的名称,这个文件需包括在文件列表中 scene.setTestFile("baidu.jmx"); request.setOpenJMeterScene(scene); SaveOpenJMeterSceneResponse response = client.saveOpenJMeterScene(request); return response.getBody().getSceneId(); } private static Client getClient() throws Exception { // 填写自己的AK/SK String accessKeyId = "ak"; String accessKeySecret = "sk"; Config config = new Config(); config.setAccessKeyId(accessKeyId); config.setAccessKeySecret(accessKeySecret); Client client = new Client(config); return client; }}
3 填写自己的 ak/sk
在上述代码的 getClient 中填写正确的 ak/sk
4 点击启动
点击 main 方法启动
上一篇:Nginx-正反向代理及负载均衡 下一篇:混合云应用双活容灾最佳实践