Skip to content

Commit ea63a54

Browse files
committed
dev: cpu usage presenter, utils
1 parent c99a929 commit ea63a54

File tree

8 files changed

+41
-24
lines changed

8 files changed

+41
-24
lines changed

bm-agent/src/main/java/org/benchmarker/bmagent/controller/AgentApiController.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public ResponseEntity<Map<Long, SchedulerStatus>> getSchedulersStatus() {
7878

7979
@GetMapping("/status")
8080
public AgentInfo getStatus() throws UnknownHostException {
81-
log.info("Check current status");
8281
InetAddress localHost = InetAddress.getLocalHost();
8382
String serverAddress = localHost.getHostAddress();
8483
String scheme = "http"; // Assuming it's always HTTP when accessed locally
@@ -87,16 +86,14 @@ public AgentInfo getStatus() throws UnknownHostException {
8786
Set<Long> longs = scheduledTaskService.getStatus().keySet();
8887

8988
String agentServerUrl = scheme + "://" + serverAddress + ":" + serverPort;
90-
AgentInfo info = AgentInfo.builder()
89+
return AgentInfo.builder()
9190
.templateId(longs)
9291
.cpuUsage(agentStatusManager.getCpuUsage())
9392
.memoryUsage(agentStatusManager.getMemoryUsage())
9493
.startedAt(agentStatusManager.getStartedAt())
9594
.serverUrl(agentServerUrl)
9695
.status(agentStatusManager.getStatus().get())
9796
.build();
98-
log.info(info.toString());
99-
return info;
10097
}
10198
}
10299

bm-agent/src/main/java/org/benchmarker/bmagent/pref/HttpSender.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,11 @@ public void sendRequests(SseEmitter sseEmitter, TemplateInfo templateInfo)
115115
log.info("Now send multiple HTTP request to target server");
116116
log.info(templateInfo.toString());
117117

118+
long startTime = System.currentTimeMillis(); // 시작 시간 기록
119+
long endTime = startTime + duration.toMillis();
118120
// Future setup
119121
futures = IntStream.range(0, templateInfo.getVuser())
120122
.mapToObj(i -> CompletableFuture.runAsync(() -> {
121-
long startTime = System.currentTimeMillis(); // 시작 시간 기록
122-
long endTime = startTime + duration.toMillis();
123-
124123
for (int j = 0; j < templateInfo.getMaxRequest(); j++) {
125124
// 테스트 시간 종료
126125
if (System.currentTimeMillis() > endTime){

bm-agent/src/main/java/org/benchmarker/bmagent/status/AgentStatusManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public Optional<AgentStatus> getAndUpdateStatusIfReady(AgentStatus status) {
147147
public void updateStats() {
148148
// Update CPU usage
149149
OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
150-
cpuUsage = osBean.getSystemCpuLoad();
150+
cpuUsage = osBean.getCpuLoad();
151151

152152
// Update memory usage
153153
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();

bm-controller/src/main/java/org/benchmarker/bmcontroller/preftest/controller/PerftestController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public ResponseEntity send(@PathVariable("group_id") String groupId,
6868
if (action.equals("stop")) {
6969
serverUrl = agentServerManager.getAgentMapped().get(Long.valueOf(templateId));
7070
agentServerManager.removeTemplateRunnerAgent(Long.valueOf(templateId));
71-
7271
log.info("stop to " + serverUrl);
7372
}else{
7473
if (perftestService.isRunning(groupId, templateId)){
@@ -99,7 +98,6 @@ public ResponseEntity send(@PathVariable("group_id") String groupId,
9998
})
10099
.subscribe(event -> {
101100
CommonTestResult commonTestResult = event.data();
102-
103101
// 결과 저장
104102
log.info("Start save Result");
105103
CommonTestResult saveReturnResult = testResultService.resultSaveAndReturn(commonTestResult)
@@ -108,6 +106,7 @@ public ResponseEntity send(@PathVariable("group_id") String groupId,
108106
log.info("End save Result");
109107
},
110108
error -> {
109+
perftestService.removeRunning(groupId,templateId);
111110
log.error("Error receiving SSE: {}", error.getMessage());
112111
});
113112

bm-controller/src/main/java/org/benchmarker/bmcontroller/template/common/TemplateUtils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
public class TemplateUtils {
88

99
public static LocalDateTime convertStringToLocalDateTime(String dateTimeString) {
10-
11-
LocalDate datePart = LocalDate.parse(dateTimeString.split("T")[0]);
12-
LocalTime timePart = LocalTime.parse(dateTimeString.split("T")[1].substring(0, 8));
13-
14-
return LocalDateTime.of(datePart, timePart);
10+
try{
11+
LocalDate datePart = LocalDate.parse(dateTimeString.split("T")[0]);
12+
LocalTime timePart = LocalTime.parse(dateTimeString.split("T")[1].substring(0, 8));
13+
return LocalDateTime.of(datePart, timePart);
14+
}catch (Exception e){
15+
return null;
16+
}
1517
}
1618
}

bm-controller/src/main/java/org/benchmarker/bmcontroller/template/model/TestResult.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import jakarta.persistence.*;
44
import lombok.*;
55
import lombok.extern.slf4j.Slf4j;
6+
import org.benchmarker.bmagent.AgentStatus;
67
import org.benchmarker.bmcontroller.common.model.BaseTime;
78
import org.benchmarker.bmcontroller.template.controller.dto.TestResultResponseDto;
89

@@ -52,6 +53,9 @@ public class TestResult extends BaseTime {
5253
@OneToMany(mappedBy = "testResult", fetch = FetchType.EAGER)
5354
private List<TestStatus> testStatuses;
5455

56+
@Enumerated(EnumType.STRING)
57+
private AgentStatus agentStatus;
58+
5559
public TestResultResponseDto convertToResponseDto() {
5660
return TestResultResponseDto.builder()
5761
.testId(this.id)

bm-controller/src/main/java/org/benchmarker/bmcontroller/template/service/TestResultService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package org.benchmarker.bmcontroller.template.service;
22

3+
import static org.benchmarker.bmcontroller.template.common.TemplateUtils.convertStringToLocalDateTime;
4+
35
import jakarta.transaction.Transactional;
6+
import java.time.LocalDateTime;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Optional;
410
import lombok.RequiredArgsConstructor;
511
import lombok.extern.slf4j.Slf4j;
612
import org.benchmarker.bmcommon.dto.CommonTestResult;
@@ -20,13 +26,6 @@
2026
import org.benchmarker.bmcontroller.user.repository.UserGroupRepository;
2127
import org.springframework.stereotype.Service;
2228

23-
import java.time.LocalDateTime;
24-
import java.util.ArrayList;
25-
import java.util.List;
26-
import java.util.Optional;
27-
28-
import static org.benchmarker.bmcontroller.template.common.TemplateUtils.convertStringToLocalDateTime;
29-
3029
@Slf4j
3130
@Service
3231
@RequiredArgsConstructor
@@ -62,6 +61,7 @@ public Optional<CommonTestResult> resultSaveAndReturn(CommonTestResult commonTes
6261
.totalError(commonTestResult.getTotalErrors())
6362
.tpsAvg(commonTestResult.getTpsAverage())
6463
.mttbfbAvg(commonTestResult.getMttfbAverage())
64+
.agentStatus(commonTestResult.getTestStatus())
6565
.build();
6666

6767
TestResult saveTestResult = testResultRepository.save(testResult);

bm-controller/src/main/resources/templates/fragments/agentStatus.html

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
stompClient.subscribe('/topic/server', (result) => {
2222
const agentInfoList = JSON.parse(result.body); // Parse the received JSON string
2323
const container = document.getElementById('agentInfoContainer');
24+
2425
container.innerHTML = '';
2526
agentInfoList.forEach((agent, index) => {
27+
const cpuUsagePercent = Math.floor(agent.cpuUsage * 100);
2628
// show info
2729
const agentInfoDiv = document.createElement('div');
2830
agentInfoDiv.id = 'agentInfoDiv';
@@ -46,7 +48,6 @@
4648
agentInfoParagraph.style.fontSize = '10px'; // 폰트 크기 설정
4749
agentInfoParagraph.style.fontWeight = 'bold'; // 폰트 굵기 설정
4850

49-
5051
// Set styles for status circles
5152
const circle = document.createElement('div');
5253
circle.style.width = '10px';
@@ -71,7 +72,22 @@
7172
agentInfoDiv.appendChild(agentInfoParagraph);
7273
agentInfoDiv.appendChild(circle);
7374

74-
container.appendChild(agentInfoDiv);
75+
const cpuUsageParagraph = document.createElement('p');
76+
cpuUsageParagraph.textContent = `CPU Usage: ${(agent.cpuUsage * 100).toFixed(2)}%`;
77+
cpuUsageParagraph.style.margin = '0px';
78+
cpuUsageParagraph.style.padding = '0px';
79+
cpuUsageParagraph.style.fontSize = '10px'; // 폰트 크기 설정
80+
cpuUsageParagraph.style.color = '#3a3a3a'; // 텍스트 색상 설정
81+
cpuUsageParagraph.style.alignSelf = 'center';
82+
83+
const infoWrapper = document.createElement('div');
84+
infoWrapper.style.display = 'flex';
85+
infoWrapper.style.flexDirection = 'column';
86+
87+
infoWrapper.appendChild(agentInfoDiv);
88+
infoWrapper.appendChild(cpuUsageParagraph);
89+
90+
container.appendChild(infoWrapper);
7591
});
7692

7793
});

0 commit comments

Comments
 (0)