Home Programming JMeter Cookbook

JMeter Cookbook

By Bayo Erinle
books-svg-icon Book
eBook $28.99 $19.99
Print $48.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $28.99 $19.99
Print $48.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    JMeter Fundamentals
About this book
Publication date:
October 2014
Publisher
Packt
Pages
228
ISBN
9781783988280

 

Chapter 1. JMeter Fundamentals

In this chapter, we will cover the following recipes:

  • Executing a test script

  • Recording a script via HTTP(S) Test Script Recorder

  • Recording scripts via the Chrome browser extension

  • Converting HTTP web archives (HAR) to JMeter test plans

  • Viewing and analyzing test results

  • Feeding data into a script

  • Using timers

  • Managing HTTP user sessions

  • Testing Single Page Applications (SPAs)

  • Testing AJAX-centric applications

 

Introduction


In this chapter, you will learn the fundamentals and intermediate skills that will help you work better with JMeter. Tasks such as executing or recording scripts are routine and you can be almost certain that you will need to perform them, sometimes on a daily basis. Also, we will learn how to view and analyze the results of test executions, feed data into test scripts, and make test scripts mimic user behavior with the aid of timers. In later recipes, we dive into dealing with handling authentication, authorization, and testing today's new breed of applications with JMeter.

 

Executing a test script


Sometimes, the test scripts to execute have already been recorded and handed over to you to run. In this recipe, we will show you just how to go about executing a script that was prerecorded.

How to do it…

To execute a test script, perform the following steps:

  1. Open the command line prompt.

  2. Change to the directory of your JMeter install. We'll refer to this as JMETER_HOME.

    Note

    Refer to Appendix for JMeter installation instructions.

  3. Change JMETER_HOME to the bin directory.

  4. To launch the JMeter GUI on Windows, type in jmeter.bat. Alternatively, for Unix/Mac OS, type in ./jmeter.

  5. Navigate to File | Open.

    Note

    Alternative key binding: Mac OS (Command + O), Windows (Ctrl + O).

  6. Navigate to the script you want to execute. For example, ch1/getting_started.jmx provided with the sample code.

  7. Press the green start icon at the top.

    Note

    Alternative key binding: Mac OS (Command + R), Windows (Ctrl + R).

  8. View the results from one of the added listeners. If you are using getting_started.jmx from step 6, click on the View Results Tree listener as shown in the following screenshot:

    Executing a test script

How it works…

Test scripts are a series of prerecorded requests issued against an application. They are captured interactions of a user's actions with the application. These include visiting a URL, navigating around several pages, logging in, and so on. JMeter, like most test tools, has the ability to record and replay back test scripts. JMeter test scripts are stored in the XML (extendable markup language) format with the .jmx extension. Curious users can open the test script in a text editor of their choice (for example, Sublime Text, Notepad, and so on) and view exactly what it is doing, though, it is much more clear to understand what a script does by looking at it in JMeter's GUI.

After executing the test, results are reported through any configured listeners. A script can have one or several listeners attached to it (see recipes in Chapter 7, Building, Debugging, and Analyzing the Results of Test Plans).

There's more…

Scripts can also be executed in what JMeter refers to as non-GUI mode, which is completely on the command line without launching the JMeter GUI. To do this, instead of executing jmeter.bat or JMeter, like we did in step 4, we'll use certain JMeter command-line arguments to specify the test script and the resulting output file, which we can then go back and view with the JMeter GUI after the tests are executed.

On Unix/Mac OS, type in the following command:

./jmeter -n -t [path to test script] -l [path to results files]

On Windows, type in the following command:

jmeter.bat -n -t [path to test script] -l [path to results files]

An example of running in non-GUI mode

Here is an example of running code in non-GUI mode: This is how we executed the google_simulation.jmx test script in our bundled sampled in non-GUI mode. As you can see, we supplied parameters –n and –t and the absolute path to where our script is located (/Users/berinle/workspace/jmeter-cookbook/ch1/google_simulation.jmx) followed by the path to the results file (/Users/berinle/workspace/jmeter-cookbook/ch1/google_simulation_result.csv).

