jmeterbeanshelljsr223

How to increase a variable value by 5% and pass it in another request


How to increase a variable value by 5% in beanshell and pass it in another request. Lets say I am fetching a value using JSON extractor and saving it in a variable. I need to add 2% in this value and to use this new value in further requests. How to achieve this. Quick help is really appreciated.

I have tried with JSR223 but the actual value is having 8-10 digits after decimal. Can someone please help on this.


Solution

  • Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting so I would recommend reconsidering using Beanshell

    Example code for incrementing the variable would be something like:

    def originalValue = vars.get('originalValue') as BigDecimal
    def increasePercentage = 5
    
    def result = (originalValue + (originalValue * increasePercentage / 100))
    
    
    log.info('Result: ' + result)
    

    If you want the resulting value to not to have any decimal numbers use

    def result = (originalValue + (originalValue * increasePercentage / 100)).setScale(0, java.math.RoundingMode.HALF_UP)
    

    More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?