Jmeter(十二)关联

时间:2023-02-19 17:51:25

  关联在实际业务需求中是随处可见的,比如:支付需要提交订单成功的订单号;修改个人资料需要登录成功响应报文信息。。。总之关联无处不在,今天来记一记Jmeter的关联功能。

  Jmeter关联的方法比较常用的是正则表达式提取器,正则表达式提取器属于后置处理器,那么久抛出了一个比较大的知识点----正则表达式;

  其实,正则表达式就是一种文本模式,相信都在windows我的电脑中搜索过文件嘛,那么肯定使用过“*”,其实都是类似。

  记几个比较常用的:

      ^ ----->为匹配输入字符串的开始位置。

      $ ----->为匹配输入字符串的结束位置。

      . ------>匹配单字符。

      + ------>匹配一次或多次(大于等于1次)

      ?------>贪婪符,匹配到立即停止。

      \d------->匹配一个数字字符

      \n ------>匹配一个换行符

      \r ------->匹配一个回车符

      。。。。。

  Jmeter(十二)关联

 Jmeter(十二)关联

 官方文档:

Attribute Description Required
Name Descriptive name for this element that is shown in the tree. No
Apply to: This is for use with samplers that can generate sub-samples, e.g. HTTP Sampler with embedded resources, Mail Reader or samples generated by the Transaction Controller.
  • Main sample only - only applies to the main sample
  • Sub-samples only - only applies to the sub-samples
  • Main sample and sub-samples - applies to both.
  • JMeter Variable - assertion is to be applied to the contents of the named variable

Matching is applied to all qualifying samples in turn. For example if there is a main sample and 3 sub-samples, each of which contains a single match for the regex, (i.e. 4 matches in total). For match number = 3, Sub-samples only, the extractor will match the 3rd sub-sample. For match number = 3, Main sample and sub-samples, the extractor will match the 2nd sub-sample (1st match is main sample). For match number = 0 or negative, all qualifying samples will be processed. For match number > 0, matching will stop as soon as enough matches have been found.

Yes
Field to check The following fields can be checked:
  • Body - the body of the response, e.g. the content of a web-page (excluding headers)
  • Body (unescaped) - the body of the response, with all Html escape codes replaced. Note that Html escapes are processed without regard to context, so some incorrect substitutions may be made.
    Note that this option highly impacts performances, so use it only when absolutely necessary and be aware of its impacts
  • Body as a Document - the extract text from various type of documents via Apache Tika (see View Results Tree Document view section).
    Note that the Body as a Document option can impact performances, so ensure it is OK for your test
  • Request Headers - may not be present for non-HTTP samples
  • Response Headers - may not be present for non-HTTP samples
  • URL
  • Response Code - e.g. 200
  • Response Message - e.g. OK

Headers can be useful for HTTP samples; it may not be present for other sample types.

Yes
Reference Name The name of the JMeter variable in which to store the result. Also note that each group is stored as [refname]_g#, where [refname] is the string you entered as the reference name, and # is the group number, where group 0 is the entire match, group 1 is the match from the first set of parentheses, etc. Yes
Regular Expression The regular expression used to parse the response data. This must contain at least one set of parentheses "()" to capture a portion of the string, unless using the group $0$. Do not enclose the expression in / / - unless of course you want to match these characters as well. Yes
Template The template used to create a string from the matches found. This is an arbitrary string with special elements to refer to groups within the regular expression. The syntax to refer to a group is: '$1$' to refer to group 1, '$2$' to refer to group 2, etc. $0$ refers to whatever the entire expression matches. Yes
Match No. (0 for Random) Indicates which match to use. The regular expression may match multiple times.
  • Use a value of zero to indicate JMeter should choose a match at random.
  • A positive number N means to select the nth match.
  • Negative numbers are used in conjunction with the ForEach Controller - see below.