./jmeter -n -t /Users/berinle/workspace/jmeter-cookbook/ch1/google_simulation.jmx -l /Users/berinle/workspace/jmeter-cookbook/ch1/google_simulation_result.csv

Created the tree successfully using /Users/berinle/workspace/jmeter-cookbook/ch1/google_simulation.jmx.

Starting the test @ Tue Feb 25 05:07:45 EST 2014 (1393322865378)

Waiting for possible shutdown message on port 4445.

Tidying up ... @ Tue Feb 25 05:08:46 EST 2014 (1393322926156)

... end of run

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

To view the results after the test has been executed, open up the JMeter GUI, create a new test plan, and add a listener to it by navigating to Test plan | Add | Listener | Aggregate Report. Click on the Browse… button to navigate to the location of the results file and open it. This is demonstrated in the following screenshot:

Viewing the results of a test run

There are several reasons why you might want to execute a test in non-GUI mode. Some of the reasons include:

  • Running on a Unix machine with no GUI layer (that is, the JMeter GUI does not even open up)

  • Running a distributed test

  • Performance reasons of the JMeter GUI, which can sometimes be resource intensive; running in non-GUI mode alleviates this issue

 

Recording a script via HTTP(S) Test Script Recorder


Recording scripts is where you will spend most of your time. It is often the first step to develop test plans for applications. Though you can develop test plans manually by building them from within the JMeter GUI, or generating them via some domain-specific language (DSL), recording scripts by JMeter's HTTP(S) Test Script Recorder is one of the available options you have when building your test plans.

How to do it…

In this recipe, we cover how to record test scripts with HTTP(S) Test Script Recorder. Perform the following steps:

  1. Start JMeter. Perform the following steps:

    • Open the command-line prompt

    • Change to the directory of your JMeter install (JMETER_HOME)

    • Change to the bin directory

    • Execute the following command:

      For Windows, type the following command:

      	jmeter.bat
      

      For Mac OS/Unix, type the following command:

      	./jmeter
      
  2. Once the GUI is opened, click on the Templates… button (the icon that appears immediately to the right of the new test plan icon) on the toolbar.

    Note

    Templates were added in JMeter 2.10, so don't expect to see them in versions prior to that.

  3. In the drop-down menu of the Select Template box, choose Recording and click on the Create button.

  4. Click on the HTTP(S) Test Script Recorder element (under WorkBench) and change Port (under Global Settings) from 8888 to 7000.

    • You can use a different port if you want to. What is important to choose is a port that is not currently used by an existing process on the machine. The default is 8888.

  5. Leave the rest of the components with the default set values.

  6. The template does an excellent job and is configured with sensible defaults. The defaults provide the following:

    • Target the recorded actions to Recording Controller added by the template.

    • Group a series of requests that constitute a page load. We will see more on this topic later.

    • Instruct the script recorder to bypass recording requests of a series of elements that are not relevant to test execution. These include JavaScript files, style sheets, and images.

    • Add the often-used components, including user-defined variables, HTTP Request Defaults, and HTTP Cookie Manager, to the test plan.

    • Add Thread Group and Recording Controller to the test plan.

    • Add a View Results Tree listener to view results of the test plan.

  7. Click on the Start button at the bottom of the HTTP(S) Test Script Recorder component.

With these settings, the test script recorder server will start on the port 7000, monitor all requests going through that port, and record them to a test plan using the default recording controller. For details, see the following screenshot:

HTTP(S) Test Script Recorder configuration

How it works…

These steps instruct JMeter to act as an HTTP proxy and listen for incoming and outgoing requests from your browser to the Internet on the assigned port, in our case 7000.

We now need to configure the browser to use the proxy server we just configured and redirect requests to the assigned port. There are several ways to do that, but we will focus here on the two most common ways.

There's more…

