How to implement a conditional loop in Rational Performance Tester

An automation script example for conditional looping

This article is for automation engineers with beginner to intermediate levels of experience in Rational Performance Tester who want to be able to create a conditional loop with minimal effort. Vishalendu Pandey takes you step-by-step through the process to make it easier to understand.

Share:

Vishalendu Pandey (vishalendu@in.ibm.com), Senior Performance Engineer, IBM

Author1 photoVishalendu Pandey has been working as a systems and performance engineer for IBM Supply Chain Application for more than five years. During this time, he has designed automation scripts using various automation tools, including Rational Performance Tester for systems and performance tests.



12 June 2012

Also available in Chinese Russian

Complexity of the automation increases day by day with changing requirements. To cope with them, we need to develop an automation framework more intelligently. Decision control is a one of the key concepts in the creation of complex automation.

In IBM® Rational® Performance Tester, you can define part of a test as a conditional loop, which is a loop that runs a specified number of times. You can set the duration of the loop according to count-based, time-based, and infinite options. But the software does not provide a way to dynamically set the target value of the loop. A beginner working in Rational Performance Tester might not have the sample code to get started on this. Therefore, the purpose of this article is to guide you on the implementation of a conditional loop. This article walks you through a complete example of an automation script for conditional looping. However, this might not be the only way of achieving the implementation.

Create a conditional loop in Rational Performance Tester

The logic or control part of the loop resides inside custom code (Java class) that is responsible for limiting the number of iterations of the loop to a finite number.

Start with a fresh performance test. Screen captures that follow show how to do each task.

Open a new test in Rational Performance Tester

Note:
Only a few steps from the Create New Test Wizard are shown here. For the rest, wizard screens assume that no changes have been made to the defaults, so you can simply click the Next button.

  1. From the File menu, click New > New Test.
Figure 1. Creating a new test
Screen show Tests > New > New Test selected
  1. In the Test File Name and Location dialog window, provide a name for the test. For this example, I named the test LoopExample.
Figure 2. Providing a test name
New Test Wizard window shows location, file name
  1. In the "Protocol and Features" dialog window, only check the Core Features check box for this demo.
Figure 3. Selecting test protocol and features
Protocol and Features view, Core Features box checked

Add an infinite loop to the test

  1. After finishing with the Create New Test wizard, in the window where the new test opens, click the Add button, and select the Loop element.

This will add a loop element to the test.

Tip:
You can invoke the same context menu by right-clicking the test name (LoopExample in Figure 4).

Figure 4. Adding a loop from the Add context menu
Loop option selected on the drop-down menu
  1. Select the newly added Loop element.
  2. On the right, in the Test Element Details section, under Duration, select the Infinite radio button from the options listed.
Figure 5. Changing the loop's default duration
Select Infinite from the radio button group

Larger view of Figure 5.

Add custom code element to control the loop

  1. Right-click the Loop element.
  2. From the context menus, select Add and then Custom Code.

This Java code will control the number of iterations of the loop, based upon some boundary condition. In our demo test script, we are going to use another class located outside of the loop in the script to get a random value for number of iterations.

Figure 6. Adding custom code inside the Loop element
Loop > Add > Custom code selected on menus

Note:
While naming the custom code, please follow naming conventions. Given that all of the classes are saved under the same directory by default, it is good to have separate namespaces for better readability and to reduce confusion. For example:

  • customcode.com.ApplicationName.search.controlSearchLoop
  • customcode.com.ApplicationName.create.controlItemAddLoop

Those two examples show two custom code names that are for two separate types of automation script. The first one is for a search type script, while the second one is a data creation type script. Simply put, this is just in keeping with Java's naming conventions and recommendations.

Add another custom code before the loop to simulate a dynamic value to control the loop

  1. Right-click the loop element, and select Insert from the context menu to insert a custom code before the loop element.

This class is added to provide a random value to the loop controlling custom code to limit the infinite iterations of the loop to a predetermined value. In real-world scenario, this value can be replaced by any value that you decide to make a loop-breaking condition. You will need to modify the loop control custom code accordingly.

For reference, these are the names of the classes in this example:

customcode.returnRandomValue

customcode.controlMyLoop

  1. Notice the interesting behavior of the Add and Insert options in the context menu. Use Add to apply an element after the reference element, and use Insert for one before the reference element. Here, the reference element is the one that is right-clicked, which is the Loop element in this case.

Quick reference:

  • Add for after
  • Insert for before

Your test should now look like Figure 7.

Figure 7. Test design screen
Test Contents view shows Loop with custom code

Write the custom code

