jemter常⽤的beanshell脚本
1. 打印⽇志,写⼊信息到jmeber.log⽂件。
log.info
2.vars即JMeterVariables,操作jmeter变量,这个变量实际引⽤了JMeter线程中的局部变量容器(本质上是Map),常⽤⽅法:
a) (String key):从jmeter中获得变量值;
b) vars.put(String key,String value):数据存到jmeter变量中;
c) vars.putObject(String key,<T> value):数据存到jmeter变量中,但是数据类型不受限制;
3.prev 获取前⾯的sample返回的信息,常⽤⽅法:
a) ResponseDataAsString():获取响应信息。
b) ResponseCode() :获取响应code。
c) ${__P(变量名,)} 获取全局变量,可跨线程组
d) ${__setProperty(全局变量名,${线程组变量名},)}; 设置跨线程组的全局变量
4.list转为String[]
List tagGroupMap = new List()
String[] tagGroup = Array(new String[tagGroupMap.size()]);
5、没有key值只有value的JSONArray
6.获取jsonobject的key值
JSONObject jsonObject = new JSONObject(str);
Iterator tagkeys = jsonObject.keys();
while(tagkeys.hasNext()){
// 获得key
String key = ();
log.info("key:"+key);
}
7、截取部分线程组的名称
group = ThreadGroup(); // 获取当前线程组
str = Name(); // 获取线程组的名称
str = str.substring(0,2); // 截取字符串
vars.put("xc",str);
vars.put("result","true");
8、类型转换
如何将字串 String 转换成整数 int?
A. 有两个⽅法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的⽅法⼤同⼩异.
2 如何将整数 int 转换成字串 String ?
A. 有叁种⽅法:
1.) String s = String.valueOf(i);
2.) String s = String(i);
mkdirs方法
3.) String s = "" + i;
注: Double, Float, Long 转成字串的⽅法⼤同⼩异.
9、对⼀串字符串进⾏拆分,拆到字符串数组中
String str = "CG0011709140215,CG0011709140216";
String[] ss=str.split(",");
System.out.println(ss.length);
for(int i=0;i<ss.length;i++)
{
log.info(ss[i]);
String aa = "getresult-D";
aa = aa + i;
vars.put(aa,ss[i]);
10、时间戳
SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
try{
Date date =new Date(); //获取当前时间
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowDate = sf.format(date);//格式化当前时间
Calendar cal = Instance();
cal.setTime(sf.parse(nowDate));
cal.add(Calendar.DAY_OF_YEAR,+0); //此处+0为当前时间,+1为加⼀天,以此例推
String orderDate = sf.Time());
cal.add(Calendar.DAY_OF_YEAR,+365); //此处在之前的时间基础上叠加,+365为加1年
String senderDate = sf.Time());
vars.put("orderDate",orderDate); //参数可以调⽤
vars.put("senderDate",senderDate); //参数可以调⽤
}
catch(Exception e){
}
11、下载⽂件
import java.io.*;
byte[] result = ResponseData();
String file_name = "D:\\gongju\\apache-jmeter-3.2\\bin\\download\\sqlEnt_${id}.zip"; // 下载⽂件保存的路径File file = new File(file_name);
FileOutputStream out = new FileOutputStream(file);
out.write(result);
out.close();
12、保存响应内容
FileWriter fstream = new FileWriter("XXX",true);
BufferedWriter out =new BufferedWriter(fstream);
out.("AAA")+","+ ("BBB"));
out.Property("line.separator"));
out.close();
fstream.close();
13、断⾔,查看接⼝调使⽤能否成功,假如返回false是成功的,true是失败的
import java.io.UnsupportedEncodingException;
import org.apache.jmeter.assertions.AssertionResult;
import org.json.*;
String str = ResponseDataAsString();
String result = "";
try {
result = java.URLDecoder.decode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
JSONObject data_obj=new JSONObject(str);
String result1 = ("dockedContact").get("key").toString();
ains("value")) {
Failure = false;
FailureMessage = "断⾔成功";
log.info("断⾔成功");
}else{
Failure = true;
Failure = true;
FailureMessage = "断⾔成功";
}
14、数据库操作
import java.sql.*;
import java.util.*;
import java.lang.*;
import p.*;
import org.json.*;
//⼀:数据库取值
String drive = "sql.Driver";
String url = "jdbc:mysql://192.168.204.129:3306/";
String dbName = "aiopms";
String user = "root";
String pass = "000000";
String history = "";
String response = "";
String failuer = "";
String query ="SELECT projectid From pms_projects Where name ='测试项⽬'  order by 'desc' limit 1"; Connection Mycon = null;
Statement Mystmt = null;
ResultSet Myrset = null;
try{
Mycon = Connection(url+dbName, user, pass);
}  catch(SQLException e){
}
Mystmt = ateStatement();
Myrset = uteQuery(query);
while (()){
history = String(1);
}
Myrset.close();
Mystmt.close();
if(history == "")
{
Failure = true;
FailureMessage = "连接数据库失败";
}
15、浮点时间戳转换为标准时间
SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
//10位的秒级时间戳
long time1 = ${time};//获取时间戳变量
String result1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time1 * 1000)); log.info("10位时间戳(秒)--->Date:" +result1);
//13位的毫秒级时间戳
//double time2 = 1515730332000d;
//String result2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time2);
//System.out.println("13位数的时间戳(毫秒)--->Date:" + result2);
16、解析json
//导⼊json包
import org.json.*;
//获取获取请求的返回值
String response_data = ResponseDataAsString();
//⽇志打印获取请求的返回值
log.info(response_data);
log.info(response_data);
//将String类型的返回值构造成JSONObject对象
JSONObject data_obj = new JSONObject(response_data);
//获取作为下⼀个请求post的参数值Province(两种⽅式)
/
/String Provincelist_str = ("Province").toString(); JSONArray Provincelist_str = JSONArray("Province");
//log.info(Provincelist_str);
//获取Province数组的长度
int len = Provincelist_str.length();
String strlen = String(len);
vars.put("MessageNum",strlen);
log.info(strlen);
int i = 0;
for(;i < len;++i)
{
//获取 data[ i ] 数组对象
JSONObject jsonTemp = (JSONObject)JSONObject(i); switch(i)
{
case 0:
//两种提取参数的写法
String NameItems = String("Name");
// String NameItems = ("Name").toString();
// 两种打印参数的⽅法
// vars.put("Name_1", String("Name"));
vars.put("Name_1", NameItems);
log.info(NameItems);
}
}
17、递归创建多级⽬录
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
StringBuffer fileBuf=new StringBuffer();
String filePar = "D:\\\⽬录1\\⽬录2";
File myPath = new File( filePar );
if ( !ists()){
myPath.mkdirs();
System.out.println("创建⽂件夹路径为:"+ filePar);
}
String filename = "列表.csv";
try {
FileWriter fw = new FileWriter(filePar + "\\\" + filename,true);
String originalLine = “”+"\n";
System.out.println("*** "+ originalLine);
fw.write(originalLine);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。