jmeter learning tool to be continued

about

Apache JMeter is an Apache open source project organization, is

A pure Java desktop application for stress testing and performance testing, it was originally designed for

web application testing, then gradually extended to other areas

jmeter can be used (file, Servlet, Perl scripts for static and dynamic resources,

Java objects, database and query, FTP servers or other resources) of performance

test. jmeter used to analyze the overall performance under variable pressure conditions. You can also use graphical interface provides jmeter, analyze performance metrics.

jmeter environment configuration

Preconditions: it must be equipped with Java if you do not turn this feature blog

Open the following link to download

Official website: https://jmeter.apache.org/download_jmeter.cgi

Version history: https://archive.apache.org/dist/jmeter/binaries/

Jmeter will extract the files to a folder (already with a good premise java environment) Note: The installation directory must not have spaces and special characters and Chinese

Now you click on jmeter jmeter.bat installation file in the directory bin directory, you can have a successful start jmeter.

1576198619706

jmeter started successfully.

1576198680432

In order for us to more easily start, all you want to add environment variables (it can be started directly in cmd).

Add the installation directory to jmeter environment variable, my jmeter installation directory is G: \ software \ apache-jmeter-5.2.1.

Select New, added to it to the following figure to variable name and the value of the control panel shown in all the advanced system settings ▶ ▶ system environment variables ▶ ▶ ▶ system variables in the control panel.

1576198801146

(Optional, but strongly recommended) add CLASSPATH

Or all Control Panel Control Panel ▶ ▶ ▶ Advanced System Settings ▶ system environment variables, the new system variables in the list (no classpath) or additional (presence classpath) variable values:

%JMETER_HOME%\lib\ext\ApacheJMeter_core.jar;%JMETER_HOME%\lib\jorphan.jar;   
 
    
ps:jmeter-4.0还要添加一个%JMETER_HOME%\lib\logkit-1.2.jar路径,但5.2.1版本我没找到,暂时不管它

1576202215472

This step is to add jmeter bin directory to environment variable, this step is no need to dwell on it.

Modify the relevant parameters

This step is to add jmeter bin directory to environment variable, this step is no need to dwell on it.

Change the language and encoding jmeter

1576200218600

ps Precautions

This option is for Windows and Mac alike. But this is only temporary, and then reopen, will become the default language. Want to permanently change, need to modify the G: \ software \ apache-jmeter-5.2.1 \ bin (the bin directory of the installation directory) of jmeter.properties file, which will be revised as default language = en language = zh_CN, click Save that is can.

1576200279059

Modify codes

Jmeter.properties same file, where the field is modified sampleresult.default.encoding utf-8.

sampleresult.default.encoding=utf-8

Directory structure

When you After installation, you will see such a directory structure (the Windows platform as an example):

1576200393897

Take a look at some important documents and description of each directory.

1576200446050

The main element jmeter

Test plan: all containers in full bloom can be understood as test suite.

Thread group: Right-test program can create a new thread group, the specific interface use cases can be created and executed within a thread group.

Listener: When performing the interface requests, how to respond to the situation to see it, you can choose to view the results in a tree listener.

Assertion: Needless to say, the interface used to make assertions.

Quick Start

We have to java project Liezi

get request parameters

First add a thread group

1576200793540

Modify the thread group name as shown in FIG.

1576200839647

An HTTP request is added in the sampler generic request.

1576200868654

Configuration request parameters.

1576200907926

It is noted that, due to the need to carry the current request parameters, where the parameters can be added in two ways, as shown above, may be key: the value of the form or with url.

In addition, Why write about ip and port and separate url, it is because a ip and port projects in different stages are different, so write for convenience,

Well, you can configure performed,

But the results of where to see it? There is also need to add a listener, the result is a tree view, add the following embodiment shown in FIG.

1576200998160

Click on the menu bar Run button (No. 3 in the figure below), look at the result tree can see the results of the execution.

1576202289332

FIG each Number Description

\ 1. If you are prompted to save the test plan, click Save, or click the image above 1 save themselves.

\ 2. Toggle button, of course, I like to call it as a comment button. If the test program, there are more thread groups, you do not want to perform a thread group, then click on the current thread group commented 2, which is the current thread group will not be executed, the current thread again selected group again click 2 It is uncommented.

