用Groovy在JMeter中执行命令行

2020-03-06 09:40:20 浏览数 (1)

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

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

这次再来分享一下,Groovy在JMeter中执行命令行的方法,目前我暂时想不到很多适用场景,因为的确我对JMeter并不是很熟悉,也就这两天才开始看一些资料。如果你有很好的适用场景,可以留言告诉我,一起交流交流。

  • 首先新建一个简单的线程组和一个简单的请求:
  • 添加JSR223 预处理程序(后置处理程序需要下一次次请求)

脚本内容:

代码语言:javascript复制
log.info("jps -lv".execute().text)

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

log.info("jmeter -v".execute().text)

控制台输出:

代码语言:javascript复制
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给你。


  • 郑重声明:文章首发于公众号“FunTester”,禁止第三方(腾讯云除外)转载、发表。

0 人点赞