package xnzx; /** * @author xn088587 * */ public class getTel{ public static int getNum(int start,int end) { return (int)(Math.random()*(end-start+1)+start); } public String getPhone() { String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(","); String time=(String.valueOf(System.currentTimeMillis()).substring(7)); String randNo1=String.valueOf(getNum(0,9)); String randNo2=String.valueOf(getNum(0,9)); int index=getNum(0,telFirst.length-1); String first=telFirst[index]; String Phone = (new StringBuilder()).append(first).append(randNo1).append(time).append(randNo2).toString(); return Phone; } /** public static void main(String[] args) { while (true) { getTel p1 = new getTel(); //getTel p2 = new getTel(); //p1.start(); //p2.start(); System.out.println("手机号:"+p1.getPhone()); } //System.out.println("手机号长度为:"+p.getPhone().length()); //System.out.println("第1个随机数*****:"+getTel.randNo1); //System.out.println("第2个随机数*****:"+getTel.randNo2); } */ }
第一种方式:直接使用源码脚本:
public class getTel{ public static int getNum(int start,int end) { return (int)(Math.random()*(end-start+1)+start); } public String getPhone() { String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(","); String time=(String.valueOf(System.currentTimeMillis()).substring(7)); String randNo1=String.valueOf(getNum(0,9)); String randNo2=String.valueOf(getNum(0,9)); int index=getNum(0,telFirst.length-1); String first=telFirst[index]; String Phone = (new StringBuilder()).append(first).append(randNo1).append(time).append(randNo2).toString(); return Phone; } } getTel p1 = new getTel(); vars.put("aa",p1.getPhone()); log.error("aa="+vars.get("aa"));
感觉这种方式比价麻烦
方式2:改用文件:
使用beanshell和jar包,都不能有主函数
source("D:\\apache-jmeter-3.0\\java\\getTel.java"); getTel p1 = new getTel(); vars.put("aa",p1.getPhone()); log.error("aa="+vars.get("aa"));
Jmeter函数的获取的值,需要通过benshell传递给变量:
https://www.cnblogs.com/tina19882010/articles/7909384.html