DevOps Automation

 View Only

Tips & Tricks - DevOps Test Performance: The Mechanics of Copying Custom Code

By Jerry Shengulette posted Tue November 29, 2022 09:34 PM

  

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:

Display sample test in RPT test editor


It’s a test generated from a recording of a simple Google search.

Some custom code was added to the test here:

Showing where custom code appears in original test


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.

Highlighting current custom code position and location to which we need to copy



  1. Click to select the complete_search page.
  2. Click the Insert button.
    Showing location of Insert button to the right of test editor panel
  3. Select Custom Code.
    Hoghlighting the Custom Code menu item in the Insert menu box
  4. Note that a placeholder for a new custom code Class has been inserted into the script.
    Here is the new custom code entry for customCode.Class, the placeholder
  5. Additionally, a default Custom Code Details panel has appeared in the right-hand panel.
    Introducing the Custom Code Details panel.

    This is just an empty template for custom code. As there is no code yet, there is a warning message to confirm that.
    Highlighting the
  6. The code we want already exists. We just need to access it. Change the Class name from the default – customcode.Class – to customcode.CountPages.
    Changing Class name from customcode.Class to customcode.CountPages
  7. The example has no arguments.
    Highlighting that the example custom code 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.

Highlighting new project, new test, location to add custom code and the


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.

  1. Click the Generate Code button.
    Highlighting the Generate Code button on the Custom Code Details panel


    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.
  2. While editing the new CountPages code, click ctrl-a to select all the Java code
    Showing the selected code in the template version of CountPages
  3. Click the Delete key to clear the editor window. The new CountPages is now “empty”.
  4. Open the original CountPages code from the original project.
  5. Click ctrl-a followed by ctrl-c to select the code and copy it to the clipboard.
  6. Return to the new, now-empty copy of CountPages and click ctrl-v to insert from the clipboard.
  7. 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:

  1. In the Test Navigator panel, select the Java file. Note the J icon.
    Highlighting the selection of the CountPages code under src/customcode in Test Navigator panel
  2. Right-click to select Export > General > File System.
    Highlighting the Export > General > File System selection
  3. Click the Next button.
  4. Expand the src folder and click on customcode:
    Highlighting checkbox in the Export select box. Checking box for CountPages.java
  5. 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.
  6. Provide a destination directory in the “To directory” field, ex. C:\tmp. If possible, you should consider using a shared directory in your environment.
  7. Check the box next to “Create only selected directories”.
  8. Click the Finish button.
  9. Double-check that <destinationDirectory>\CountPages.java exists.

On the machine to which you would like to copy the code:

  1. 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”.
  2. 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.
  3. Import the custom code with File > Import > General > File System.
    Displaying an empty Import > General > File System dialog.
  4. Browse to the directory to which you exported the custom code. Select the custom code elements to copy.
    Displaying the From directory details for the import action.


  5. Browse into the new project. Make sure your Into folder path is <NewProject>/src/customcode. Click the OK button.
    Displaying Into folder details for the import action
  6. Check the box next to “Overwrite existing resources without warning”.
  7. Click the Finish button.


The correct custom code is now in place in the new workspace.



 

 


#RationalPerformanceTester

0 comments
13 views

Permalink