Jmeter presentation and script production and testing

Jmeter Introduction

Jmeter is a pure Java open source projects from the Apache Foundation, Jakarta, initially for Web-based stress test (pressure test), then gradually expand its scope of application of file transfer to FTP, large databases (JDBC mode), script (CGI aspects of Perl, etc.), Web Services, Java applications and other tests. Jmeter itself primarily used for performance testing, such as the system pressure. In addition, Jmeter able to do functional testing and regression testing of applications, and the system can be verified through the use of scripts with assertions and then returned to the user desired results. In order to increase application flexibility tools, JMeter allows the use of regular expressions to create assertion. Because of its flexibility and scalability, JMeter is becoming popular open source testing tools.

Jmeter Features:

  • Support for multiple service types for testing;
  • Support for test scripts by recording \ playback mode;
  • With high portability, a Java program 100%;
  • Multithreaded frame, allows multiple threads concurrently by separate threads, and the sampling group while sampling different functions;
  • Well-designed GUI user support high-speed operation and precise timing;
  • Analysis \ playback test results support caching and offline mode;
  • L highly scalable to support HTTP, JAVA request, JMS, EJB, WebService, JDBC, FTP, LDAP, SMTP, Junit, Mail, MongoDB etc.

Jmeter contrast with the Loadrunner:
1, interface, installation, protocol support, libraries, cost, open source
2, can achieve distributed load, relatively speaking, more powerful Loadrunner
3, support the load generator windows and linux environment in . Console aspects, Jmeter cross-platform, rather than LoadRunner
4, LoadRunner can specify each load generator to run a different number of concurrent users, while not currently Jmeter
5, data files outside of Jmeter test plan will not be sent together to generate a load , while LoadRunner files may be sent together to the remote management by selecting inclusion LoadRunner

Jmeter installation

Jmeter is pure JAVA development, the system can run Java programs can run Jmeter general, such as: Windows, Linux, mac and so on. Under Windows installation steps as follows:
Step 1: Install JDK, must JDK1.7 above, version 1.8 is recommended
Step Two: Go jmeter download face: http://jmeter.apache.org/download_jmeter.cgi download the appropriate installation package, such as : apache-jmeter-3.0.zip
third step: extracting installation to the next English name directory, such as C: 
step Four: configure the environment variables: New JMETER_HOME: C: \ apache-jmeter -3.0 modify the CLASSPATH: plus: % JMETER_HOME% \ lib \ ext \ ApacheJMeter_core.jar;% JMETER_HOME% \ lib \ jorphan jar;% JMETER_HOME% \ lib \ logkit-2.0.jar;.
step 5: enter C: \ apache-jmeter-3.0 \ bin, after entering the bin directory, double-click operation jmeter.bat, when there will be two windows open, Jmeter command window and Jmeter graphical user interface, can not close the command window.

The main components of the test Jmeter

The main components of JMeter tests are summarized below:

  1. Using JMeter test plan is the starting point for the test, which is a container for other JMeter test element.
  2. Thread group represents a certain number of concurrent users, it can be used to simulate the transmission request concurrent users. The matters defined in the actual request Sampler, which is a thread group comprises.
  3. Listeners responsible for collecting test results, but also was told that the way results are displayed.
  4. The controller logic can customize the behavior of the transmission request logic JMeter can simulate complex request sequence used in conjunction with it Sampler.
  5. Assertions can be used to determine whether the result request response as desired by the user. It can be used to isolate problem domain, namely the implementation of stress tests to ensure the proper function of the premise. The limit for effective testing is very useful.
  6. Sampler configuration elements needed to maintain the configuration information, and change the content of the request according to actual needs.
  7. Pre-processor and post-processor is responsible for generating requests before and after the work is completed. Pre-processor is often used to modify setting request, the post-processor often used to process the response data.
  8. Delay interval between the request timer responsible for defining.
    9. Sampler (Sampler) is to performance test server sends a request, in response to recording information, the minimum recording unit response time, the JMeter native support many different Sampler, such as HTTP Request Sampler, FTP Request Sample, TCP Request Sample, JDBC request Sampler and the like, each different type of sampler may emit different types of requests to the server according to the parameters set.
    10. The test sequences (Test Fragment) test element is a fragment of a special kind of thread group on the controller, it is a thread group level in the test tree. It is different with the thread group, because it is not executed, will be executed unless it is a controller or module is being referenced when the controller.
    11. table: When recording http protocol script used, it can create a proxy server under the original, after setting agent during recording; setting server monitoring, server monitoring test performance, it is not recommended, will be jmeter impact generated when the test; jmeter can display information; backup scripts, script debugging when you can use it as a staging area of the original.

