Commit ef622d52 by 王祖波

log

parent 1b2ae7d6
......@@ -83,8 +83,11 @@ public class ImageCombined {
}
//压缩图片的参数
imageUrl = imageUrl + "?imageView2/2/w/1080/h/10800/format/jpg";
logger.info("加载要合成的图片开始1");
BufferedImage largeImage = loadImageWithCache(imageUrl);
logger.info("加载要合成的图片开始2");
BufferedImage qrCodeImage = loadImageWithReader(qrCodeUrl);
logger.info("加载要合成的图片结束");
if (largeImage == null || qrCodeImage == null) {
return null;
}
......
......@@ -534,6 +534,7 @@ public class MaterialApiServiceImpl implements MaterialApiService {
try {
future = ImageCombined.EXECUTOR.submit(() -> {
TraceIdUtil.traceBegin(TraceIdUtil.EntranceType.QUARTZ);
logger.info("线程池合成图片");
JSONResponse jr = materialService.getMaterialIdByCombined(wxEnterpriseId, combinedQDTO);
TraceIdUtil.traceEnd();
return jr;
......
......@@ -19,9 +19,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
@RunWith(SpringJUnit4ClassRunner.class)
......@@ -65,45 +63,51 @@ public class QWmediaTest {
jo.put("lineTwo", "宇智222为您推荐");
ExecutorService executorService = Executors.newFixedThreadPool(50); // 创建线程池
CountDownLatch latch = new CountDownLatch(TOTAL_REQUESTS); // 创建CountDownLatch,初始值为请求总数
Semaphore semaphore = new Semaphore(50); // 限制并发请求数为50
// 用于统计请求耗时
// 用于统计请求耗时
AtomicLong totalTime = new AtomicLong(0);
AtomicLong maxTime = new AtomicLong(0);
AtomicLong minTime = new AtomicLong(Long.MAX_VALUE);
for (int i = 0; i < list.size(); i++) {
for (int i = 0; i < TOTAL_REQUESTS; i++) {
final int requestId = i;
executorService.submit(() -> {
long startTime = System.currentTimeMillis();
try {
semaphore.acquire(); // 获取许可,控制并发请求
jo.put("imageUrl", list.get(requestId));
String post = HttpUtil.post("https://www.gicdev.com/haoban-manage3-wx/combined-qw-materialid.json", jo.toJSONString());
System.out.println(post);
} catch (Exception e) {
System.out.println("异常");
System.out.println("异常: " + e.getMessage());
} finally {
long duration = System.currentTimeMillis() - startTime;
totalTime.addAndGet(duration);
maxTime.updateAndGet(x -> Math.max(x, duration));
minTime.updateAndGet(x -> Math.min(x, duration));
latch.countDown();
semaphore.release(); // 释放许可
}
});
}
executorService.shutdown(); // 关闭线程池
// 打印统计信息
try {
latch.await();
System.out.println("完成所有请求");
long averageTime = totalTime.get() / TOTAL_REQUESTS;
System.out.println("平均耗时: " + averageTime + " ms");
System.out.println("最大耗时: " + maxTime.get() + " ms");
System.out.println("最小耗时: " + (minTime.get() == Long.MAX_VALUE ? 0 : minTime.get()) + " ms");
if (!executorService.awaitTermination(1, TimeUnit.MINUTES)) {
executorService.shutdownNow(); // 强制关闭
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
executorService.shutdown();
executorService.shutdownNow();
}
long averageTime = totalTime.get() / TOTAL_REQUESTS;
System.out.println("完成所有请求");
System.out.println("平均耗时: " + averageTime + " ms");
System.out.println("最大耗时: " + maxTime.get() + " ms");
System.out.println("最小耗时: " + (minTime.get() == Long.MAX_VALUE ? 0 : minTime.get()) + " ms");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment