Sie sind auf Seite 1von 75

JMeter

A Performance Testing Tool


Date:08/04/2011

Introduction
Definition JMeter is an Apache Jakarta project that can be used as a load testing tool for analysing and measuring the performance of a variety of services, with a focus on web applications.

Why JMeter
JMeter can be used as a unit test tool for JDBC database connection, FTP, LDAP, WebServices, JMS, HTTP and generic TCP connections. JMeter can also be configured as a monitor, although this is typically considered an ad-hoc solution in lieu of advanced monitoring solutions.

May 13, 2009

Introduction - Features
100% pure Java Open Source Desktop application. Designed for performance/functional/load/stress testing. Extensible- write your own tests.

Pluggable sampler allows unlimited testing capabilities.


Simulate heavy load (application, server and network). Gives instant visual feedback. Distributed testing.

Various protocols- HTTP, JDBC, LDAP, SOAP, JMS, FTP.


Multi-platform. Full multi-threading framework. Test plans can be stored in XML and can be version controlled. Caching and offline analysis/ replaying of test results

May 13, 2009

Elements of Test Plan


Thread Group Logic Controllers Samplers Configuration elements

Pre-processor elements
Post-processor elements Timers Listeners Assertions

May 13, 2009

Execution Order
Timers Preprocessors Samplers

Post-processors
Assertions Listeners

May 13, 2009

JMS Point to Point Test Plan


Adding users Adding JMS Point to Point Sampler

Monitor Results

May 13, 2009

JMS Point to Point Test Plan Adding Users


Add Thread Group Specify No. of users Ramp up Period Loop count

May 13, 2009

JMS Point to Point Test Plan: JMS Sampler Configuration

Specify QueueConnectionFactory (default JNDI entry for the connection factory


within active mq)

Specify JNDI Name Request Queue (equal to the JNDI name defined in the JNDI
properties)

Specify JNDI Name Reply Queue (equal to the JNDI name defined in the JNDI
properties)

Communication Style
(you need at least a service that responds to the requests)

Content
(the content of the message)

Specify InitialContextFactory (standard InitialContextFactory for Active MQ ) Specify provider URL (defines the URL of the active mq messaging
system)

May 13, 2009

JMS Topic Test Plan


Adding Users Adding JMS Subscriber Adding JMS Publisher Adding a Listener to View Store the Test Results

May 13, 2009

JMS Topic: Adding Users


Add Thread Group Specify No. of users Ramp up Period Loop count

10

May 13, 2009

JMS Topic :JMS Subscriber

Start by adding the JMS Subscriber to the Subscribers element (Add --> Sampler --> JMS Subscriber).

If the JMS provider uses the jndi.properties file, check the box.

Enter the name of the connection

factory, while ActiveMQ and MQSeries


does not require authentication

Enter 10 in "Number of samples to aggregate".

11

May 13, 2009

JMS Topic : Adding Publisher

Add the sampler JMS Publisher to the Publisher element (Add --> Sampler -> JMS Publisher). If the JMS provider uses the jndi.properties file, check the box Enter the name of the InitialContextFactory class. Enter the name of the connection factory. Enter the name of the message topic Enter 10 in "Number of samples to aggregate". elect the appropriate configuration for getting the message to publish. If you want the sampler to randomly select the message, place the messages in a directory and select the directory using browse. Select the message type. If the message is in object format, make sure message is generated correctly.

12

May 13, 2009

Monitor Results

This element is responsible for storing all of the results of your JMS requests in a file and presenting a visual model of the data.

Select the Thread Group element and add a Graph Results listener (Add --> Listener --> Graph Results).

13

May 13, 2009

Functions and Variables


JMeter functions are special values that can populate fields of any Sampler or other element in a test tree. A function looks like ${__functionName(var1,var2,var3)}. A function with no parameter looks like ${__functionName}. If a function parameter contains a comma, then be sure to escape this with "\",

otherwise JMeter will treat it as a parameter delimiter. Example ${__time(EEE\, d


MMM yyyy)}. Variables are referenced as ${VARIABLE}. Variable and functions are all case-sensitive.

14

May 13, 2009

What can functions do ?


Two kinds of functions:

user-defined static values (or variables). built-in functions.

User-defined static values allow the user to define variables to be replaced with their
static value when a test tree is compiled and submitted to be run.

This replacement happens once at the beginning of the test run. Variables cannot be nested. With built-in functions users can compute new values at run-time based on previous response data.