Element scope and the execution order Jmeter

Scope element:
configuration elements (config elements): element affects all elements within its range of action.
Pre-processing procedure (Per-processors): a sampler element performed before each element within its range of action.
Timers (timers): a sampler of each of its active elements within the scope of the
post processing (Post-processors): After performing a sampler element in each element within its range of action.
Assert (Assertions): element performs its check result of each element within the scope of the sampler performed.
Listener (Listeners): scope element to collect information about each of a sampler element and rendering. Summary: Analyzing the scope of each element from each element in the hierarchy

Execution sequence elements:
configuration elements -> pre-processing program -> Timer -> Sampler -> post processing program (return result obtained unless Sampler empty) -> assert (obtained unless Sampler return result is null) -> listener (unless Sampler get returns a value of null)

On the order of execution, there are two things to note:
preprocessor, and postprocessor assertions element capable of acting well on the sampler, and therefore, if there is no sample in the scope thereof, it will not be executed.
If there are multiple elements of the same type in the same scope range, once these elements are performed in the vertical order in the test plan.

Jmeter operating principle

Jmeter virtual machine running on the JVM, the cost of each process is relatively large, and java support multithreading, so Jmeter is a thread way to run tests.
Jmeter by thread group to drive multiple threads to run test scripts for load testing server is initiated, can be run on each machine load more than one thread group, Jmeter running scene not only can be done in GUI mode, you can also use the command line, and the command way line to run would be smaller for resource consumption of the load machine.
Machine control: when using more than one machine Jmeter load performance testing, was selected as the machine is called a managed machine control machine, the machine can run the script, but also for administrative tasks remotely load machine running, and collect Test Results.
Load machine: server initiates a test machine load control chance to send a script to the load test machine, when you run the test script parameter files and jar package dependencies, the control unit can not send, need to be manually copied to the local machine load.

Operation process is as follows:
the remote load machine starts and wait for the control unit is connected;
a control unit connected to the remote load unit;
control sends commands (scripts and start command) to start the thread;
dynamometer running script, return test data;
controlled machine collects the results and display

Jmeter script production

Jmeter script production is generally divided into two ways:
using the tool to record a script, scripting manual

Http proxy mode recording: The principle of this function is to parse network packets, packaged into Http Request, Http Response and other objects by way of agreement Http
third-party tools to record Badboy: Badboy is a simple browser simulation tool, has recorded and playback functions, but also for debugging.

Scripts written by hand, usually by fiddler capture or development design documents provided the form of interface for script generation operation Jmeter GUI interface can also be used to write java code jar packaged into packets to generate the like.

Badboy Description:
Badboy is a dynamic application testing tool for C ++ development, which has a powerful screen recording and playback functions, while providing graphical results analysis.
Badboy scripts recorded can be exported into .jmx suffix format, it is saved jmx Jmeter script format, and its essence is an XML format file.
Badboy recorded in two ways: one is the Request, one is Navigation mode, by switching the toolbar 'N' button; Request, and analog forms browser sends information to the server, each request resources as ; Navigation mode is to record the user's operation of the mouse movements, clicks during playback analog interface, similar to the UI automated testing tools selenium.
Jmeter due to the need for the script, the way we use the Request recording format jmx generated script.

Installation Badboy:
Badboy installed like any other Windows application installation, double-click the exe file to complete the installation.

Badboy interface presentation (to LR example site, for example):

Badboy使用:
1、在地址栏输入被测网站地址,回车、F5或者点击 ;
2、左边显示script(脚本)的目录结构;
3、Test Suite1:脚本的根节点,类似Jmeter的测试计划节点;
4、Test1:测试场景根节点,类似一个业务功能的脚本存放在此目录下;
5、Step1:测试活动的步骤,如果一个业务操作过长,比如:订票业务它分为登 录、进入订票界面、订票等步骤,可以把它分为几个Step,也可以录制成一个 Step,导入Jmeter后再根据业务进行拆分;
6、录制完测试脚本后,通过点击File→Export to JMeter...→另存为".jmx"→保存;
7、启动jmeter,点击文件→打开→选中“
.jmx”→打开, 添加"查看结束树"和"聚合报告",右击Thread Group→添加→监听器→查看结果树, ,右击Thread Group→添加→监听器→聚合报告;
8、启动脚本运行完毕后"查看结果树",验证脚本的正确性

Jmeter的Http代理方式录制:
1、浏览器配置代理:
打开IE浏览器,进入工具--Internet选项-连接--局域网设置,如下图设置:

备注:如果有本地使用了8080端口,为了避免端口冲突,可以换成其它的

2、Jmeter配置控制器:
打开Jmeter,新增一个线程组,然后在其下面新增一个控制器(录制控制器、简单控 制器)。

