HaveComputerWillCode.Com

Welcome!
Life is a Non-Deterministic Finite State Automata
Automation ? (*pGeekiness)++ : Code /eneration;

January 19, 2011

Dynamically changing Sampler properties during a Jmeter test

Filed under: Performance,Testing — Tags: , — admin @ 7:35 am

I often want to vary the value of a Sampler property such as ‘Retrieve All Embedded Resources from HTML Files’ at runtime. The approach I am about to show will let you change (probably) *ANY* Sampler property at runtime providing you know its name!

Create a simple Test Plan with one HTTP Sampler which here goes to www.bbc.co.uk:

To change the value of ‘Retrieve All Embedded Resources from HTML Files’ at runtime, we need to know its underlying property name. The easiest way to do this is to Enable the property, save the JMX file and then open the JMX file in a text editor. Then Disable the property, save the JMX file and open that one up in a text editor: then spot the difference :-) In the case of a HTTP Sampler, it is:

<boolProp name="HTTPSampler.image_parser">true</boolProp>

To change the value of ‘HTTPSampler.image_parser’ during a test, the easiest way is to add a ‘BSF PreProcessor’ to the HTTP Sampler like so:

Dont forget to set the language! Notice the use of the ‘sampler’ object name in the script. It goes without saying that this is the Sampler Context that the PreProcessor applies to. More information on the methods exposed from ‘sampler’ – such as setProperty – can be found here. You can do a lot more with this than you think :-)

Now when you run the test, you will find all of the Content has been downloaded even though it is disabled in the Test Plan:

You could go one step further and use a Jmeter Property that can be changed live during a running test session to represent the Enabled/Disable state of a Sampler property. See my previous post here.

Kool!

January 16, 2011

Interactively Changing Load And Throughput Properties In Jmeter During A Test

Filed under: Performance,Programming,Testing — Tags: , — admin @ 2:02 am

This uses Jmeter 2.4 (available here)

Given the time it takes to ramp up and down all of the virtual users in a stress test, I’ve often wanted to tweak the throughput and load in Jmeter, live, during a test so that I can immediately see the feedback in the environment via Perfmon or whatever monitoring tool I am using. Jmeter gives you the means to do just that via Telnet! However, I’ve taken this one step further and written a crude UI Application to wrap the Telnet calls. But more on that later in the post!

The first thing you need to do is to set up Jmeter to ‘open up’ the running Jmeter application instance so that you can connect externally via Telnet. Modify your bin/jmeter.properties file so that the beanshell.server.port property is set (its probably there but commented out):

beanshell.server.port=9000

Now when you start Jmeter, two ports will be opened on your system – 9000 which is a HTTP Server, and 9001 which is a Telnet Server. Ensure your firewall has not blocked these ports. You can read all of the information about this here in 16.7 and will not repeat it.

Suffice to say, if you open up your browser and navigate to:


http://localhost:9000

…you will get access to the JSwing Console. Or, you can use the standard Telnet in CMD.EXE and do this:

telnet localhost 9001

You are now ‘in’ the running Jmeter instance and you can change its properties using this as a starting point. However, that’s all a bit clunky and the Telnet session is not friendly to mistakes – what I wanted was a simple User Interface tool in C# that would let me change Jmeter Properties during a test (such as Throughput) without using Telnet:

The ‘Get All Properties From Jmeter’ and ‘Send Property Value to Jmeter’ buttons make the Telnet calls to Jmeter on our behalf behind the scenes.

You can download all of the source code here. It includes the C# Application (already built), the source code (Visual Studio 2010/.Net 2.0) and the simple JMX file. If you open up the JMX file, you will see the following property set up which dictates the Throughput:

… and the ‘Constant Throughput Timer’ set to that value:

As it stands, that means ‘make 12 requests every minute’. But if we changed the value of ‘REQUESTSPERMINUTE’ whilst the test is running, we could vary the load on demand. To show this working:

1. Ensure that beanshell.server.port=9000 is set in the jmeter.properties files.
2. Start up Jmeter and make sure none of your previous Telnet sessions are running.
3. Open up the JMX file (it is in the Zip file mentioned earlier) and run it. Observe the TreeView – it should make a request to Google about every 5 seconds.
4. Start the C# Application.
5. Set the IP Address of the Server and the Port Number. Assuming you are running it on the same machine as Jmeter and using the default port, you have nothing to change.
6. Click ‘Get All Properties from Jmeter’. Behind the scenes, this will Telnet into Jmeter and pull out all of its properties. Of interest is the REQUESTSPERMINUTE Property that was set by the load test:

