JSR223
JSR223
JSR:
Java Specification Requests的缩写,意思是Java规范提案,是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求。
JSR 223:
Java 规范请求223 的缩写,它定义了一种在JVM 上运行脚本语言的标准接口。该规范允许在Java 应用程序中使用各种脚本语言如(Grovvy、JavaScript、Python)
Beanshell:
- JSR223 规范的一种实现
- BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法;
- BeanShell是一种松散类型的脚本语言(这点和JS类似);
- BeanShell是用Java写成的,一个小型的、免费的、可以下载的、嵌入式的Java源代码解释器,具有对象脚本语言特性,非常精简的解释器jar文件大小为175k。
- BeanShell执行标准Java语句和表达式,另外包括一些脚本命令和语法。
官网:http://www.BeanShell.org/
GitHub: https://github.com/beanshell/beanshell/wiki
参考文章:
https://blog.csdn.net/ni_hao_fan/article/details/100011142
https://blog.csdn.net/BASK2311/article/details/128231278
https://blog.51cto.com/u_11529070/6665909
JMeter 常用内置变量
vars
public class JMeterVariables
extends Object
介绍:Class which defines JMeter variables. These are similar to properties, but they are local to a single thread.
类,它定义JMeter变量。这些类似于属性,但它们是单个线程的局部属性
API: https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
打印当前线程的全部变量:
vars.put("zhangsan", "21");
it =vars.getIterator();
while(it.hasNext()){
entry = it.next();
log.info("key:" + entry.getKey() + "value:" + entry.getValue().toString());
}
结果:
2024-05-17 10:20:12,549 INFO o.a.j.u.BeanShellTestElement: key:START.MSvalue:1715912384063
2024-05-17 10:20:12,549 INFO o.a.j.u.BeanShellTestElement: key:TESTSTART.MSvalue:1715912412323
2024-05-17 10:20:12,549 INFO o.a.j.u.BeanShellTestElement: key:JMeterThread.last_sample_okvalue:true
2024-05-17 10:20:12,549 INFO o.a.j.u.BeanShellTestElement: key:__jm__Thread Group__idxvalue:0
2024-05-17 10:20:12,549 INFO o.a.j.u.BeanShellTestElement: key:__jmeter.U_T__value:Thread Group 1-1
2024-05-17 10:20:12,549 INFO o.a.j.u.BeanShellTestElement: key:START.HMSvalue:101944
2024-05-17 10:20:12,550 INFO o.a.j.u.BeanShellTestElement: key:zhangsanvalue:21
2024-05-17 10:20:12,550 INFO o.a.j.u.BeanShellTestElement: key:JMeterThread.packvalue:org.apache.jmeter.threads.SamplePackage@7d70dcc6
2024-05-17 10:20:12,550 INFO o.a.j.u.BeanShellTestElement: key:START.YMDvalue:20240517
prev
public class SampleResult
extends Object
implements Serializable, Cloneable, Searchable
介绍:This is a nice packaging for the various information returned from taking a sample of an entry.
对于从一个条目中获取样本返回的各种信息来说,这是一个很好的包装。
API: https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html
ctx
public class JMeterContext
extends Object
Holds context for a thread. Generated by JMeterContextService.
The class is not thread-safe - it is only intended for use within a single thread.
保存线程的上下文。由JMeterContextService生成。该类不是线程安全的,它只适用于单个线程。
API: https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html
sample
API: https://jmeter.apache.org/api/org/apache/jmeter/samplers/Sampler.html
public class HTTPSampler
extends HTTPSamplerBase
implements Interruptible
A sampler which understands all the parts necessary to read statistics about HTTP requests, including cookies and authentication. This sampler uses the default Java HTTP implementation
一个采样器,它了解读取HTTP请求统计数据所需的所有部分,包括Cookie和身份验证。此采样器使用默认的Java HTTP实现
继承了 HTTPSamplerBase, 大部分方法来自于此:
修改请求体数据
Arguments api: https://jmeter.apache.org/api/org/apache/jmeter/config/Arguments.html
public class Arguments
extends ConfigTestElement
implements Serializable, Iterable<JMeterProperty>
A set of Argument objects.
应用场景:将请求体进行加密处理
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.Argument;
import org.json.*;
Arguments a = sampler.getArguments();
// 获取所有参数
log.info("Arguments a: " + a.toString());
//获取请求体数据
Argument arg = a.getArgument(0);
//请求体封装为JSON
JSONObject o = new JSONObject(arg.getValue());
log.info("JSONObject o" + o.toString());
o.put("name", "zhangsan"); //添加值
arg.setValue(o.toString()); //重新设置请求体
输出:
2023-06-30 11:26:00,747 INFO o.a.j.u.BeanShellTestElement: Arguments a: ={
"dispatchType": true,
"expressType": "",
"invoiceType": null,
"remark": "",
"invoiceOrganization": null,
"customerInvoiceInfoAccessId": null,
"preOrderBatchNumber": "cec944da85964c2fb36e5462ec91827e",
"receiverInfoAccessId": "f1bf62a1049143069c0d670c073c9c02",
"collarCouponNo": "",
"bizOrderType": "fa",
"deliveryBatchCode": ""
}()
2023-06-30 11:26:00,748 INFO o.a.j.u.BeanShellTestElement: JSONObject o{"customerInvoiceInfoAccessId":null,"dispatchType":true,"invoiceOrganization":null,"receiverInfoAccessId":"f1bf62a1049143069c0d670c073c9c02","collarCouponNo":"","expressType":"","invoiceType":null,"remark":"","bizOrderType":"fa","preOrderBatchNumber":"cec944da85964c2fb36e5462ec91827e","deliveryBatchCode":""}
添加参数成功:
看一下一个请求中的Arguments 中都有什么内容:Arguments 实现了 Iterable
public interface JMeterProperty
extends Serializable, Cloneable, Comparable<JMeterProperty>
比较用的方法:
- String getName()
- String getStringValue()
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.testelement.property.JMeterProperty;
Arguments a = sampler.getArguments();
for(Object o : a){
JMeterProperty js = (JMeterProperty) o;
log.info("name: " + js.getName() + "value:" + js.getStringValue());
}
结果:
添加请求头数据
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.Header;
// 获得请求头信息
HeaderManager headers = sampler.getHeaderManager();
// 打印全部请求头信息
log.info("添加前" + headers.getHeaders().getStringValue());
// new一个Header对象
myHeader = new Header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36");
// 添加Header到请求头管理器
headers.add(myHeader);
// 打印全部请求头信息
log.info("添加后" + headers.getHeaders().getStringValue());
log:
请求头成功添加User-Agent:
删除请求头
需求:删除 User-Agent
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.Header;
// 获得请求头信息
HeaderManager headers = sampler.getHeaderManager();
// 打印全部请求头信息
log.info("添加前" + headers.getHeaders().getStringValue());
// new一个Header对象
headers.removeHeaderNamed("User-Agent");
// 打印全部请求头信息
log.info("添加后" + headers.getHeaders().getStringValue());
log:
删除请求头User-Agent 成功:
获取每个请求头对象:
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.JMeterProperty;
// 获得请求头信息
HeaderManager headerManager = sampler.getHeaderManager();
CollectionProperty headers = headerManager.getHeaders();
for(JMeterProperty p : headers){
log.info("header name:" + p.getName()+",header value:" + p.getStringValue());
}
log.info(headers.toString());
ctx
Holds context for a thread. Generated by JMeterContextService.
The class is not thread-safe - it is only intended for use within a single thread.
API: https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html
获取Jmeter的所有属性信息:
p = ctx.getProperties();
Set keys = p.stringPropertyNames();
for(String key: keys){
log.info("key:" + key +"," + "value: " + p. getProperty(key));
}
CookieManage 和 HeaderManager
https://jmeter.apache.org/api/org/apache/jmeter/protocol/http/control/package-summary.html
断言
https://blog.csdn.net/weixin_50829653/article/details/117482501
官方资料
https://www.blazemeter.com/blog/beanshell-vs-jsr223-vs-jmeter
https://www.openhab.org/docs/configuration/jsr223.html
JSR223 规范具体内容:
https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/18119332