Using Groovy in JMeter
Note: This is a copy from my old blog posted in 2007. Some things mentioned here have been changed since then.
Groovy is a great scripting language intimately integrated with Java. Being able to leverage any code written in Java and at the same time having all the advantages of dynamic programming language, makes it an excellent choice for tasks such as file management, build systems and testing.
JMeter is an open source load test tool. It supports many protocols (HTTP, FTP, JMS and others) out-of-the-box.
Many times I found myself in situation when I wanted to run load test against stand-alone Java components or against an application that uses proprietary binary TCP based protocol. Groovy would be an excellent choice for this task. Some work on integrating JMeter and Groovy was done by Alexandru Popescu, however the project is not available to the public yet. Then I discovered that there is an indirect support for groovy in JMeter itself. JMeter supports BSF, which is an abstraction layer on top of many scripting languages including groovy. BSF distribution that comes with JMeter actually has some glitch in groovy support, but after upgrading to the latest version of bsf.jar it was very easy to start writing JMeter scripts in groovy.
Here the steps I followed:
- download latest groovy binaries from http://groovy.codehaus.org/
- copy groovy-all-1.0.jar to jmeter/lib directory
- download latest bsf binaries from http://jakarta.apache.org/bsf/ (I was using version 2.4.0)
- replace bsf-2.3.0.jar in jmeter/lib with bsf.jar from the version you just downloaded
- start jmeter
- create BSF Sampler and specify "groovy" as a scripting language (see screenshot below)
- type your groovy script and run it
This method is far from being perfect. It actually monitors how much time it takes to execute the script itself, but in most cases you’d like to specify exactly when to start counting execution time [results.sampleStart(); results.samplePause(); results.sampleResume(); results.sampleEnd();] I hope Alexandru will make his project available soon, or may be I’ll find some time to create more sophisticated JMeter Groovy plug-in.
Another issue is how to transfer data between several scripts. Let’s say you’d like to create 1st script that establishes TCP connection; 2nd that sends proprietary login message and gets back some security token; 3rd that sends some business message (using the same TCP connection and providing the token) and 4th that just disconnects. How to transfer Socket and Token objects between scripts? Well, you can use ThreadLocal. Following code snippet defines ThreadLocalStorage class that can be used from the scripts (isn’t it great that you can just call Java from Groovy?) to save and retrieve any data:
Just package it as a jar file and put into jmeter/lib directory. Now you can write your scripts like this:
Connect:
Echo Message
Disconnect