• jmeter 保存响应数据


    创建存放相应文件

    String filepath = vars.get("RenderTradeContractFlow_resp_filepath");
    log.info("----- file path :" + filepath);
    //如果文件存在,则追加内容;如果文件不存在,则创建文件
    File f = new File(filepath);
    
    if(f.exists()){
        f.delete();
    } else {
        File fileParent = f.getParentFile(); 
        if(! fileParent.exists()){
            fileParent.mkdirs();
        }
    }
    f.createNewFile();
    log.info("----- success create file :" + filepath);
    ${__setProperty("fpp", vars.get("RenderTradeContractFlow_resp_filepath"),)};
    
    
    

    调用接口

    // import cn.hutool.core.io.FileUtil;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.nio.channels.FileChannel;
    import java.nio.channels.FileLock;
    import java.nio.charset.Charset;
    import com.google.common.io.Files;
    // import java.util.regex.Matcher;
    // import java.util.regex.Pattern;
    
    
    // import com.google.common.io.Files;
    // import java.nio.charset.Charset;
    
    if ("200".equals(prev.getResponseCode())) {
        String last_response = prev.getResponseDataAsString();
        JSONObject jO = JSONObject.parseObject(last_response).getJSONObject("res");
        String canBuy = jO.getJSONObject("buyCanBuyTO").getString("canBuy");
        if ("true".equals(canBuy)) {
            Failure = false;
            // FileWriter fw;
            String filepath = ${__property("fpp")};
            //如果文件存在,则追加内容;如果文件不存在,则创建文件
            File f = new File(filepath);
    
            try {
    
                //对该文件加锁
                RandomAccessFile out = new RandomAccessFile(f, "rw");
                FileChannel fcout=out.getChannel();
                FileLock flout=null;
                while(true){
                    try {
                        flout = fcout.tryLock();
                        break;
                    } catch (Exception e) {
                        System.out.println("有其他线程正在操作该文件,当前线程休眠500毫秒");
                        Thread.sleep(500);
                    }
    
                }
    
                // String pattern = "(?<=\\"tradeContract\\").*(?=,\\s*\\"visibleTO\\")";
    
                // Pattern r = Pattern.compile(pattern);
                // Matcher m = r.matcher(str);
                int le = "tradeContract\":".length();
                int index=last_response.indexOf("tradeContract\":") + le;
                String aaa = last_response.substring(index);
                int index2 = aaa.lastIndexOf(",\"visibleTO\":");
    //            log.info("---------: " + index2);
                String reqbody = "[" + aaa.substring(0,index2) + "]";
    
                // JSONObject j = jO.getJSONObject("tradeContract");
                // JSONArray ja22 = new JSONArray();
                // ja22.add(j);
                String req = vars.get("userid") + "^" + vars.get("cookies_gen") + "^" + reqbody + "^" + vars.get("mobile") + "\n";
                // String req = ja22.toString();
                // if (! req.endsWith("\n")) {
                //     req += "\n";
                // }
                // log.info(req);
                // FileUtil.appendUtf8String(req, f);
                Files.append(req,f,Charset.defaultCharset());
                // FileUtil.appendString(req, f, "utf-8");
    
                flout.release();
                fcout.close();
                out.close();
                out=null;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        // FailureMessage = "执行成功" + renderTrade_resp;
        // log.error("执行成功");
    }
    
    
    
  • 相关阅读:
    HTML5/CSS3滑块动画菜单
    基于HTML5手机登录注册表单代码
    基于HTML5手机上下滑动翻页特效
    基于jQuery+HTML5页面整屏滑动切换代码
    基于html5可拖拽图片循环滚动切换
    基于html5背景图片自适应代码是一款背景不随滚动条滚动,会根据分辨率不同自动匹配对应的背景图片
    HTML5实现摇一摇
    html5桌面通知,notification的使用,右下角出现通知框
    html5全局属性
    HTML5 QQ登录背景动态图片
  • 原文地址:https://www.cnblogs.com/blogabc/p/15702596.html
Copyright © 2020-2023  润新知