fix(性能测试): 测试报告上的定时刷新修改
This commit is contained in:
parent
1f6c3891a3
commit
f9326c7b43
|
@ -12,7 +12,8 @@ import javax.annotation.Resource;
|
|||
import javax.websocket.*;
|
||||
import javax.websocket.server.PathParam;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ServerEndpoint("/performance/report/{reportId}")
|
||||
|
@ -21,7 +22,7 @@ public class ReportWebSocket {
|
|||
|
||||
private static ReportService reportService;
|
||||
private static PerformanceTestService performanceTestService;
|
||||
private static ConcurrentHashMap<Session, Integer> refreshTimes = new ConcurrentHashMap<>();
|
||||
private static ConcurrentHashMap<Session, Timer> refreshTasks = new ConcurrentHashMap<>();
|
||||
|
||||
@Resource
|
||||
public void setReportService(ReportService reportService) {
|
||||
|
@ -37,12 +38,11 @@ public class ReportWebSocket {
|
|||
* 开启连接的操作
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(@PathParam("reportId") String reportId, Session session) throws IOException {
|
||||
//开启一个线程对数据库中的数据进行轮询
|
||||
ReportThread reportThread = new ReportThread(session, reportId);
|
||||
refreshTimes.put(session, 20);
|
||||
Thread thread = new Thread(reportThread);
|
||||
thread.start();
|
||||
public void onOpen(@PathParam("reportId") String reportId, Session session) {
|
||||
Timer timer = new Timer(true);
|
||||
ReportTask task = new ReportTask(session, reportId);
|
||||
timer.schedule(task, 0, 10 * 1000);
|
||||
refreshTasks.putIfAbsent(session, timer);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -50,23 +50,31 @@ public class ReportWebSocket {
|
|||
*/
|
||||
@OnClose
|
||||
public void onClose(Session session) {
|
||||
refreshTimes.remove(session);
|
||||
Timer timer = refreshTasks.get(session);
|
||||
if (timer != null) {
|
||||
timer.cancel();
|
||||
refreshTasks.remove(session);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 给服务器发送消息告知数据库发生变化
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(Session session, String message) {
|
||||
int refreshTime = 20;
|
||||
public void onMessage(@PathParam("reportId") String reportId, Session session, String message) {
|
||||
int refreshTime = 10;
|
||||
try {
|
||||
refreshTime = Integer.parseInt(message);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
refreshTimes.put(session, refreshTime);
|
||||
try {
|
||||
session.getBasicRemote().sendText("refresh-" + Math.random());
|
||||
} catch (IOException e) {
|
||||
Timer timer = refreshTasks.get(session);
|
||||
timer.cancel();
|
||||
|
||||
Timer newTimer = new Timer(true);
|
||||
newTimer.schedule(new ReportTask(session, reportId), 0, refreshTime * 1000L);
|
||||
refreshTasks.put(session, newTimer);
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
@ -80,48 +88,35 @@ public class ReportWebSocket {
|
|||
error.printStackTrace();
|
||||
}
|
||||
|
||||
public static class ReportThread implements Runnable {
|
||||
private boolean stopMe = true;
|
||||
private final String reportId;
|
||||
private final Session session;
|
||||
private int refresh;
|
||||
public static class ReportTask extends TimerTask {
|
||||
private Session session;
|
||||
private String reportId;
|
||||
|
||||
public ReportThread(Session session, String reportId) {
|
||||
ReportTask(Session session, String reportId) {
|
||||
this.session = session;
|
||||
this.reportId = reportId;
|
||||
this.refresh = 0;
|
||||
}
|
||||
|
||||
public void stopMe() {
|
||||
stopMe = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (stopMe) {
|
||||
try {
|
||||
LoadTestReportWithBLOBs report = reportService.getReport(reportId);
|
||||
if (report == null || StringUtils.equalsAny(report.getStatus(), PerformanceTestStatus.Completed.name())) {
|
||||
this.stopMe();
|
||||
session.close();
|
||||
break;
|
||||
}
|
||||
if (StringUtils.equals(report.getStatus(), PerformanceTestStatus.Error.name())) {
|
||||
this.stopMe();
|
||||
session.getBasicRemote().sendText("Error: " + report.getDescription());
|
||||
performanceTestService.stopErrorTest(reportId);
|
||||
session.close();
|
||||
break;
|
||||
}
|
||||
if (!session.isOpen()) {
|
||||
return;
|
||||
}
|
||||
if (StringUtils.equalsAny(report.getStatus(), PerformanceTestStatus.Running.name(), PerformanceTestStatus.Reporting.name())) {
|
||||
session.getBasicRemote().sendText("refresh-" + this.refresh++);
|
||||
}
|
||||
Thread.sleep(refreshTimes.get(session) * 1000L);
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
try {
|
||||
LoadTestReportWithBLOBs report = reportService.getReport(reportId);
|
||||
if (report == null || StringUtils.equalsAny(report.getStatus(), PerformanceTestStatus.Completed.name())) {
|
||||
session.close();
|
||||
}
|
||||
if (StringUtils.equals(report.getStatus(), PerformanceTestStatus.Error.name())) {
|
||||
session.getBasicRemote().sendText("Error: " + report.getDescription());
|
||||
performanceTestService.stopErrorTest(reportId);
|
||||
session.close();
|
||||
}
|
||||
if (!session.isOpen()) {
|
||||
return;
|
||||
}
|
||||
if (StringUtils.equalsAny(report.getStatus(), PerformanceTestStatus.Running.name(), PerformanceTestStatus.Reporting.name())) {
|
||||
session.getBasicRemote().sendText("refresh-" + Math.random());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -266,8 +266,6 @@ export default {
|
|||
this.result = this.$post('/performance/run', {id: testId, triggerMode: 'MANUAL'}, (response) => {
|
||||
this.reportId = response.data;
|
||||
this.$router.push({path: '/performance/report/view/' + this.reportId});
|
||||
// 注册 socket
|
||||
this.initWebSocket();
|
||||
});
|
||||
}).catch(() => {
|
||||
});
|
||||
|
@ -375,7 +373,7 @@ export default {
|
|||
});
|
||||
},
|
||||
refresh() {
|
||||
if (this.status === 'Running') {
|
||||
if (this.status === 'Running' || this.status === 'Starting') {
|
||||
if (this.websocket && this.websocket.readyState === 1) {
|
||||
this.websocket.send(this.refreshTime);
|
||||
}
|
||||
|
@ -403,7 +401,6 @@ export default {
|
|||
this.initBreadcrumb((response) => {
|
||||
this.initReportTimeInfo();
|
||||
});
|
||||
this.initWebSocket();
|
||||
} else {
|
||||
// console.log("close socket.");
|
||||
this.websocket.close(); //离开路由之后断开websocket连接
|
||||
|
|
Loading…
Reference in New Issue