关于jmeter+ant+jenkins性能自动化将测试结果文件jtl转换成html文件遇到的问题。

时间:2021-07-08 00:44:27

1、ant自身缺陷,返回结果中有特殊字符,乱码字符,无法识别,jtl文件转换时报错。

2、jtl文件过大转换成html文件时出现内存溢出。

针对以上情况:可考虑使用BeenShell Sampler:对返回结果的乱码、特殊字符进行处理,返回结果内容过大,通过代码进行截取,但是会对性能测试结果又小许影响。

引入的外部java文件代码如下:

import java.net.URI;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class GetRequest {
public static String doGet(String url, String keyValueParam,String proxy,String port){
// 构建htpp客户端对象
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpGet get = new HttpGet();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(20000)
.setConnectTimeout(20000)
.setConnectionRequestTimeout(20000)
.build();
if(!proxy.equals("")){
requestConfig = RequestConfig.custom()
.setSocketTimeout(20000)
.setConnectTimeout(20000)
.setConnectionRequestTimeout(20000).setProxy(new HttpHost(proxy, Integer.valueOf(port)))
.build();
}

get.setConfig(requestConfig);
// 将url和键值对参数拼接成新的url地址
get.setURI(URI.create(url + keyValueParam));
HttpResponse response;
String strResponse ="No HttpResponse";
try {
response = client.execute(get);
// 获取返回主体
strResponse = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
strResponse ="Send Request,But HttpResponse is error";
}finally {

try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}

}

return strResponse;
}

public static String doGet(String url, String keyValueParam, String header,String proxy,String port){
// 构建htpp客户端对象
CloseableHttpClient client = HttpClientBuilder.create().build();

HttpGet get = new HttpGet();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000)
.setConnectionRequestTimeout(20000).build();
if (!proxy.equals("")) {
requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000)
.setConnectionRequestTimeout(20000).setProxy(new HttpHost(proxy, Integer.valueOf(port))).build();
}
get.setConfig(requestConfig);
// 将url和键值对参数拼接成新的url地址
get.setURI(URI.create(url + keyValueParam));
get.addHeader("Cookie", header);

HttpResponse response;
String strResponse="No HttpResponse";
try {
response = client.execute(get);
strResponse = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
strResponse="Send Request,But HttpResponse is error";
}finally {

try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}

}
// 获取返回主体

return strResponse;
}
}

BeenShell  Sampler的代码如下:

source("D:/Jmeter/运营平台核心性能场景/java/GetRequest.java");
String actualResponse = GetRequest.doGet("${Agreement}://${Host}:${Port}","${qualityCertificationUrl}","${Cookie}","${ProxyHost}","${ProxyPort}");
String sonStr= "<h3>初级认证2.0</h3>";
String response="";
//判断是否包含期望的字符串
if(actualResponse.indexOf(sonStr)!=-1){
response = "<h3>初级认证2.0</h3>";
}else{
response = actualResponse;
}
//为jmeter响应结果赋值
ResponseMessage=response;