首页 > 数据库 > 数据库开发技术 > 如何对多行单次update接口进行压测
每一行的数据值允许update一次,百万级测试数据。
上次聊到如何对单行多次update进行压测,主要是为了解决单线程中请求参数如何每次都跟上次不一样这个难点。
本文讲的是多行单次update进行压测,就是大量数据需要在压测过程中update,但是每条数据只能update一次,在请求参数中必需保证,所有线程的请求参数必需都不一样,而且得跟已有的数据保持一致。
这里用到了之前讲到的线程安全的只是,思路如下:构建一个线程安全的队列,在压测前把数据读到这个队列中,然后压测开始后每次请求都先从这个队列中取参数。
还有一个思路,在压测前为每一个线程构建一个的队列(互不相同),这里可以非安全,压测过程中每一个线程从自己的队列中读取参数。
伪代码如下:
1Queue q = Date.init();
2//某一个线程开始循环开始
3doRequest(q.take());//需要控制q的大小,也可以再新启线程不断往q添加数据
4//单线程执行完成
其中,从队列中取值的方法根据用例设计不同而不同,也可以用poll(),防止阻塞。
分享一下我的代码:
1public static LinkedBlockingQueue<String> msgs = addmsg();
2
3
4 public static LinkedBlockingQueue<String> addmsg() {
5 String absolutePath = new File("").getAbsolutePath();
6 List<String> strings = WriteRead.readTxtFileByLine(absolutePath + "/queue");
7 LinkedBlockingQueue<String> ss = new LinkedBlockingQueue<>();
8 ss.addAll(strings);
9 logger.info("重新读取队列值");
10 return ss;
11 }
12
13public int deleteQ() throws InterruptedException {
14 if (msgs.size() == 0) {
15 logger.info("队列为空了");
16 msgs = addmsg();
17 }
18 String absolutePath = new File("").getAbsolutePath();
19 List<String> strings = WriteRead.readTxtFileByLine(absolutePath + "/dubbo");
20
21 new Concurrent(new ThreadBase(SourceCode.changeStringToInt(strings.get(0))) {
22 @Override
23 protected void before() {
24
25 }
26
27 @Override
28 protected void doing() throws Exception {
29 String msg = msgs.poll(100, TimeUnit.MILLISECONDS);
30 logger.info("msg:{}", msg);
31 DeleteQueueRequest deleteQueueRequest0 = new DeleteQueueRequest();
32 deleteQueueRequest0.setMsg(msg);
33 deleteQueueRequest0.setTaskTypeEnum(TaskTypeEnum.PUBLISH_PROMU);
34 CommonResponse<String> queue3 = commonDelayQueueService.deleteQueue(deleteQueueRequest0);
35 logger.info("deleteQueue2 {}", JsonUtil.obj2Json(queue3));
36 }
37
38 @Override
39 protected void after() {
40
41 }
42 }, SourceCode.changeStringToInt(strings.get(1))).start();
43
44 return 0;
45 }
这里分享了第一种思路的实现,由于测试数据数量巨大,所以不担心发生阻塞或者拿不到对象的情况。
原文作者:FunTester;如有侵权,请联系管理员删除。