JMeter:建立一个Web测试计划


前言

自序

  1. 文章正文的英文部分为JMeter用户手册原文,放在中文后面。
  2. 中文源于英文,但作为参考和便于阅读,进行了增删和重新排版。
  3. 中文看不懂的情况下可以参考英文,之所以放在一起,是因为跳转到官方网页浏览不方便对照阅读。

参考

原文参考:https://jmeter.apache.org/usermanual/build-web-test-plan.html

建立一个Web测试计划

在本节中,你将学习如何创建一个基本的测试计划来测试一个网站。你将创建五个用户,向JMeter网站的两个页面发送请求。此外,你将告诉用户运行他们的测试两次。因此,请求的总数是(5个用户)x(2个请求)x(重复2次)=20个HTTP请求。为了构建测试计划,你将使用以下元素:线程组,HTTP请求,HTTP请求默认值,以及图表结果。

对于一个更高级的测试计划,请看:构建一个高级Web测试计划( https://jmeter.apache.org/usermanual/build-adv-web-test-plan.html ).。

In this section, you will learn how to create a basic Test Plan to test a Web site. You will create five users that send requests to two pages on the JMeter Web site. Also, you will tell the users to run their tests twice. So, the total number of requests is (5 users) x (2 requests) x (repeat 2 times) = 20 HTTP requests. To construct the Test Plan, you will use the following elements: Thread Group, HTTP Request, HTTP Request Defaults, and Graph Results.

For a more advanced Test Plan, see: Building an Advanced Web Test Plan( https://jmeter.apache.org/usermanual/build-adv-web-test-plan.html).

(一)添加用户

  • 1)添加线程组元素
    每个JMeter测试计划要做的第一步是添加线程组元素。线程组告诉JMeter你想模拟的用户数量,用户应该多久发送一次请求,以及他们应该发送多少请求。继续添加线程组元素,首先选择测试计划,点击鼠标右键,获得添加菜单,然后选择添加线程组。

    现在你应该看到测试计划下的线程组元素。如果你没有看到这个元素,那么通过点击测试计划元素 "扩展"测试计划树。

  • 2)修改默认属性
    点击“线程组”选项。现在你应该在JMeter窗口的右边部分看到线程组控制面板(见下图)。

    (1)为我们的线程组提供一个更具描述性的名称。在名称栏中,输入JMeter用户。
    (2)将用户数量(即线程属性中的线程数)增加到5。
    (3)Ramp-Up Period,保留默认值1秒。这个属性告诉JMeter在启动每个用户之间要延迟多长时间。例如,如果你输入一个5秒的Ramp-Up Period,JMeter将在5秒结束时完成所有用户的启动。因此,如果我们有5个用户和5秒的Ramp-Up Period,那么启动用户之间的延迟将是1秒。如果你把这个值设置为0,那么JMeter将立即启动所有的用户。
    (4)Loop Count(循环次数)中输入一个2的值。这个属性告诉JMeter要重复测试多少次。如果你输入一个1的循环计数值,那么JMeter将只运行你的测试一次。要让JMeter重复运行你的测试计划,请选择“永远”复选框。
    参见下图,完成JMeter用户线程组。


The first step you want to do with every JMeter Test Plan is to add a Thread Group element. The Thread Group tells JMeter the number of users you want to simulate, how often the users should send requests, and how many requests they should send.

Go ahead and add the ThreadGroup element by first selecting the Test Plan, clicking your right mouse button to get the Add menu, and then select Add → ThreadGroup.

You should now see the Thread Group element under Test Plan. If you do not see the element, then "expand" the Test Plan tree by clicking on the Test Plan element.

Next, you need to modify the default properties. Select the Thread Group element in the tree, if you have not already selected it. You should now see the Thread Group Control Panel in the right section of the JMeter window (see Figure 4.1 below)

Start by providing a more descriptive name for our Thread Group. In the name field, enter JMeter Users.

Next, increase the number of users (called threads) to 5.

In the next field, the Ramp-Up Period, leave the default value of 1 seconds. This property tells JMeter how long to delay between starting each user. For example, if you enter a Ramp-Up Period of 5 seconds, JMeter will finish starting all of your users by the end of the 5 seconds. So, if we have 5 users and a 5 second Ramp-Up Period, then the delay between starting users would be 1 second (5 users / 5 seconds = 1 user per second). If you set the value to 0, then JMeter will immediately start all of your users.