Listing 1. customcode.returnRandomValue (for random loop executions)
public String exec(ITestExecutionServices tes, String[] args) {
	int rndNumber = (new Random().nextInt(10))+1;
	tes.getTestLogManager().reportMessage("Random Number:"+rndNumber);
return  ""+(rndNumber);}

In this code, to simulate a dynamic value, you are using a random number to generate an integer number between 1 and 10. You are going to pass this to the custom code (customcode.controlMyLoop) inside the loop for controlling the loop iterations.

You can find the complete code in this file (see the Downloads section):
returnRandomValue.java

Note:
tes.getTestLogManager().reportMessage() is a Rational Performance Tester method that I used in the previous listing to print the random number. This will appear in two places:

  • In the test Log
  • Under Protocol Data > Event Log

Generally, while designing the scripts, I use a lot of these statements to check dynamic values. These are good for debugging functional and code issues. However, I disable them when doing a long-duration run.

Listing 2. customcode.controlMyLoop
public String exec(ITestExecutionServices tes, String[] args) {
                int i = tes.getLoopControl().getIterationCount();
                if(i>Integer.parseInt(args[0]))
                       tes.getLoopControl().breakLoop();
                else
                       tes.getTestLogManager().reportMessage("Iteration No:"+i);
		
                return null;
         }

You can find the complete code in this file (see the Downloads section):
controlMyLoop.java

The screen capture in Figure 8 shows how the return value of returnRandomValue custom code is passed to the controlMyLoop custom code as an argument (args[0]), under Test Element Details section for customcode.controlMyLoop.

Figure 8. Random output from returnRandomValue
Arguments section under Test Element Details

The code tests the iteration number for the innermost loop. The innermost loop is broken when its iteration number is greater than the test value (which, in this case, is the output of customcode.returnRandomValue)

If the iterations are less than the test value, the code prints the corresponding iteration number.

Note:
One important thing to keep in mind when dealing with the tes.getLoopControl()object is that, when there are multiple loops, it gives the handle to the innermost loop.

For example, in the piece of pseudo code in Listing 3, the myloop reference variable will have handle to LOOP2. Hence, the object can be used to get iteration number or break or continue the loop.

Listing 3.
LOOP1
    {LOOP2
         { ILoopControl myloop = tes.getLoopControl();
    }
  }

After completing the previous steps, start running the script.

The implementer should be able to see that the loop is breaking after a certain number of iterations.

Execute the script

Figure 9 shows that the random number generated by customcode.returnRandomValue was 1 (see the right-side text box). Although the loop runs two times, the condition check in customcode.controlMyLoop (2>1) is satisfied, thus the loop is broken on the second execution.

Therefore, our objective of breaking a loop when a certain condition is met is achieved. However, the conditional check for a number as displayed here can be replaced by any other comparisons.

For example, we can use a reference value from an HTTP request (rather than the custom code output used here), and based upon custom logic, we can break the loop.

Figure 9. Test logs after execution
Test log showing loop breaking with the condition

How to continue a loop with a condition

An added feature of loops is a continue operation. This is useful in certain conditions when you want the loop to skip executing when a certain condition is met. For example, I can use this if I want to execute the loop body only for even values up to a maximum value. In Listing 4, I have modified the earlier code to include this condition, as well.

Listing 4 shows the code to achieve this.

Listing 4. Code change required in customcode.controlMyLoop to skip executing the loop body for non-even iterations
public String exec(ITestExecutionServices tes, String[] args) {
		int i = tes.getLoopControl().getIterationCount();	
    if(i>Integer.parseInt(args[0]))
			tes.getLoopControl().breakLoop();
		else
			if(i%2 !=0)
				tes.getLoopControl().continueLoop();
			else
			tes.getTestLogManager().reportMessage("Iteration No:"+i);
		return null;
	}

On running the test with those changes, the test logs look something like Figure 10.

Figure 10. Test log with the code change to continue the loop
Test log showing loop continue condition

As you can see, the random number generated in customcode.returnRandomValue is 9. The loop execution has been skipped for iterations that are not even-numbered, up until the tenth iteration, where the loop is finally broken.

I hope this article is useful for anyone taking up Rational Performance Tester as for test automation. After going through this article, you should be able to create a conditional loop, even if you are new to Rational Performance Tester. Although there can be other ways to achieve the same goal, this article demonstrates the way that we successfully implemented it in our projects.


Downloads

DescriptionNameSize
Tests the iteration number for the innermost loopcontrolMyLoop.java359KB
Generate an integer number between 1 and 10returnRandomValue.java1KB

Resources

Learn

Get products and technologies

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Rational software on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational
ArticleID=819977
ArticleTitle=How to implement a conditional loop in Rational Performance Tester
publish-date=06122012