你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

将 JMeter 用户属性与 Azure 负载测试配合使用

本文介绍如何在 Azure 负载测试中配置和使用 Apache JMeter 用户属性。 通过使用用户属性,可以通过在 JMeter 测试脚本之外保留测试设置来使测试可配置。 用户属性的用例包括:

  • 你希望在具有不同应用程序终结点的多个部署环境中使用 JMX 测试脚本。
  • 测试脚本需要适应多个负载模式,例如冒烟测试、峰值负载或浸泡测试。
  • 想要通过配置 JMeter 设置(例如结果文件格式)来替代默认 JMeter 行为。

Azure 负载测试支持标准的 Apache JMeter 属性并且支持上传用户属性文件。 可以为每个负载测试配置一个用户属性文件。

或者,还可以在 Azure 负载测试中使用环境变量和机密以使测试可配置。

注意

Azure 负载测试会替代特定的 JMeter 属性,并忽略为这些属性指定的任何值。 详细了解 Azure 负载测试重写JMeter 属性列表。

先决条件

  • 具有活动订阅的 Azure 帐户。 如果没有 Azure 订阅,请在开始之前创建一个免费帐户
  • Azure 负载测试资源。 如果需要创建 Azure 负载测试资源,请参阅创建并运行负载测试快速入门。

将 JMeter 用户属性文件添加到负载测试

可以通过将 .properties 文件上传到负载测试来定义 JMeter 测试脚本的用户属性。 Azure 负载测试支持每个负载测试的单个 JMeter 属性文件。 其他属性文件将忽略。

以下代码片段显示了一个示例用户属性文件,该文件定义三个用户属性并配置 jmeter.save.saveservice.thread_name 配置设置:

# peak-load.properties
# User properties for testing peak load
threadCount=250
rampUpSeconds=30
durationSeconds=600

# Override default JMeter properties
jmeter.save.saveservice.thread_name=false

要使用 Azure 门户将用户属性文件添加到负载测试,请按照以下步骤操作:

  1. Azure 门户中,转到 Azure 负载测试资源。

  2. 在左窗格中,选择“测试”以查看测试列表

  3. 通过选中相应复选框来从列表中选择你的测试,然后选择“编辑”。 或者,选择“创建测试”以创建新的负载测试

  4. 选择“测试计划”选项卡

  5. 从计算机中选择属性文件,然后选择“上传”,将文件上传到 Azure

    Screenshot that shows the steps to upload a user properties file on the Test plan tab on the Edit test pane.

  6. 在“文件相关性”下拉列表中,选择“用户”属性

    Screenshot that highlights the file relevance dropdown for a user properties file on the Test plan pane.

    只能选择一个文件作为负载测试的用户属性文件。

  7. 选择“应用”以修改测试,或选择“查看 + 创建”,然后选择“创建”来创建新测试

引用 JMeter 中的属性

Azure 负载测试支持内置的 Apache JMeter 功能,以引用 JMeter 测试脚本 (JMX) 中的用户属性。 可以使用 __property__P 函数从之前上传的属性文件中检索属性值

以下代码片段演示了引用 JMX 文件中的属性的示例:

<ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="Test home page" enabled="true">
<stringProp name="ThreadGroup.on_sample_error">continue</stringProp>
<elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true">
    <boolProp name="LoopController.continue_forever">false</boolProp>
    <intProp name="LoopController.loops">-1</intProp>
</elementProp>
<stringProp name="ThreadGroup.num_threads">${__P(threadCount,1)}</stringProp>
<stringProp name="ThreadGroup.ramp_time">${__P(rampUpSeconds,1)}</stringProp>
<boolProp name="ThreadGroup.scheduler">true</boolProp>
<stringProp name="ThreadGroup.duration">${__P(durationSeconds,30)}</stringProp>
<stringProp name="ThreadGroup.delay"></stringProp>
<boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>
</ThreadGroup>

或者,还可以在 JMeter 用户界面指定属性。 下图显示了如何使用属性配置 JMeter 线程组:

Screenshot that shows how to reference user properties in the JMeter user interface.

可以下载 JMeter 错误日志以排查负载测试过程中的错误。

后续步骤