fix: 修复下载性能测试日志的oom

This commit is contained in:
Captain.B 2020-07-22 19:07:40 +08:00
parent 90489ae2a3
commit 59cd84efa2
2 changed files with 15 additions and 12 deletions

View File

@ -15,12 +15,11 @@ import io.metersphere.performance.controller.request.ReportRequest;
import io.metersphere.performance.service.ReportService; import io.metersphere.performance.service.ReportService;
import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles; import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List; import java.util.List;
@RestController @RestController
@ -112,11 +111,15 @@ public class PerformanceReportController {
} }
@GetMapping("log/download/{reportId}/{resourceId}") @GetMapping("log/download/{reportId}/{resourceId}")
public ResponseEntity<byte[]> downloadLog(@PathVariable String reportId, @PathVariable String resourceId) { public void downloadLog(@PathVariable String reportId, @PathVariable String resourceId, HttpServletResponse response) throws Exception {
byte[] bytes = reportService.downloadLog(reportId, resourceId); try (OutputStream outputStream = response.getOutputStream()) {
return ResponseEntity.ok() List<String> content = reportService.downloadLog(reportId, resourceId);
.contentType(MediaType.parseMediaType("application/octet-stream")) response.setContentType("application/x-download");
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"jmeter.log\"") response.addHeader("Content-Disposition", "attachment;filename=jmeter.log");
.body(bytes); for (String log : content) {
outputStream.write(log.getBytes());
}
outputStream.flush();
}
} }
} }

View File

@ -25,6 +25,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@ -210,14 +211,13 @@ public class ReportService {
return loadTestReportLogMapper.selectByExampleWithBLOBs(example); return loadTestReportLogMapper.selectByExampleWithBLOBs(example);
} }
public byte[] downloadLog(String reportId, String resourceId) { public List<String> downloadLog(String reportId, String resourceId) {
LoadTestReportLogExample example = new LoadTestReportLogExample(); LoadTestReportLogExample example = new LoadTestReportLogExample();
example.createCriteria().andReportIdEqualTo(reportId).andResourceIdEqualTo(resourceId); example.createCriteria().andReportIdEqualTo(reportId).andResourceIdEqualTo(resourceId);
example.setOrderByClause("part desc"); example.setOrderByClause("part desc");
List<LoadTestReportLog> loadTestReportLogs = loadTestReportLogMapper.selectByExampleWithBLOBs(example); List<LoadTestReportLog> loadTestReportLogs = loadTestReportLogMapper.selectByExampleWithBLOBs(example);
String content = loadTestReportLogs.stream().map(LoadTestReportLog::getContent).reduce("", (a, b) -> a + b); return loadTestReportLogs.stream().map(LoadTestReportLog::getContent).collect(Collectors.toList());
return content.getBytes();
} }
public LoadTestReport getReport(String reportId) { public LoadTestReport getReport(String reportId) {