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:
fit2-zhao 2023-03-24 12:52:04 +08:00 committed by fit2-zhao
parent c668b3d203
commit a097b46f65
5 changed files with 65 additions and 38 deletions

View File

@ -10,11 +10,14 @@ import org.apache.commons.lang3.StringUtils;
public class JMeterLoggerAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
private final static String THREAD_SPLIT = " ";
@Override
public void append(ILoggingEvent event) {
try {
if (!event.getLevel().levelStr.equals(LogUtil.DEBUG)) {
if (!event.getLevel().levelStr.equals(LogUtil.DEBUG) && StringUtils.isNotEmpty(event.getThreadName())) {
StringBuffer message = new StringBuffer();
String threadName = StringUtils.substringBeforeLast(event.getThreadName(), THREAD_SPLIT);
message.append(DateUtils.getTimeStr(event.getTimeStamp())).append(StringUtils.SPACE)
.append(event.getLevel()).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 (FixedCapacityUtils.fixedCapacityCache.containsKey(event.getTimeStamp())) {
FixedCapacityUtils.fixedCapacityCache.get(event.getTimeStamp()).append(message);
} else {
FixedCapacityUtils.fixedCapacityCache.put(event.getTimeStamp(), message);
}
if (message != null && !message.toString().contains("java.net.UnknownHostException")
&& FixedCapacityUtils.containsKey(threadName)) {
FixedCapacityUtils.get(threadName).append(message);
}
}
} catch (Exception e) {

View File

@ -155,7 +155,7 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
dto.setReportId("send." + 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=")) {
requestResult.getSubRequestResults().forEach(transactionResult -> {
transactionResult.getResponseResult().setConsole(console);//解析误报内容

View File

@ -216,8 +216,8 @@ public class CustomFunctionService {
LogUtil.error(e.getMessage());
MSException.throwException(e.getMessage());
}
if (!FixedCapacityUtils.jmeterLogTask.containsKey(reportId)) {
FixedCapacityUtils.jmeterLogTask.put(reportId, System.currentTimeMillis());
if (!FixedCapacityUtils.containsKey(reportId)) {
FixedCapacityUtils.put(reportId,new StringBuffer());
}
addDebugListener(reportId, hashTree);
LocalRunner runner = new LocalRunner(hashTree);
@ -231,9 +231,9 @@ public class CustomFunctionService {
return (HashTree) field.get(scriptWrapper);
}
private void addDebugListener(String testId, HashTree testPlan) {
private void addDebugListener(String reportId, HashTree testPlan) {
MsDebugListener resultCollector = new MsDebugListener();
resultCollector.setName(testId);
resultCollector.setName(reportId);
resultCollector.setProperty(TestElement.TEST_CLASS, MsDebugListener.class.getName());
resultCollector.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ViewResultsFullVisualizer"));
resultCollector.setEnabled(true);
@ -242,6 +242,7 @@ public class CustomFunctionService {
HashTree test = ArrayUtils.isNotEmpty(testPlan.getArray()) ? testPlan.getTree(testPlan.getArray()[0]) : null;
if (test != null && ArrayUtils.isNotEmpty(test.getArray()) && test.getArray()[0] instanceof ThreadGroup) {
ThreadGroup group = (ThreadGroup) test.getArray()[0];
group.setName(reportId);
group.setProperty(BackendListenerConstants.MS_DEBUG.name(), true);
}
testPlan.add(testPlan.getArray()[0], resultCollector);

View File

@ -1,21 +1,39 @@
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.HashMap;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class FixedCapacityUtils {
public static Map<Long, StringBuffer> fixedCapacityCache = Collections.synchronizedMap(new LRUHashMap<>());
public final static Map<String, Long> jmeterLogTask = new HashMap<>();
private static Map<String, StringBuffer> fixedCapacityCache = Collections.synchronizedMap(new LRUHashMap<>());
public static StringBuffer get(Long key) {
public static StringBuffer get(String key) {
return fixedCapacityCache.get(key);
}
public static void put(Long key, StringBuffer value) {
fixedCapacityCache.put(key, 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);
}
}
public static void remove(String key) {
if (fixedCapacityCache.containsKey(key)) {
fixedCapacityCache.remove(key);
}
}
public static int size() {
@ -24,7 +42,7 @@ public class FixedCapacityUtils {
static class LRUHashMap<K, V> extends LinkedHashMap<K, V> {
private int capacity = 100;
private int capacity = 3000;
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
@ -32,28 +50,36 @@ public class FixedCapacityUtils {
}
}
public static String getJmeterLogger(String reportId, boolean isClear) {
public static String getJmeterLogger(String reportId) {
try {
Long startTime = FixedCapacityUtils.jmeterLogTask.get(reportId);
if (startTime == null) {
startTime = FixedCapacityUtils.jmeterLogTask.get("[" + reportId + "]");
StringBuffer console = fixedCapacityCache.get(reportId);
if (FileUtils.isFolderExists(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) {
startTime = System.currentTimeMillis();
if (FileUtils.isFolderExists("tmp" + File.separator + reportId)) {
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();
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;
return console.toString();
} catch (Exception e) {
return "";
return StringUtils.EMPTY;
} finally {
if (isClear && FixedCapacityUtils.jmeterLogTask.containsKey(reportId)) {
FixedCapacityUtils.jmeterLogTask.remove(reportId);
if (fixedCapacityCache.containsKey(reportId)) {
fixedCapacityCache.remove(reportId);
}
}
}

View File

@ -263,7 +263,7 @@
</rollingPolicy>
<encoder>
<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>
</appender>
<appender name="runLogAppender" class="ch.qos.logback.classic.AsyncAppender">