3、Jmeter中配置代理:
在Jmeter中,右击控制台--添加--非测试原件--HTTP代理服务器,然后排除模式添加一 个设置: .*.(bmp|css|js|gif|ico|jp?g|png|swf|woff),然后勾选Regex matching(正则 匹配)

4、启动Jmeter代理服务器并开始录制:
拖动Jmeter代理服务器界面的滚动条到最下面,点击启动按钮。然后直接在IE浏览器下 操作即可,此时可以看见操作过程中会在录制控制器下生成请求。由于在排除模式添 加了正则表达式。可以排除一些图片等的请求。

手工脚本制作:
在Jmeter手工制作脚本之前,先要获取HTTP请求的相关信息,如请求地址、 请求参数、请求类型等,一般情况下,有两种方法获取这些相关信息:
1、根据开发提供的接口设计规范文档;
2、使用第三方抓包工具如Fiddler、Charles等抓包,然后从中提取信息。 HTTP请求添加方法:线程组 -->右键添加 -->Sampler -->HTTP请求。如下图界面

HTTP请求界面说明:
名称:本属性用于标识一个取样器,建议使用一个有意义的名称。
注释:对于测试没有任何作用,仅用户记录用户可读的注释信息。
服务器名称或IP :HTTP请求发送的目标服务器名称或IP地址。
端口号:目标服务器的端口号,默认值为80 。
Timeouts:指定超时时间,单位是毫秒,Connect指定连接超时时间,Response指定 响应超时时间。
Implementation:实现方式,有三个选择项,默认值为Jmeter属性:jemter.
httpsampler,其中HttpClient可以高效的访问HTTP协议的资源,可以看成是一个没 有界面的浏览器。Java选项,使用JDK提供的net包中的工具类来访问。一般建议使用
HttpClient4。 协议:向目标服务器发送HTTP请求时的协议,可以是HTTP或者是HTTPS ,默认值 为http 。
方法:发送HTTP请求的方法,可用方法包括GET、POST、HEAD、PUT、OPTIONS、 TRACE、DELETE等。
Content encoding :内容的编码方式,默认值为iso8859,如果不确定可以向开发团 队确定,大多数会指定为utf-8格式。

路径:目标URL路径(不包括服务器地址和端口)
自动重定向:如果选中该选项,当发送HTTP请求后得到的响应是302/301时,JMeter 自动重定向到新的页面。
跟随重定向:Http Request 取样器的默认选项,当响应Code是3**时,自动跳转到目标地址。与自动重定向不同的是,Jmeter会记录重定向过程中的所有的请求响应,在 查看结果树时可以看到服务器返回的内容。
Use keep Alive : 当该选项被选中时,jmeter 和目标服务器之间使用 Keep-Alive方式 进行HTTP通信,默认选中。
Use multipart/from-data for HTTP POST :当发送HTTP POST 请求时,使用Use
multipart/from-data方法发送,默认不选中。比如可以用它来做文件上传。
Browser-compatible headers:浏览器兼容模式,如果使用Use multipart/from-data for HTTP POST,建议勾选此项。
Parameters 同请求一起发送参数 :可以把要发送的参数与值填如此域。
Body Data:同请求一起发送的数据信息,这里可以支持多种数据格式,json、xml等
Files Upload:同请求一起发送的文件。MIME类型有STRICT、RFC6532、
BROWSER_COMPATTIBLE等,如不知道MIME类型,可以使用抓包工具获取。
Proxy Server:当测试时网络使用了网络代理就需要在此配置代理服务器。

请求头设置:
http请求头设置在Jmeter中需要使用到HTTP信息头管理器。该属性管理器用 于定制Sampler发出的HTTP请求的请求头的内容。不同的浏览器发出的HTTP请求 具有不同的Agent,访问某些有防盗链的页面时需要正确的Refer...这些情况下都需 要通过HTTP信息头管理器来保证发送的HTTP请求是正确的。

https请求配置: 要发送https的请求,需要在http请求或者http请求默认值做如下配置:

发送带参数的post请求:

发送带Json串的post请求:
第一步:需要在HTTP信息头管理器添加一条名称为 Content-Type,值为 application/json
第二步:如下图操作,把json数据放入到Body Data中

Jmeter脚本调试

调试与调优:
调试:当开发的代码或者编写的脚本出现功能不能实现的时候,去修改代码,让
功能实现的过程称为调试。
调优:当开发的代码功能没有问题,效率或者可读性不佳的时候,去改进代码效
率或可读性的过程称为调优。

Jmeter脚本调试即调整修改脚本让其完成录制时的业务功能。

