Jmeter(四十)BeanShell范例

这世间,死一个人是一件大事,人名、地名都会被广传;死一百人就只是一个数字了。

                          ---《传记文学:从晚清到民国》

一、生成随机手机号码 

编译器调试:

package performance.java.top;

import java.util.Random;

/**
 * BeanShell生成随机手机号码方法
 * @author Richered
 *
 */
public class BeanShell {
    public void BeanShell() {
    String phone = "1";
    String number = System.currentTimeMillis() + "";

    for(int i = 0;i<100;i++){
        int second = new Random().nextInt(10);
        if (second == 3 || second == 4 || second == 5 || second == 7 || second == 8) {
            phone = phone + second;
            break;
        }
    }

    if("13".equals(phone)||"18".equals(phone))
    {
        int third = new Random().nextInt(10);
        phone = phone + third;
    }

    if("14".equals(phone))
    {
        int[] arr = { 5, 7, 9 };
        phone = phone + arr[new Random().nextInt(3)];
    }

    if("15".equals(phone))
    {
        int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        phone = phone + arr[new Random().nextInt(arr.length)];
    }

    if("17".equals(phone))
    {
        int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
        phone = phone + arr[new Random().nextInt(arr.length)];
    }

    phone=phone+number.substring(5,5+8);
    System.out.println(phone);
    
    }
    public static void main(String[] args) {
        BeanShell test = new BeanShell();
        test.BeanShell();
    }
}

jmeter调试:


/**
 * BeanShell生成随机手机号码方法
 * @author Richered
 *
 */
import java.util.Random;

    String phone = "1";
    String number = System.currentTimeMillis() + "";

    for(int i = 0;i<100;i++){
        int second = new Random().nextInt(10);
        if (second == 3 || second == 4 || second == 5 || second == 7 || second == 8) {
            phone = phone + second;
            break;
        }
    }

    if("13".equals(phone)||"18".equals(phone))
    {
        int third = new Random().nextInt(10);
        phone = phone + third;
    }

    if("14".equals(phone))
    {
        int[] arr = { 5, 7, 9 };
        phone = phone + arr[new Random().nextInt(3)];
    }

    if("15".equals(phone))
    {
        int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        phone = phone + arr[new Random().nextInt(arr.length)];
    }

    if("17".equals(phone))
    {
        int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
        phone = phone + arr[new Random().nextInt(arr.length)];
    }
    
 phone = phone + number.substring(5, 5+8);
 vars.put("mobphone", phone);

 请求查看:

 

二、数据转换

字符串转换为十六进制,十六进制转换为json

编译器调试:

/**
     * 16进制转换为json
     * @param s
     * @return
     */
    public static String toStringHex(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(
                        i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "utf-8");// UTF-16le:Not
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    /**
     * 字符串转换为十六进制
     * @param str
     * @return
     * @throws Exception
     */
    public static String toHexString(String str) throws Exception{
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            int ch = (int) str.charAt(i);
            String strHex = Integer.toHexString(ch);
            hexString.append(strHex);
        }
        return hexString.toString();
    }

jmeter前置处理器demo:

String json = "{\"appVersion\":\"\",\"autoLogin\":true,\"deviceId\":\"\",\"jid\":\"16780330/p_web_monitor\",\"sdkVersion\":\"\",\"token\":\"xxx\"}";

public static String toHexString(String str){
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            int ch = (int) str.charAt(i);
            String strHex = Integer.toHexString(ch);
            hexString.append(strHex);
        }
        return hexString.toString();
    }
    
vars.put("data", toHexString(json));

 

三、生成随机身份证号码

import java.util.*;  
StringBuilder generater = new StringBuilder();  
int sex=0; // 1为男  0 为女
int age=1979; //1979为大于18岁  2000小于18岁
Map areaCode = new HashMap();  
areaCode.put("北京市", 110000);
areaCode.put("市辖区", 110100);
areaCode.put("东城区", 110101);
areaCode.put("西城区", 110102);
areaCode.put("崇文区", 110103);
areaCode.put("宣武区", 110104);
areaCode.put("朝阳区", 110105);
//地区号  
String  randomAreaCode="";  
int index = (int) (Math.random() * areaCode.size());  
Collection values = areaCode.values();  
Iterator it = values.iterator();  
int i = 0;  
int code = 0;  
while (i < index && it.hasNext()) {  
 i++;  
 randomAreaCode = it.next().toString();  
}  
generater.append(randomAreaCode);  

//生日  
String randomBirthday="";  
Calendar birthday = Calendar.getInstance();  
birthday.set(Calendar.YEAR, (int) (Math.random() * 20) + age);  
birthday.set(Calendar.MONTH, (int) (Math.random() * 12));  
birthday.set(Calendar.DATE, (int) (Math.random() * 31));  
StringBuilder builder = new StringBuilder();  
builder.append(birthday.get(Calendar.YEAR));  
long month = birthday.get(Calendar.MONTH) + 1;  
if (month < 10) {  
 builder.append("0");  
}  
builder.append(month);  
long date = birthday.get(Calendar.DATE);  
if (date < 10) {  
 builder.append("0");  
}  
builder.append(date);  
randomBirthday= builder.toString();  
generater.append(randomBirthday);  
//随机码  
String randomCode="";  

int code = (int) (Math.random() * 1000);  
if (code < 10) {  
//    randomCode= "00" + code;
    if(code%2==sex)
    {

    randomCode= "00" + code;
    }
    else
    {
    code=code +  1;
    randomCode= "00" + code;
    }
   
 
} else if (code < 100) {  
// randomCode= "0" + code;  
 if(code%2==sex)
    {
    randomCode= "0" + code;
    }
    else
    {
    code=code +  1;
    randomCode= "0" + code;
    }
} else {  
// randomCode= "" + code;  
  if(code%2==sex)
    {
    randomCode= "" + code;
    }
    else
    {
    code=code +  1;
    randomCode= "" + code;

    }
}  


generater.append(randomCode);  
//验证码  
char[]  chars= generater.toString().toCharArray();  
int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };  
char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };  
int[] n = new int[17];  
int result = 0;  
for (int i = 0; i < n.length; i++) {  
 n[i] = Integer.parseInt(chars[i] + "");  
}  
for (int i = 0; i < n.length; i++) {  
 result += c[i] * n[i];  
}  
char validateCode = r[result % 11];  
generater.append(validateCode);  


vars.put("idNumber",generater.toString());  
SampleResult.setResponseData(generater.toString());

 

posted @ 2018-12-09 23:13  紫陌花间客  阅读(801)  评论(0编辑  收藏  举报