jmeter生成随机字符串

一.
第一种方法,使用jmeter自带的random函数生成

1.打开jmeter右上角的函数助手,找到_RandomString 函数

 2.右键-添加-配置元件-添加用户自定义变量

 3.${name},放入需要入参的地方即可

 其他类型数据生成方式类似

 二.生成随机汉字也可以通过 jmeter BeanShell PreProcessor 写脚本来完成

1.右键-添加-前置处理器-添加 BeanShell PreProcessor
2.script 栏添加脚本代码

 代码

import java.util.Random;

public class Random_str {
public static String RandomJianHan(int len) {
String ret = "";
for (int i = 0; i < len; i++) {
String str = null;
int hightPos, lowPos; // 定义高低位
Random random = new Random();
hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
byte[] b = new byte[2];
b[0] = (new Integer(hightPos).byteValue());
b[1] = (new Integer(lowPos).byteValue());
try {
str = new String(b, "GBk"); // 转成中文
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
}
ret += str;
}
return ret;
}
}

Random_str ran = new Random_str();
String content1 =  ran.RandomJianHan(4); //此处生成的是长度为4的字符串
vars.put("content_post",content1);

 3.将content_post这个变量添加到你想要入参的地方,格式为${content_post}

原文地址:https://www.cnblogs.com/chenlimei/p/13912095.html