Apache JMeter负载测试简介

本文介绍了Apache JMeter在负载测试中的应用。通过理解负载测试与性能测试的区别,了解JMeter的工作原理,逐步学习如何创建线程组、设置配置、添加HTTP请求、创建监听器并运行测试,对一个简单的Vert.x服务器进行负载测试。
摘要由CSDN通过智能技术生成

你好! 在本文中,我想解决一个负载测试问题。 可用的最佳工具之一是Apache JMeter。 它是免费的开放源代码,并且由于其Java性质,因此可以实现跨平台。 尽管它的界面有点奇怪,所以本文还可以作为JMeter的快速入门。 在这篇文章中,我们将观察什么是负载测试以及它与性能测试有何不同; JMeter的工作方式以及如何编写测试; 最后-我们将测试一个使用纯Vert.x编写的非常简单的服务器。

What is a load testing?

在开始负载测试之前,让我们快速指出什么是负载测试本身。 首先,我们应该记住,负载测试是非功能测试:指软件的某些方面,它们可能与特定功能或用户操作无关。 我们用负载测试 in order to study app's behaviour under a specific load. We need to distinguish 负载测试 and 性能测试. We use 性能测试分析高负载下的整体应用性能。

Introduction to JMeter

In this section we would quickstart with Apache JMeter. I assume, that you already have it installed on your machine. If you don't have it, you can download it on Apache Foundation website. But keep in mind, that it requires at least Java 8 to run.

Getting started with JMeter

首先,让我们快速观察JMeter的界面。 运行它时,您将看到与以下屏幕截图类似的窗口:

JMeter UI

我已将JMeter窗口划分为3个区域:

  1. 测试计划区=这是一个基于树的结构,您可以在其中管理测试的结构,添加/删除项目(请参阅下文)工具列=此栏包含应用于测试的最重要的操作-保存/打开保存的测试,运行等。元素面积=在此部分中,您可以配置单个项目(线程组,元素等)

现在,让我们看看JMeter在测试计划结构方面向我们提供了什么。

Test plan structure

JMeter使用您应该熟悉的自己的术语。 这里的关键概念之一是测试计划。 测试计划是负载测试的基本结构,表示要测试的内容以及如何进行测试。 它可以包括线程组,用于控制JMeter在测试期间将使用的线程数。 线程组包括不同的元素, 例如采样器(用于将请求发送到服务器;例如HTTP或FTP请求),逻辑控制器(定义逻辑条件;如果/针对等),听众 (used to visualize tests output in the form of tables,graphs,trees,or simple text in some log files). These 元素 are most used,but not only ones. There also others,like 断言,configuration 元素,计时器 etc.,that you can use in your tests. Also,it is worth to remember an execution order of 测试计划 元素:

  1. 配置元素预处理器计时器取样器后处理器(除非SampleResult为null)断言(除非SampleResult为null)侦听器(除非SampleResult为null)

You can find a more detailed description of test plan elements in this Tutorialspoint tutorial. In my turn, I would not waste your time - so let move directly to the example API.

Example API

编写REST API并不是本文的主题,因此我们将使用非常简单的API。 对于此示例,我们甚至不会使用任何外部依赖项-Vert.x Core软件包绰绰有余。 我们只需要有一个虚拟端点,仅此而已。 添加此代码:

public class DummyService extends AbstractVerticle {

    private long start;

    @Override
    public void start() throws Exception {
        HttpServer server = vertx.createHttpServer();
        start = System.currentTimeMillis();
        server.requestHandler(req->{
            long stop = System.currentTimeMillis();
            long duration = stop - start;
            req.response().end("Service is running "+duration+" ms");
        });
        server.listen(8800, "localhost");
    }

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        vertx.deployVerticle(new DummyService());
    }

}

运行此命令并在浏览器中访问,以检查一切是否正常运行:

现在,我们有了一个虚拟端点,让我们使用Apache JMeter编写一个简单的负载测试。

How to do load testing with JMeter?

在本节中,我们将使用Apache JMeter创建一个简单的负载测试。 您应该执行以下步骤:

Step 1. Create new Thread Group

Create new thread group in Apache JMeter

要开始负载测试,我们需要创建新的线程组。 右键点击测试计划然后选择Add > Threads (Users) > Thread Group。 在这里我们可以指定以下参数:

  • 线程数= 10加速周期= 1循环计数= 100

Step 2. Set up configuration

Setup test configuration in Apache JMeter

下一步是为我们的负载测试设置配置。 我们的虚拟api运行为本地主机:8800,因此我们应该告诉JMeter在哪里找到它。 右键点击线程组然后选择Add > Config Element > Http Request Default。 在这里我们添加:

  • 协议= http服务器名称=本地主机端口= 8800路径= /

Step 3. Add HTTP Request

Add HTTP Request in Apache JMeter

我们现在可以添加一个取样器-在我们的情况下是HTTP请求。 我们在上一步中预定义了所有内容,因此无需在此处添加任何内容。 尽管在更复杂的测试中,当然必须指定它。 现在将所有内容留空。

Step 4. Create a listener

Create a listener in Apache JMeter

最后,添加一个听众-显示结果的元素。 去添加Add > Listener > Summary Report。 摘要报告为测试中每个不同名称的请求创建一个表行。 使用此组件,您还可以将数据另存为CSV。

Step 5. Run a test

Run Apache JMeter test

现在,您可以运行负载测试了(NB不要忘记保存测试)。 我们收到了可以解释的数据表:

  • 标签= HTTP请求采样器的名称样本=每个请求的虚拟用户数平均值=所有样本执行特定标签所花费的平均时间Min =特定标签样本所花费的最短时间。Max =特定标签样本所花费的最长时间St.Dev =一组例外情况,它们偏离了样本响应时间的平均值。错误=每个标签失败请求的百分比。吞吐量=服务器按时间单位(秒,分钟,小时)处理的请求数

I recommend you to observe the documentation for summary report or this post on understanding a summary report.

Conclusion

In this post we observed load testing of Java web applications. One of the best tools available for this is Apache JMeter. It is free, open-source and, thanks to its Java nature, cross-platform. In this post, we have checked what is a load testing and how it is different from performance testing; how JMeter works and how to compose your tests; and finally - we tested a very simple server, written with plain Vert.x Core.

References

  • Bernhard Knasmüller. How to Use JMeter to Performance Test a REST API (2019), read here
  • Mutiara Hana. Performance Testing with JMeter for Dummies (2018), Medium, read here
  • Ragnar Lonn. When should I start load testing?, TechBeacon, read here
  • Rishil Bhatt. Understand and Analyze Summary Report in Jmeter (2017), read here

from: https://dev.to//andreevich/introduction-to-load-testing-with-apache-jmeter-482a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值