\ 3 is the Execute button, you look at the situation which execute button to select.

\ 4. If you repeat the test, then look at the resulting tree will be saved with the implementation of the results of each example, you want to clear on the choice of 4 on the right.

\ 5. Implementation of use cases is the current test plan. This contains a request to a thread group, so one. Clear also two buttons, you can choose to see the situation.

key: post value format request

Interface project common practice interface:/pinter/com/login

Parameter is k: v format.

Click generic request thread group, add an HTTP sampler, the relevant settings as FIG. To key parameters in the parameter options: Fill value format.

1576202547122

Now you can click on the run, look at the result tree

1576202577600

We can see, just get request also performed. Or you want to get a request to perform, you can click a button to switch it commented.

The parameters k = json POST Interface

Let's look at another post request, it's just like post request, all k: v parameter format, just slightly different value, it is json type.

url or common practice interface:/pinter/com/buy

Interface parameters as follows FIG.

1576202697955

Look at the results

1576202736801

no problem.

Json POST parameters of the interface

General practice or interface type of post url /pinter/com/register

FIG interface parameters are set as follows

ps interfaces of other parameters set how the how settings,

Note that the types of parameters need to be placed json message body data, must be a standard json format.

1576202808924

Other parameters of how to set how the interface is provided, to be noted that the type of json parameter data need to be placed in the message body, json must be a standard format.

imgNow click Run, go you!

img

Being given, 415, error is not supported by media type. What causes it, this request follows the HTTP protocol, it defaults to identify k: v parameter format,

But we carry a json type, it can not identify, how to solve it?

那就是再发请求的时候header头中携带一个标识,标识是Content-Type: application/json;charset=UTF-8 ,告诉它解析json类型的参数。

选中当前的接口,右键如下图添加一个HTTP信息头管理器。添加标识。需要注意的是,这个http信息头管理器 是在当前接口中的,也就是只作用于当前接口,请放心使用。再来执行该接口就解决了这个问题。

img

ps重要

纯josn数据要注意添加http请求头

jmeter超时处理

在接口请求中一般会遇到超时问题,超时问题一般可以分为请求超时,客户端发一个请求,怎么也无法建立连接。响 应超时是客户端怎么也接收不到服务端返回的数据。

1576203265749

那在jmeter中如何设置呢?又要设置哪些参数呢?

例如下图所示的http请求中,在高级选项中,可以设置两个两个参数:

  • 实现选项,有httpclient 和java实现的http请求。 httpclient 是默认的请求方式,我们该选项保持默认即可。
  • 超时选项有两个参数,请求超时和响应超时

超时单位是毫秒,1秒等于0.001毫秒

如下图设置

1576203342026

一般的,在接口请求中,出现TimeOut 情况,如果请求本身写的没有问题的话,通常是服务端问题,去服务端查问题即可。

断言

响应断言

选择线程组 ▶ 添加 ▶ 断言 ▶ 响应断言 。

断言作用的范围与其作用域有关

1576203446795

1576203596693

1576203607799

响应断言相关参数填充完毕,那么该响应断言作用于当前线程组内的每一个请求,上面的预期值是"code":"0" ,那执行结果都会通过,如下图,察看结果树中一片绿代表都通过了。

1576203652536

现在,如果我们将预期值修改为"message":"success" ,再重新执行,会发现有个请求没有通过,原因是接口返回值的字段与预期不符,它是"message":"注册成功" 而不是"message":"success"

1576203698263

json断言

!在工作中,难免遇到判断某个较深路径路径中的某个值,比如下面这样的,判断json字符串中data 下面的skuName

这个key是否存在,或者判断key对应的value是否符合预期值。

1576203871259

这就用到json path

imgjson-path:https://github.com/json-path/JsonPath

怎么用呢?来几个练习:

1$.message
2#  $代表整个json字符串
3$.data.skuName # 获取json字符串中data下的skyName

一路点下去就行了,这里不存在同级路径下key重复的问题。来看jmeter中怎么用,以线程组中get请求用例为例。

断言某个路径下的key是否存在

新建一个json断言

1576206696607

我们来断言$.data.message 这个key是否存在

