一.
第一種方法,使用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}