These new values are generated fresh for every request throughout the course of the

test.

15

May 13, 2009

Where can functions and variables be used?


Functions and variables can be written into any field of any test component (apart from the TestPlan). Some fields do not allow random strings because they are expecting numbers, and thus will not accept a function. However, most fields will allow functions. Functions which are used on the Test Plan have some restrictions.

JMeter thread variables will have not been fully set up when the functions are
processed, so variable names passed as parameters will not be set up, and variable references will not work, so split() and regex() and the variable evaluation functions won't work.

The following functions should work OK on the test plan - intSum, longSum,
machineName, BeanShell, javaScript, jexl, random, time, property functions, log functions.

16

May 13, 2009

Types of Functions

Information Related Functions Input Related Functions

Calculation Related Functions


Scripting Related Functions Properties Related Functions Variables Related Functions String Related Functions

17

May 13, 2009

Information Related Functions

ThreadNum MachineName

Time
Log Logn

18

May 13, 2009

ThreadNum

The thread number function simply returns the number of the thread currently being executed. ${__threadNum} //will return the thread count

This function does not work in any Configuration elements (e.g. User Defined Variables) as these are run from a separate thread

Request

Response

19

May 13, 2009

MachineName

The machineName function returns the local host name. Its very useful while implementing Distributed testing ${__machineName(Machine_Name)} Request //argument is the Variable name to store value Response

20

May 13, 2009

Time

The time function returns the current time in various formats. ${__time(yyyyMMdd-HHmmss,time1)} * YMD = yyyyMMdd * HMS = HHmmss * YMDHMS = yyyyMMdd-HHmmss //1st argument is format. It can be

Second parameter is variable to store value.

${__time(${__property(time1.USER1)})}
Jmeter property

//Assign value to time.USER1 variable in the

Default is YMD.

21

May 13, 2009

Time contd...

Request

Response

22

May 13, 2009

Log

The log function logs a message, and returns its input string ${__log(Message)} ${__log(Message,OUT)} ${__log(${VAR},,,VAR=)} Request //will write to the log file as "...thread Name Message" //will write to console window //will write to log file as "...thread Name VAR=value"

23

May 13, 2009

Log contd..
Request Response

24

May 13, 2009

Logn

The logn function logs a message, and returns the empty string ${__logn(VAR1=${VAR1},OUT)} Request //will write the value of the variable to the console Response

25

May 13, 2009

Input Related Functions:

StringFromFile FiletoString CSVRead Xpath

26

May 13, 2009

StringFromFile

The StringFromFile function can be used to read strings from a text file.