Yes
Default Value If the regular expression does not match, then the reference variable will be set to the default value. This is particularly useful for debugging tests. If no default is provided, then it is difficult to tell whether the regular expression did not match, or the RE element was not processed or maybe the wrong variable is being used.

However, if you have several test elements that set the same variable, you may wish to leave the variable unchanged if the expression does not match. In this case, remove the default value once debugging is complete.

No, but recommended
Use empty default value If the checkbox is checked and Default Value is empty, then JMeter will set the variable to empty string instead of not setting it. Thus when you will for example use ${var} (if Reference Name is var) in your Test Plan, if the extracted value is not found then ${var} will be equal to empty string instead of containing ${var} which may be useful if extracted value is optional. No

正则表达式会写,用这个很eazy。

  现如今,restful风格(http+json)的接口很是流行,响应信息为json格式的,那么就还能简单一点,不用正则表达式那么复杂。

  而json的数据类型有对象、数组、字符串、数字(整型、浮点)、布尔、null;使用jsonpath语法来进行提取判断。

  Jmeter也有专门提取json的提取器,当然是第三方插件咯。。。Json Path Extractor

  Jmeter(十二)关联

  json是key-value类型的,当然也会碰到数组,关于这些也来记一记。

  (参考:http://goessner.net/articles/JsonPath/)

Demo(一段json报文):

{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
XPath JSONPath 结果
/store/book/author $.store.book[*].author
书点所有书的作者
//author $..author
所有的作者
/store/* $.store.*
store的所有元素。所有的bookst和bicycle
/store//price $.store..price
store里面所有东西的price
//book[3] $..book[2]
第三个书
//book[last()] $..book[(@.length-1)] 最后一本书
//book[position()<3] $..book[0,1]
$..book[:2]
前面的两本书。
//book[isbn] $..book[?(@.isbn)]  过滤出所有的包含isbn的书。
//book[price<10] $..book[?(@.price<10)] 过滤出价格低于10的书。
//* $..*
所有元素。

可以看出其中的缺省符,通配符还是很常用的。经常会懵的就是碰到数组;还有就是jsonpath是从0开始数节点。

  那么有jsonpath,也就有xpath^_^

  Jmeter(十二)关联

  同样,它对于xml类型的报文信息提取比较简洁,数节点即可^_^。上方表格第一列便是xpath的相关语法。只是需要谨记的一点就是jsonpath数节点是从0开始数,而xpath数节点是从1开始数。

  

XPath JSONPath Description
/ $ 表示根元素
. @  当前元素
/ . or [] 子元素
.. n/a 父元素
// .. 递归下降,JSONPath是从E4X借鉴的。
* * 通配符,表示所有的元素
@ n/a  属性访问字符
[] []
子元素操作符
| [,]
连接操作符在XPath 结果合并其它结点集合。JSONP允许name或者数组索引。
n/a [start:end:step]
数组分割操作从ES4借鉴。
[] ?()
应用过滤表示式
n/a ()
脚本表达式,使用在脚本引擎下面。
() n/a Xpath分组

   

Jmeter(十二)关联的更多相关文章

  1. JMeter&lpar;十&rpar;-正则表达式关联

    jmeter中,接口自动化的关键在于参数关联.比如需要登录的接口,如何调用登录口令?一个增删改查的闭环,如何将接口参数上下传递?下面就以实际的例子来仔细说一说 1:登录接口 这里有一个实际的登录接口, ...

  2. Jmeter&lpar;十二&rpar;&lowbar;打印时间戳

    Jmeter中提供了一种函数,可以打印时间戳,如下图 年: yyyy 月:MM 日:dd 时: HH 分: mm 秒:ss 关于时间戳的格式,可以*组合定义,这里我写成这样 yyyy-MM-dd H ...

  3. JMeter 十二:命令行执行

    参考文档:http://jmeter.apache.org/usermanual/get-started.html#non_gui 真正开始跑压力测试时,我们就不能使用GUI模式了.这时候需要采用命令 ...

  4. Jmeter&lpar;十二&rpar;响应断言之响应文本和响应信息的差别

    在Jmeter的后置处理器中添加响应断言, 要测试的响应字段中有两个很难区分的选项, 响应文本和响应信息. 我做了两个小实验来进行区别. 1. 用Fiddler捕捉了一个POST请求, 其响应是suc ...

  5. Jmeter&lpar;十二&rpar; - 从入门到精通 - JMeter逻辑控制器 - 终篇(详解教程)

    1.简介 Jmeter官网对逻辑控制器的解释是:“Logic Controllers determine the order in which Samplers are processed.”. 意思 ...

  6. JMeter—监听器&lpar;十二&rpar;

    参考<全栈性能测试修炼宝典JMeter实战>第六章 JMeter 元件详解中第七节监听器用来显示JMeter取样器的测试结果,能够以树.表.图形形式显示,也可以以文件方式保存. 一.设置默 ...

  7. Jmeter学习(三十二)调试工具Debug Sampler(转载)

    转载自 http://www.cnblogs.com/yangxia-test 一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug S ...

  8. CRL快速开发框架系列教程十二&lpar;MongoDB支持&rpar;

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  9. 我的MYSQL学习心得(十二) 触发器

    我的MYSQL学习心得(十二) 触发器 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数 ...

  10. &lt&semi;构建之法&gt&semi;第十一章、十二章有感

    十一章:软件设计与实现 工作时要懂得平衡进度和质量.我一直有一个困扰:像我们团队这次做 男神女神配 社区交友网,我负责主页的设计及内容模块,有个队友负责网站的注册和登录模块,有个队友负责搜索模块,有个 ...

随机推荐

  1. c&num;实现邮件发送链接激活

    2016-08-24 10:09:52 public void MailSend(string email) { MailMessage MyMail = new MailMessage(); MyM ...

  2. Linux运行级详解

    对于那些在DOS/Win9x/NT平台下的高级用户而言,Linux似乎是一个怪物.没有config.sys,没有 autoexec.bat,具有个人特色的机器配置不知道从何开始. 需要说明的是,很多人 ...

  3. 组合数(codevs 1631)

    1631 组合数  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 组合数C(N, K)表示 ...

  4. Oracle 存储过程&lpar;2&rpar;

    http://www.cnblogs.com/chinafine/archive/2010/07/12/1776102.html http://blog.itpub.net/29485627/view ...

  5. javascript实现函数的默认參数值方法

    近期在学python,得益于python中的decorator思路,想到在javascript中參数是不能定义默认值的,可是能够通过decorator给它模拟出来,话不多说,上代码 <!DO ...

  6. &lpar;Review cs231n&rpar; Training of Neural Network2

    FFDNet---matlab 调用并批处理 format compact; global sigmas; % input noise level or input noise level map a ...

  7. JAVA记录-java代码优化策略

    java代码优化策略 1.生成对象时,合理分配空间和大小:new ArrayList(100); 2.优化for循环: Vector vect = new Vector(1000); For(int ...

  8. eclipse更改workspace中出现The superclass &quot&semi;javax&period;servlet&period;http&period;HttpServlet&quot&semi; was not found on the Java----问题》》

    第一步:那是因为在项目中没有告诉它应该在哪个tomcat中运行,右击项目名称----->build path-->configure   path---->library------ ...

  9. aws的安全组

    aws有安全组来控制进入和去除的规则. 入站:就是外网访问你出站:就是你访问外网用户可以创建入站和出站规则,从而阻挡或者允许特定程序或者端口进行连接;用户可以将规则应用于一组程序.端口或者服务,也可以 ...

  10. 使用Java Api 操作HDFS

    如题 我就是一个标题党  就是使用JavaApi操作HDFS,使用的是MAVEN,操作的环境是Linux 首先要配置好Maven环境,我使用的是已经有的仓库,如果你下载的jar包 速度慢,可以改变Ma ...