refactor(项目设置): 代码片段执行日志隔离处理
--bug=1024856 --user=赵勇 【测试跟踪】自定义代码片段控制台日志未隔离 https://www.tapd.cn/55049933/s/1355517 Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
parent
c668b3d203
commit
a097b46f65
|
@ -10,11 +10,14 @@ import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
|
||||||
public class JMeterLoggerAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
|
public class JMeterLoggerAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
|
||||||
|
private final static String THREAD_SPLIT = " ";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void append(ILoggingEvent event) {
|
public void append(ILoggingEvent event) {
|
||||||
try {
|
try {
|
||||||
if (!event.getLevel().levelStr.equals(LogUtil.DEBUG)) {
|
if (!event.getLevel().levelStr.equals(LogUtil.DEBUG) && StringUtils.isNotEmpty(event.getThreadName())) {
|
||||||
StringBuffer message = new StringBuffer();
|
StringBuffer message = new StringBuffer();
|
||||||
|
String threadName = StringUtils.substringBeforeLast(event.getThreadName(), THREAD_SPLIT);
|
||||||
message.append(DateUtils.getTimeStr(event.getTimeStamp())).append(StringUtils.SPACE)
|
message.append(DateUtils.getTimeStr(event.getTimeStamp())).append(StringUtils.SPACE)
|
||||||
.append(event.getLevel()).append(StringUtils.SPACE)
|
.append(event.getLevel()).append(StringUtils.SPACE)
|
||||||
.append(event.getThreadName()).append(StringUtils.SPACE)
|
.append(event.getThreadName()).append(StringUtils.SPACE)
|
||||||
|
@ -29,12 +32,9 @@ public class JMeterLoggerAppender extends UnsynchronizedAppenderBase<ILoggingEve
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (message != null && !message.toString().contains("java.net.UnknownHostException")) {
|
if (message != null && !message.toString().contains("java.net.UnknownHostException")
|
||||||
if (FixedCapacityUtils.fixedCapacityCache.containsKey(event.getTimeStamp())) {
|
&& FixedCapacityUtils.containsKey(threadName)) {
|
||||||
FixedCapacityUtils.fixedCapacityCache.get(event.getTimeStamp()).append(message);
|
FixedCapacityUtils.get(threadName).append(message);
|
||||||
} else {
|
|
||||||
FixedCapacityUtils.fixedCapacityCache.put(event.getTimeStamp(), message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -155,7 +155,7 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
|
||||||
dto.setReportId("send." + this.getName());
|
dto.setReportId("send." + this.getName());
|
||||||
dto.setToReport(this.getName());
|
dto.setToReport(this.getName());
|
||||||
|
|
||||||
String console = FixedCapacityUtils.getJmeterLogger(this.getName(), false);
|
String console = FixedCapacityUtils.getJmeterLogger(this.getName());
|
||||||
if (StringUtils.isNotEmpty(requestResult.getName()) && requestResult.getName().startsWith("Transaction=")) {
|
if (StringUtils.isNotEmpty(requestResult.getName()) && requestResult.getName().startsWith("Transaction=")) {
|
||||||
requestResult.getSubRequestResults().forEach(transactionResult -> {
|
requestResult.getSubRequestResults().forEach(transactionResult -> {
|
||||||
transactionResult.getResponseResult().setConsole(console);//解析误报内容
|
transactionResult.getResponseResult().setConsole(console);//解析误报内容
|
||||||
|
|
|
@ -216,8 +216,8 @@ public class CustomFunctionService {
|
||||||
LogUtil.error(e.getMessage());
|
LogUtil.error(e.getMessage());
|
||||||
MSException.throwException(e.getMessage());
|
MSException.throwException(e.getMessage());
|
||||||
}
|
}
|
||||||
if (!FixedCapacityUtils.jmeterLogTask.containsKey(reportId)) {
|
if (!FixedCapacityUtils.containsKey(reportId)) {
|
||||||
FixedCapacityUtils.jmeterLogTask.put(reportId, System.currentTimeMillis());
|
FixedCapacityUtils.put(reportId,new StringBuffer());
|
||||||
}
|
}
|
||||||
addDebugListener(reportId, hashTree);
|
addDebugListener(reportId, hashTree);
|
||||||
LocalRunner runner = new LocalRunner(hashTree);
|
LocalRunner runner = new LocalRunner(hashTree);
|
||||||
|
@ -231,9 +231,9 @@ public class CustomFunctionService {
|
||||||
return (HashTree) field.get(scriptWrapper);
|
return (HashTree) field.get(scriptWrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addDebugListener(String testId, HashTree testPlan) {
|
private void addDebugListener(String reportId, HashTree testPlan) {
|
||||||
MsDebugListener resultCollector = new MsDebugListener();
|
MsDebugListener resultCollector = new MsDebugListener();
|
||||||
resultCollector.setName(testId);
|
resultCollector.setName(reportId);
|
||||||
resultCollector.setProperty(TestElement.TEST_CLASS, MsDebugListener.class.getName());
|
resultCollector.setProperty(TestElement.TEST_CLASS, MsDebugListener.class.getName());
|
||||||
resultCollector.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ViewResultsFullVisualizer"));
|
resultCollector.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ViewResultsFullVisualizer"));
|
||||||
resultCollector.setEnabled(true);
|
resultCollector.setEnabled(true);
|
||||||
|
@ -242,6 +242,7 @@ public class CustomFunctionService {
|
||||||
HashTree test = ArrayUtils.isNotEmpty(testPlan.getArray()) ? testPlan.getTree(testPlan.getArray()[0]) : null;
|
HashTree test = ArrayUtils.isNotEmpty(testPlan.getArray()) ? testPlan.getTree(testPlan.getArray()[0]) : null;
|
||||||
if (test != null && ArrayUtils.isNotEmpty(test.getArray()) && test.getArray()[0] instanceof ThreadGroup) {
|
if (test != null && ArrayUtils.isNotEmpty(test.getArray()) && test.getArray()[0] instanceof ThreadGroup) {
|
||||||
ThreadGroup group = (ThreadGroup) test.getArray()[0];
|
ThreadGroup group = (ThreadGroup) test.getArray()[0];
|
||||||
|
group.setName(reportId);
|
||||||
group.setProperty(BackendListenerConstants.MS_DEBUG.name(), true);
|
group.setProperty(BackendListenerConstants.MS_DEBUG.name(), true);
|
||||||
}
|
}
|
||||||
testPlan.add(testPlan.getArray()[0], resultCollector);
|
testPlan.add(testPlan.getArray()[0], resultCollector);
|
||||||
|
|
|
@ -1,22 +1,40 @@
|
||||||
package io.metersphere.code.snippet.util;
|
package io.metersphere.code.snippet.util;
|
||||||
|
|
||||||
|
import io.metersphere.commons.utils.DateUtils;
|
||||||
|
import io.metersphere.commons.utils.FileUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.Date;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class FixedCapacityUtils {
|
public class FixedCapacityUtils {
|
||||||
public static Map<Long, StringBuffer> fixedCapacityCache = Collections.synchronizedMap(new LRUHashMap<>());
|
private static Map<String, StringBuffer> fixedCapacityCache = Collections.synchronizedMap(new LRUHashMap<>());
|
||||||
public final static Map<String, Long> jmeterLogTask = new HashMap<>();
|
|
||||||
|
|
||||||
public static StringBuffer get(Long key) {
|
public static StringBuffer get(String key) {
|
||||||
return fixedCapacityCache.get(key);
|
return fixedCapacityCache.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void put(Long key, StringBuffer value) {
|
public static boolean containsKey(String key) {
|
||||||
|
if (StringUtils.isEmpty(key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return fixedCapacityCache.containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void put(String key, StringBuffer value) {
|
||||||
|
if (!fixedCapacityCache.containsKey(key)) {
|
||||||
fixedCapacityCache.put(key, value);
|
fixedCapacityCache.put(key, value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void remove(String key) {
|
||||||
|
if (fixedCapacityCache.containsKey(key)) {
|
||||||
|
fixedCapacityCache.remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static int size() {
|
public static int size() {
|
||||||
return fixedCapacityCache.size();
|
return fixedCapacityCache.size();
|
||||||
|
@ -24,7 +42,7 @@ public class FixedCapacityUtils {
|
||||||
|
|
||||||
|
|
||||||
static class LRUHashMap<K, V> extends LinkedHashMap<K, V> {
|
static class LRUHashMap<K, V> extends LinkedHashMap<K, V> {
|
||||||
private int capacity = 100;
|
private int capacity = 3000;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
||||||
|
@ -32,28 +50,36 @@ public class FixedCapacityUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getJmeterLogger(String reportId) {
|
||||||
public static String getJmeterLogger(String reportId, boolean isClear) {
|
|
||||||
try {
|
try {
|
||||||
Long startTime = FixedCapacityUtils.jmeterLogTask.get(reportId);
|
StringBuffer console = fixedCapacityCache.get(reportId);
|
||||||
if (startTime == null) {
|
if (FileUtils.isFolderExists(reportId)) {
|
||||||
startTime = FixedCapacityUtils.jmeterLogTask.get("[" + reportId + "]");
|
console.append(StringUtils.LF)
|
||||||
|
.append(DateUtils.getTimeString(new Date()))
|
||||||
|
.append(" INFO ").append("Tmp folder ")
|
||||||
|
.append(FileUtils.BODY_FILE_DIR)
|
||||||
|
.append(File.separator)
|
||||||
|
.append(reportId)
|
||||||
|
.append(" has deleted.");
|
||||||
}
|
}
|
||||||
if (startTime == null) {
|
if (FileUtils.isFolderExists("tmp" + File.separator + reportId)) {
|
||||||
startTime = System.currentTimeMillis();
|
console.append(StringUtils.LF)
|
||||||
|
.append(DateUtils.getTimeString(new Date()))
|
||||||
|
.append(" INFO ")
|
||||||
|
.append("Tmp folder ")
|
||||||
|
.append(FileUtils.BODY_FILE_DIR)
|
||||||
|
.append(File.separator)
|
||||||
|
.append("tmp")
|
||||||
|
.append(File.separator)
|
||||||
|
.append(reportId)
|
||||||
|
.append(" has deleted.");
|
||||||
}
|
}
|
||||||
Long endTime = System.currentTimeMillis();
|
return console.toString();
|
||||||
Long finalStartTime = startTime;
|
|
||||||
String logMessage = FixedCapacityUtils.fixedCapacityCache.entrySet().stream()
|
|
||||||
.filter(map -> map.getKey() > finalStartTime && map.getKey() <= endTime)
|
|
||||||
.map(map -> map.getValue()).collect(Collectors.joining());
|
|
||||||
|
|
||||||
return logMessage;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return "";
|
return StringUtils.EMPTY;
|
||||||
} finally {
|
} finally {
|
||||||
if (isClear && FixedCapacityUtils.jmeterLogTask.containsKey(reportId)) {
|
if (fixedCapacityCache.containsKey(reportId)) {
|
||||||
FixedCapacityUtils.jmeterLogTask.remove(reportId);
|
fixedCapacityCache.remove(reportId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -263,7 +263,7 @@
|
||||||
</rollingPolicy>
|
</rollingPolicy>
|
||||||
<encoder>
|
<encoder>
|
||||||
<charset>UTF-8</charset>
|
<charset>UTF-8</charset>
|
||||||
<Pattern>%d [%thread] %-5level %logger{36} %line - %msg%n</Pattern>
|
<Pattern>%d %t %-5level %logger{36} %line - %msg%n</Pattern>
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</appender>
|
||||||
<appender name="runLogAppender" class="ch.qos.logback.classic.AsyncAppender">
|
<appender name="runLogAppender" class="ch.qos.logback.classic.AsyncAppender">
|
||||||
|
|
Loading…
Reference in New Issue