Transcription

Apache JMeter#jmeter

Table of ContentsAbout1Chapter 1: Getting started with Apache JMeter2Remarks2Versions2Examples3Installation or Setup3Overview of Apache JMeter components at high level4Chapter 2: Apache JMeter Correlations7Introduction7Examples7Correlation Using the Regular Expression Extractor in Apache JMeter7Correlation Using the XPath Extractor in JMeter12Correlation Using the CSS/JQuery Extractor in JMeter14Correlation Using the JSON Extractor17Automated Correlation by Using BlazeMeter’s ‘SmartJMX’20Chapter 3: Apache JMeter rization using external files24Parameterization using databases31Parameterization using the ‘Parameterized Controller’ plugin41Chapter 4: Apache JMeter: Test scenario recording47Introduction47Examples47Script Recording with the JMeter Template Feature47Script Recording with the JMeter Proxy Recorder49Recording Performance Scripts for Mobile Devices52Recording HTTPS Traffic53Script Recording with the BlazeMeter Chrome Extension56Script Recording with BadBoy58

Credits60

AboutYou can share this PDF with anyone you feel could benefit from it, downloaded the latest versionfrom: apache-jmeterIt is an unofficial and free Apache JMeter ebook created for educational purposes. All the contentis extracted from Stack Overflow Documentation, which is written by many hardworking individualsat 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 eachchapter are provided in the credits section at the end of this book. Images may be copyright oftheir respective owners unless otherwise specified. All trademarks and registered trademarks arethe property of their respective company owners.Use the content presented in this book at your own risk; it is not guaranteed to be correct noraccurate, please send your feedback and corrections to [email protected]://riptutorial.com/1

Chapter 1: Getting started with ApacheJMeterRemarksJMeter is a Load-Testing Tool used for Performance Testing. A Performance Tester can recordactions in a web browser or manually build a script which can then be run with hundreds orthousands 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 webapplication. The Regular Expression Extractor or the CSS/JQuery Extractor can be used to savesession ids to be used in future requests. The JSR223 PreProcessor coupled to Groovy languagecan be used to create dynamic unique data for each user to be sent as part of a POST body.VersionsVersionJava VersionRelease Date3.2Java 8 2017-04-143.1Java 7 2016-11-203.0Java 7 2016-05-172.13Java 6 2015-03-132.12Java 6 2014-11-102.11Java 6 2014-01-052.10Java 6 2013-10-212.9Java 6 2013-01-282.8Java 5 2012-10-062.7Java 5 2012-05-272.6Java 5 2012-02-012.5.1Java 5 2011-10-032.5Java 5 2011-08-172.4Java 5 2010-07-12https://riptutorial.com/2

VersionJava VersionRelease Date2.3.4Java 1.4 2009-06-21ExamplesInstallation or Setup1. Download a distributed archive from Binaries section of JMeter from Download ApacheJMeter page.2. Depending on the version you downloaded, check minimal Java version requirements andinstall Java if needed. Ensure the JAVA HOME environment variable is set and points to acorrect 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 orjmeter.bat On Linux/Mac: navigate to jmeter location /bin directory and run jmeter or'jmeter.sh .For example:cd /Users/me/apache-jmeter/bin./jmeterNote: if the above command fails with Permissionon jmeter file:deniederror, set execute permissioncd /Users/me/apache-jmeter/binchmod u x ./jmeterIf you are able to see JMeter UI, basic setup was successful.https://riptutorial.com/3

Overview of Apache JMeter components at high levelApache 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 addcomponents to the Test Plan by Right Click on the Test Pand and navigating to thecomponent you want to add.https://riptutorial.com/4

