Apache Jmeter
Total Page:16
File Type:pdf, Size:1020Kb
Apache JMeter #jmeter Table of Contents About 1 Chapter 1: Getting started with Apache JMeter 2 Remarks 2 Versions 2 Examples 3 Installation or Setup 3 Overview of Apache JMeter components at high level 4 Chapter 2: Apache JMeter Correlations 7 Introduction 7 Examples 7 Correlation Using the Regular Expression Extractor in Apache JMeter 7 Correlation Using the XPath Extractor in JMeter 12 Correlation Using the CSS/JQuery Extractor in JMeter 14 Correlation Using the JSON Extractor 17 Automated Correlation by Using BlazeMeter’s ‘SmartJMX’ 20 Chapter 3: Apache JMeter parameterization 24 Introduction 24 Examples 24 Parameterization using external files 24 Parameterization using databases 31 Parameterization using the ‘Parameterized Controller’ plugin 41 Chapter 4: Apache JMeter: Test scenario recording 47 Introduction 47 Examples 47 Script Recording with the JMeter Template Feature 47 Script Recording with the JMeter Proxy Recorder 49 Recording Performance Scripts for Mobile Devices 52 Recording HTTPS Traffic 53 Script Recording with the BlazeMeter Chrome Extension 56 Script Recording with BadBoy 58 Credits 60 About You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: apache-jmeter It is an unofficial and free Apache JMeter ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Apache JMeter. The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners. Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected] https://riptutorial.com/ 1 Chapter 1: Getting started with Apache JMeter Remarks JMeter is a Load-Testing Tool used for Performance Testing. A Performance Tester can record actions in a web browser or manually build a script which can then be run with hundreds or thousands of users. JMeter can be used to create incredibly dynamic users and scenarios using its various elements. For instance, the CSV Data Set Config can be used to specify a set of users to log into a web application. The Regular Expression Extractor or the CSS/JQuery Extractor can be used to save session ids to be used in future requests. The JSR223 PreProcessor coupled to Groovy language can be used to create dynamic unique data for each user to be sent as part of a POST body. Versions Version Java Version Release Date 3.2 Java 8+ 2017-04-14 3.1 Java 7+ 2016-11-20 3.0 Java 7+ 2016-05-17 2.13 Java 6+ 2015-03-13 2.12 Java 6+ 2014-11-10 2.11 Java 6+ 2014-01-05 2.10 Java 6+ 2013-10-21 2.9 Java 6+ 2013-01-28 2.8 Java 5+ 2012-10-06 2.7 Java 5+ 2012-05-27 2.6 Java 5+ 2012-02-01 2.5.1 Java 5+ 2011-10-03 2.5 Java 5+ 2011-08-17 2.4 Java 5+ 2010-07-12 https://riptutorial.com/ 2 Version Java Version Release Date 2.3.4 Java 1.4+ 2009-06-21 Examples Installation or Setup 1. Download a distributed archive from Binaries section of JMeter from Download Apache JMeter page. 2. Depending on the version you downloaded, check minimal Java version requirements and install Java if needed. Ensure the JAVA_HOME environment variable is set and points to a correct version. 3. Extract the distribution archive in the directory of your choice. 4. Open JMeter UI: • On Windows: navigate to <jmeter_location>\bin directory and run jmeterw.bat or jmeter.bat • On Linux/Mac: navigate to <jmeter_location>/bin directory and run jmeter or 'jmeter.sh`. For example: cd /Users/me/apache-jmeter/bin ./jmeter Note: if the above command fails with Permission denied error, set execute permission on jmeter file: cd /Users/me/apache-jmeter/bin chmod u+x ./jmeter If you are able to see JMeter UI, basic setup was successful. https://riptutorial.com/ 3 Overview of Apache JMeter components at high level Apache JMeter segregated all the components into following groups based on their functionality: 1. Test Plan: Starting point for scripting. JMeter saves the Test Plan in .jmx format. You add components to the Test Plan by Right Click on the Test Pand and navigating to the component you want to add. https://riptutorial.com/ 4 2. Workbench: Is a temporary place to start scripting. Along with all the components available in Test Plan, you get HTTP(s) Test Script Recorder in order to record the browser actions. Scripts can be saved in the Workbench provided you check the "Save Workbench" checkbox, otherwise they are no. 3. Threads (Users): you can define a number of (virtual) users to run, ramp-up time and loop count. you can also define on Test Plan whether Thread Groups need to run in sequential or parallel in the case of multiple Thread Groups. some examples are Thread Group, setUp Thread Group, and tearDown Thread Group 4. Logic Controller: Allows you define the flow of execution and grouping of the samplers. one of the useful examples is Transaction Controller, where you combine all the samplers of Login page (all resources including images, .css, and .js files) so that combined response time can be retrieved. 5. Sampler: Sampler is the core of the JMeter. It gives components to simulate requests of various protocols such as HTTP, JDBC, FTP, SMTP etc. for example, HTTP sampler allows you simulate an HTTP packet (of GET, POST or any supported methods). Main stream protocols are supported, for others you can use Free or Commercial plugins. 6. Config Element: Configuration elements can be used to set up defaults and variables for later use by samplers. Note that these elements are usually processed at the start of the scope in which they are found, i.e. before any samplers in the same scope. CSV Dataset Config allows you to provide test data like usernames, passwords of Login scenario from a file. User Defined variables config element allows you define variables which can be used across the Test Plan but where each Thread has its own copy. 7. Timer: By default, a JMeter thread executes samplers in sequence without pausing. Components presented here provide the functionality to introduce User Think Time in various forms among samplers. some examples are Constant Timer, Constant Throughput Timer. 8. Pre Processors: allow you to perform operations/actions before sampler gets executed. JSR223 Pre Processor with Apache Groovy (similar to java coding style) allows you to make changes to the sampler before sending it. 9. Post Processors: allow you perform operations/actions after sampler get executed. some useful examples are retrieving dynamic value such as Session ID, using Regular Expression Extractor post processor for any type of text, CSS/JQuery Extractor for HTML, JSON Extractor for JSON, XPath Extractor for XML. 10. Assertions: As the name suggests, you can assert the response of samplers in different ways like searching for some text, the size of the response, and duration to receive the response etc. For example, you can use Response Assertion to search for some text in the response. If Assertion fails, JMeter marks the sampler, to which Assertion is applied, as Failure. 11. Listeners: Listeners allow you to save the test results, see the test execution etc. for example, using View Results Tree, you can see the samplers request/response and whether they marked as PASS (green colour)/FAIL (red colour) by JMeter. using Aggregate Report, you can save the test results in CSV format. Important note is that, you use listeners either before the test run (for test script debug) or after the test run (to view results in graphs or summary) but not during the run. we must remove the listeners during the test as it consume a lot of system resources. So, we run the test in non-GUI mode and save the results using -l option in .csv/.jtl formats. Post the test, you can load this saved files into any of the listeners in the JMeter to view graphs/summary. Following is the general syntax (you add any component on need basis): https://riptutorial.com/ 5 Test Plan Thread Group Config Element Logic Controller Pre Processor Sampler Timer Post Processor Assertion Listener References: 1. Test Plan and Components 2. Execution Order 3. Scoping Rules Read Getting started with Apache JMeter online: https://riptutorial.com/jmeter/topic/1941/getting- started-with-apache-jmeter https://riptutorial.com/ 6 Chapter 2: Apache JMeter Correlations Introduction In JMeter performance testing, Correlations means the ability to fetch dynamic data from the server response and to post it to the subsequent requests. This feature is critical for many aspects of testing, like token-based protected applications. Examples Correlation Using the Regular Expression Extractor in Apache JMeter If you need to extract information from a text response, the easiest way is to use Regular Expressions. The matching pattern is very similar to the one used in Perl. Let’s assume we want to test a flight ticket purchase workflow. The first step is to submit the purchase operation. The next step is to ensure we are able to verify all the details by using the purchase ID, which should be returned for the first request. Let’s imagine the first request returns a html page with this type of ID that we need to extract: <div class="container"> <div class="container hero-unit"> <h1>Thank you for you purchse today!</h1> <table class="table"> <tr> <td>Id</td> <td>Your purchase id is 1484697832391</td> </tr> <tr> <td>Status</td> <td>Pending</td> </tr> <tr> <td>Amount</td> <td>120 USD</td> </tr> </table> </div> </div> This kind of situation is the best candidate for using the JMeter Regular Expression extractor.