jmeter中beanshell提供了写入csv文件的方法,主要代码如下
//保存的本地文件 FileWriter fo = new FileWriter("E:\\test\\result_jm.csv",true); BufferedWriter out = new BufferedWriter(fo); //需要保存的字段信息 out.write(vars.get("cal")+":"+vars.get("name")+","); //每个信息完成之后需要换行 out.write(System.getProperty("line.separator")); out.close(); fo.close();这里想实现的主要是:对于响应结果非空的数据则保存响应结果到文件1,而对于响应结果为空,则只需要保存输入的参数信息到文件2,以便后期对比确认接口数据准确度。
完整代码如下
import org.json.*; import org.json.JSONObject; import org.json.JSONAarry; //设置文件编码 prev.setDataEncoding("UTF-8"); int i=0; try{ log.info("响应结果中的cal_name是"+vars.get("cal")); //获取json提取器中的变量cal //String cal_test = vars.get("cal"); String response_data = prev.getResponseDataAsString();//获取请求返回值,此处获取到String类型; //JSONObject data_obj = new JSONObject(response_data);//将string类型的返回值构建成JSONObject对象 //JSONArray data_array = response_data.getJSONArray[0];//返回结果是数组 JSONArray data_array = new JSONArray(response_data); int len = data_array.length();//获取data数组的长度 String strlen = Integer.toString(len); log.info("响应结果字符串长度是"+strlen); String str_array = data_array.toString(); log.info("响应结果字符串是:"+str_array); //cal_test str_array == null ;cal_test.length() !=null;str_array.equals(0) if(len==0){ log.info("*****************食物匹配失败********************"); //如果响应结果为空,则只保存food名字到本地文件 FileWriter fos = new FileWriter("E:\\test\\HB\\food_result2_jm.csv",true); BufferedWriter outs = new BufferedWriter(fos); outs.write(vars.get("food")+","); log.info("原来的食谱名称是:"+ vars.get("food")); outs.write(System.getProperty("line.separator")); outs.close(); fos.close(); }else{ log.info("*************响应结果中获取食物cal和name****************"); for(i=0;i<len;i++){ //返回数组 JSONObject object = data_array.getJSONObject(i); //只获取响应结果中第一个properites的信息,需要取得多个properties,则可以使用for循环 JSONObject temp_properties = (JSONObject)object.getJSONObject("properties"); //只获取响应结果中properties中的calname值 cal_name_string = temp_properties.getString("cal_name"); log.info("Beanshell 后置处理器的提取的响应cal_name响应结果是:"+ cal_name_string); //保存响应结果中的cal_name变量 vars.put("cal_name_string",cal_name_string); name_string = temp_properties.getString("name"); log.info("Beanshell 后置处理器的提取的响应name_string响应结果是:"+ name_string); //保存响应结果中的cal_name变量 vars.put("name_string",name_string); log.info("*************食物匹配有结果****************"); //保存的本地文件,保存响应结果非空的数据 FileWriter fo = new FileWriter("E:\\test\\HB\\food_result1_jm.csv",true); BufferedWriter out = new BufferedWriter(fo); out.write(vars.get("name_string")+":"+vars.get("cal_name_string")+":"+vars.get("food")+","); log.info("响应结果中食物名称是:"+ vars.get("cal_name_string")); log.info("响应结果中食物名称是:"+ vars.get("name_string")); log.info("原来的食谱名称是:"+ vars.get("food")); out.write(System.getProperty("line.separator")); out.close(); fo.close(); } } //JSONObject object = data_array.getJSONObject(0); //log.info(object); //String cal_test = object.getString("cal_name"); } catch(Throwable ex){ log.error("Failed in Beanshell",ex); throw ex; // //如果响应结果为空,则只保存food名字到本地文件 // FileWriter fos = new FileWriter("E:\\test\\HB\\food_result2_jm.csv",true); // BufferedWriter outs = new BufferedWriter(fos); // outs.write(vars.get("food")+","); // log.info(vars.get("food")); // outs.write(System.getProperty("line.separator")); // // outs.close(); // fos.close(); }这里使用到了json包,提供一个下载地址:https://repo1.maven.org/maven2/org/json/json/20190722/
ok,到这里jmeter一个完整的流程就结束了,这里有部分jmeter的结果信息,如下
2019-08-23 15:08:28,064 INFO o.a.j.t.JMeterThread: Thread is done: chatbot 1-2628 2019-08-23 15:08:28,064 INFO o.a.j.t.JMeterThread: Thread finished: chatbot 1-2628 2019-08-23 15:08:28,065 INFO o.a.j.u.BeanShellTestElement: 响应结果字符串长度是1 2019-08-23 15:08:28,065 INFO o.a.j.u.BeanShellTestElement: 响应结果字符串是:[{"sub_properties":{"unit":"杯","quantifier_id":32,"quantity":1,"grams":250,"is_default":true},"type":"Entity","properties":{"start_index":0,"time_stamp":1566544108,"cal_name":"野菊","name":"野菊","end_index":2,"kcal_unit_weight":46.8,"food_id":5559,"e_type":"food"}}] 2019-08-23 15:08:28,065 INFO o.a.j.u.BeanShellTestElement: *************响应结果中获取食物cal和name**************** 2019-08-23 15:08:28,065 INFO o.a.j.u.BeanShellTestElement: Beanshell 后置处理器的提取的响应cal_name响应结果是:野菊 2019-08-23 15:08:28,065 INFO o.a.j.u.BeanShellTestElement: Beanshell 后置处理器的提取的响应name_string响应结果是:野菊 2019-08-23 15:08:28,065 INFO o.a.j.u.BeanShellTestElement: *************食物匹配有结果**************** 2019-08-23 15:08:28,067 INFO o.a.j.u.BeanShellTestElement: 响应结果中食物名称是:野菊 2019-08-23 15:08:28,067 INFO o.a.j.u.BeanShellTestElement: 响应结果中食物名称是:野菊 2019-08-23 15:08:28,067 INFO o.a.j.u.BeanShellTestElement: 原来的食谱名称是:野菊(鲜) ... 2019-08-23 15:08:30,407 INFO o.a.j.t.JMeterThread: Thread is done: chatbot 1-2690 2019-08-23 15:08:30,407 INFO o.a.j.t.JMeterThread: Thread finished: chatbot 1-2690 2019-08-23 15:08:30,522 INFO o.a.j.u.BeanShellTestElement: 响应结果中的cal_name是竹参 2019-08-23 15:08:30,524 INFO o.a.j.u.BeanShellTestElement: 响应结果字符串长度是3 2019-08-23 15:08:30,524 INFO o.a.j.u.BeanShellTestElement: 响应结果字符串是:[{"sub_properties":{"unit":"份","quantifier_id":18,"quantity":1,"grams":200,"is_default":true},"type":"Entity","properties":{"start_index":8,"time_stamp":1566544110,"cal_name":"竹参","name":"竹参","end_index":10,"kcal_unit_weight":155,"food_id":12628,"e_type":"food"}},{"sub_properties":{"unit":"份","quantifier_id":18,"quantity":1,"grams":200,"is_default":true},"type":"Entity","properties":{"start_index":5,"time_stamp":1566544110,"cal_name":"干竹笙","name":"竹笙","end_index":7,"kcal_unit_weight":312.116,"food_id":10653,"e_type":"food"}},{"sub_properties":{"unit":"片","quantifier_id":34,"quantity":1,"grams":7,"is_default":true},"type":"Entity","properties":{"start_index":0,"time_stamp":1566544110,"cal_name":"竹荪","name":"竹荪","end_index":2,"kcal_unit_weight":155,"food_id":5607,"e_type":"food"}}] 2019-08-23 15:08:30,524 INFO o.a.j.u.BeanShellTestElement: *************响应结果中获取食物cal和name**************** 2019-08-23 15:08:30,524 INFO o.a.j.u.BeanShellTestElement: Beanshell 后置处理器的提取的响应cal_name响应结果是:竹参 2019-08-23 15:08:30,524 INFO o.a.j.u.BeanShellTestElement: Beanshell 后置处理器的提取的响应name_string响应结果是:竹参 2019-08-23 15:08:30,524 INFO o.a.j.u.BeanShellTestElement: *************食物匹配有结果**************** 2019-08-23 15:08:30,526 INFO o.a.j.u.BeanShellTestElement: 响应结果中食物名称是:竹参 2019-08-23 15:08:30,526 INFO o.a.j.u.BeanShellTestElement: 响应结果中食物名称是:竹参 2019-08-23 15:08:30,526 INFO o.a.j.u.BeanShellTestElement: 原来的食谱名称是:竹荪(干)竹笙、竹参 2019-08-23 15:08:30,526 INFO o.a.j.u.BeanShellTestElement: Beanshell 后置处理器的提取的响应cal_name响应结果是:干竹笙 2019-08-23 15:08:30,526 INFO o.a.j.u.BeanShellTestElement: Beanshell 后置处理器的提取的响应name_string响应结果是:竹笙 2019-08-23 15:08:30,526 INFO o.a.j.u.BeanShellTestElement: *************食物匹配有结果**************** 2019-08-23 15:08:30,527 INFO o.a.j.u.BeanShellTestElement: 响应结果中食物名称是:干竹笙 2019-08-23 15:08:30,527 INFO o.a.j.u.BeanShellTestElement: 响应结果中食物名称是:竹笙 2019-08-23 15:08:30,527 INFO o.a.j.u.BeanShellTestElement: 原来的食谱名称是:竹荪(干)竹笙、竹参 2019-08-23 15:08:30,527 INFO o.a.j.u.BeanShellTestElement: Beanshell 后置处理器的提取的响应cal_name响应结果是:竹荪 2019-08-23 15:08:30,527 INFO o.a.j.u.BeanShellTestElement: Beanshell 后置处理器的提取的响应name_string响应结果是:竹荪 2019-08-23 15:08:30,527 INFO o.a.j.u.BeanShellTestElement: *************食物匹配有结果**************** 2019-08-23 15:08:30,528 INFO o.a.j.u.BeanShellTestElement: 响应结果中食物名称是:竹荪 2019-08-23 15:08:30,528 INFO o.a.j.u.BeanShellTestElement: 响应结果中食物名称是:竹荪 2019-08-23 15:08:30,528 INFO o.a.j.u.BeanShellTestElement: 原来的食谱名称是:竹荪(干)竹笙、竹参 2019-08-23 15:08:30,528 INFO o.a.j.t.JMeterThread: Thread is done: chatbot 1-2685 2019-08-23 15:08:30,528 INFO o.a.j.t.JMeterThread: Thread finished: chatbot 1-2685 2019-08-23 15:08:30,529 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test 2019-08-23 15:08:30,529 INFO o.a.j.s.FileServer: Close: E:\test\HB\qa.csv 2019-08-23 15:08:30,529 INFO o.a.j.s.FileServer: Close: E:\test\HB\food_jm.csv 2019-08-23 15:08:30,529 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)