Modern browsers have a vibrant and active plugin ecosystem that allows you to extend the capabilities of your browser with an added plugin. FoxyProxy is one such plugin (http://getfoxyproxy.org/). It is a neat add-on for a browser that allows you to set up various proxy settings and toggle between them on the fly, without having to mess around with system settings on the machine. It really makes the work hassle free. Thankfully, FoxyProxy has a plugin for all major browsers including Google Chrome, Firefox, and Internet Explorer. If you are using any of those, you are lucky; head over and grab it!

Changing the machine system settings

The other common way to configure the browser is to change the system settings. The following are the details on how to configure Windows and Mac OS.

On Windows OS, perform the following steps to configure a proxy:

  1. Navigate to Start | Control Panel | Network and Internet | Internet Options.

  2. In the Internet Options dialog box, click on the Connections tab.

  3. Click on the LAN Settings button.

  4. To enable the use of a proxy server, check the Use a proxy server for your LAN (These settings will not apply to dial-up or VPN connections) box as shown in the following screenshot.

  5. In the proxy Address box, enter localhost in the IP address.

  6. In the Port number text box, enter 7000 (to match the port you set up for your JMeter proxy earlier).

  7. If you want to bypass the proxy server for the local IP addresses, select the Bypass proxy server for local addresses checkbox.

  8. Click on OK to complete the proxy configuration process. This is shown in the following screenshot:

    Manually setting a proxy on Windows 7

On Mac OS, perform the following steps to configure a proxy:

  1. Navigate to System Preference | Network | Advanced….

  2. Go to the Proxies tab.

  3. Check Web Proxy (HTTP).

  4. Under Web Proxy Server, enter localhost.

  5. For port, enter 7000 (to match the port you set up for your JMeter HTTP proxy earlier).

  6. Do the same for Secure Web Proxy (HTTPS).

  7. Click on OK, as shown in the following screenshot:

    Manually setting a proxy on Mac OS

For all other systems, consult the related operating system documentation.

With both JMeter's HTTP(S) Test Script Recorder and browser configured to use it, we are now ready to record a test. Perform the following steps:

  1. Point your browser to a website of your choice.

  2. Navigate to a few links.

  3. Notice that all user actions are being captured under the Thread Group component in the JMeter GUI.

  4. Stop the recording by stopping HTTP(S) Test Script Recorder.

The HTTP(S) Test Script Recorder server is set up to listen to requests on a particular port and then set the browser or system to redirect all requests to the assigned port. This allows us to capture the user actions as they interact with web applications, allowing us to replay their actions like we did in the Executing a test script recipe.

See also

  • The Recording scripts via the Chrome browser extension recipe in Chapter 8, Beyond the Basics

  • The Writing Test scripts through Ruby DSL recipe

  • The Converting HTTP web archives (HAR) to JMeter test plan recipe

 

Recording scripts via the Chrome browser extension


Recording test scripts is one you will be doing quite often. There are many alternatives to how we can record test scripts in JMeter.

How to do it…

In this recipe, we show you how to record test scripts with just a browser add-on installed on the Google Chrome browser. Perform the following steps:

  1. Install the Google Chrome browser if it's not already installed on your machine.

  2. Open the Chrome Web Store.

  3. Search for blazemeter on the web store.

  4. Install the BlazeMeter browser extension by clicking on the Free button.

    Note

    Once installed, a new BlazeMeter icon will be placed in the top-right corner of your browser toolbar.

  5. Click on the newly added BlazeMeter extension button in the top-right corner of your browser toolbar.

  6. Press the Record button.

  7. Point your browser to a website of your choice.

  8. Navigate through the website as you will normally do as a user.

  9. After the previous step, stop the recording and click on the export to jmx button (.jmx). This will download a copy of your script to your local machine.

  10. Open the exported test script in JMeter.

  11. Add View Results in Tree Listener to the test plan (Test plan | Add | Listener | View Results in Tree Listener).

  12. Run the exported test script with JMeter.

  13. View the results.

    Tip

    Exporting your recorded test plan requires a free account with BlazeMeter for you to be logged in.

    Installing the BlazeMeter Chrome extension

The BlazeMeter Chrome extension should look similar to what is shown in the preceding screenshot. Using this extension, additional properties can be configured in the Advance section of the extension. The following is a screenshot of the extension:

Using the BlazeMeter Chrome extension

How it works…

The browser extension captures the user interaction with the website in the JSON format as long as the recorder is on.

When the recording is stopped, no further user actions are captured. After the export button has been clicked on the JSON format is then converted to the XML format, which JMeter understands and can work with. The downloaded recorded script can then be edited and run within JMeter like any other script.

There's more…

The extension allows you to selectively capture requests by providing you with the ability to pause recording in between the user interactions. It also allows you to filter out unwanted traffic you might not be interested in capturing in your test scripts through the include filter pattern text area.

Furthermore, the Advance menu option allows you to gain more control over which requests are captured in your script, allowing you to capture only the top-level requests (default), disable browser cache (default), disable recording cookies (default), and edit the setting before proceeding to run.

Finally, as BlazeMeter (one of the many providers offering distributed testing in the Cloud) provides this extension, it offers additional features including the following:

  • Allows you to run your scripts directly in the Cloud (see Chapter 5, Diving into Distributed Testing)

  • Allows you to simulate thousands of users with ease (see Chapter 5, Diving into Distributed Testing)

 

Converting HTTP web archives (HAR) to JMeter test plans


Another alternative to recording test scripts in JMeter is converting the existing HTTP web archives directly into JMeter test scripts.

How to do it…

This recipe shows you how to generate a test script from captured HTTP web requests in your browser. Perform the following steps:

  1. Install and launch the Google Chrome browser (if you don't already have it).

  2. Open Developer Tools by navigating to Tools | Developer Tools.

    Note

    Alternative key bindings: Mac OS (Command + Shift + I), Windows (Ctrl + Shift + I).

    Opening Chrome Developer Tools

  3. Click on the Network tab.

  4. Make sure that the Preserve log checkbox is checked.

  5. Browse the websites as you normally would.

  6. Once you have finished browsing, open the developer tool window, right-click and select Copy All as HAR.

  7. Point your browser to https://flood.io/har2jmx.

  8. Copy the contents of the clipboard to the input box.

  9. Click on the Convert button.

  10. Launch the JMeter GUI.

  11. Open and examine the downloaded test script.

  12. Add View Results in Tree Listener to the test plan (Test plan | Add | Listener | View Results in Tree Listener).

  13. Save and run the exported test script.

  14. View the results.

Make sure that the Preserve log checkbox in the Network tab is checked to ensure all the user actions are captured, as illustrated in the following screenshot:

Preserve log checkbox in the Network tab

As shown in the following screenshot, while on the Network tab, right-click to see the Copy All as HAR option:

Copy All as HAR

How it works…

With the help of built-in browser tools and plugins, a browser is able to capture all interactions between a user and a web application into the JSON (JavaScript Object Notation) format known as the web archive (HAR). This resulting JSON object includes all the information about each request including HTTP headers, methods, query strings, and so on. Given the JSON object or a file with its content, the HAR to JMX converter can then read, parse, and construct JMeter elements for each user request, similar to what a JMeter HTTP proxy would have done.

There's more…

For command-line fans who do not wish to go through the Flood IO's website to do a conversion, there's a command-line tool that can be used to achieve the HAR TO JMX conversion. Har2jmeter located at http://seitenbau.github.io/har2JMeter/ is a utility written in the Groovy language. It's still in its infancy, so you might encounter a few quirks here and there. While testing it, one thing we found out is that it currently didn't play well with secure connections (HTTPS). Also, it didn't group requests into logical controllers. We mention it here just to make you aware that it exists and such bugs will probably be fixed in the near future.

Tip

For Firefox, the firebug plugin allows you the same capability to capture requests as HAR. Visit http://www.ehow.com/how_8789702_create-har-files-using-firebug.html to see how this can be accomplished.

 

Viewing and analyzing test results


When it comes to viewing your test results, JMeter comes with a vast number of components to aid this regard. Each component gives a different perspective into how to visualize your test results. Deciding which to use depends mostly on what kind of data you are interested in for a test run. Also, JMeter doesn't place a limit on how many of these listeners you can use on a test script, so it's not uncommon to see scripts with several results listeners.

How to do it…

In this recipe, we will cover three of the most popular built-in viewing components you will most likely need for your scripts. This is by no means an exhaustive list, so be sure to explore other viewing components. Perform the following steps:

  1. Launch JMeter.

  2. Open a previously recorded test script.

  3. Add a View Results Tree listener by navigating to Test plan | Add | Listener | View Results Tree.

  4. Add a Aggregate Report listener by navigating to Test plan | Add | Listener | Aggregate Report.

  5. Add View Results in table by navigating to Test plan | Add | Listener | View Results In Table.

  6. Start the test.

  7. Examine the results on each of the added results listeners. The entire process is shown in the following screenshot:

    Adding the View Results Tree listener

The View Results Tree component is shown in the following screenshot:

How it works…

Once added, the test results will be written in real time to these listeners as the test is going on. Each listener gives a different data representation of the test results. Depending on what you are interested in, you will use a different set of listeners.

There's more…

There are graph listeners that plot graphs in real time, helping you visualize your test results in a graphical way. Also, there are extensions to JMeter that provide even additional result listeners just in case you need more than the built-in listener components.

 

Feeding data into a script


More often than not, we will need to provide varying input datasets to our scripts to simulate realistic user interaction with an application. Most applications, for instance, need to be authenticated against to view certain restrictive areas of the application. The way to accomplish this is by supplying an input datafile to the test script. The file is normally in the form of comma-separated values (CSV).

How to do it…

This recipe shows you how to feed data into your script to handle such scenarios. Perform the following steps:

  1. Download the book code samples from http://www.packtpub.com/jmeter-cookbook/book.

    Note

    Alternatively, you can clone the sample from the GitHub repository at http://github.com/jmeter-cookbook/bundled-code.

  2. Extract the contents of the ZIP file.

  3. Launch JMeter.

  4. Open the ch1_feed_me_data.jmx script from the scripts/ch1 directory.

  5. Add CSV Data Set Config to the test plan by navigating to Test Plan | Add | Config Element | CSV Data Set Config. Let's configure it:

    1. In the Filename box, enter input.txt.

    2. Leave the rest of the entries blank.

  6. Save and run the test. The execution of the script is shown in the following screenshot:

How it works…

Assuming you haven't changed the default sharing mode of the added CSV dataset configuration, with each iteration run of our test plan, a line of the input.txt file is consumed. The consumed values are then turned into JMeter variables named after the first line of our input datafile, which are user and pass in our case. The values are then used further down the execution chain to fill in the username and password to access our application. In this particular test plan, as we have more threads than the amount of input data, the input data is recycled once the end of the file is reached.

There's more…

In our example, we have just provided the name of the file to read the input feed from. If you examine the CSV dataset configuration element closely, you will notice that it has other properties that can be filled in. These include the following:

  • Variable Names: Provide the comma-separated names of the variables. As we left this blank, the first line of our input script will be treated as the variable name.

  • Delimeter: Change to this option if you want to use a different delimiter other than a comma. For instance, you can use tab spaces (\t) or the colon symbol (:) instead of commas.

  • Allow Quoted Data: Set to True, if you would like to allow quoted data. For example, 'hello' as opposed to hello.

  • Recycle on EOF: In cases where you have more threads than the input data, this attribute specifies whether to allow the input data to be recycled. In such a case, the input feed will start from the beginning of the file once the data feed has run out.

  • Stop thread on EOF: This option determines whether to kill the thread has been reached.

  • Sharing mode: The default option is All meaning all threads; even those in a different thread group get to share the same data. The other options are Current thread group and Current thread, which specify that only the threads within a particular thread group and only a particular thread can retrieve data from the file, respectively.

 

Using timers


By default, when test scripts are recorded in JMeter, they contain no pauses between page requests. This is sometimes known as the think time in test scripts. In reality though, most users will frequently pause for a duration of time on every page before moving on to another page in the application. In JMeter, the ways to achieve these pauses are through timers. JMeter comes with a wealth of timers to achieve this purpose.

How to do it…

In this recipe, we will show you how to mimic realistic user behaviors using the JMeter timers.

  1. Launch JMeter.

  2. Open the ch1_add_timers.jmx test script located in the scripts/ch1 directory.

  3. Add Gaussian Random Timer by navigating to Test plan | Add | Gaussian Random Timer.

  4. Change Deviation (in milliseconds) from 100 to 1000.

  5. Change Constant Delay Offset (in milliseconds) from 300 to 500.

  6. Save and execute the test script.

How it works…

With our configuration, each thread will pause for a duration of 0.5 to 1.5 seconds after each request before proceeding to the next request. This is because we applied the timer at the root level of the test plan. If we wanted to have pauses after each logical page rendering, we will add a timer at the transaction controller level. This option is mostly what you will go with as a single rendered web page often consists of multiple requests.

There's more…

There are many more timers that come bundled with JMeter even through additional plugins that serve different purposes. There are timers that simulate constant delay, constant throughput, random delay, and so much more. Depending on your use case and what you are trying to simulate, you will choose one or several of these.

 

Managing HTTP user sessions


Most websites have a means of authenticating users, thereby enabling them to gain access to protected data or content that only pertains to them. Also, it is very common for websites to give users different roles based on what the user will be doing. For instance, a user called Joe could be given admin privileges while Susan is only given user privileges. Whatever the case, JMeter has a way to maintain HTTP user sessions for your test scripts.

How to do it…

In this recipe, we will detail how to accomplish this with JMeter. Perform the following steps:

  1. Launch JMeter.

  2. Open the add_cookie_manager.jmx test script.

  3. Run the test plan.

  4. Notice the failed requests, even though the log in request succeeded:

    • The Request immediately option following the log in request is indicative of this. The Request tab shows no cookies were sent with the request. Refer to the following code:

      	GET http://evening-citadel-2263.herokuapp.com/
      	 [no cookies]
      	Request Headers:
      	Connection: keep-alive
      	Host: evening-citadel-2263.herokuapp.com
      	User-Agent: Apache-HttpClient/4.2.6 (java 1.5)
    • Furthermore, our response assertions fail, as subsequent requests were not associated with the existing user, but were treated as new requests.

  5. Clear the results (Mac OS: Command + Shift + E and Windows: Ctrl + Shift + E).

  6. Add HTTP Cookie Manager to the test plan by navigating to Test plan | Add | Config Element | HTTP Cookie Manager.

  7. Save and run the test plan.

  8. Observe the results in the View Results in Tree listener and notice how all the requests now succeed:

    • If you observe the request immediately following the log in request, you will notice that the cookie information is correctly sent with the request, thanks to the added HTTP Cookie Manager. You should see something similar to this:

      	GET http://evening-citadel-2263.herokuapp.com/
      	Cookie Data:		     	connect.sid=s%3AWN5ITZxWEKyzmmAB5sct7PjA.6UAJ36%2F9%2BWFQPjdzA%2B7%2B1NL4%2Bf0HzC %2BOQI%2Bol0V0eJ0
      	Request Headers:
      	Connection: keep-alive
      	Host: evening-citadel-2263.herokuapp.com
      	User-Agent: Apache-HttpClient/4.2.6 (java 1.5) 

How it works…

The HTTP Cookie Manager stores and sends cookies like a web browser does. The cookie of any request that contains one is automatically extracted and stored by the component to be used for all future requests from that particular thread. Each JMeter thread gets its own session just like in a regular web browser to prevent users' sessions from overlapping with each other.

There's more…

Apart from the automatically extracted cookies by the HTTP Cookie Manager component, JMeter provides a way to add user-defined cookie values to a test plan. Unlike their automatically extracted counter parts, once defined, all threads executing in the test plan will share these values.

 

Testing Single Page Applications (SPAs)


Single-page applications are the new wave of web applications being developed in today's technological industry. The benefits of developing such applications can't be overstated. They relieve the servers of enormous load and bring faster responsiveness to the end user. They are architected differently from traditional web applications, which make them slightly different to develop test scripts for.

How to do it…

In this recipe, we will work through creating a script for an SPA. Perform the following steps:

  1. Launch JMeter.

  2. Add HTTP(S) Test Script Recorder and set it up for recording (see the Recording a script via JMeter HTTP proxy recipe).

  3. Click the Add suggested Excludes button.

  4. Start the HTTP(S) Test Script Recorder.

  5. Point your browser to http://angular.github.io/peepcode-tunes/public/.

  6. Add the two available albums (on the right) to your playlist (on the left).

  7. Randomly select a song to listen to.

  8. Use the buttons on the player to jump to the next or previous song or pause them.

  9. Stop the HTTP(S) Test Script Recorder.

  10. Add a listener to your test plan.

  11. Save and run the test plan.

How it works…

The actions of the user are tracked and recorded via the script recorder we set up in step 2. Like traditional web applications, we record all actions and exclude irrelevant static resources, such as images, JavaScript, and style sheets. Every call that makes a server call to backend services is also recorded as part of the test plan and is available for replay during test execution. At the end, we have a script that is almost identical to what we would get when recording a traditional web application.

There's more…

Most SPAs tend to be JavaScript-intensive. As JMeter isn't a web browser, JavaScripts embedded within a page or included from a separate file altogether won't be executed. With this in mind, it means JMeter can't be accurately used to measure page load times or browser DOM rendering. What it can do, however, is record any calls made to the server from your JavaScript files and play those back when the test is re-executed.

Such calls have an impact on the servers and are critical to measure to know how slow or fast a user's experience will be when the application is under considerable load.

 

Testing AJAX-centric applications


In today's stack of web applications, it is almost unthinkable to build an application without some dynamic content being retrieved from a backend service. Most of the time, these applications tend to lean towards partial page fresh. That is, only the needed sections of a rendered page are refreshed as opposed to the whole page loading its entire content again. This leads to more responsive pages, which improves the overall experience of the user.

How to do it…

In this recipe, we will walk through how you can test such applications using JMeter. Perform the following steps:

  1. Launch JMeter.

  2. Add HTTP(S) Test Script Recorder and set it up for recording (see the Recording a script via JMeter HTTP proxy recipe).

  3. Click the Add suggested Excludes button.

  4. Start the HTTP(S) Test Script Recorder.

  5. Point your browser to https://maps.google.com.

  6. Perform a couple of searches in the provided search box (for example, pizza, restaurants, hotels, and so on).

  7. Click on a few of the returned results.

  8. Repeat steps 6 and 7 two more times.

  9. Stop the HTTP(S) Test Script Recorder.

  10. Add a listener to your test plan.

  11. Save and run the test plan.

How it works…

The actions of the user are tracked and recorded via the script recorder we set up in step 2. Like traditional web applications, we record all actions and exclude irrelevant static resources, such as images, JavaScript, and style sheets. Calls that trigger backend server calls are trapped and are available for replay later. At the end, we have a script that is almost identical to what we would get when recording a traditional web application.

There's more...

AJAX-centric applications tend to involve a lot of asynchronous server requests made from within the client (frontend). These calls are normally made from embedded script tags within a view technology, for example, HTML, Java Server Pages (JSP), Groovy Server Pages (GSP), or from an included JavaScript file in the page. As such, JMeter records all such server requests and they can be played back at the time of running our test plans. This allows us to simulate the intended load on our servers, as though the application was the one making the AJAX calls.

About the Author
  • Bayo Erinle

    ● Proficient Java developer with more than 11 years experience ● Skilled at defining the architecture for stand-alone, distributed, two tier, three tier or multi tier applications ● Productive and creative both as a software developer and analyst ● Extensive practical hands-on experience in developing, designing, maintaining and recapturing object oriented applications ● Effective working alone or as a cooperative team member ● Adapting new technologies and developments in creating quality products for customers Bayo Erinle is a senior software engineer with over nine years' experience in designing, developing, testing, and architecting software. He has worked in various spectrums of the IT field, including government, finance, and health care. As a result, he has been involved in the planning, development, implementation, integration, and testing of numerous applications, including multi-tiered, standalone, distributed, and cloud-based applications. He is always intrigued by new technology and enjoys learning new things. He currently resides in Maryland, US, and when he is not hacking away at some new technology, he enjoys spending time with his wife Nimota and their three children, Mayowa, Durotimi, and Fisayo.

    Browse publications by this author
Latest Reviews (6 reviews total)
Excellent.
I bought last month the ebook "JMeter-Cookbook" Edition 2014 from Mr Bayo Erinle and I was quite satisfied with it. I get a good practice with the code examples provided with this book. A little regret however, this 2014 edition book doesn't deal with the "Fragment Test" of JMeter that enables to factorize in modules, functions that are shared between several "Test Plan". This feature is very useful and I had to do Internet search to get more information about this subject.
In many ways helpful, but advanced users will expect more.
JMeter Cookbook
Unlock this book and the full library FREE for 7 days
Start now