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