🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
>[info] 图形化运行有助于调试;非图形化运行做性能测试更准确! [TOC] <br> ### 一.GUI mode 图形化界面运行 JMeter运行脚本,存放在安装目录下的bin文件夹下 对于windows环境,启动脚本文件是 `jmeter.bat` 对于linux环境,启动脚本文件是`jmeter` #### 常用的脚本 - jmeter:启动 JMeter (默认图形化界面) - jmeter-server:启动JMeter 服务模式 ### 二、Non-GUI mode( 命令行模式) 非图形化界面运行 在负载测试中,我们必须使用非图形化界面运行的模式 #### 命令选项 ```cmd D:\apache-jmeter-3.3\bin>jmeter -? _ ____ _ ____ _ _ _____ _ __ __ _____ _____ _____ ____ / \ | _ \ / \ / ___| | | | ____| | | \/ | ____|_ _| ____| _ \ / _ \ | |_) / _ \| | | |_| | _| _ | | |\/| | _| | | | _| | |_) | / ___ \| __/ ___ \ |___| _ | |___ | |_| | | | | |___ | | | |___| _ < /_/ \_\_| /_/ \_\____|_| |_|_____| \___/|_| |_|_____| |_| |_____|_| \_\ 3.3 r1808647 Copyright (c) 1999-2017 The Apache Software Foundation --? 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. "-t LAST" will load last used file -l, --logfile <argument> the file to log samples to -i, --jmeterlogconf <argument> jmeter logging configuration file (log4j2.xml) -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) -f, --forceDeleteResultFile force delete existing results files before start the test -L, --loglevel <argument>=<value> [category=]level e.g. jorphan=INFO, jmeter.util=DEBUG or com .example.foo=WARN -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 ``` #### 命令行模式运行举例 ```cmd jmeter -n -t my_test.jmx -l log.jtl -H my.proxy.server -P 8000 ``` -n: 非图形化运行 -t: 指定jmx脚本文件 -l: 指定log文件 -H: 指定代理服务器 -P: 指定代理服务器端口 ### 三、分布式运行 (1)**控制机**:也可以参与脚本的运行,同时它也担负着管理远程负载机指挥远程负载机运行的任务,并且收集远程负载机的测试结果。 (2)**负载机**:向被测应用服务器发起负载的机器。负载机首先要启动一个客户端程序(Agent:jmeter-server.bat),这样控制机才能接管负载机。控制机会把运行的脚本隐蔽地发送到远程负载机,但是如果运行的测试脚本有参数文件及依赖的jar包时,控制机并不能把它们发送到远程负载机,这种情况就需要手动拷贝了。 (3)**远程运行逻辑**: 1. 远程负载机首先启动Agent程序,待控制机连接; 2. 控制机连接上远程负载机; 3. 控制机发送指令(脚本及启动命令)启动线程; 4. 负载机运行脚本,回传状态(包括测试结果) 5. 控制机收集结果并显示。 >实例:在10.1.102.68 机器中启动jmeter 作为控制机,在10.1.102.75 机器中启动jmeter作为负载机。 #### 方式一:控制机以GUI方式运行 1. 首先在68 jmeter 的jmeter.properties 中指定"remote_hosts" (修改了jmeter.properties 文件需要重新启动JMeter才可以生效) ``` # Remote Hosts - comma delimited remote_hosts=10.1.102.75 #remote_hosts=localhost:1099,localhost:2010 ``` 2. 在75 中,启动JMeter Agent 程序,如 ![](https://box.kancloud.cn/17ac8961c98c21608186fa1f7d66b8d4_860x77.jpg) 3. 在68中,启动jmeter,做一个简单配置,如下: ![](https://box.kancloud.cn/94b40d3817cf8c46fb2e110c63753b9b_901x389.jpg) 4. 点击远程全部启动 ![](https://box.kancloud.cn/26fd8f967f7e7785e051060625f1ee7e_134x79.jpg) 如果运行正常,在75中的agent 控制台中输出如下: ![](https://box.kancloud.cn/20dc88de0a69d293cb40e1672ad0eb08_661x78.jpg) #### 方式二:控制机通过非GUI方式运行 1. 配置测试计划 ![](https://box.kancloud.cn/bc5fb5400b60aa020a42cc91c4af47e9_972x430.jpg) 2. 在75 中,启动JMeter Agent 程序,如 ![](https://box.kancloud.cn/d484de13680710be7ae78d9e636392e6_640x52.jpg) 3. 在68中, 命令行执行如下: ```cmd jmeter -n -t F:\jmeter_workspace\测试计划-远程运行实例.jmx -l F:\jmeter_workspace\remote.jtl -R 10.1.102.75 ``` 此时如果正常运行,则75中的agent 控制台中输出如下: ![](https://box.kancloud.cn/1258e559a7dfa808ba868b84b3f4bc83_769x116.jpg) ### 四、通过命令行覆盖属性配置 #### 参数选项 在运行jmeter的命令行中,可以传入指定参数(非修改jmeter.properties配置文件)可以覆盖配置文件中的参数。可用的参数选项有: -D[prop_name]=[value]   defines a java system property value. -J[prop_name]=[value]   defines a local JMeter property. -G[prop_name]=[value]   defines a JMeter property to be sent to all remote servers. -G[propertyfile]   defines a file containing JMeter properties to be sent to all remote servers. -L[category]=[priority]   overrides a logging setting, setting a particular category to the given priority level **例子**: ```cmd jmeter -Duser.dir=/home/mstover/jmeter_stuff -Jremote_hosts=127.0.0.1 -Ljmeter.engine=DEBUG ``` #### 实例:从命令行中,指定运行线程数与url 1. 设置线程数:`${__P(threadCount,)}` ![](https://box.kancloud.cn/ff5ad49ec51ee5e3dee227565ebce399_830x437.jpg) 2. 设置服务器名称:`${__property(url,,)}` ![](https://box.kancloud.cn/8fef5df188e1d4ffe3baf875ac02d003_924x431.jpg) 3. 命令行执行命令: ```cmd jmeter -n -t "mock_api .jmx" -JthreadCount=5 -Durl=10.1.102.75 ``` ### 五、命令行选项与properties配置的处理顺序 1. -p propfile 2. jmeter.properties (or the file from the -p option) is then loaded 3. -j logfile 4. Logging is initialised 5. user.properties is loaded 6. system.properties is loaded 7. all other command-line options are processed <hr style="margin-top:100px"> :-: ![](https://box.kancloud.cn/331f659e8e6cddb0d9f182e00e32803f_258x258.jpg) ***微信扫一扫,关注“python测试开发圈”,获取更多测试开发分享!***