如何通过Gradle任务运行JMeter GUI?

时间:2018-05-07 00:49:57

标签: gradle jmeter

我想从我的Gradle脚本运行JMeter GUI,以便我可以仔细控制版本和环境因素,因此我的本地开发人员机器构建 1没有先决条件设置

我的JMeter的build.gradle(多项目构建的一部分):

plugins{
  id 'base'
  id 'java'
}

repositories {
  jcenter()
}

dependencies {
  compile 'org.apache.jmeter:ApacheJMeter:4.0'
}

task jmeterGui(type: JavaExec){
  workingDir = "$project.buildDir/jmeter-working-dir"
  classpath = sourceSets.main.runtimeClasspath

  main = "org.apache.jmeter.NewDriver"

  doFirst{
    println "running Jmeter from Gradle"
    mkdir workingDir

  }
}

这会导致错误:

> Task :functional-test:jmeterGui
java.lang.Throwable: Could not access <source root>\functional-test\build\lib
  at org.apache.jmeter.NewDriver.<clinit>(NewDriver.java:102)
java.lang.Throwable: Could not access <source root>\functional-test\build\lib\ext
  at org.apache.jmeter.NewDriver.<clinit>(NewDriver.java:102)
java.lang.Throwable: Could not access <source root>\functional-test\build\lib\junit
  at org.apache.jmeter.NewDriver.<clinit>(NewDriver.java:102)
java.lang.ClassNotFoundException: org.apache.jmeter.JMeter
  at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  at org.apache.jmeter.NewDriver.main(NewDriver.java:242)
JMeter home directory was detected as: <source root>\functional-test\build

我无法在JMeter用户手册中找到任何内容。它可行吗?

编辑:我启动的这个GUI仅用于编辑测试计划。我使用Redline13来运行实际的负载测试。

1 Currenly在我的构建中,所有内容都是严格版本控制的,并且是Gradle构建的一部分。这包括NodeJS,NPM和Terraform。从字面上看,唯一的设置先决条件是Java 8 JDK。我想用JMeter继续这种方法。

2 个答案:

答案 0 :(得分:1)

这是我目前从Gradle运行JMeter GUI的最佳尝试。请注意,GUI启动,但几乎不起作用(甚至无法打开测试计划)。

dependencies {
  // using the runtime classpath instead of compile in order to avoid
  // downloading all these and their deps in places where we never intend
  // to run the JMeter GUI (continuous build environments, etc.)
  runtime 'org.apache.jmeter:ApacheJMeter:4.0'
  runtime 'org.apache.jmeter:jorphan:4.0'
  runtime 'org.apache.jmeter:ApacheJMeter_core:4.0'
  runtime 'org.apache.jmeter:ApacheJMeter_components:4.0'
  runtime 'org.apache.jmeter:ApacheJMeter_config:4.0'
  runtime 'org.apache.jmeter:ApacheJMeter_functions:4.0'
  runtime 'org.apache.jmeter:ApacheJMeter_java:4.0'
  runtime 'org.apache.jmeter:ApacheJMeter_http:4.0'
  runtime 'org.apache.jmeter:ApacheJMeter_tcp:4.0'
  runtime 'org.apache.jmeter:ApacheJMeter_parent:4.0'
}

task launchJmeterGui(type: JavaExec){
  description = "Launch the JMeter GUI for editin test plans"
  workingDir = "$project.buildDir/jmeter-working-dir"
  classpath = sourceSets.main.runtimeClasspath

  // This is the main class that the jmeter.bat file invokes.
  // I think this thing is specifically intended to be invoked from a batch/
  // shell script and is not intended to be invoked like this.
  main = "org.apache.jmeter.NewDriver"

  // I think this controls where the log file goes, not sure.
  args "--homedir=${workingDir.absolutePath}"

  doFirst{
    println "Launching Jmeter from Gradle"

    // log file ends up in here
    mkdir workingDir

    // I don't put anything in here, but there will be errors in the console
    // output if they don't exist.  I think this is where the darcula jar
    // needs to be in order to work?
    mkdir "$buildDir/lib"
    mkdir "$buildDir/lib/ext"
    mkdir "$buildDir/lib/junit"

    // Copied into the source tree from a JMeter 4.0 install.
    // Will run without these files but will print errors to console/log file.
    // I'm happy to put these under source control, that makes sense to me.
    project.copy {
      from "src/test/resources/jmeter.properties"
      from "src/test/resources/log4j2.xml"
      into "$buildDir/bin"
    }

  }
}

答案 1 :(得分:0)

在尝试在运行时使用时,您正在添加编译时依赖项。

更改此行:

compile 'org.apache.jmeter:ApacheJMeter:4.0'

到这一个:

runtime group: 'org.apache.jmeter', name: 'ApacheJMeter', version: '4.0'

这应该可以解决您的错误。

参考文献:

请注意,JMeter GUI用于测试开发和调试,当涉及到加载测试执行时,建议run JMeter in non-GUI mode,因此我强烈建议您重新考虑此“GUI”步骤。