美文网首页
基于Jmeter的BeanShell PreProcessor实

基于Jmeter的BeanShell PreProcessor实

作者: 花生草 | 来源:发表于2023-11-29 16:54 被阅读0次

需求

进行一个post请求接口的压测,有如下需求

  1. 请求的header中,time取系统当前时间,token取time的md5值
  2. 请求的body是JSON格式,body中含有一个数组,压测要求数组按照最大长度5000来构造
  3. 原始的csv文件大约40万条数据,每一行标识一条数据。实际请求的body数组来源于这里

经过分析,该需求可以通过Jmeter来实现

背景知识

BeanShell与Java的关系

Jmeter的BeanShell PreProcessoBeanShell组件实质上是一个Java源代码解释器,也就是说我们从Intellij Idea中写的代码,是可以拷贝到beanshell中直接执行的

当缺少类时

归根结底,BeanShell是一个小型的Java源代码解释器,使用外部类的时候,也需要导入该类。具体的说就是缺少import中涉及的lib包,不过这个不算什么大问题,可以通过下载bundle放到jmeter的ext/lib中解决
以本文想要组装成Json body为例,需要使用到json的lib,但是直接在jmeter中使用,会报错,找不到这个对象

import org.json.JSONArray;
import org.json.JSONObject;

让jmeter支持的方法是

1. 访问https://mvnrepository.com/
2. 选择一个最合适的版本,点击下载bundle文件
3. 将该jar包放到jmeter安装目录lib/ext下,并重新启动jmeter
image.png
image.png

JMX文件的执行流程

建立3个线程组,顺次执行

线程组1:大文件切分

新建线程组 -> 新建HTTP Request -> 新建BeanShell PreProcessor

import java.io.*;

    String targetFile = "D:/test/aaa.csv";//源文件目录
    
    String saveDir = "D:/test";//分割后文件目录
    long splitSize = 5000;//每个文件记录条数
    
    String saveFileName = "target";//分割后文件名开头
    String suffix = "csv";//分割后文件格式后缀
    
    public static void splitFile(String targetFile, String saveDir , String saveFileName, String suffix,long splitSize) throws Exception {
    
            if( !saveDir.endsWith("\\") ){
                saveDir += File.separator;
            }
    
            File file = new File(targetFile);
            if (!file.exists()) {
                throw new Exception("目标路径:[ " + targetFile + " ] 有错误...");
            }
    
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
    
            String str = null;
            
            long len = 0;
    
            log.info("开始写入......请等待......");
            long startTime = System.currentTimeMillis();
           
            BufferedWriter writer = null;
            while ((str = reader.readLine()) != null) {
                long txtSize = (len / splitSize) + 1;
                String fileName = saveDir + saveFileName + txtSize + "." + suffix;
    
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true)));
                writer.write(str + System.lineSeparator() );
                writer.flush();
                len ++;
                writer.close();
            }

            reader.close();
    
            log.info("写入完毕,一共 " + len + " 记录,耗时:" + ( System.currentTimeMillis() - startTime ) / 1000 + " s" );
    }

    try {
      splitFile(targetFile, saveDir, saveFileName, suffix, splitSize); //调用文件分割方法
    } catch (Exception e) {
      e.printStackTrace();
    }

线程组2:组装body

基于上一步生成的小文件,每个小文件组装成一条请求的body,并将所有的body写入最终的压测文件中
新建线程组 -> 新建HTTP Request -> 新建BeanShell PreProcessor

import org.apache.commons.io.FileUtils;
import java.io.File;
import org.json.JSONArray;
import org.json.JSONObject;

// 指定文件夹路径
String folderPath = "D:/test";
// 指定新文件路径
String newFilePath = "D:/test/rawbody";

// 获取文件夹下的所有文件
File folder = new File(folderPath);
File[] files = folder.listFiles();

// 创建新文件
File newFile = new File(newFilePath);

// 遍历每个文件并读取内容
for (File file : files) {
    // 读取文件内容
    boolean yes = file.getName().startsWith("target");

    if(!yes){
         continue;
    }
    JSONObject json;
    try{
            FileInputStream inputStream = new FileInputStream(folderPath+"/"+file.getName());
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            String str = null;
            json = new JSONObject();
            json.put("reqId", "test_" + UUID.randomUUID());
            JSONArray ids = new JSONArray();
           
            while((str = bufferedReader.readLine()) != null)
            {

                String[] deviceIds = str.split(",");
                String deviceIdType = "test";
                String deviceId = deviceIds[0];
               
                JSONObject item = new JSONObject();
                item.put("deviceId", deviceId);
                item.put("deviceType", deviceIdType);
        ids.put(item);

                
            }
            json.put("deviceInfos", ids);
            //close
            inputStream.close();
            bufferedReader.close();

        }catch (Exception e){
            e.printStackTrace();
        }

    // 写入新文件
    FileUtils.writeStringToFile(newFile, json.toString()+"\n", "UTF-8", true);
}

线程组3:发送压测请求

压测线程组的示例
  • 新建User Defined Variables,用于配置变量和引用
  • 新建HTTP Header Manager,按照如下设置time和token
time设置为${__time(,ts)},token设置为${__MD5(${ts})}
  • 新建CSV Data Set Config,变量名称设置为mybody
  • 新建HTTP Request,配置为POST请求,消息体数据为${mybody}
    完成上述配置后,可以设置线程数和循环次数,发送压测请求了

相关文章

网友评论

      本文标题:基于Jmeter的BeanShell PreProcessor实

      本文链接:https://www.haomeiwen.com/subject/ewdfgdtx.html