JMeter Advanced

From Training Material
Revision as of 16:49, 24 November 2014 by Cesar Chew (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Transaction Controller

  • Allows to aggregate results for many samplers
  • Useful for Ajax
  • Can be used as an alternative for downloading embedded elements

Sachin garg 9871702532

Connecting to a Database

Setting up a connection

JMeter MySQL Connection Configuration.png

Extracting data from SQL result

  • This example takes tab separated data returned by a database call and adds it to db_rows
  • Note the -1, which when combined with a ForEach Logic Controller processes all rows

JMeter Extracting Data From SQL Query.png

Extracting Multiple Values

Pattern

name="file.name" value="readme.txt"

Reference Name:

MYREF

Regex:

name="(.+?)" value="(.+?)"

Template:

$1$$2$

The following variables would be set:

MYREF: file.namereadme.txt
MYREF_g0: name="file.name" value="readme.txt"
MYREF_g1: file.name
MYREF_g2: readme.txt
${MYREF}, ${MYREF_g1} etc

Using Beanshell

  • BeanShell interpreter: http://www.beanshell.org/download.html
  • BeanShell is one of scripting languages available within JMeter
  • With BeanShell you can create custom outputs, perform logical operations, manipulate variables, etc.

Simple BeanShell Example

  • In this BeanShell example having gathered the last known user ID from a user table with a JDBC Request and putting it in a variable with a Regex Extractor, this is used to calculate what the next two user ID's will be with BeanShell:

JMeter - BeanShell Script.png

  • Note ${lastid} is passed in using the Parameters box, but this is not necessary – you can also use vars.get(“lastid”) from within the script itself
  • Anything passed in to a BeanShell Sampler using the Parameters field is held in the Parameters variable

BeanShell Inline

  • You can run snippets of BeanShell wherever by taking advantage of JMeter BeanShell function. The syntax is:
${__BeanShell(code here as escaped string)}
  • E.g. setting a variable:
${__BeanShell(vars.put(“name”\,”value”))}
  • Note the “slash” before the comma – special characters must be escaped within the __BeanShell function

BeanShell Inclusion

  • You may also call external BeanShell scripts, like this example from the JMeter manual:
${__BeanShell(source("function.bsh"))} 
  • The above line invokes the BeanShell script in the file “function.bsh”
  • The __BeanShell function (like all JMeter functions) can be invoked anywhere you can normally use a variable

ForEach Logical Controller

  • Amongst its many uses, this can be used, in conjunction with a JDBC Request, to cycle through all the rows returned from a database select query:

JMeter ForEach Controler.png

  • The input variable must match the reference name in your Regex Extractor for the sampler
  • Output variable is the data for the specific row

Manipulating data with BeanShell

  • Within a ForEach controller, use a BeanShell Sampler to load, format and output each row of the database result
  • Don't forget your BeanShell Sampler must be nested under the ForEach controller to be affected by it – otherwise it will only execute once on the final value

JMeter Calculating Frequencies

// noOfThreads1 = Integer.parseInt(Parameters);
noOfThreads1 = Integer.parseInt(vars.get("NT_ANON")); 
noOfThreads1 = noOfThreads1 * 2;
vars.put("NT_ANON",noOfThreads1.toString());

Functions

  • ${__time(yyyyMMdd-HHmmss ,TIME)}

Extras