The Mechanics of Copying Custom Code
Custom code in performance test scripts can be intimidating. What does it do? How can we take advantage of custom code from this script in other scripts, and even in other places within the same script?
We will not be spending time on the first question – “What does it do?”. There is no generic way to discuss the purpose of every custom code Class. Here we will be discussing how to use code from Point A at Point B.
The examples will center around this test – OriginalTest.testsuite:

It’s a test generated from a recording of a simple Google search.
Some custom code was added to the test here:
As the Class name – CountPages – suggests, the code counts the pages in the test. It is certainly not complicated code as custom code goes, but the intent is educational rather than functional.
package customcode;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
/**
* @author RPT L2 support
*/
public class CountPages implements com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {
private static int numPages = 0;
/**
* Instances of this will be created using the no-arg constructor.
*/
public CountPages() {
}
/**
* For javadoc of ICustomCode2 and ITestExecutionServices interfaces, select 'Help Contents' in the
* Help menu and select 'Extending Rational Performance Tester functionality' -> 'Extending test execution with custom code'
*/
public String exec(ITestExecutionServices tes, String[] args) {
return Integer.toString(++numPages);
}
}
In a typical scenario, you might have this test with this custom code and the general instruction to “use the same custom code at other places in your test or in new tests recorded against the same server application.” It’s not necessarily intuitive how to do that.
Reusing Code Within Same Test
From the example, the CountPages code is only used once, at the end of the Google page. Obviously, that only counts one page; we’d want to call that code at the end of every page. Let us work through adding it at the end of the third page - /gen_204_gen_204.
- Click to select the complete_search page.
- Click the Insert button.
- Select Custom Code.
- Note that a placeholder for a new custom code Class has been inserted into the script.
- Additionally, a default Custom Code Details panel has appeared in the right-hand panel.
This is just an empty template for custom code. As there is no code yet, there is a warning message to confirm that.
- The code we want already exists. We just need to access it. Change the Class name from the default – customcode.Class – to customcode.CountPages.
- The example has no arguments.
If your code does require arguments, use the Add or Text button to supply those arguments as appropriate.
Reusing Code Within Same Project
Copying code to a new test within the same project as your existing custom code works the same as copying within a single test.
Reusing Code Within Same Workspace
It’s trickier to copy custom code when the custom code resides in another project.
We have created a second project – SecondSampleProject – and a new test – NewTestNewProject.testsuite. We identified the proper location at which to insert the CountPages custom code. We have modified the Class name.
The warning message that there is no Java file persists:
Custom Code customcode.CountPages has no Java file.
- Click the Generate Code button.
Generate Code does what the name implies. It creates some code for your custom code Class. However, it is “empty” code; it’s just a template within which you are expected to add your code.
- While editing the new CountPages code, click ctrl-a to select all the Java code
- Click the Delete key to clear the editor window. The new CountPages is now “empty”.
- Open the original CountPages code from the original project.
- Click ctrl-a followed by ctrl-c to select the code and copy it to the clipboard.
- Return to the new, now-empty copy of CountPages and click ctrl-v to insert from the clipboard.
- Save your changes.
Reusing Code Within A New Workspace
Copying code to a new test within a new workspace works the same as copying to a new test in a new project.
What If Cut/Paste Is Not Practical?
Sometimes you want to move custom code from one workspace to another and those workspaces are on different machines… and you don’t have a copy/paste option.
On the machine where the original custom code exists:
- In the Test Navigator panel, select the Java file. Note the J icon.
- Right-click to select Export > General > File System.
- Click the Next button.
- Expand the src folder and click on customcode:
- The right-hand panel displays all the custom code files that exist in the custom code folder. Select all that you would like to copy to the new workspace. In the example we only have interest in CountPages.
- Provide a destination directory in the “To directory” field, ex. C:\tmp. If possible, you should consider using a shared directory in your environment.
- Check the box next to “Create only selected directories”.
- Click the Finish button.
- Double-check that <destinationDirectory>\CountPages.java exists.
On the machine to which you would like to copy the code:
- If not already done, create the test and the custom code template for the custom code Class to be copied. The new template must have the same Class name as the code you wish to copy. The example uses “CountPages”.
- Make sure the custom code tab is closed (not visible to be edited). You want to make sure to avoid a potential conflict when trying to overwrite.
- Import the custom code with File > Import > General > File System.
- Browse to the directory to which you exported the custom code. Select the custom code elements to copy.
- Browse into the new project. Make sure your Into folder path is <NewProject>/src/customcode. Click the OK button.
- Check the box next to “Overwrite existing resources without warning”.
- Click the Finish button.
The correct custom code is now in place in the new workspace.
#RationalPerformanceTester