Search code examples
javajmeterload-testingbeanshell

Extending JMeter Timer to delay timer variably


I'm using JMeter for my load testing. I want to extend the Timer to take in a delay in millis and then use it to delay the next sample. I am currently using BeanShell Script to accomplish this but I want to have a Java class for it rather than a Script Snippet. What Timer class should I extend and is there an example that I can look at ? I haven't found anything online :-|


Solution

  • I never had to implement a timer, but I did implement Sampler, Post-Processor and Listener, so I am making some assumptions about Timer's similarity with other components. Also I didn't ever see any good documentation about JMeter plug-ins development, I usually dig in JMeter code to understand how they do it, then check API docs that explain various functions.

    Basically you will need to implement 2 classes

    1. A class that will run a timer. That class should extend AbstractTestElement and implement Timer interface
    2. A GUI class to represent your timer in JMeter UI. That class should extend AbstractTimerGui

    So the skeleton of the plug-in would look like this:

    public class MyTimerGui extends AbstractTimerGui {
        // usually has at least the following 3 functions:
    
        @Override
        public TestElement createTestElement() {
        }
    
        @Override
        public void modifyTestElement(TestElement timer) { 
        }
    
        @Override
        public void configure(TestElement el) {
        }
    }
    
    public class MyTimer extends AbstractTestElement implements Timer {
    
        long delay() {
            return someDelayOfYourChoice;
        }
    
        // you may need some other functions, e.g. running on test start or sample start
    }
    

    In terms of examples, you can look at the built-in timers, specifically looks like ConstantTimer is good and concise example of how it's done. You could probably even directly extend ConstantTimer, like RandomTimer does for instance, instead of implementing Timer interface.

    For UI part you can also follow ConstantTimerGui example, or if your interface is closer to random timer, you could extend AbstractRandomTimerGui

    To build your plug-in, I'd recommend maven project similar to this example. And then for run-time you'd deploy it into lib/ext folder. If your plug-in uses any libraries not present in JMeter, you will also need to deploy them in lib folder of JMeter.