7. Select that property and change the value to ’30′. Click ‘Send Property Value to Jmeter’.
8. Assuming your connection to the Internet is fast, you should start seeing one request to Google every 2 seconds (ie: 30 per minute).

I’ve given you ‘just enough’ to get going with this… there is very little error checking in the UI and the code uses synchronous blocking calls and rather obscure sentinels to establish when all Telnet content has been returned.

L8r!

January 14, 2011

Launching external applications from Jmeter on Windows

Filed under: Performance,Testing — Tags: — admin @ 9:03 am

I’m a Microsoft Shop – I only use Jmeter because it’s free and it usually does what I need. But as soon as I need to do anything that uses anything Java-ish, even when it’s simple, I end up scratching my head!

I hit a problem today where I needed to simulate a legacy protocol that was closed source and for which no Plugins existed in Jmeter – but only at well defined points in sequence with everything else that was happening as part of the load test. After much pondering, and just to get something to work, I found that the application in question had a ‘command line’ controller application so I could automate it (like I said: legacy!). As I only needed to do this about 20 times every few minutes, I figured the easiest thing to do would be to launch the external application from within Jmeter.

There’s many ways of doing this in Jmeter, all of which took me a while to find, depending on the language you select, but the easiest way is to create a BSF Sampler and set the Scripting Language to ‘beanshell’:

Then just call ‘exec()’: Don’t forget that the backslashes need to be escaped…!

I’ve also found this an easy way to launch VBScript and PowerShell scripts that interact with WMI and change the state of the test environment (such as shutting down Firewall ports to simulate error conditions or to change QoS Parameters via WMI). You can see the call to the Windows Scripting Host (WScript) in the sample above.

You can get going with Beanshell via this link. .

L8r!

January 11, 2011

Processing JSON Responses with Jmeter and the BSF Post Processor

Filed under: Performance,Testing — Tags: , , , — admin @ 9:20 am

The JMX file can be downloaded here.

When testing web services these days with Jmeter, you will almost certainly come across the JSON Notation [JavaScript Object Notation]. It is nothing fancy – it is just a very compact way of serializing and deserializing objects in a way best suited to JavaScript. Think of it as XML without the Lard.

When processing responses with Jmeter, the usual approach is to use the ‘Regular Expression Post Processor’ to pull out matches and stash them into variables. That generally works fine with raw HTML but not with complex JSON structures. This post will show you how to make a request to the Google Search Service using Jmeter that returns a response in the JSON Format. We will then use a BSF Post Processor to turn that JSON into a real Javascript object and extract the URLs using code. Each URL will be placed into its own Jmeter variable.

To get started with JSON in Jmeter, add a HTTP Sampler in Jmeter and then a Debug Sampler. At the very end, add a Tree View Listener so we can see everything that happened. The HTTP Sampler that does the search should look like this:

That sampler is making a call to the Google Web API for ‘havebootswilltrek’ (my other blog) and returning the matching results. If I run it, I will end up with this output from the Search:

The structure is obvious if you have worked with JSON before: responseData.results[]…. etc. Without going into JSON in too much detail – you can read about the returned Google structure here – what I want to do is to turn every matching URL into a Jmeter variable of the form haveBoots[Number].

Let’s get too it!

To the above Google Request, add a BSF Post Processor and set it up as follows:

In the ‘Script’ part, we will add the following code:

// Turn the JSON into an object called 'SearchResponse'
eval('var SearchResponse = ' + prev.getResponseDataAsString());

// Create a variable called haveBoots_# containing the number of matching URLs
// For Each Result
//     Create a variable called haveBoots and assign it the URL
vars.put("haveBoots_#", SearchResponse.responseData.results.length);

for (var counter = 1; counter <= SearchResponse.responseData.results.length; counter++)	
{		
    var x = SearchResponse.responseData.results[counter - 1];

   // haveBoots = theUrl
   vars.put("haveBoots" + counter, x.unescapedUrl);
}

That will extract the ResponseData – which in this case is JSON – from the ‘Previous’ Sampler (ie: the parent ‘Search’ Sampler) and then turn the JSON into a JavaScript object using eval(). It’s then a simple case of iterating the .results. and creating a variable for each match.

When you rerun the script, the output from the Debug Sampler will look like this:

To extend this, you could create variables using the BSF Post Processor that look like the Regular Expression Post Processor extractor.

L8r!

Older Posts »

Powered by WordPress