Performance Test 101

无论是哪种测试:单元测试、集成测试、回归测试等,目的都是找到 breaking points、消除性能瓶颈。

对于 Performance Test 来说,我们希望在期望的负载下验证我们的系统是否在 速度伸缩性稳定性 方面满足系统对性能的需求。一般来说我们希望系统能在 RPS 和 latency 之间达到一个不错的平衡。

最最基础的两个指标:

RPS (request per sec):
每秒系统可接受/处理的请求数。值得注意的时,虽然请求可被接收或者处理,但是处理的结果可能是正常启动的 value 也有可能因为系统压力的原因而是 Error。

这篇文章讲了RPS 和 Virtual User 的概念 ,可以帮助理解为什么不是 RPS 越高越好而是 RPS 与 Latence 之间的平衡。其中讲到 Virutal User 的生命周期是到测试结束,这一点对我们理解和写测试代码挺重要的。
The typical virtual user life cycle looks like this — it picks up the scripted tasks and performs them, once it finishes all of them it will start the loop again. And it keeps it going until the test is terminated.

这个和 Locust 文档 中描述的行为是一致的: This user will make HTTP requests to /hello, and then /world, again and again.

Latency:
从请求被创建出来发送到接收的 endpint 开始到收到 response 的这个段时间。

1. 性能测试类型

这里列出的类型分类是从稍微具体一点的关注点来分类的。我们做性能测试就是希望在下面几个方面得到系统当前的处理能力,以及它的 breaking points。

  • 负载测试 Load Test

  • 压力测试 Stress Test

  • 耐久/疲劳测试 Endurance Test

  • 尖峰测试 Spike Test

  • 容量 Volume Test

  • 伸缩测试 Scaliability Test

Load testing – checks the application’s ability to perform under anticipated user loads. The objective is to identify performance bottlenecks before the software application goes live.

Stress testing – involves testing an application under extreme workloads to see how it handles high traffic or data processing. The objective is to identify the breaking point of an application.

Endurance testing – is done to make sure the software can handle the expected load over a long period of time.

Spike testing – tests the software’s reaction to sudden large spikes in the load generated by users.

Volume testing – Under Volume Testing large no. of. Data is populated in a database and the overall software system’s behavior is monitored. The objective is to check software application’s performance under varying database volumes.

Scalability testing – The objective of scalability testing is to determine the software application’s effectiveness in “scaling up” to support an increase in user load. It helps plan capacity addition to your software system.

2. 性能测试的三个方面

上面列出的几个测试关注点,都可以放到这三点:速度、伸缩性、稳定性。
速度: 负载测试, 压力测试
伸缩: 压力测试, Spike Test, Volume Test, Scaliability Test
稳定性: Endurance testing

1. 有多快?—— 速度

常用指标:

  • 加载时长 Load time
  • 响应时长 Response time
  • RPS
  • 每秒网络字节数 Network bytes total per second
  • 网络输出队列长度 Network output queue length

只看平均值是没有太多意义的,要看百分比分布。

2. 还能有多快? —— 伸缩性

这里的 scalability 是说在外部请求不断增加的情况下,系统的资源分配是否能支持这些增加的负载。同样我们也期望当外部负载变小时,系统占用的资源也能够释放出来。
这里的 memeory footprint、CPU usage、bandwidth usage 都是系统垂直伸缩的指标。
如果系统支持水平伸缩,那么就要看系统是否能够在输入负载增加的情况下,系统是否能够通过水平扩展节点来保持期望的处理速度。

常用指标:

  • 内存
  • 带宽
  • CPU
  • 并发用户数
  • 系统处理节点
  • … …

3. 可以持续多久?—— 稳定性

持续的给系统一定的输入负载,看看系统是否能够正常处理请求。一般来说,通过这种持续性测试可以发现系统是否能够对使用后的资源进行了释放,比如内存泄露的情况。
如果使用 Gatling,对应如下语句进行 stability 的测试。

jumpToRps(20),
holdFor(Duration.ofSeconds(30)),

常用指标:

  • Page faults
  • Committed memory
  • Maximum active sessions
  • Thread counts
  • Transactions passed or failed
  • Error rate

3. 性能测试步骤

1. Identify your testing environment

软硬件环境、网络配置、测试工具。

2. Identify the performance acceptance criteria

针对 吞吐量、响应时间、资源占用等,设定被测程序可接受的性能指标。比如:

  • 1000 个用户同时访问网站时,响应时间不超过 4s。
  • 应用崩溃前可处理器的最大并发用户数。
  • 在峰值负载的情况下,CPU、内存的使用情况
  • 测试应用在低、一般、大、超大负载情况下应用的响应时间、吞吐量。

3. 规划设计性能测试方案

对于测试指标因为人力、需求的不同,也没必要面面俱到, 根据需要和场景重点设计需要的测试类型:比如 Load Test, Endurance Test 等

4. 配置测试环境

5. 实现并运行测试

6. Analyze,Tune,Retest



Reference:
[1]: https://www.guru99.com/performance-testing.html
[2]: https://www.onpathtesting.com/blog/performance-testing-metrics
[3]: https://nbomber.com/docs/loadtesting-basics/
[4]: https://medium.com/@novyludek/virtual-users-vs-rps-77627b384127