在本章中,我们将讨论如何使用 JMeter 创建一个测试计划来监视 web 服务器。 监测测试的用途如下:
需要监控的服务器必须是 Tomcat5 或者更高版本。本次测试,我们将监控 Tomcat 8.0.26 服务器。您可以测试任何支持 Java 扩展(JMX)的 servlet 容器。我们来编写一个测试用例来监控 Tomcat 服务器。 首先设置我们的 tomcat 服务器。
我们在启动 Tomcat 之前,先编辑用户配置文件 <TOMCAT_HOME>/conf/tomcat-users.xml 中 tomcat-users 部分,内容如下:
- <tomcat-users xmlns="http://tomcat.apache.org/xml"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
- version="1.0">
- <!--
- NOTE: By default, no user is included in the "manager-gui" role required
- to operate the "/manager/html" web application. If you wish to use this app,
- you must define such a user - the username and password are arbitrary.
- -->
- <!--
- NOTE: The sample user and role entries below are wrapped in a comment
- and thus are ignored when reading this file. Do not forget to remove
- <!.. ..> that surrounds them.
- -->
- <!--
- <role rolename="tomcat"/>
- <role rolename="role1"/>
- <user username="tomcat" password="tomcat" roles="tomcat"/>
- <user username="both" password="tomcat" roles="tomcat,role1"/>
- <user username="role1" password="tomcat" roles="role1"/>
- -->
- </tomcat-users>
下面我们给该部分添加 admin 角色,管理员,管理界面,并分配 "admin" 用户。修订后的文件如下:
- <tomcat-users>
- <role rolename="manager-gui"/>
- <role rolename="manager-script"/>
- <role rolename="manager-jmx"/>
- <role rolename="manager-status"/>
- <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status"/>
- </tomcat-users>
现在通过
Setup Tomcat Server
从上面的截图,我们可以注意几个东西:
让我们通过一个测试计划来监控 Tomcat 服务。
Add Thread Group
HTTP Authorization Manager
我们已经定义了用户,现在我们来执行,首先添加一个 HTTP 请求。
Add Sampler
要定期请求服务器的状态,需要添加一个固定定时器,它可以设置每个请求之间的时间间隔。通过选择 添加 > 计时器 > 固定定时器 向此线程组中添加计时器。
在 "线程延迟" 框中输入 5000 毫秒。一般来说,使用间隔小于 5 秒可能会增加服务器的压力。在生产环境中部署监视器之前,要了解可接受的间隔是多长时间。
Add a Constant Timer
我们需要添加的最后一个元素是监听器,我们添加两类监听器,一种将结果存储在文件中,另一种图形视图中显示结果。
Simple Data Writer
Monitor Results
保存测试计划为 monitor_test.jmx,通过 运行 > 启动 来执行这个测试计划。
结果将保存在 C:\Users\POO\Desktop\sample.csv 文件中,你也能在监视器结果中看到一个图形结果,如下所示:
View the Output
View the Output
请注意,图表两侧都有字幕。左边是百分比,右边是 Health / 非活动的。如果内存线快速上升和下降,它表示内存抖动。其中蓝色表示 "负载",黄色表示 "内存",红色表示 "线程",绿色表示 "Health"。
来源: http://www.jianshu.com/p/0698e75bfcac