2. Workbench: Is a temporary place to start scripting. Along with all the components available inTest 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 loopcount. you can also define on Test Plan whether Thread Groups need to run in sequential orparallel in the case of multiple Thread Groups. some examples are Thread Group, setUpThread Group, and tearDown Thread Group4. Logic Controller: Allows you define the flow of execution and grouping of the samplers. oneof the useful examples is Transaction Controller, where you combine all the samplers ofLogin page (all resources including images, .css, and .js files) so that combined responsetime can be retrieved.5. Sampler: Sampler is the core of the JMeter. It gives components to simulate requests ofvarious protocols such as HTTP, JDBC, FTP, SMTP etc. for example, HTTP sampler allowsyou simulate an HTTP packet (of GET, POST or any supported methods). Main streamprotocols 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 lateruse by samplers. Note that these elements are usually processed at the start of the scope inwhich they are found, i.e. before any samplers in the same scope. CSV Dataset Config allowsyou to provide test data like usernames, passwords of Login scenario from a file. UserDefined variables config element allows you define variables which can be used across theTest 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 variousforms among samplers. some examples are Constant Timer, Constant Throughput Timer.8. Pre Processors: allow you to perform operations/actions before sampler gets executed. JSR223Pre Processor with Apache Groovy (similar to java coding style) allows you to make changesto the sampler before sending it.9. Post Processors: allow you perform operations/actions after sampler get executed. someuseful examples are retrieving dynamic value such as Session ID, using Regular ExpressionExtractor post processor for any type of text, CSS/JQuery Extractor for HTML, JSON Extractorfor JSON, XPath Extractor for XML.10. Assertions: As the name suggests, you can assert the response of samplers in different wayslike searching for some text, the size of the response, and duration to receive the responseetc. For example, you can use Response Assertion to search for some text in the response. IfAssertion 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. forexample, using View Results Tree, you can see the samplers request/response and whetherthey 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 eitherbefore the test run (for test script debug) or after the test run (to view results in graphs orsummary) but not during the run. we must remove the listeners during the test as it consumea lot of system resources. So, we run the test in non-GUI mode and save the results using -loption in .csv/.jtl formats. Post the test, you can load this saved files into any of thelisteners in the JMeter to view graphs/summary.Following is the general syntax (youhttps://riptutorial.com/add any component on need basis):5

Test PlanThread GroupConfig ElementLogic ControllerPre ProcessorSamplerTimerPost ProcessorAssertionListenerReferences:1. Test Plan and Components2. Execution Order3. Scoping RulesRead Getting started with Apache JMeter online: tarted-with-apache-jmeterhttps://riptutorial.com/6

Chapter 2: Apache JMeter CorrelationsIntroductionIn JMeter performance testing, Correlations means the ability to fetch dynamic data from theserver response and to post it to the subsequent requests. This feature is critical for many aspectsof testing, like token-based protected applications.ExamplesCorrelation Using the Regular Expression Extractor in Apache JMeterIf you need to extract information from a text response, the easiest way is to use RegularExpressions. The matching pattern is very similar to the one used in Perl. Let’s assume we want totest a flight ticket purchase workflow. The first step is to submit the purchase operation. The nextstep is to ensure we are able to verify all the details by using the purchase ID, which should bereturned for the first request. Let’s imagine the first request returns a html page with this type of IDthat 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.Regular Expression is a special text string for describing a search pattern. There are lots of onlineresources that help writing and testing Regular Expressions. One of them is https://regex101.com/.https://riptutorial.com/7

To use this component, open the JMeter menu and: Add - Post Processors - RegularExpression Extractorhttps://riptutorial.com/8

The Regular Expression Extractor contains these fields: Reference Name - the name of the variable that can be used after extraction Regular Expression - a sequence of symbols and characters expressing a string (pattern)that will be searched for within the text Template - contains references to the groups. As a regex may have more than one group, itallows to specify which group value to extract by specifying the group number as 1 or 2 or 1 2 (extract both groups) Match No. - specifies which match will be used (0 value matches random values / anypositive number N means to select the Nth match / negative value needs to be used with theForEach Controller) Default - the default value which will be stored into the variable in case no matches arefound, is stored in the variable.The “Apply to” checkbox deals with samples that make requests for embedded resources. Thisparameter defines whether Regular Expression will be applied to the main sample results or to allrequests, including embedded resources. There are several options for this param: Main sample and sub-samplesMain sample onlySub-samples onlyJMeter Variable - the assertion is applied to the contents of the named variable, which canhttps://riptutorial.com/9

be filled by another requestThe “Field to check” checkbox enables choosing which field the Regular Expression should beapplied to. Almost all parameters are self descriptive: Body - the body of the response, e.g. the content of a web-page (excluding headers) Body (unescaped) - the body of the response, with all HTML escape codes replaced. Notethat HTML escapes are processed without regard to context, so some incorrect substitutionsmay be made (*this option highly impacts performance) Body - Body as a Document - the extract text from various type of documents via ApacheTika (*also might impact performance) Body - Request Headers - may not be present for non-HTTP samples Body - Response Headers - may not be present for non-HTTP samples Body - URL Response Code - e.g. 200 Body - Response Message - e.g. OKAfter the expression is extracted, it can be used in subsequent requests by using the {purchaseId} variable.https://riptutorial.com/10

This table contains all the contractions that are supported by JMeter Regular Expressions:https://riptutorial.com/11

https://riptutorial.com/12

Report Errors - if a Tidy error occurs, set the Assertion accordingly Show Warnings - sets the Tidy show warnings optionIf ‘Use Tidy’ is unchecked: Use Namespaces - if checked the XML parser will use the namespace resolutionValidate XML - check the document against its specified schemaIgnore Whitespace - ignore Element WhitespaceFetch External DTDs - if selected, external DTDs are fetched‘Return entire XPath fragment instead of text content’ is self descriptive and should be used if youwant to return not only the xpath value, but also the value within its xpath locator. It might beuseful for debugging needs.It is also worth mentioning there are list of very convenient browser plugins for testing XPathlocators. For Firefox you can use the ‘Firebug’ plugin while for Chrome the ‘XPath Helper’ is themost convenient tool.https://riptutorial.com/13

Correlation Using the CSS/JQuery Extractor in JMeterThe CSS/JQuery extractor enables extracting values from a server response by using aCSS/JQuery selector syntax, which might have otherwise been difficult to write using RegularExpression. As a post-processor, this element should be executed to extract the requested nodes,text or attribute values from a request sampler, and to store the result into the given variable. Thiscomponent is very similar to the XPath Extractor. The choice between CSS, JQuery or XPathusually depends on user preference, but it’s worth mentioning that XPath or JQuery can traversedown and also traverse up the DOM, while CSS can not walk up the DOM. Let’s assume that wewant to extract all the topics from the Stack Overflow documentation that are related to Java. Youcan use the Firebug plugin to test your CSS/JQuery selectors in Firefox, or the CSS SelectorTester in Chrome.https://riptutorial.com/14

To use this component, open the JMeter menu and: Add - Post Processors - CSS/JQueryExtractorhttps://riptutorial.com/15

Almost all of this extractor’s fields are similar to the Regular Expression extractor fields, so youcan get their description from that example. One difference however is the “CSS/JQuery Extractorimplementation” field. Since JMeter 2.9 you can use the CSS/JQuery extractor based on twodifferent implementations: the jsoup implementation (detailed description of its syntax here) or theJODD Lagarto (detailed syntax can be found here). Both implementations are almost the sameand have only small syntax differences. The choice between them is based on user’s preference.https://riptutorial.com/16

Based on the above mentioned configuration, we can extract all the topics from the requestedpage and verify the extracted results by using the “Debug Sampler” and the “View Results Tree”listener.Correlation Using the JSON ExtractorJSON is a commonly used data format that is used in web based applications. The JMeter JSONExtractor provides a way to use JSON Path expressions for extracting values from JSON-basedresponses in JMeter. This post processor must be placed as a child of the HTTP Sampler or forany other sampler that has responses.https://riptutorial.com/17

To use this component, open the JMeter menu and: Add - Post Processors - JSON Extractor.The JSON Extractor is very similar to the Regular Expression Extractor. Almost all the main fieldsare mentioned in that example. There is only one specific JSON Extractor parameter: ‘Computeconcatenation var’. In case many results are found, this extractor will concatenate them by usingthe ‘,’ separator and storing it in a var named ALL.Let’s assume this server response with JSON:{"store": {"book": [{ "category": "reference","author": "Nigel Rees","title": "Sayings of the Century","price": 8.95},{ "category": "fiction","author": "Evelyn Waugh","title": "Sword of Honour","price": 12.99},{ "category": "fiction","author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99},https://riptutorial.com/18

{ "category": "fiction","author": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99}],"bicycle": {"color": "red","price": 19.95}}}The table below provides a great example of different ways to extract data from a specified JSON:https://riptutorial.com/19

Through this link you can find a more detailed description of the JSON Path format, with relatedexamples.Automated Correlation by Using BlazeMeter’s ‘SmartJMX’When you manually write your performance scripts, you need to deal with correlation yourself. Butthere is another option to create your scripts - automation scripts recording. On the one hand, themanual approach helps your write structured scripts and you can add all the required extractors atthe same time. On the other hand, this approach is very time consuming.https://riptutorial.com/20

Automation scripts recording is very easy and lets you to do the same work, only much faster. Butif you use common recording ways, the scripts will be very unstructured and usually require addingadditional parametrization. The “Smart JMX” feature on the Blazemeter recorder combines theadvantages of both ways. It can be found at this link: [1]After registration go to “Recorder” section.To start script recording, first you need to configure your browser’s proxy (covered here), but thistime you should get a proxy host and a port provided by the BlazeMeter recorder.https://riptutorial.com/21

When the browser is configured, you can go ahead with script recording by pressing the red buttonat the bottom. Now you can go to the application under test and perform user workflows forrecording.https://riptutorial.com/22

After the script is recorded, you can export the results into a “SMART” JMX file. An exported jmxfile contains a list of options that allow you to configure your script and parameterize, withoutadditional efforts. One of these improvements is that the “SMART” JMX automatically findscorrelation candidates, substitutes it with the appropriate extractor, and provides an easy way forfurther parametrization.Read Apache JMeter Correlations online: metercorrelationshttps://riptutorial.com/23

Chapter 3: Apache JMeter parameterizationIntroductionParameterization is the creation of different data sets for different users in the same test script. Forexample, running multiple users with different credentials in the same script. This makes it one ofthe main aspects in performance tests creation.ExamplesParameterization using external filesOne of the common ways to parametrize your performance scripts is to use a CSV file. The bestexample of CSV input files usage is a login process. If you want to test your application acrossdifferent users, you need to provide a list of user credentials.Let’s assume that we have a login request that works for one specific user:https://riptutorial.com/24

We can easily parametrize that request by using an external CSV file and running the script acrossdifferent users. To add CSV parametrization config:Right click on login request - Add - Config Element - CSV Data Set Confighttps://riptutorial.com/25

A short explanation of ‘CSV Data Set Config’ parameters: Name - element name as it will be used in the JMeter tree Filename - name of the input file. Relative file names are resolved based on the path of theactive test plan. Absolute filenames are also supported File Encoding - encoding of input file, if it’s not the platform default Variable Names - list of separated variable names that will be used as a container for parsedvalues. If empty, the first line of the file will be interpreted as the list of variable names Delimiter - delimiter that will be used to split the parsed values from the input file Allow quoted data? - true in case you want to ignore double quotes and allow such elementsto contain a delimiter. Recycle on EOF? - true in case the file test plan should iterate over the file more than once.It will instruct JMeter to move the cursor at the beginning of the file Stop thread on EOF? - false in case of loop iteration over the CDC file and true if you want tostop the thread after reading the whole file Sharing mode:All threads - the file is shared between all virtual users (the default) https://riptutorial.com/26

Current thread group - the file will be opened once for each thread groupCurrent thread - each file will be opened separately for each of threadsIdentifier - all threads sharing the same identifier also share the same fileLet’s create a csv file containing different users with names and passwords:We can now use this file with the CSV Data Set config. In our case, it’s enough to add the“Filename” and “Variables Names” config values:https://riptutorial.com/27

The last step we have to take is to parametrize the login request with CSV variables. This can bedone by substituting the initial values with appropriate variables from the “Variable Names”configuration field of the CSV Data Set Config, like this:https://riptutorial.com/28

If we run our test script now, JMeter will substitute these variables with values from the‘TestUsers.csv’ file. Each JMeter virtual user will receive credentials from the following csv file line.The login request by the first user:https://riptutorial.com/29

The login request by the second user:https://riptutorial.com/30

Parameterization using databasesAnother way to parametrize your performance scripts is to use database data through JDBC.JDBC is an application programming interface that defines how a client can access a database.https://riptutorial.com/31

First of all, download the JDBC driver to your database (refer to the database vendor). Forexample, mysql driver can be found here. Then, you can add it by adding the .jar file to the testplan by using the form below:But it is better to add the Jar file in lib folder and restart JMeter.After that, configure the database connection by using the ‘JDBC Connection Configuration’element. Like this: Right click on Thread Group - Add - Config Element - JDBC ConnectionConfigurationhttps://riptutorial.com/32

‘JDBC Connection Configuration’ parameters: Name - name of the connection configuration that will be shown in the thread group tree Variable Name - name that will be used as unique identifier for the db connection (multipleconnections can be used and each one will be tied to a different name) Max Number of Connections - maximum number of connections allowed in the connectionpool. In case of 0, each thread will get its own pool with a single connection in ithttps://riptutorial.com/33

Max Wait (ms) - pool throws an error if the specified timeout is exceeded during dbconnection Time Between Eviction Runs (ms) - number of milliseconds to pause between runs of thethread that evicts unused connections from the db pool Auto Commit - yes to turn auto commit for related db connections Test While Idle - check idle connections before an effective request is detected. More details:BasicDataSource.html#getTestWhileIdle Soft Min Evictable Idle Time(ms) - period of time during which a specified connection mightbe idle in the db pool before it can be evicted. More leTimeMillis Validation Query - healthcheck query that will be used to verify if the database is stillresponding Database URL - JDBC connection string for the database. See here for examples JDBC Driver class - appropriate name of driver class (specific for each db). For example,‘com.mysql.jdbc.Driver’ for MySql db Username - database username Password - database password (will be stored unencrypted in the test plan)In our case we need to setup the mandatory fields only: Variable name Bound to Pool.Database URLJDBC Driver classUsernamePasswordThe rest of the fields in the screen can be left as defaults:https://riptutorial.com/34

Let’s assume that we store test user credentials in the database:https://riptutorial.com/35

Now when the database connection is configured, we can add the JDBC request itself and use itsquery to get all the credentials from database: Right click on Thread Group - Add - Sample - JDBC RequestBy using the ‘Select Statement’ query and ‘Variable Names’ we can parse the response to customvariables.https://riptutorial.com/36

We will now have JMeter variables that can be used further in subsequent requests. Specifiedvariables will be created with incremental suffix (email 1, email 2, email 3 .).To use these variables in the ‘Login request’, we need to add a counter that will be used to accessthe right values from the JDBC query response. To add the ‘Counter’ element in JMeter: Rightclick on Thread Group - Add - Config Element - Counterhttps://riptutorial.com/37

After that, we can update the ‘Login request’ using the V function. This returns the result ofevaluating a variable name expression and can be used to evaluate nested variable references:https://riptutorial.com/38

The specified configuration is enough to use database values to run the script across differentusers:https://riptutorial.com/39

https://riptutorial.com/40

Parameterization using the ‘Parameterized Controller’ pluginIf you need to execute a repeating sequence of the same action with different parameters, use the‘Parameterized Controller’ 3rd party plugin from JMeter-Plugins project.You need to install this plugin first by following installation procedure.Let’s assume that we want to parameterize the login workflow:https://riptutorial.com/41

First of all, you need to install the ‘Parameterized Controller’ plugin as it is not included into JMetercore. Installation steps of that process can be found here.Let’s move the ‘Login Request’ into a separate controller and disable it (right-click it and select"Disable"). This is the most preferable way to have a modules container inside your test plan andavoid using Workbench as such a container. After the installation is over, you can add two‘Parameterized Controller’ controllers with different user credentials: Right click on Thread Group Add - Logic Controller - Parameterized Controllerhttps://riptutorial.com/42

Parameterized Controllers contains the ‘User Defined Variables’ section, where you can specifyyour parameters. Put the credentials of the first user in the first parameterized controller and thesecond user credentials in the second parameterized controller.https://riptutorial.com/43

Inside both parameterized controllers, add references to the ‘Reusable Controller’ to call the ‘Loginrequest’ with different parameters. It can be done this way:Right click on ‘Parameterized Controller’ - ‘Add’ - ‘Logic Controller’ - ‘Module Controller’https://riptutorial.com/44

When running your script, you will see that the ‘Login request’ triggered each of the parameterizedcontrollers separately. It can be very useful in case you need to run your script across differentcombinations of input parameters.https://riptutorial.com/45

Read Apache JMeter parameterization online: eter-parameterizationhttps://riptutorial.com/46

Chapter 4: Apache JMeter: Test scenariorecordingIntroductionRecording test scenarios is one of the most convenient ways to create test scripts. This is becausetest recordings let you mimic realistic user workflows, instead of having to manually create a testscript. The recordings capture all browser requests to the web application, and then automaticallycreate a jmx file that can be run in performance tests. By using JMeter’s recording/playbackfunctionality or 3rd party tools like BlazeMeter and BadBoy, testers can make their work 3 timesfaster.ExamplesScript Recording with the JMeter Template FeatureIn version 2.10, JMeter introduced a mechanism that saves you time when creating scripts JMeter Templates. These templates are skeletons that can be reused as the base for your newscripts.JMeter already has several available templates with detailed descriptions, and you can also addyour own. Templates have all the required configuration and elements to record your performancescripts from scratch.Here’s how to use the JMeter Template Feature:Configure JMeter1. Open JMeter2. Select the template for script recording:File - Templates - Select Template - Recording - Create JMeter will add the relevantelements to the test tree.https://riptutorial.com/47

Configure your browser proxyTo use the JMeter Recorder, you need to configure your browser to send all the requests viaproxy. Any browser can be used for these needs, although there might be differences between thelocations of the browsers’ configurations, which are browser-specific and might vary according toOS.3. To configure your browser:Chrome: Menu button - Settings - Show advanced settings - Network - Changeproxy settingsSafari: Preferences - Advanced - Proxies - Change Settings.Firefox: Menu button - Preferences - Advanced - Network - Connection - Settings.4. For example, you can use the localhost 127.0.0.1. Change the port to the port in theHTTP(S) Script Recorder.If you have issues while completing this stage, check that you don’t have any 3rd party pluginshttps://riptutorial.com/48

that can manage your browser’s proxy settings. If you do, like Hola VPN for example, the proxysettings will be unavailable in your browser’s menu.5. Click on the ‘Start’ button, which is at the bottom of the “HTTP(

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.