美文网首页我爱编程
Jmeter&Apache&LR.VS.RPT

Jmeter&Apache&LR.VS.RPT

作者: 燕京博士 | 来源:发表于2017-10-25 15:57 被阅读955次

    在使用Jmeter进行接口的性能测试时,由于Jmeter 是JAVA应用,对于CPU和内存的消耗比较大,所以,当需要模拟数以万计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至会引起JAVA内存溢出错误。为了让jmeter工具提供更大的负载能力,这时可以使用Jmeter提供的分布式功能来启动多台电脑来分压测试。
    熟悉 LoadRunner 的朋友一定不会对其 TPS(每秒事务数)、TRT(事务响应时间) 等视图感到陌生,因为这是压力测试最为关键的两个指标。JMeter 以其开源、轻巧、灵活、扩展性高等特性赢得了广大测试从业人员的青睐,但是它没有像 LoadRunner 那样提供一个 TPS、TRT 之类的视图,虽然聚合报告也能说明一些整体以及实时的问题,但是就整体趋势、实时平均值走向、稳定性报告方面就无能无力了,见下图:

    JMeter没有TPS结果输出界面.jpg 所幸的是,jmeter-plugins.org 这个网站为 JMeter 提供了一些增强型功能的插件,使用起来就像 Eclipse 装插件一样,完全做到了插件的可插拔特性。本文简要介绍如何使用这些插件让你的 JMeter 支持 TPS、TRT 视图。本文背景:JDK:1.7.0,
    JMeter3.0,
    OS:Win7 旗舰版。
    http://www.cnblogs.com/Lam7/p/6833501.html
    http://www.mamicode.com/info-detail-1394555.html
    http://m.blog.csdn.net/DearMorning/article/details/71124175
    http://www.cnblogs.com/wnfindbug/p/5505470.html
    1. 插件下载
      下载地址:https://jmeter-plugins.org/wiki/TransactionsPerSecond/,该插件最新版本为 2.0,大小 913 KB。笔者上传了一份最新版本的 jpgc-graphs-basic-2.0.zip,有兴趣的朋友可以去下载,下载地址:JMeter 每秒事务数 TPS 插件。2. 解压安装
      将 jpgc-graphs-basic-2.0.zip 解压缩后只有一个 lib 目录,该目录下有一个 ext 文件夹和一个 jmeter-plugins-cmn-jmeter-0.3.jar 包,ext 文件夹中有 jmeter-plugins-graphs-basic-2.0.jar 和 jmeter-plugins-manager-0.10.jar 包。将 lib 目录下的 jmeter-plugins-cmn-jmeter-0.3.jar 拷贝到 %JMeter%/lib 目录下,将 ext 目录下的 jmeter-plugins-graphs-basic-2.0.jar 和 jmeter-plugins-manager-0.10.jar 拷贝到 %JMeter%/lib/ext 目录下,重启 JMeter,发现已经支持 TPS、TRT 等视图了: 已经支持TPS了.png 3. 压测使用
      3.1. 添加 TPS 视图
      线程组 -> 添加 -> 监听器 -> jp@gc Transaction per Second。某次压测实际效果图: TPS实际效果图.png 可以看出在该段时间内 TPS 大约维持在 6300 个左右。3.2. 添加 TRT 视图
      线程组 -> 添加 -> 监听器 -> jp@gc Response Times Over Time。某次压测实际效果图: TRT实际效果图.png 可以看出在该段时间内 TRT 平均在 70ms 左右。
      http://www.cnblogs.com/whitewasher/p/6946207.html
      一、Jmeter分布式执行原理:
        1、Jmeter分布式测试时,选择其中一台作为控制机(Controller),其它机器做为代理机(Agent)。
        2、执行时,Controller会把脚本发送到每台Agent上,Agent 拿到脚本后开始执行,Agent执行时不需要启动Jmeter,只需要把jmeter-server.bat文件打开,它应该是通过命令行模式来执行的。
        3、执行后,Agent会把结果回传给Controller,Controller会收集所有Agent的信息并汇总。

    二、代理机(Agent)配置:
    1、Agent机上需要安装JDK、Jmeter,并且配置好环境变量。
    2、打开“运行”,输入"cmd",打开运行面板,输入“ipconfig”,找到IP地址,例如是:192.168.8.149

    3、打开Jmeter/bin/jmeter.properties,找到”remote_hosts=127.0.0.1”,把这一行修改为”remote_hosts=192.168.8.149:1099,1099是端口号,可以随意自定义。


    4、打开jmeter-server.bat文件,就设置完成了,等待控制机(Controller)启动。

    三、控制机(Controller)配置:
    1、Controller机上需要安装JDK、Jmeter,并且配置好环境变量。
    2、打开“运行”,输入"cmd",打开运行面板,输入“ipconfig”,找到IP地址,例如是:192.168.8.174
      


    3、打开Jmeter/bin/jmeter.properties,找到”remote_hosts=127.0.0.1”,把这一行修改为”remote_hosts=192.168.8.149:1099,192.168.8.174:1099,1099是端口号,可以随意自定义。如果有多台代理机,这里需要把所有的代理机的IP地址和端口号都加入进来。

    4、打开jmeter-server.bat文件,设置完成了。

    四、开始添加线程组和请求来运行查看结果:
    1、打开jmeter.bat文件,添加线程组,编辑线程数,这里设置100个线程数,循环2次,就是一台机器发送100*2=200个请求。

    2、添加HTTP请求,这里以访问淘宝为例。


    3、添加察看结果数和聚合报告,点击运行,可以选择远程启动或者远程全部启动,如果是点击远程启动,可以选择任意一台电脑来运行,如果是点击远程全部启动就会运行控制机和所有的代理机。


    4、这里以点击远程全部启动为例。运行结束后,查看聚合报告,每台电脑设置的线程数为200,这里一共是两台电脑,所以是200*2=400个线程数。


    进行Web的压力测试
    JMeter中最小的单位就是元件.你可以给你的测试计划中增加若干的元件,每一个元件其实就是一个步骤.JMeter中设置了各种不同的元件:有设置用户的,有设置定时器的,有设置前后置处理的,有设置断言的还有设置监听器的.通过这些不同的元件的组合,我们就能很很容易的组合出更多不同的压力测试用例.
    我们以最简单的Web服务器的压力测试为例,来演示下如何使用JMeter设置测试用例.
    设置线程组(模拟用户)
    压力测试不同于功能测试,软件的正确性并不是它的测试重点。它所看重的是软件的执行效率,尤其是短时间内访问用户数爆炸性增长时软件的响应速度.因此就需要同时模拟多个用户对系统进行请求.因此,一般设置测试计划的第一步都是创建一个线程组,用来模拟多个用户的操作.

    在这里我只增加了一个最简单的通过响应代码来判断请求是否正确.

    100个并发打压2分钟.png

    HTTP请求,填写IP,端口,请求方法,路径,参数名称和值


    HTTP请求.png

    以上截图中的参数名称是接口文档里面定义的,值是我们设定的。设定参数值的方法很多,第一个方法 是在文件中取值,比如第一个参数 UserID 是在一个文件中去的,如果在文件中取值需要添加 配置元件(CSV Date Set Config)进行参数化,如下图:


    配置元件.png

    取参数也可以通过函数 动态生成数据,如 ${__RandomString(8,324YFHDDN0098432U2J32EWWDDYEHD,)}指在后面的字符中随机取8个数字。
    查看结果树和聚合报告是用来查看执行计划是否成功以及各项指标的。


    保存测试计划.png

    最后把跑通的脚本保存为 .jmx文件。
    2.在linux环境执行测试计划(打压)
    把脚本上传到 linxu环境,可以在脚本里面直接修改参数(并发数、运行时间、参数文件的位置)在 jmeter 的bin目录下执行测试计划,执行命令如下;
    jmeter -n -t ncindex-collect.jmx -l result.jtl -e -o ResultReport#ncindex-collect.jmx是脚本名字,result.jtl 是生成的日志文件,ResultReport是生成的报告目录· -h 帮助 -> 打印出有用的信息并退出· -n 非 GUI 模式 -> 在非 GUI 模式下运行 JMeter· -t 测试文件 -> 要运行的 JMeter 测试脚本文件· -l 日志文件 -> 记录结果的文件· -r 远程执行 -> 启动远程服务· -H 代理主机 -> 设置 JMeter 使用的代理主机· -P 代理端口 -> 设置 JMeter 使用的代理主机的端口号

    执行命令后还需要观察打压过程是否有报错,监控linux服务器的cpu 、内存、负载等。


    跑脚本的过程.png

    服务器性能监控.png

    probe监控应用耗费内存.png

    如果脚本过程有报错,还要去监控应用的日志,我在打压的时候应用日志就报了内存泄露;


    应用日志报错.png

    这时候需要分析内存泄露在什么地方,什么地方占用内存,执行命令:
    jmap -dump:format=b,file=mem.dat PID

    dump下来的文件需要用工具分析,具体使用工具 Memory Analyzer,是一个eclipse插件 ,也可以单独使用,安装以及使用方法见 :MAT Memory Analyzer Tool 插件安装(图解) 性能分析工具之-- Eclipse Memory Analyzer tool(MAT)(二)使用 Eclipse Memory Analyzer 进行堆转储文件分析分析后会生成的报告见:

    转储分析报告.png

    大体意思就是 大部分的内存泄露是因为 "java.util.concurrent.ConcurrentHashMap$Node[]" 还可以看具体报告的细节,虽然看不大懂,但是知道肯定是代码引起的。打压过程还有个很奇怪的现象就是打压完成后 内存和cpu好久都下不来,这明显是不正常的。对比图如下:

    Paste_Image.png

    后来就把报告发给研发分析,也发给我们经理看了下,最终他们给出的结果是 被压的页面没有关闭session,如下修改:
    <%@ page session="false" contentType="text/html;charset=UTF-8" language="java"%>

    后来关闭后再打压果然不报错了,老大说这种这种问题很常见,不得不感叹经验很重要呀!
    最后展示一下某个接口的打压情况:


    接口打压统计.png

    接口打压统计.png

    现在打压出了接口的TPS,但是我还不知道要根据这个TPS怎么判断出使用几台服务器,周一把数据汇报给经理再确定。和老大商讨后的结果:根据现网10W认证用户可以算出:


    现网目前的TPS.png

    就算用户增加到200W,算出来的 是:600多,但是打压出来的系统能力远不止这些,所以目前2台服务器就可以支撑了。

    ################################################################
    Jmeter之http性能测试实战 非GUI模式压测 NON-GUI模式 结果解析TPS——干货(十一)

    性能测试计划
    性能测试用例
    录制脚本
    性能测试结果
    性能测试报告
    性能测试监控报告

    准备工作
    从脚本已录制成功之后开始进行压测
    安装Jmeter拓展插件 查看 Transactions per Second* https://jmeter-plugins.org/wiki/TransactionsPerSecond/ ←插件地址*
    在压测前先安装 Nmon监控工具在服务器linux性能监控分析及通过nmon_analyse生成分析报表

    PS :安装 TPS教程

    解压安装
    将 jpgc-graphs-basic-2.0.zip 解压缩后只有一个 lib 目录,该目录下有一个 ext 文件夹和一个 jmeter-plugins-cmn-jmeter-0.3.jar 包,ext 文件夹中有 jmeter-plugins-graphs-basic-2.0.jar 和 jmeter-plugins-manager-0.10.jar 包。将 lib 目录下的 jmeter-plugins-cmn-jmeter-0.3.jar 拷贝到 %JMeter%/lib 目录下,将 ext 目录下的 jmeter-plugins-graphs-basic-2.0.jar 和 jmeter-plugins-manager-0.10.jar 拷贝到 %JMeter%/lib/ext 目录下,重启 JMeter,发现已经支持 TPS、TRT 等视图了:


    TPS导入解析测试报告

    导入之后就可以看到 TPS数据了


    取至Jmeter官方文档
    1.0.2 Load Test running
    Once your Test Plan is ready, you can start your Load Test. The first step is to configure the injectors that will run JMeter, this as for any other Load Testing tool includes:
    Correct machine sizing in terms of CPU, memory and network
    OS Tuning
    Java setup: Ensure you install the latest version of Java supported by JMeter
    Correct sizing of Java Heap. By default JMeter runs with a heap of 512MB, this might not be enough for your test and depends on your test plan and number of threads you want to run

    Once everything is ready, you will use Command-line mode (called Non-GUI mode) to run it for the Load Test.
    Don't run load test using GUI mode !
    不要使用GUI模式运行负载测试!因为GUI模式的话会占用比较大的内存空间,并发数量上不去等等的问题

    Using Non-GUI mode, you can generate a CSV (or XML) file containing results and have JMeter generate an HTML report at end of Load Test. JMeter will by default provide a summary of load test while it's running. You can also have real-time results during your test using Backend Listener.

    NON-GUI模式允许得到结果后可以在后端监听器的测试期间获得实时结果。这句话如何理解呢,见上面“TPS导入解析测试报告

    Running JMeter
    To run Apache JMeter in NON_GUI
    直接进入脚本路径,输入 Jmeter的启动路径
    可以查看到Jmeter的相关命令帮助
    小七的Jmeter路径是安装在D:\Jmeter\jmeter-3.1 所以直接进入bin目录下 找到jmeter 查看H帮助文档


    C:\Users\lamw\Desktop\lamwλ D:\Jmeter\jmeter-3.1\bin\jmeter -hWriting log file to: C:\Users\lamw\Desktop\lamw\jmeter.log
    _ ____ _ ____ _ _ _____ _ __ __ _____ _____ _____ ____ / \ | _ \ / \ / | | | | ____| | | / | ____| | ____| _ \ / _ \ | |) / _ | | | || | | _ | | |/| | | | | | | | |) | / ___ | / ___ \ || _ | |_ | || | | | | |__ | | | || _ <// __| // __|| ||_____| _/|| ||_____| || |_____|| _\ 3.1 r1770033

    Copyright (c) 1999-2016 The Apache Software Foundation
    To list all command line options, open a command prompt and type:
    jmeter.bat(Windows)/jmeter.sh(Linux) -?


    To run Apache JMeter in GUI mode, open a command prompt and type:
    jmeter.bat(Windows)/jmeter.sh(Linux) [-p property-file]


    To run Apache JMeter in NON_GUI mode:Open a command prompt (or Unix shell) and type:
    jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l results-file] [-j log-file]


    To run Apache JMeter in NON_GUI mode and generate a report at end :Open a command prompt (or Unix shell) and type:
    jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l results-file] [-j log-file] -e -o [Path to output folder]
    --------------------------------------------------To generate a Report from existing CSV file:Open a command prompt (or Unix shell) and type:
    jmeter.bat(Windows)/jmeter.sh(Linux) -g [csv results file] -o [path to output folder (empty or not existing)]


    To tell Apache JMeter to use a proxy server:Open a command prompt and type:
    jmeter.bat(Windows)/jmeter.sh(Linux) -H [your.proxy.server] -P [your proxy server port]


    To run Apache JMeter in server mode:Open a command prompt and type:
    jmeter-server.bat(Windows)/jmeter-server(Linux)


    上面包含了一些参数 可以输入 -l 查看


    复制代码

    C:\Users\lamw\Desktop\lamwλ D:\Jmeter\jmeter-3.1\bin\jmeter -lUsage --? print command line options and exit -h, --help print usage information and exit -v, --version print the version information and exit -p, --propfile <argument> the jmeter property file to use -q, --addprop <argument> additional JMeter property file(s) -t, --testfile <argument> the jmeter test(.jmx) file to run -l, --logfile <argument> the file to log samples to -j, --jmeterlogfile <argument> jmeter run log file (jmeter.log) -n, --nongui run JMeter in nongui mode -s, --server run the JMeter server -H, --proxyHost <argument> Set a proxy server for JMeter to use -P, --proxyPort <argument> Set proxy server port for JMeter to use -N, --nonProxyHosts <argument> Set nonproxy host list (e.g. *.apache.org|localhost) -u, --username <argument> Set username for proxy server that JMeter is to use -a, --password <argument> Set password for proxy server that JMeter is to use -J, --jmeterproperty <argument>=<value> Define additional JMeter properties -G, --globalproperty <argument>=<value> Define Global properties (sent to servers) e.g. -Gport=123 or -Gglobal.properties -D, --systemproperty <argument>=<value> Define additional system properties -S, --systemPropertyFile <argument> additional system property file(s) -L, --loglevel <argument>=<value> [category=]level e.g. jorphan=INFO or jmeter.util=DEBUG -r, --runremote Start remote servers (as defined in remote_hosts) -R, --remotestart <argument> Start these remote servers (overrides remote_hosts) -d, --homedir <argument> the jmeter home directory to use -X, --remoteexit Exit the remote servers at end of test (non-GUI) -g, --reportonly <argument> generate report dashboard only, from a test results file -e, --reportatendofloadtests generate report dashboard after load test -o, --reportoutputfolder <argument> output folder for report dashboard


    复制代码

    下面我们就采用 NON_GUI模式执行脚本 如下:


    复制代码

    --------------------------------------------------To run Apache JMeter in NON_GUI mode and generate a report at end :Open a command prompt (or Unix shell) and type:jmeter.bat(Windows)/jmeter.sh(Linux) -n -t test-file [-p property-file] [-l results-file] [-j log-file] -e -o [Path to output folder]--------------------------------------------------


    复制代码 复制代码

    输入以下命令直接执行录制好的脚本文件C:\Users\lamw\Desktop\lamwλ D:\Jmeter\jmeter-3.1\bin\jmeter.bat -n -t test.jmx -l test_report_01.csv -e -o test_report_01命令解析:
    C:\Users\lamw\Desktop\lamw 这个是测试脚本所在的文件目录


    D:\Jmeter\jmeter-3.1\bin\jmeter.bat --------Jmeter.bat 的执行路径
    -n, --nongui -------------以nongui模式执行run JMeter in nongui mode

    -t, --testfile <argument>-------------压测脚本文件jmxthe jmeter test(.jmx) file to run
    -l, --logfile <argument>-------------将样本记录到的文件the file to log samples to
    -e, --reportatendofloadtests------------ 在加载测试后生成报告仪表板generate report dashboard after load test-o, --reportoutputfolder <argument>---------------- 报告仪表板的输出文件夹output folder for report dashboard

    复制代码

    运行脚本进行压测



    当然,在运行脚本进行压测的时候 需要在服务器 先执行 采集命令
    ps -ef | grep nmon 查看采集数据的进程
    [root@lamw /home/lam7/nmon]# ps -ef | grep nmon
    [root@lamw /home/lam7/nmon]# nmon -f -s 1 -c 60

    -f 表示生成的数据文件名中有时间;
    -t 输出中包括占用率较高的进程;
    -s 1 表示每 1 秒采集一次数据;
    -c 60 表示采集 60 次,1s*60=60秒;
    至于监控的方式可以查看上面所讲的准备工作第三点
    .

    相关文章

      网友评论

        本文标题:Jmeter&Apache&LR.VS.RPT

        本文链接:https://www.haomeiwen.com/subject/hnwqpxtx.html