Jmeter脚本调试的过程如下:
录制脚本-->回放-->处理关联(如果需要) -->再次回放-->验证脚本的正确性

Jmeter关联的概念:
在脚本回放过程中,客户端发出请求,通过Jmeter中的正 则表达式提取器所
定义的左右边界值(也就是关联规则),在服务器所响应的内容中查找,得到相
应的值,以变量的形式替换录制时的静态值,从而向服务器发出正确的请求,这
种动态获得服务器响应内容的方法称作关联。通俗点说,就是把脚本中某些写死
的(hard-coded) 数据,转变成撷取自服务器所送的、动态的、每次都不一样的
数据。

关联的应用场合:
当客户端的某个请求是随着服务器端的响应而动态变化的时候,我们就需要 用到关联。
举例一:登录过程
客户端发出获得登录页面的请求, 服务器端得到该请求后,返回登录页面, 同时动态生成一个Session Id;当用户输入用户名密码,请求登录时,该Session Id同时被发送到服务器端;如果该Session Id在当前会话中有效,那么返回登录 成功的页面,如果不正确则登录失败。
在第一次录制过程中Jmeter把这个值记录了下来,写到了脚本中,但再次回 放时,客户端发出同样的请求,而服务器端再一次动态的生成了Session Id,此 时客户端发出的请求就是错误的,为了获得这个动态的Session Id我们就要用到关联技术。
针对Jmeter实际操作来讲,就是使用正则表达式提取器从上一个请求的返回值中取出需要关联的数据做成Jmeter参数,把这个Jmeter参数提供给下面接口 使用的过程。
举例二:随机发帖过程
一个论坛系统,我们录制脚本指定版块发帖后,之后的脚本都会只对这个版块发帖,假设这个版块不存在,或者要做随机发帖的时候,那明显一个固定的版块不满足实际的业务需求,此时也可以使用关联来解决此问题。

Jmeter关联实现:
在需要获得数据的请求上右击添加一个后置处理器-->正则表达式提取器,界面
如下:

名称:随意设置,最好具有业务上的意义,方便区分
注释:随意设置,一般不填写
Apply to:应用范围,包含4个选项。
Main sample and sub-samples:匹配范围包括当前父取样器并覆盖子取样器。 Main sample only:匹配范围是当前父取样器(一般默认选择这个)。

Sub-samples only:仅匹配子取样器。
Jmeter Variable:支持对Jmeter变量值进行匹配。
要检查的响应字段:针对响应数据的不同部分进行匹配,共七个选项。
主体:响应数据的主体部分,排除Header部分;Http协议返回请求的主体部分就是 Body。(一般默认选择这个)
Body(unescaped):针对替换了转义码的Body部分。
Body as a Document:返回内容作为一个文档进行匹配。
信息头:只匹配信息头部分的内容。
URL:只匹配URL链接。
响应代码:匹配响应代码,比如状态码200代表成功等。
响应信息:匹配响应信息,比如”成功” “OK”等。
引用名称:即下一个请求要引用的参数名称,如填写sessionid,则可用${sessionid} 引用它。
正则表达式:正则表达式提取器根据该处的设置进行信息匹配
模板:用$$引用起来,如果在正则表达式中有多个正则表达式,则可以是$1$,$2$ 等等,表示解析到的第几个值给sessionid,$1$表示第一个模板,$0$表示全文匹配。
匹配数字:用正则表达式匹配的时候,可能出现多个值的情况,为正数用来确定取 一组值中的第几个,为0表示随机取匹配值,负数取所有值。
默认值:如果没有匹配到可以指定一个默认值。

正则表达式使用:
1、提取单个字符串:
假如想匹配Web页面的如下部分:id = "kw" name = "wd">并提取wd。一个合适 的正则表达式:id = "kw" name = "(.+?)">
2、提取多个字符串:
假如想匹配Web页面的如下部分:id = "kw" name = "wd">并提取kw和wd。一个 合适的正则表达式:id = "(.+?)" name = "(.+?)"。这样就会创建2个组,分别用于$1$ 和$2$
比如:
引用名称:Data
模板:$1$$2$
如下变量的值将会被设定:
Data: kwwd
Data_g0: id = "kw" name = "wd"
Data_g1: kw
Data_g2: wd
在需要引用地方可以通过:${Data}, ${Data_g1}进行使用。
其中:.代表任意字符,+表示一次或多次,代表出现任意次,?表示懒惰模式, 必须跟在或者+后边用

为了查看正则表达式提取器的取值,我们可以添加一个Debug Sampler,添加后, 运行脚本,然后可以在察看结果树中查看正则表达式提取器的取值。如下图所示:

Guess you like

Origin www.cnblogs.com/TD1900/p/12050969.html