1.背景

接口压力测试是产品上线前很重要的一项测试,我们可以使用很多开源工具测试,
当然我们也可以简单的写一个多线程并发测试案例

2.代码

controller接口

  1. /**
  2. * 查询订单
  3. *
  4. * @return
  5. */
  6. @RequestMapping("/api/order")
  7. public Object product(Integer id) {
  8. // 为了便于分析,设置一个线程号
  9. Thread.currentThread().setName("thread-" + id);
  10. log.info("查询订单-" + id);
  11. // 模拟随机耗时
  12. ThreadUtil.sleepRandom();
  13. return "订单编号-" + id;
  14. }

3.测试

测试代码

  1. package com.ldp.jucproject.controller;
  2. import cn.hutool.http.HttpRequest;
  3. import cn.hutool.http.HttpUtil;
  4. import com.ldp.jucproject.utils.ThreadUtil;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.junit.jupiter.api.Test;
  7. import java.util.concurrent.CountDownLatch;
  8. /**
  9. * @author 姿势帝-博客园
  10. * @address https://www.cnblogs.com/newAndHui/
  11. * @WeChat 851298348
  12. * @create 11/06 10:18
  13. * @description
  14. */
  15. @Slf4j
  16. class OrderControllerTest {
  17. /**
  18. * 简单测试
  19. */
  20. @Test
  21. void product01() {
  22. Integer id = 1;
  23. String url = "http://localhost:8001/api/order?id=" + id;
  24. HttpRequest request = HttpUtil.createGet(url);
  25. String response = request.execute().body();
  26. System.out.println("response=" + response);
  27. }
  28. /**
  29. * 模拟多个请求
  30. */
  31. @Test
  32. void product02() {
  33. for (int i = 1; i <= 100; i++) {
  34. Integer id = i;
  35. String url = "http://localhost:8001/api/order?id=" + id;
  36. HttpRequest request = HttpUtil.createGet(url);
  37. String response = request.execute().body();
  38. System.out.println("response=" + response);
  39. }
  40. }
  41. /**
  42. * 模拟多线程请求
  43. */
  44. @Test
  45. void product03() throws InterruptedException {
  46. for (int i = 1; i <= 100; i++) {
  47. ThreadUtil.sleepRandom();
  48. Integer id = i;
  49. new Thread(() -> {
  50. String url = "http://localhost:8001/api/order?id=" + id;
  51. System.out.println("待查询订单号=" + id);
  52. HttpRequest request = HttpUtil.createGet(url);
  53. String response = request.execute().body();
  54. System.out.println("response=" + response);
  55. }).start();
  56. }
  57. // 避免线程终止
  58. Thread.sleep(20 * 1000);
  59. }
  60. /**
  61. * 模拟多线程并发请求
  62. */
  63. @Test
  64. void product04() throws Exception {
  65. // 并发请求数
  66. int num = 100;
  67. CountDownLatch countDownLatch = new CountDownLatch(num);
  68. for (int i = 1; i <= num; i++) {
  69. ThreadUtil.sleepRandom();
  70. // 计数器减一
  71. countDownLatch.countDown();
  72. Integer id = i;
  73. new Thread(() -> {
  74. try {
  75. String url = "http://localhost:8001/api/order?id=" + id;
  76. // 等待计数器归零,归零前都是处于阻塞状态
  77. System.out.println("待查询订单号=" + id);
  78. countDownLatch.await();
  79. HttpRequest request = HttpUtil.createGet(url);
  80. String response = request.execute().body();
  81. System.out.println("response=" + response);
  82. } catch (Exception e) {
  83. log.error("模拟并发出错:{}", e);
  84. }
  85. }).start();
  86. }
  87. // 避免线程终止
  88. Thread.sleep(60 * 1000);
  89. }
  90. }

4.完美!