Finally enter a value of 2 in the Loop Count field. This property tells JMeter how many times to repeat your test. If you enter a loop count value of 1, then JMeter will run your test only once. To have JMeter repeatedly run your Test Plan, select the Forever checkbox.

In most applications, you have to manually accept changes you make in a Control Panel. However, in JMeter, the Control Panel automatically accepts your changes as you make them. If you change the name of an element, the tree will be updated with the new text after you leave the Control Panel (for example, when selecting another tree element).

See Figure 4.2 for the completed JMeter Users Thread Group.

(二)添加默认HTTP请求属性

现在我们已经定义了我们的用户,是时候定义他们将要执行的任务了。在本节中,你将为你的HTTP请求指定默认设置。然后,在下面“(三)添加Cookie支持”中,你将添加HTTP请求元素,这些元素使用你在这里指定的一些默认设置。

  • 1)打开HTTP请求默认值面板
    JMeter用户(线程组)→添加→配置元件→HTTP请求默认值。
    然后选择这个新元素,查看其控制面板(见下图)。

  • 2)设置HTTP请求默认值名称
    像大多数JMeter元素一样,HTTP Request Defaults控制面板有一个可以修改的名称字段。在这个例子中,让这个字段保持默认值。

  • 3)设置Web服务器
    设置Web服务器的服务器名称/IP(这是填写域名,比如在 https://baijiahao.baidu.com/s?id=1727327368863172625 这个URL中,服务器名称/IP是:baijiahao.baidu.com)。对于你正在建立的测试计划,所有的HTTP请求将被发送到同一个Web服务器,jmeter.apache.org。在这个字段中输入这个域名。这是唯一一个我们要指定默认值的字段,所以剩下的字段保留其默认值。

  • 4)设置协议
    按实际的协议填写即可(比如HTTPS/HTTP/...),也可缺省。

    HTTP Request Defaults并没有告诉JMeter发送HTTP请求。它只是定义了HTTP请求所使用的默认值。

    参见下图,设置完整的HTTP请求默认值。


Now that we have defined our users, it is time to define the tasks that they will be performing. In this section, you will specify the default settings for your HTTP requests. And then, in section 4.3, you will add HTTP Request elements which use some of the default settings you specified here.

Begin by selecting the JMeter Users (Thread Group) element. Click your right mouse button to get the Add menu, and then select Add → Config Element → HTTP Request Defaults. Then select this new element to view its Control Panel (see Figure 4.3).

Like most JMeter elements, the HTTP Request Defaults Control Panel has a name field that you can modify. In this example, leave this field with the default value.

Skip to the next field, which is the Web Server's Server Name/IP. For the Test Plan that you are building, all HTTP requests will be sent to the same Web server, jmeter.apache.org. Enter this domain name into the field. This is the only field that we will specify a default, so leave the remaining fields with their default values.

The HTTP Request Defaults element does not tell JMeter to send an HTTP request. It simply defines the default values that the HTTP Request elements use.

See Figure 4.4 for the completed HTTP Request Defaults element

(三)添加Cookie支持

几乎所有的Web测试都应该使用cookie支持,除非你的应用程序比较特殊而不使用cookie。要添加cookie支持,只需在测试计划中为每个线程组添加一个HTTP Cookie管理器。这将确保每个线程得到自己的cookie,并在所有HTTP请求对象中共享。

JMeter用户(线程组)→添加→配置元件→HTTP Cookie管理器。


Nearly all web testing should use cookie support, unless your application specifically doesn't use cookies. To add cookie support, simply add an HTTP Cookie Manager to each Thread Group in your test plan. This will ensure that each thread gets its own cookies, but shared across all HTTP Request objects.

To add the HTTP Cookie Manager, simply select the Thread Group, and choose Add → Config Element → HTTP Cookie Manager, either from the Edit Menu, or from the right-click pop-up menu.

(四)添加HTTP请求

