2
votes

I'm trying to use Jenkins to automate performance testing with JMeter, each build is a single JMeter test and I want to increase the number of users(threads) for each Jenkins build if the previous was successful.

I have configured most of the build, with SSH plugin I can restart Tomcat, copy catalina.out, with publishing performance I can open the .jtl file and determine if the build was successful.

What I want is to execute a different batch command for the next build(to increase the number of users(threads) and user id's) For example:

jmeter -Jthreads=10 -n -t C:\TestScripts\script.jmx -l C:\TestScripts\Jenkins.jtl

jmeter -Jthreads=20 -n -t C:\TestScripts\script.jmx -l C:\TestScripts\Jenkins.jtl

jmeter -Jthreads=30 -n -t C:\TestScripts\script.jmx -l C:\TestScripts\Jenkins.jtl...

Is there some good jmeter plugin some counter that i can use to increase some variable by 10 each time:

jmeter -Jthreads=%variable1%...

I have tried by setting environmental variables and then incrementing that variable by:

"SET /A thread+=10"

but it doesn't change that variable because jenkins opens its own CMD, a new process :

("cmd /c call C:\WINDOWS\TEMP\jenkins556482303577128680.bat")

4

4 Answers

0
votes

Use the following SET command to increase threads variable by 10:

SET /A threads=threads+10

Or inside double quotes:

SET /A "threads+=10"
0
votes

Not knowing your Jenkins configuration, and which plugins you have installed and how do you run the test it is quite hard to come up with the best solution.

The only "universal" workaround I can think of is writing the current number of threads into a file in Jenkins workspace and reading the value of threads from the file on next execution.

  1. Add setUp Thread Group to your Test Plan
  2. Add JSR223 Sampler to your Thread Group
  3. Put the following Groovy code into "Script" area:

    import org.apache.jmeter.threads.ThreadGroup
    import org.apache.jorphan.collections.SearchByClass
    import org.apache.commons.io.FileUtils
    
    SampleResult.setIgnore()
    
    def file = new File(System.getenv('WORKSPACE') + System.getProperty('file.separator') + 'threads.number')
    
    if (file.exists()) {
    
    
        def newThreadNum = (FileUtils.readFileToString(file, 'UTF-8') as int) + 10
        FileUtils.writeStringToFile(file, newThreadNum as String)
    
        def engine = ctx.getEngine()
        def test = org.apache.commons.lang.reflect.FieldUtils.getField(engine.getClass(), 'test', true)
        def testPlanTree = test.get(engine)
    
        SearchByClass<ThreadGroup> threadGroupSearch = new SearchByClass<>(ThreadGroup.class)
        testPlanTree.traverse(threadGroupSearch)
        def threadGroups = threadGroupSearch.getSearchResults()
        threadGroups.each {
            it.setNumThreads(newThreadNum)
        }
    } else {
        FileUtils.writeStringToFile(file, props.get('threads'))
    }
    

    The code will write down the current number of threads in all Thread Groups into a file called threads.number in Jenkins Workspace and on subsequent runs it reads the value from it, adds 10 and writes it back.

0
votes

For now i am creating 20 .jmx files (1.jmx, 2.jmx , 3.jmx ...) each whith a different number of users. and calling them whit this command :

jmeter -n -t C:\TestScripts\%BUILD_NUMBER%.jmx -l C:\TestScripts\%BUILD_NUMBER%.jtl

the first billd will call 1.jmx the second 2.jmx ... it isn't the best method but it works for now. I will try your advice over the weekend when i have more time.

0
votes

i have found the a solution that works for me, it inst pretty. I created a python script which changes a .CVS fil from which JMeter reads the number of threads and the starting user id. This python script incremets the starting user id by the number of threads in the previous bild and the number of threads by 10

file = open('C:\\Users\\mp\\AppData\\Local\\Programs\\Python\\Python37-32\\eggs.csv', 'r') 
a,b=file.readlines()[0].split(",")
print(a,b)
b=int(b)
a=int(a)
b=a+b
a=a+10
print(a,b)
f = open("C:\\Users\\mp\\AppData\\Local\\Programs\\Python\\Python37-32\\eggs2.csv", "a")
f.write(str(a)+","+str(b))
f.close()

I have python on my pc and a i am calling the script in Jenkins as a windows Bach command

C:\Users\mp\AppData\Local\Programs\Python\Python37-32\python.exe C:\Users\mp\AppData\Local\Programs\Python\Python37-32\rename_write_file.py

I am much better in python than Java so I implemented this in Python. So for each new test,the CSV file from which jmeter reads values is changed.