用Groovy在JMeter中执行命令行

2022年01月15日 阅读数:5
这篇文章主要向大家介绍用Groovy在JMeter中执行命令行,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

前两期文章讲了一些Groovy和JMeter一些配合使用:java

  • 用Groovy处理JMeter断言和日志
  • 用Groovy处理JMeter变量

此次再来分享一下,Groovy在JMeter中执行命令行的方法,目前我暂时想不到不少适用场景,由于的确我对JMeter并非很熟悉,也就这两天才开始看一些资料。若是你有很好的适用场景,能够留言告诉我,一块儿交流交流。redis

  • 首先新建一个简单的线程组和一个简单的请求:

用Groovy在JMeter中执行命令行_java

  • 添加JSR223 预处理程序(后置处理程序须要下一次次请求)

用Groovy在JMeter中执行命令行_ide_02

脚本内容:app

log.info("jps -lv".execute().text)

"jps -lv".execute().text.eachLine{
log.error("222222222 ${it}")
}

log.info("jmeter -v".execute().text)
  • 控制台输出:
2020-03-04 15:25:56,544 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2020-03-04 15:25:56,550 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2020-03-04 15:25:56,551 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2020-03-04 15:25:56,905 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : 线程组
2020-03-04 15:25:56,905 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group 线程组.
2020-03-04 15:25:56,906 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2020-03-04 15:25:56,906 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 perThread=1000.0 delayedStart=false
2020-03-04 15:25:56,907 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2020-03-04 15:25:56,907 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2020-03-04 15:25:56,908 INFO o.a.j.t.JMeterThread: Thread started: 线程组 1-1
2020-03-04 15:25:57,220 INFO o.a.j.m.J.JSR223 预处理程序: 48993 -Xms512m -Xmx1500m -XX:ReservedCodeCacheSize=500m -XX:+UseCompressedOops -Dfile.encoding=UTF-8 -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -Dsun.io.useCanonCaches=false -Djava.net.preferIPv4Stack=true -Djdk.http.auth.tunneling.disabledSchemes="" -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Xverify:none -javaagent:/Users/fv/jetbrains-agent.jar -Djb.vmOptionsFile=/Users/fv/Library/Preferences/IntelliJIdea2018.3/idea.vmoptions -Didea.java.redist=jdk-bundled -Didea.paths.selector=IntelliJIdea2018.3 -Didea.executable=idea -Didea.home.path=/Applications/IntelliJ IDEA.app/Contents
49572 sun.tools.jps.Jps -Dapplication.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home -Xms8m
48524 ./ApacheJMeter.jar -Dapple.laf.useScreenMenuBar=true -Dapple.eawt.quitStrategy=CLOSE_ALL_WINDOWS
49053 org.jetbrains.idea.maven.server.RemoteMavenServer -Djava.awt.headless=true -Didea.version==2018.3.5 -Xmx768m -Didea.maven.embedder.version=3.3.9 -Dfile.encoding=UTF-8

2020-03-04 15:25:57,393 ERROR o.a.j.m.J.JSR223 预处理程序: 222222222 48993 -Xms512m -Xmx1500m -XX:ReservedCodeCacheSize=500m -XX:+UseCompressedOops -Dfile.encoding=UTF-8 -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -Dsun.io.useCanonCaches=false -Djava.net.preferIPv4Stack=true -Djdk.http.auth.tunneling.disabledSchemes="" -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Xverify:none -javaagent:/Users/fv/jetbrains-agent.jar -Djb.vmOptionsFile=/Users/fv/Library/Preferences/IntelliJIdea2018.3/idea.vmoptions -Didea.java.redist=jdk-bundled -Didea.paths.selector=IntelliJIdea2018.3 -Didea.executable=idea -Didea.home.path=/Applications/IntelliJ IDEA.app/Contents
2020-03-04 15:25:57,393 ERROR o.a.j.m.J.JSR223 预处理程序: 222222222 49573 sun.tools.jps.Jps -Dapplication.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home -Xms8m
2020-03-04 15:25:57,393 ERROR o.a.j.m.J.JSR223 预处理程序: 222222222 48524 ./ApacheJMeter.jar -Dapple.laf.useScreenMenuBar=true -Dapple.eawt.quitStrategy=CLOSE_ALL_WINDOWS
2020-03-04 15:25:57,393 ERROR o.a.j.m.J.JSR223 预处理程序: 222222222 49053 org.jetbrains.idea.maven.server.RemoteMavenServer -Djava.awt.headless=true -Didea.version==2018.3.5 -Xmx768m -Didea.maven.embedder.version=3.3.9 -Dfile.encoding=UTF-8
2020-03-04 15:25:58,421 INFO o.a.j.m.J.JSR223 预处理程序: _ ____ _ ____ _ _ _____ _ __ __ _____ _____ _____ ____
/ \ | _ \ / \ / ___| | | | ____| | | \/ | ____|_ _| ____| _ \
/ _ \ | |_) / _ \| | | |_| | _| _ | | |\/| | _| | | | _| | |_) |
/ ___ \| __/ ___ \ |___| _ | |___ | |_| | | | | |___ | | | |___| _ <
/_/ \_\_| /_/ \_\____|_| |_|_____| \___/|_| |_|_____| |_| |_____|_| \_\ 5.1 r1853635

Copyright (c) 1999-2019 The Apache Software Foundation


2020-03-04 15:25:58,424 INFO o.a.j.t.JMeterThread: Thread is done: 线程组 1-1
2020-03-04 15:25:58,424 INFO o.a.j.t.JMeterThread: Thread finished: 线程组 1-1
2020-03-04 15:25:58,424 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2020-03-04 15:25:58,425 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)

虽然不很擅长JMeter,这些功能需求主要仍是来自于测试同行同事,我顺便查一下,写一些Demo,若是你对Groovy在JMeter应用也有相似的功能需求,能够留言,我趁着JMeter知识还没在我脑海中衰退,写个Demo给你。less