This commit is contained in:
fit2-zhao 2021-01-27 11:18:10 +08:00
commit eefb6be647
2 changed files with 3 additions and 40 deletions

View File

@ -21,9 +21,8 @@ public class JmeterFileController {
@GetMapping("download")
public ResponseEntity<byte[]> downloadJmeterFiles(@RequestParam("testId") String testId, @RequestParam("resourceId") String resourceId,
@RequestParam("ratio") double ratio, @RequestParam("startTime") long startTime,
@RequestParam("reportId") String reportId, @RequestParam("resourceIndex") int resourceIndex,
@RequestParam("threadNum") int threadNum) {
byte[] bytes = jmeterFileService.downloadZip(testId, resourceId, ratio, startTime, reportId, resourceIndex, threadNum);
@RequestParam("reportId") String reportId, @RequestParam("resourceIndex") int resourceIndex) {
byte[] bytes = jmeterFileService.downloadZip(testId, resourceId, ratio, startTime, reportId, resourceIndex);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + testId + ".zip\"")

View File

@ -2,9 +2,6 @@ package io.metersphere.performance.service;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.base.mapper.LoadTestMapper;
import io.metersphere.commons.exception.MSException;
@ -12,7 +9,6 @@ import io.metersphere.commons.utils.LogUtil;
import io.metersphere.performance.engine.EngineContext;
import io.metersphere.performance.engine.EngineFactory;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -20,7 +16,6 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -31,12 +26,11 @@ public class JmeterFileService {
@Resource
private LoadTestMapper loadTestMapper;
public byte[] downloadZip(String testId, String resourceId, double ratio, long startTime, String reportId, int resourceIndex, int threadNum) {
public byte[] downloadZip(String testId, String resourceId, double ratio, long startTime, String reportId, int resourceIndex) {
try {
LoadTestWithBLOBs loadTest = loadTestMapper.selectByPrimaryKey(testId);
// deep copy
LoadTestWithBLOBs subTest = SerializationUtils.clone(loadTest);
setThreadNum(subTest, threadNum);
EngineContext context = EngineFactory.createContext(subTest, resourceId, ratio, startTime, reportId, resourceIndex);
return zipFilesToByteArray(context);
} catch (MSException e) {
@ -49,36 +43,6 @@ public class JmeterFileService {
return null;
}
private void setThreadNum(LoadTestWithBLOBs t, Integer limit) {
// 传入limit才去改这个值
if (limit <= 0) {
return;
}
String loadConfiguration = t.getLoadConfiguration();
JSONArray jsonArray = JSON.parseArray(loadConfiguration);
for (int i = 0; i < jsonArray.size(); i++) {
if (jsonArray.get(i) instanceof Map) {
JSONObject o = jsonArray.getJSONObject(i);
if (StringUtils.equals(o.getString("key"), "TargetLevel")) {
o.put("value", limit);
break;
}
}
if (jsonArray.get(i) instanceof List) {
JSONArray o = jsonArray.getJSONArray(i);
for (int j = 0; j < o.size(); j++) {
JSONObject b = o.getJSONObject(j);
if (StringUtils.equals(b.getString("key"), "TargetLevel")) {
b.put("value", limit);
break;
}
}
}
}
// 设置线程数
t.setLoadConfiguration(jsonArray.toJSONString());
}
private byte[] zipFilesToByteArray(EngineContext context) throws IOException {
String testId = context.getTestId();
String fileName = testId + ".jmx";