Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: new link to ronnies wiki

A more recent version of this wiki page can be found here

https://github.com/Ronnie76er/jmeter-maven-plugin/wiki

This is a Maven 2 plugin that allows you to run JMeter tests as part of the build. I am using this with JChav and Continuum to automate the running of a suite of JMeter load tests, and publish the results to a web page. Sorry this is not more polished. It could use some TLC to make it more user-friendly to actually plug it into a project. I just thought I'd stick the code out there as quickly as possible and see if anyone is interested in it before I went any further with it.

...

  • Download the attached maven-jmeter-plugin-src.tar.gz. Untar it.
  • Run "mvn install" to build and install the plugin.
  • In your project's pom.xml, add a dependency on this plugin:
    No Format
    <dependency>
      <groupId>org.apache.jmeter</groupId>
      <artifactId>maven-jmeter-plugin</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    

Configure the Plugin

  • Create a src/test/jmeter directory, and place your JMeter load tests there.
  • Create a jmeter.properties file in src/test/jmeter. It's fine to just copy the default properties file from the JMeter install if you want.
  • Optionally configure includes and excludes in your pom.xml for which tests to run. If you don't, it will just run **/*.jmx. For example:
    No Format
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>maven-jmeter-plugin</artifactId>
            <configuration>
              <includes>
                <include>myTest1.jmx</include>
                <include>myTest2.jmx</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build> 
    

  • The default reports directory is 'jmeter-reports' which is created in the base folder ( usually where maven is run from )
  • Changing the reports directory : Add a reportDir setting to the plugin configuration. for example:
    No Format
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>maven-jmeter-plugin</artifactId>
            <configuration>
              <includes>
                <include>myTest1.jmx</include>
                <include>myTest2.jmx</include>
              </includes>
              <reportDir>target/jmeter-reports</reportDir>
            </configuration>
          </plugin>
        </plugins>
      </build> 
    

Executing the Plugin

  • Run "mvn org.apache.jmeter:maven-jmeter-plugin:jmeter" to run the tests.

...

Code changes to org.apache.jmeter.JMeterMojo.java:

No Format
	        private void executeTest(File test) throws MojoExecutionException {
		                /...	    cut out from mail
			try {
				                        try {
                                // This mess is necessary because the only way to know when JMeter
				                                // is done is to wait for all of the threads that it spawned to exit.
				                                new JMeter().start(args.toArray(new String[]{}));
				                                BufferedReader in = new BufferedReader(new FileReader(jmeterLog));
				                                while (!checkForEndOfTest(in)) {
					try {
						                                        try {
                                                Thread.sleep(1000);
					                                        } catch (InterruptedException e) {
						break;
					}
				}
				                                                break;
                                        }
                                }
                                in.close();
			                        } catch (ExitException e) {
				                                if (e.getCode() != 0) {
					                                        throw new MojoExecutionException("Test failed", e);
				}
			                                }
                        } finally {
				                                System.setSecurityManager(oldManager);
				                                Thread.setDefaultUncaughtExceptionHandler(oldHandler);
			}
		                        }
                } catch (IOException e) {
			                        throw new MojoExecutionException("Can't execute test", e);
		}
	}

	                }
        }

        private boolean checkForEndOfTest(BufferedReader in) throws MojoExecutionException {
		                boolean testEnded = false;
		                try {
			                        String line;
			                        while ( (line = in.readLine()) != null) {
				                                if (line.indexOf("Test has ended") != -1) {
					                                        testEnded = true;
					break;
				}
			}
		                                        break;
                                }
                        }
                } catch (IOException e) {
			                        throw new MojoExecutionException("Can't read log file", e);
		}
		                }
                return testEnded;
	}

        }