Jmeter beanshell preprocessor用法


上传图片太麻烦,具体操作见:http://blog.csdn.net/quiet_girl/article/details/50577324


我大概介绍下别人没有的。

1、对请求报文的预处理,beanshell preprocessor的用法。在beanshell preprocessor页面,script脚本处,加入你需要对采样的报文的一些处理。

记得引入jar包,点击测试计划,在本页面,点击添加目录或者jar 到classpath。本实例需要的jar包已上传到我的下载资源了

如果缺少jar宝,会报异常,

 org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval ''import .... 


在beanshell preprocessor页面,script脚本和如下:

其中com.util.SignUtils该类为自己在eclipse下写的java工具类,用于对请求报文参数所做的一些加密算法处理,并将加密后的密文,添加到请求报文中,再发接口。

import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;
import com.util.SignUtils;
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.samplers.*;
import org.apache.jmeter.config.*;   
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.protocol.http.util.*;
import java.util.HashMap;
import java.util.Map;
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
try {
 //自定义变量
 //读取配置.设置头信息和公共信息
    
     //请求体重置加密
    Arguments arguments =  sampler.getArguments();
     Map inMap=new HashMap();
//    Argument arg = arguments.getArgument(0);
     int len=arguments.getArgumentCount();
            
            for(int i=0;i<len;i++){
                Argument jpro=arguments.getArgument(i);
                String key=jpro.getName();
                String ret=jpro.getValue();
                System.out.println(key+","+ret);
                inMap.put(key,ret);
            }
    
 
    if(inMap.size() !=0){
          JSONObject parseObject=new JSONObject();
     String jsonStr=parseObject.fromObject(inMap).toString();;
     log.info("jsonStr====********************************************************************"+ jsonStr);   
        SignUtils signUtils=new SignUtils();
        //加密
        String postData = signUtils.getSigeStr(inMap);
           
          log.info("postData====**************************************postData******************************"+ postData);
    
      arguments.addArgument(new HTTPArgument("encodeStr",postData));    
        
sampler.setArguments(arguments);
//设置新post 数据    
} } catch (Exception ex) { log.info("Script execution failed===========================================", ex);}

com.util.SignUtils
自定义工具类如下:

public class SignUtils {
	public static String getSigeStr(Map<String, Object> paramMap){
		String encodeAture="";

encodeAture = MD5Encoder(
paramMap
)

return encode;}
}





 

另外一个实例:

请求报文的前置处理脚本:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.xx.x.util.*;
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.samplers.*;
import org.apache.jmeter.config.*;   
import org.apache.jmeter.protocol.http.sampler.*;
import org.apache.jmeter.protocol.http.util.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import com.doctor.commons.*;

try {

      //自定义变量
     //读取配置.设置头信息和公共信息

      Properties properties = new Properties();
      String fileName = ${configFile}+"";
      FileInputStream inputStream = new FileInputStream(fileName);
      properties.load(inputStream);

      String accountId =  properties.getProperty("accountId");
      String sign = properties.getProperty("sign");
      String accessKey = properties.getProperty("accessKey");
      String token = properties.getProperty("token");

      vars.put("accountId",accountId);
      vars.put("sign",sign);
      vars.put("accessKey",accessKey);
      vars.put("token",token);

      if (inputStream != null) {
            inputStream.close();
      }
    
    String deviceId = ${deviceId}+"";
    String secretKey = ${secretKey}+"";

     //请求体重置加密
    Arguments arguments =  sampler.getArguments();
    Argument arg = arguments.getArgument(0);
    String body = arg.getValue();
    log.info("PreProcessor==========================================="+ body);
    
     JSONObject parseObject = JSON.parseObject(body);
     log.info("====********************************************************************"+ parseObject);
     String data =   parseObject.getString("data");

    if(data !=null){
         log.info("====********************************************************************"+ data);
        
        //加密
        String encryptData = AESUtis.appAESEncrypt(data, deviceId, secretKey);
          log.info("encryptData====********************************************************************"+ encryptData);
 
           JSONObject jsonObject = new JSONObject();
          jsonObject.put("data", encryptData);
          String  postData = jsonObject.toJSONString();
          log.info("postData====**************************************postData******************************"+ postData);
      
        //设置新post 数据    
         arg.setValue(postData);
    }

    
} catch (Exception ex) {
    log.info("Script execution failed===========================================", ex);
}






 


响应报文的后置处理脚本:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.customerdefine.util.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import com.doctor.commons.*;

    
 
try {

      //先取自定义变量,等用到的时候,再取说找不到定义    
       String secretKey =  ${secretKey}+"";
       String deviceId = ${deviceId}+"";
      //服务器返回的公共配置属性保存
       String fileName = ${configFile}+"";
       log.info("====*fileName*******************************************************************"+ fileName);
        

     //对返回的json数据处理
      String response_data = prev.getResponseDataAsString();
       JSONObject parseObject = JSON.parseObject(response_data);
       log.info("====********************************************************************"+ parseObject);
       String data = parseObject.getString("data");

       //这是加密的数据处理
       if (data != null) {

            log.info("====PostProcessor********************************************************************"+ data);
                //解密
               String reqDencryptJSON = AESUtis.appAESDencrypt(data, deviceId,secretKey );

               log.info("===PostProcessor**********************************responseBody*****************************"+ reqDencryptJSON);
         
            //解密的数据写回返回body中
            prev.setResponseData(reqDencryptJSON.getBytes("UTF-8"));


              //取用户属性保存,以便以后用到
            JSONObject  resultJson= JSON.parseObject(reqDencryptJSON);
                JSONObject result = resultJson.getJSONObject("result");
                 if (result != null) {
                     log.info("===PostProcessor*********************************result *******===************" );
                    String accessKey = result.getString("accessKey");
            
                    if (accessKey != null) {
                          String accountId = result.getString("accountId");
                          String token = result.getString("token");

                   //配置.设置头信息和公共信息
                     
 
                     String sign =  MD5Utils.md5To32LowerCaseHexString( secretKey + accountId+ deviceId + secretKey);

                    //文件读取放到这里, 文件内容会被重写,而不是追加模式,所以不能实际保存内容的地方申明FileOutputStream
                     Properties properties = new Properties();
       
                     FileOutputStream outputStream = new FileOutputStream(fileName);
        
                     properties.setProperty("accountId",accountId);
                     properties.setProperty("accessKey",accessKey);
                      properties.setProperty("sign",sign);
                      properties.setProperty("token",token);
                      properties.store(outputStream, "");//
                      log.info("===PostProcessor**********************************properties.store*******===************" );

                       //资源释放
    
        
                      if (outputStream != null) {
                            outputStream.close();
                    }
                      
                  }
              }
        }
    

   } catch (Exception ex) {
        
    log.info("Script execution failed================PostProcessor=========================", ex);
}



  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值