jmeter

JMeter - Utilize functions


I have the following function to generate a random MAC address:

`def macAddress() { def rnd = new Random() def mac = new StringBuilder()

(1..6).each {
    int value = rnd.nextInt(256)
    def hexStr = Integer.toHexString(value).padLeft(2, '0')
    mac.append(hexStr)
    if (it < 6) {
        mac.append(':')
    }
}

return mac.toString()

}`

How do I evaluate this function? Where do I view the output of this function?

What components should I use to consume this function? Please help.

Thanks for your support.

Regards, Ajith


Solution

  • If you're using this code in one of the JSR223 Test Elements with Groovy language you could add the following line at the bottom:

    vars.put('mac', macAddress())
    

    Once done you should be able to access the random MAC address as ${mac} where required

    In the above code vars stands for JMeterVariables class instance, see the JavaDoc for comprehensive information about all functions and fields