在这个的测试计划中,我们需要做两个HTTP请求。第一个是JMeter主页(http://jmeter.apache.org/) ,
第二个是更改页面(http://jmeter.apache.org/changes.html)。

JMeter按照请求在树上出现的顺序发送请求。所谓的“树”,就是不同层级组成的结构。顺序即下图箭头所指的顺序。

  • 添加第一个HTTP请求:JMeter用户(线程组)→添加→采样器→HTTP请求
    选择树中的HTTP请求元素,编辑以下属性(见下图)。
    1)将名称字段改为 "Home Page"。
    2)将路径字段设为"/"。记住,你不必设置服务器名称字段,因为你已经在HTTP请求默认值元素中指定了这个值。

  • 添加第二个HTTP请求:JMeter用户(线程组)→添加→采样器→HTTP请求
    选择树中的HTTP请求元素,编辑以下属性(见下图)。
    1)将Name字段改为 "Change"。
    2)将路径字段设为"/changes.html"。


In our Test Plan, we need to make two HTTP requests. The first one is for the JMeter home page (http://jmeter.apache.org/), and the second one is for the Changes page (http://jmeter.apache.org/changes.html).

JMeter sends requests in the order that they appear in the tree.
Start by adding the first HTTP Request to the JMeter Users element (Add → Sampler → HTTP Request). Then, select the HTTP Request element in the tree and edit the following properties (see Figure 4.6):

  • 1)Change the Name field to "Home Page".
  • 2)Set the Path field to "/". Remember that you do not have to set the Server Name field because you already specified this value in the HTTP Request Defaults element.

Next, add the second HTTP Request and edit the following properties (see Figure 4.7:

  • 1)Change the Name field to "Changes".
  • 2)Set the Path field to "/changes.html".

(五)添加监听器用于查看/存储测试结果

你需要添加到测试计划中的最后一个元素是一个监听器。这个元素负责将所有HTTP请求的结果存储在一个文件中,并呈现出数据的可视化模型。

  • JMeter用户(线程组)→添加→监听器→常看结果树(View Results Tree)

切换不同的监听器可以得到不同的可视化结果。常用的是“查看结果树”


The final element you need to add to your Test Plan is a Listener. This element is responsible for storing all of the results of your HTTP requests in a file and presenting a visual model of the data.

Select the JMeter Users element and add a Graph Results listener (Add → Listener → View Results Tree ).

(六)登录网站

在本次Web测试中你不需要这样做(JMteter开源,本次Home Page和Change的测试不涉及登录注册等行为),但有些网站要求你在允许执行某些操作之前先登录。在网络浏览器中,登录将显示为一个用户名和密码的表单,以及一个提交表单的按钮。该按钮会生成一个POST请求,将表单的值作为参数传递。

  • 假如需要测试登录注册等行为,在JMeter中你应该按如下步骤操作:
    添加一个HTTP请求,并将方法设置为POST。你需要知道表单所使用的字段的名称,以及目标页面。这些可以通过检查登录页面的代码来发现。(如果这很难做到,你可以使用JMeter Proxy Recorder来记录登录顺序。) 设置提交按钮的目标路径。点击两次添加按钮,输入用户名和密码的详细信息。有时,登录表格包含额外的隐藏字段。这些也需要被添加。


It's not the case here, but some web-sites require you to login before permitting you to perform certain actions. In a web-browser, the login will be shown as a form for the user name and password, and a button to submit the form. The button generates a POST request, passing the values of the form items as parameters.

To do this in JMeter, add an HTTP Request, and set the method to POST. You'll need to know the names of the fields used by the form, and the target page. These can be found out by inspecting the code of the login page. (If this is difficult to do, you can use the JMeter Proxy Recorder to record the login sequence.) Set the path to the target of the submit button. Click the Add button twice and enter the username and password details. Sometimes the login form contains additional hidden fields. These will need to be added as well.

(七)执行测试计划与查看测试结果

  • 1)执行测试计划
    有点击工具栏按钮和右击线程组(JMeter)点击“Start”两种方式。

  • 2)查看测试结果
    点击之前选择的监听器(Listener),此处是“查看结果树(View Results Tree)”
    现在,你可以看到“请求”和“响应数据”了。

(八)其他设置

由于JMeter版本的迭代及用户手册的更新,请及时更新软件和参考最新的文档:https://jmeter.apache.org/usermanual/build-web-test-plan.html。