Commit 75872a14 by 王祖波

释放内存

parent dde171dc
...@@ -29,14 +29,14 @@ public class ImageCombined { ...@@ -29,14 +29,14 @@ public class ImageCombined {
private static final Logger logger = LoggerFactory.getLogger(ImageCombined.class); private static final Logger logger = LoggerFactory.getLogger(ImageCombined.class);
private static final Cache<String, BufferedImage> imageCache = Caffeine.newBuilder() private static final Cache<String, BufferedImage> imageCache = Caffeine.newBuilder()
.maximumWeight(100 * 1024 * 1024) // 100MB .maximumWeight(50 * 1024 * 1024) // 50MB
.weigher((String key, BufferedImage image) -> image.getWidth() * image.getHeight() / 4) .weigher((String key, BufferedImage image) -> image.getWidth() * image.getHeight() / 4)
.expireAfterAccess(30, TimeUnit.MINUTES) // 可设置失效时间 .expireAfterAccess(30, TimeUnit.MINUTES) // 可设置失效时间
.build(); .build();
public static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor( public static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
20, 10,
50, 30,
60L, 60L,
TimeUnit.SECONDS, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(100), new ArrayBlockingQueue<>(100),
...@@ -95,6 +95,9 @@ public class ImageCombined { ...@@ -95,6 +95,9 @@ public class ImageCombined {
ImageIO.write(combinedImage, "jpg", baos); ImageIO.write(combinedImage, "jpg", baos);
// 返回字节数组 // 返回字节数组
return baos.toByteArray(); return baos.toByteArray();
}finally {
qrCodeImage.flush();
combinedImage.flush();
} }
} }
......
...@@ -6,11 +6,13 @@ import java.util.Collections; ...@@ -6,11 +6,13 @@ import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.gic.commons.util.TraceIdUtil;
import com.gic.haoban.manage.api.qdto.combined.CombinedQDTO; import com.gic.haoban.manage.api.qdto.combined.CombinedQDTO;
import com.gic.haoban.manage.service.context.combined.ImageCombined; import com.gic.haoban.manage.service.context.combined.ImageCombined;
import com.gic.redis.data.util.RedisUtil; import com.gic.redis.data.util.RedisUtil;
...@@ -534,8 +536,12 @@ public class MaterialApiServiceImpl implements MaterialApiService { ...@@ -534,8 +536,12 @@ public class MaterialApiServiceImpl implements MaterialApiService {
Future<JSONResponse> future = null; Future<JSONResponse> future = null;
JSONResponse json = null; JSONResponse json = null;
try { try {
future = ImageCombined.EXECUTOR.submit(() -> future = ImageCombined.EXECUTOR.submit(() -> {
materialService.getMaterialIdByCombined(wxEnterpriseId, combinedQDTO)); TraceIdUtil.traceBegin(TraceIdUtil.EntranceType.QUARTZ);
JSONResponse jr = materialService.getMaterialIdByCombined(wxEnterpriseId, combinedQDTO);
TraceIdUtil.traceEnd();
return jr;
});
json = future.get(); json = future.get();
if (json.getErrorCode() == 0) { if (json.getErrorCode() == 0) {
String mediaId= json.getResult().toString() ; String mediaId= json.getResult().toString() ;
......
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.JSONResponse; import com.gic.api.base.commons.JSONResponse;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO; import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.api.qdto.combined.CombinedQDTO; import com.gic.haoban.manage.api.qdto.combined.CombinedQDTO;
...@@ -15,6 +18,12 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -15,6 +18,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 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.atomic.AtomicLong;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-conf.xml"}) @ContextConfiguration(locations = {"classpath:applicationContext-conf.xml"})
public class QWmediaTest { public class QWmediaTest {
...@@ -45,4 +54,56 @@ public class QWmediaTest { ...@@ -45,4 +54,56 @@ public class QWmediaTest {
} }
public static void main(String[] args) {
int TOTAL_REQUESTS = 100; // 总请求数
List<String> list = FileUtil.readLines("/Users/wang/Downloads/image.txt", "UTF-8");
JSONObject jo = new JSONObject();
jo.put("wxEnterpriseId", "b18ffdc9d0644912865a248859914d80");
jo.put("qrCodeUrl", "https://gicinner-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-4ffc77073ca1476fb264bf1be9f11383.png");
jo.put("showStyle", "0");
jo.put("lineOne", "门店CCA");
jo.put("lineTwo", "宇智222为您推荐");
ExecutorService executorService = Executors.newFixedThreadPool(50); // 创建线程池
CountDownLatch latch = new CountDownLatch(TOTAL_REQUESTS); // 创建CountDownLatch,初始值为请求总数
// 用于统计请求耗时
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++) {
final int requestId = i;
executorService.submit(() -> {
long startTime = System.currentTimeMillis();
try {
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("异常");
} 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();
}
});
}
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");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
executorService.shutdown();
}
}
} }
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