${__stringency(PIN#'.'DAT,,1,2)} ${_StringFromFile(PIN.DAT,,,2)} Request

//reads PIN1.DAT, PIN2.DAT // reads PIN.DAT twice Response

27

May 13, 2009

FiletoString

The FileToString function can be used to read an entire file. Each time it is called it reads the entire file. ${__fileToString(abc.txt,,rname)} //First argument specifies file name,second

specifies file encoding and third specifies reference variable) Request Response

28

May 13, 2009

CSVRead

The CSVRead function returns a string from a CSV file. ${__CSVRead(data.txt,0) //will return first column first value from first row ${__CSVRead(data.txt,1) ${__CSVRead(data.txt,next) //will return second column first value and change the row ${__CSVRead(data.txt,0) //will return first column first value from second row

29

May 13, 2009

CSVRead contd...
Request Response

30

May 13, 2009

XPath

The XPath function reads an XML file and matches the XPath. Each time the function is called, the next match will be returned. At end of file, it will wrap around to the start. If no nodes matched, then the function will return the empty string, and a warning message will be written to the JMeter log file. ${__XPath(/home/Garvita/build.xml, //target/@name)} //will return the contents of the next name attribute in build.xml

31

May 13, 2009

Xpath contd...
Request Response

32

May 13, 2009

Calculation Related Functions

Counter intSum

longSum
Random

33

May 13, 2009

Counter

The counter generates a new number each time it is called, starting with 1 and incrementing by +1 each time. The counter can be configured to keep each simulated user's values separate, or to use the same counter for all users. A global counter is like counting how many times that request was run. ${__counter(true,x)} //First argument specifies whether the counter is global or

not, false => global counter, true => each user counter to be kept independent. second argument specifies reference variable

34

May 13, 2009

Counter contd....
Request Response

35

May 13, 2009

intSum

The intSum function can be used to compute the sum of two or more integer values. ${__intSum(${x},5,z)} //1st Argument retrieve x value of variable x, 2nd is constant value, 3rd argument will store result Request Response

36

May 13, 2009

longSum

The longSum function can be used to compute the sum of two or more long values. ${__longSum(109999,100009,z)} //First and second argument specifies value, 3rd will store result. Request Response

37

May 13, 2009

Random

The random function returns a random number that lies between the given min and max values. ${__random(1,100,x) //1st argument is minimum value, 2nd argument is maximum value and 3rd argument is variable to store value. Request Response

38

May 13, 2009

Scripting Related Functions

JavaScript BeanShell

39

May 13, 2009

Javascript

The javaScript function executes a piece of JavaScript (not Java!) code and returns its value. ${__javascript(new Date)} Request //will return current date Response

40

May 13, 2009

Beanshell

The BeanShell function evaluates the script passed to it, and returns the result. ${__beanshell(123*456)} //will return 56088

${__beanshell(source("/home/Gavita/abc.bsh"))} //if abc.bsh is present at this location ${__beanshell(source(abc.bsh)) //if abc.bsh is present in JMeter bin directory If abc.bsh contains a=8; eval("b=a*2"); print(b); //It will Print result to terminal as 16

41

May 13, 2009

Beanshell contd...
Request Response

42

May 13, 2009

Properties Related Functions

P Property

SetProperty

43

May 13, 2009

This is a simplified property function which is intended for use with properties defined on the command line. and if no default value is supplied, it is assumed to be 1. The value of 1 was chosen because it is valid for common test variables such as loops, thread count, ramp up etc. Define the property value: jmeter -Jgroup1.threads=7 -Jhostname1=www.realhost.edu Fetch the values: ${__P(group1.threads)} ${__P(group1.loops)} //return the value of group1.threads //return the value of group1.loops

${__P(hostname,www.dummy.org)} //return value of property hostname or

www.dummy.org if not defined


In the examples above, the first function call would return 7, second would return 1 and last would return www.dummy.org (unless those properties were defined elsewhere!)
44 May 13, 2009

P contd...
Request Response

45

May 13, 2009

Property

The property function returns the value of a JMeter property. If the property value cannot be found, and no default has been supplied, it returns the property name. When supplying a default value. For example: ${__property(x)} - return value of x ${__property(x,y)} - return value of x and save in y ${__property(x,y,a)} - return value of property x (or "a" if not defined) and save in y ${__property(x,,a)} - return value of property x (= "a" if not defined) but don't save it

46

May 13, 2009

Property contd...
Request Response

47

May 13, 2009

SetProperty

The setProperty function sets the value of a JMeter property. The default return value from the function is the empty string, so the function call can be used anywhere functions are valid. The original value can be returned by setting the optional 3rd parameter to "true". ${__setProperty(total_Count,500,)} //First argument is reference name for the property, second argument is value, third argument is optional if set true returns the original value.

48

May 13, 2009

SetProperty contd...
Request Response

49

May 13, 2009

Variables Related Functions

Split V

Eval
EvalVar

50

May 13, 2009

Split

The split function splits the string passed to it according to the delimiter, and returns the original string. If any delimiters are adjacent, "?" is returned as the value. The split strings are returned in the variables ${VAR_1}, ${VAR_2} etc. The count of variables is returned in ${VAR_n}. Example: name="Garvita&&mehta&" in the test plan. ${__split(${name},name,&)} //returns the contents of name, i.e. "Garvita&&mehta&" name_n=4 name_1=Garvita name_2=? name_3=mehta //will return the number of splited strings //will return the first splitted string //return ? as two delimiters are adjacent //will return the third splited string and so on.

51

May 13, 2009

Split contd...
Request Response

52

May 13, 2009

The V (variable) function returns the result of evaluating a variable name expression. This can be used to evaluate nested variable references. ${__V(A${N})} //will return the value of A1 when N is defined as N=1 Response

Request

53

May 13, 2009

Eval

The eval function returns the result of evaluating a string expression. It allows to interpolate variable and function references in a string which is stored in a variable. Example: name=Garvita column=Emp_Id table=Employee SQL=select ${column} from ${table} where name='${name}'

${__eval(${SQL})}

// will evaluate as "select Emp_Id from Employee where


name='Garvita'".

54

May 13, 2009

Eval contd...
Request Response

55

May 13, 2009

EvalVar

The eval function returns the result of evaluating an expression stored in a variable. This allows one to read a string from a file, and process any variable references in it. Example: query = "select ${column} from ${table}" column=name table=customers ${__evalVar(query)} //will evaluate as "select name from customers"

56

May 13, 2009

EvalVar contd...
Request Response

57

May 13, 2009

String Related Functions

Regexfunction Char

Unescape
UnescapeHtml escapeHtml

58

May 13, 2009

RegexFunction

The Regex Function is used to parse the previous response (or the value of a variable) using any regular expression (provided by user). The function returns the template string with variable values filled in.

The __regexFunction can also store values for future use.

59

May 13, 2009

RegexFunction contd....
Request Response

60

May 13, 2009

Char

The char function returns the result of evaluating a list of numbers as Unicode characters.

This allows one to add arbitrary character values into fields. Examples: ${__char(0xC,0xA)} = CRLF ${__char(165)} = (yen)

61

May 13, 2009

Char contd....
Request Response

62

May 13, 2009

Unescape

The unescape function returns the result of evaluating a Java-escaped string. See also __char() above.

This allows one to add characters to fields which are otherwise tricky (or impossible) to define via Examples: ${__unescape(\r\n)} = CRLF ${__unescape(1\t2)} = 1[tab]2 the GUI.

63

May 13, 2009

Unescape contd...
Request Response

64

May 13, 2009

UnescapeHtml

Function to unescape a string containing HTML entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports HTML 4.0 entities. Example, the string "&lt;Fran&ccedil;ais&gt;" will become "<Franais>" . If an entity is unrecognized, it is left alone, and inserted verbatim into the result string. e.g. ">&zzzz;x" will become ">&zzzz;x" . Uses StringEscapeUtils#unescapeHtml(String) from Commons

65

May 13, 2009

UnescapeHtml contd...
Request Response

66

May 13, 2009

EscapeHtml

Function which escapes the characters in a String using HTML entities. Supports HTML 4.0 entities. Example: "bread" & "butter" becomes: &quot;bread&quot; &amp; &quot;butter&quot; . Uses StringEscapeUtils#escapeHtml(String) from Commons Lang

67

May 13, 2009

EscapeHtml contd....
Request Response

68

May 13, 2009

Pre-defined Variables
Most variables are set by calling functions or by test elements such as User Defined Variables. In user define variable the user has full control over the variable name

Some variables are defined internally by JMeter.


These are listed below

JMeterThread.last_sample_ok - whether or not the last sample was OK true/false Cookiename - contains the cookie value START variable

69

May 13, 2009

Variable Reporting

The __logn() function reference can be used anywhere in the test plan after the variable has been defined.

The Java Request sampler can be used to create a sample containing variable references; the output will be shown in the appropriate Listener.

For versions of JMeter later than 2.3, there is a Debug Sampler that can be used to display the values of variables etc in the Tree View Listener

70

May 13, 2009

Latest Jmeter Release

Jmeter latest release is Jmeter 2.4. Jmeter 2.4 was released on 2010-07-08.

Earlier Stable release was Jmeter 2.3.4.

71

May 13, 2009

Features in Latest release

HTTP Proxy can now record HTTPS sessions. JUnit sampler now supports JUnit4 annotations. Added JSR223 (javax.script) test elements. MailReader Sampler can now use any protocol supported by the underlying implementation.

An SMTP Sampler has been added.

JMeter now allows users to provide their own Thread Group implementations.
View Results Tree now supports more display options, including search and Regex Testing.

StatCalculator performance is much improved; Aggregate Report etc. need far less memory.

JMS samplers have been extensively reworked, and should no longer lose messages. Correlation processing is improved. JMS Publisher and Subscriber now support both Topics and Queues
72 May 13, 2009

Tips
Use timers to avoid hammering the server Limit the Number of Threads

Hardware will limit the number of threads you can effectively run. A faster machine makes JMeter work harder since it returns request quicker.

User variables

Create a text file containing the user names and passwords. Add a CSV DataSet configuration element. Name the variables USER and PASS. Use ${USER} and ${PASS} within samplers.

Reducing resource requirements


Use non-GUI mode. Use as few Listeners as possible. Reduce samplers by looping (by thread and by controller), and use variables (CSV Data Set) to vary the sample. Use CSV output rather than XML.

73

May 13, 2009

References
JMeter has 2 types of mailing list

The JMeter User List The JMeter Developer List

To subscribe to any mailing list

http://jakarta.apache.org/site/mail2.html

74

May 13, 2009

Thank You !