fix(接口测试): 接口测试导出jmx或者转性能测试时去掉DNSCacheManager

接口测试导出jmx或者转性能测试时去掉DNSCacheManager,并将域名按照DNSCacheManager配置的进行转化
This commit is contained in:
song-tianyang 2022-05-18 10:37:21 +08:00 committed by 刘瑞斌
parent 0218cd3d95
commit 56762abd26
3 changed files with 28 additions and 3 deletions

View File

@ -164,7 +164,6 @@ public class HarParser extends HarAbstractParser {
msResponse.setBody(new Body());
msResponse.setHeaders(new ArrayList<>());
msResponse.setType(RequestType.HTTP);
// todo 状态码要调整
msResponse.setStatusCode(new ArrayList<>());
if (response != null) {
String responseCode = String.valueOf(response.status);

View File

@ -19,7 +19,9 @@ import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
@EqualsAndHashCode(callSuper = true)
@ -65,6 +67,22 @@ public class MsDNSCacheManager extends MsTestElement {
}
}
public static Map<String, String> getEnvironmentDns(EnvironmentConfig config, HttpConfig httpConfig) {
Map<String, String> dnsMap = new HashMap<>();
if (config.getCommonConfig().isEnableHost() && CollectionUtils.isNotEmpty(config.getCommonConfig().getHosts()) && httpConfig != null && httpConfig.getDomain() != null) {
String domain = httpConfig.getDomain().trim();
config.getCommonConfig().getHosts().forEach(host -> {
if (StringUtils.isNotBlank(host.getDomain())) {
String hostDomain = host.getDomain().trim().replace("http://", "").replace("https://", "");
if (StringUtils.equals(hostDomain, domain)) {
dnsMap.put(hostDomain, host.getIp());
}
}
});
}
return dnsMap;
}
private static Arguments arguments(String name, List<KeyValue> variables) {
Arguments arguments = new Arguments();
arguments.setEnabled(true);

View File

@ -213,6 +213,15 @@ public class MsHTTPSamplerProxy extends MsTestElement {
}
}
if (config.isOperating() && config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& config.getConfig().get(this.getProjectId()).getCommonConfig().isEnableHost()) {
//导出的需要将DNSCache去掉并把域名进行ip替换
Map<String,String> dnsMap = MsDNSCacheManager.getEnvironmentDns(config.getConfig().get(this.getProjectId()), httpConfig);
String domain = sampler.getDomain();
if(dnsMap.containsKey(domain)){
sampler.setDomain(dnsMap.get(domain));
}
}
final HashTree httpSamplerTree = tree.add(sampler);
// 注意顺序放在config前面会优先于环境的请求头生效
@ -236,7 +245,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
httpSamplerTree.add(arguments);
}
//判断是否要开启DNS
if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
if (!config.isOperating() && config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& config.getConfig().get(this.getProjectId()).getCommonConfig().isEnableHost()) {
MsDNSCacheManager.addEnvironmentVariables(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId()));
MsDNSCacheManager.addEnvironmentDNS(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId()), httpConfig);
@ -276,7 +285,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
el.toHashTree(httpSamplerTree, el.getHashTree(), config);
}
}
//根据配置增加全局前后至脚本
if (httpConfig != null) {
JMeterScriptUtil.setScriptByHttpConfig(httpConfig, httpSamplerTree, config, useEnvironment, this.getEnvironmentId(), true);