Jmeter: Setting up and running sample test using Jenkins

Please follow this link for very nicely written tutorial for entire Jmeter series.
https://artoftesting.com/jmeter-tutorial

Official Link :
https://jmeter.apache.org/usermanual/get-started.html

However while working with Jmeter I felt the need of few important things that would help me perform some basic test and generate report, which I want to highlight here to get you all started quickly.

  1. Download Jmeter and launch the jar
  2. Create a TestPlan:
    1. By default a new test plan is opened, rename it as per the test scenario name
  3. Create a ThreadGroup:
    1. R-click on Testplan — > Add– > Threads — > Thread Group
    2. Rename the Thread group as per the number of concurrent users_ramp up time. Ex: User10_RampUpTime60Sec
  4. Create HTTP Request:
    1. R-click on ThreadGroup àsampler à HTTP Request
    2. Rename the request as per the request being called
    3. Protocol: specify https / leave blank if http
    4. Server Name/Ip: provide the BaseURL of application (appname.com/api)
    5. Method: Specify GET/POST
    6. Path:  Provide the path ( Ex: /user)
    7. Port: if any port available (Ex: 8080 )
  5. Add listener (Result ):
    1. All Listeners can be found by clicking Add->Listeners. A JMeter Listener will only collect data from JMeter elements at or above its level. If a listener is added to the script as a child element, it will only show the data related to its parent. If a listener is added under a thread group of a script that has a few thread groups, that listener will display the data of all the samplers that belong to that thread group. If you need to review reports of all the samplers in a script, place the listener at the same level of all thread groups in the script.
    2. R-click on HTTP request and add à Listener à View Results Tree
    3. Save the response : Browse the path of the result to be stored in filename field with extension of .jtl/.csv
    4. More on listeners can be found in this article:
      https://www.blazemeter.com/blog/jmeter-listeners-part-1-basic-display-formats
  6. Assertion:
    1. Add response assertion: R-click Http request àaddàAssertionàResponse assertion
      Apply to – Main sampler , Field to test – Response code , Patterns to Test – 200
    2. If you want more advanced assertion for JSON response(API)
      Add JSR223  assertion to validate.
      R-click on http request à add à Assertions à JSR223 Assertion
    3. JSR223 Assertion supports assertion using groovy/Javascript language
    4. Select language as javascript/groovy
    5. Javascript

      //Store response to var
      var responseBody = prev.getResponseDataAsString();
      log.info(responseBody)
      var jsonData = JSON.parse(responseBody);
      log.info(“Value of a field “+jsonData[0].key)

      //Save value using jsonPath expression
      var field1=jsonData[0].field1_value

      //Assert using default AssertResult utility
      if(field1!=” field1_value “){
      AssertionResult.setFailureMessage(“Test Failure”)
      AssertionResult.setFailure(true);
      }
    6. Groovy assertion script:
      https://www.blazemeter.com/blog/scripting-jmeter-assertions-in-groovy-a-tutorial
  7. Save response to file:
    1. If you want to save the response to a file, add listener , Http request à listener à save response to file.
      It will save the response to json file in jmeter/bin dir
      Check the option “Add timestamp” for saving file with timestamp
    2. Save response to CSV:
      https://medium.com/@priyank.it/responses
  8. Extract data from response:
    1. Using Extract data from response and save to variable
      First add a Debug sampler to Thread group sampler Debug sampler.
      This will help verifying response and variables in View result tree listener
      And a View result tree listener in Thread group, to capture all request info.
      If you want to extract data from XML reponse , then add post processor extractor in HTTP request à Xpath2 Extractor
    2. Provide name of variable and xpath query
      If you want to extract json value then add post processor to request à JSON Extractor
    3. Give a name to variable , json path , Match no: -1 (for all matches) , 1 for only 1 match
    4. Add a JSR223 PostProcessor to extract or execute any custom script (java/groovy/javascript)
    5. Here is an example of extracting data from response and saving to CSV file

      import org.apache.commons.io.FileUtils
      import java.text.SimpleDateFormat;

      // Get total number of matches. (Returns string)
      def resultCount = vars.get(“JsonBody_matchNr”) //This value can be get from Debug sampler response tab.
      log.info(resultCount)
      log.warn ‘Output to jmeter console ‘ +  resultCount

      // Generate timestamp to create uniue file name.
      String fileSuffix = new SimpleDateFormat(“ddMMyyyyHHmm”).format(new Date())

      // Create file Object f = new File(“results_”+fileSuffix+”.csv”)
      for (int i=1; i<=resultCount.toInteger(); i++)
      {
        // Get each matching result one by one.
      records = vars.get(“JsonBody_”+i)

      // Write result to csv file.
        FileUtils.writeStringToFile(f,records + System.getProperty(“line.separator”),true)
      }
  9. Generate HTML report:
    1. In the view result tree listener or any other listener we need to add the path to save the repost as csv or jtl file, once it is done
    2. Go to toolsà Generate HTML report
    3. Browse the jtl/csv file which was saved after execution
    4. Browser user.properties file from  C:\apache-jmeter-5.2.1\bin\user.properties
    5. Set a out directoty ( should be empty dir)
    6. Click generate report
  10. Thread Group:
    1. Number of threads: Number of users to participate in load
    2. Ramp-up period (sec): Duration to onboard the given number of threads(users) to be fully available
    3. same user on each iteration: same thread will re spawn once done
    4. Loop count: How many loops will the given threads will be executed
    5. Specify thread lifetime:
      Duration(sec): Time duration till when all the threads will be active. This duration is counted from the very 1st thread is on boarded. This needs loop count to be defined either custom number or infinite.
      ( Be aware , if you set to infinite and duration as 60 sec, it will keep looping the defined number of threads for 60 seconds, it might result in sending samples of more than 10 times than number of defined samples, as it keeps on looping…)
      More info on :
      http://www.testingjournals.com/5-must-know-features-thread-group-jmeter/
      startup delay(sec): Pause between starting new thread.
  11. Running Jmeter via command line (windows )
    1. To run jmx file + save result file as jtl + save log + generate Html report
      jmeter -n -t \Full_path_of_jmx\your_script.jmx -f -l \Full_path_of_result_file\result.jtl -j \Full_path_to_logdir\logfile.log -e -o
    2. To run jmx file + save result file as CSV + generate Html report
      jmeter -n -t \Full_path_of_jmx\your_script.jmx -f -l \Full_path_of_result_file\result.jtl -e -o \Full_path_to_report_location_should_be_empty_nonExistent
    3. To run jmx file + save result file in csv
      jmeter -n -t \Full_path_of_jmx\your_script.jmx -l \Full_path_to_report_location\csvFileFileName.csv
    4. To generate a Report from existing CSV/JTL file:
      jmeter -g \Full_path_of_result_file\result.jtl/.csv -o [path to output folder (empty or not existing)]

      jmeter -g \Full_path_of_result_file\result.jtl -o \Full_path_to_report_location_should_be_empty_nonExistent
      -n : non gui mode
      -t : specifies the path to source .jmx
      -j : log-file
      -f: overwrite the existing results file
      -l: results-file
      -p: property-file
  12. Running jmeter via Jenkins
  13. Adding performance dashboard in jenkins
    1. Add this plugin to jenkins
      https://plugins.jenkins.io/performance
    2. In the jenkins job, add post build action –
      Publish performance test report
    3. Source data files (autodetects format): Provide the path of .jtl/.csv result file
    4. After execution this is show in dashboard
  14. Running jenkins job periodically ( scheduler)
    Refer the help section of jenkins
Sample Test plan
Example of Loop count and Duration , Delay. Notice the thread start and end time.

Best practices :
https://jmeter.apache.org/usermanual/best-practices.html

  • Disable / Delete listeners while running in command line to save memory and faster execution
  • Use Full path while specifying path of result, log, html report

Leave a Reply