1576206731492

很明显,会执行失败, $.data 下没有message 这个key 下面是结果图。

1576206753369

换一个断言$.data.skuName 是否存在。答案是肯定的

1576206789725

断言某个路径下的key值是否符合预期

那么现在需求是判断$.data.skuName 对应的值是ptest-1 么,怎么断言呢?按照如下图的操作即可。

1576206858691

添加判断value,并且填写value值即可(上图如示)。执行也是成功的。

自定义变量

看图所示 发现一个问题

1576206954676

现在我们的项目在本地运行,测试用例中的ip和端口这么写没问题,然后我们就写了好几百个 用例 然后,项目上线了,IP和端口变了,我们的用例是不是都要一个个的修改成新的?是的!但是,这就太麻烦了,所以,我们要学习一种新的方式,就像写配置文件一样,将ip和端口之类的容易变的值定义成变量,然后在脚本 中引用变量,将来有变化的话,我们直接修改变量对应的值就完了

在测试计划 的用户定义的变量 中,我们在这里定义一些变量,然后其他的地方用到就可以直接引用了 如下图

1576207147355

引用方式使用${变量} 就可以 下图使用示例

1576207234618

调试取样器

查看jmeter在执行请求中都使用了哪些变量?(比如我们要查看ip和端口这两个变量。那就是使用调试取样器

创建调试取样器

1576207334275

调试取样器无需添加什么配置,它主要配合察看结果树来使用

1576207350318

点击运行当前线程组,然后察看结果树中会发现调试取样器选项。

如上图,我们在这里找到ip和端口两个变量,当然还有其他的变量 请保证变量名的唯一性

1576207390385

作用域

作用域这个概念,说简单也简单。比如,察看结果树,如果它位于线程组下面,跟其他的请求用例同级,那它作用于 所有的请求;如果它位于某个请求下面,那它只作用于该请求:如果它跟线程组同级,那么它作用于所有的线程组,其他元件也是一样的。

导入导出jmx脚本

当你点击保存按钮的时候,在本地就生成了一个jmx文件 如下图所示

1576207528333

那怎么引入呢,也非常的简单,直接拖到jmeter中就行了

参数化

jmeter内置了很多函数来供我们使用,比如:

  • 随机数函数
  • 随机字符串函数
  • 时间戳函数

怎么用呢?在你的菜单栏,有个函数助手对话框,里面包含了jmeter提供的所有的函数,内置的和自定义的都有。

随机数函数

1576207666564

  1. 在菜单栏,点击函数助手对话框 。
  2. 在弹出的窗口中,下拉选择随机数函数。
  3. 对于随机数范围的限制,和将随机数函数赋值给一个变量(可选参数,可以不写),方便别的地方引用。
  4. 点击生成,左侧的输入栏就是jmeter生成的随机数函数,把它拷贝走即可。
  5. 当你点击生成的时候,自动的执行了该函数,随机生成了一个随机数。

使用截图

1576207729646

上图,展示了函数的两种用法,第一是直接引用即可,第二种是通过变量的形式引用。

变量的引用方式适用于,此次请求需要一个随机数,但在此次请求中这个随机数要多处使用,并且值是一样的。 来看是否应用成功:

1576207883856

随机字符串函数

1576207910216

选择 RandomString 随机字符串函数。

Several parameters length of the random string, generating strings (The downside is that does not support what a-zA-Z0-9 this), that is our alternate character from the alternate characters abcd which then generated random string is generated from the abcd; last parameter is also optional since a variable, ease of reference.

Examples of Use

Now we use / pinter / com / register this interface, this interface will need to bring parameters are:

{"userName":"test","password":"1234","gender":1,"phoneNum":"110","email":"[email protected] m","address":"Beijing"}

UserName that can replace the random function, can be:

{"userName":"${ RandomString(4,abcd,r_str)}","password":"1234","gender":1,"phoneNum": "110","email":"[email protected]","address":"Beijing"}

img

Note that you may be questioned here, "test" is a string, when using the function to replace, or remove the quotation marks around, the answer is not removed.

1576208185067

To see if the successful application

1576208207830

CSV file read

Guess you like

Origin www.cnblogs.com/saoqiang/p/12034345.html