Enhance a Java script

This topic describes how to enhance custom Java scripts.

Insert transactions

You define transactions to measure the performance of the server. Each transaction measures the time it takes for the server to respond to specified requests. These requests can be short or complex tasks.

When working with LoadRunner Professional, you can analyze the performance per transaction during and after the scenario run, using online monitor and graphs.

You can also specify a transaction status: lr.PASS or lr.FAIL. You can let the Vuser automatically determine if the transaction was successful, or you can incorporate it into a conditional loop. For example, in your code you can check for a specific return code. If the code is correct, you issue a lr.PASS status. If the code is wrong, you issue an lr.FAIL status.

Back to top

Mark a transaction

  1. Insert lr.start_transaction into the script, at the point where you want to begin measuring the timing of a task.

  2. Insert lr.end_transaction into the script, at the point where you want to stop measuring the task. Use the transaction name as it appears in the lr.start_transaction function.

  3. Specify the desired status for the transaction: lr.PASS or lr.FAIL.

    public int action() {
    for(int i=0;i<10;i++)
       {
        lr.message("action()"+i);
        lr.start_transaction("trans1");
        lr.think_time(2);
        lr.end_transaction("trans1",lr.PASS);
       }
        return 0;
    }
    

Back to top

Insert rendezvous points

To emulate heavy user load on your client/server system, you synchronize Vusers to perform a task at exactly the same moment by creating a rendezvous point. When a Vuser arrives at the rendezvous point, it is held by Controller until all Vusers participating in the rendezvous arrive.

You designate the meeting place by inserting a rendezvous function (lr.rendezvous) into your Vuser script at the point where you want the Vusers to perform a rendezvous.

public int action() {
  for(int i=0;i<10;i++)
   {
    lr.rendezvous("rendz1");
    lr.message("action()"+i);
    lr.think_time(2);
   }
   return 0;
}

Back to top

Obtain Vuser information

You can add the following functions to your Vuser scripts to retrieve Vuser information:

lr.get_attrib_string
Returns a string containing command line argument values or runtime information such as the Vuser ID or the load generator name.
lr.get_group_name
Returns the name of the Vuser's group.
lr.get_host_name
Returns the name of the load generator executing the Vuser script.
lr.get_master_host_name
Returns the name of the machine running Controller or Business Process Monitor.
lr.get_scenario_id
Returns the ID of the current scenario. (LoadRunner Professional only)
lr.get_vuser_id
Returns the ID of the current Vuser. (LoadRunner Professional only)

In the following example, the lr.get_host_name function retrieves the name of the computer on which the Vuser is running.

String my_host = lr.get_host_name();

Back to top

Issue output messages

When you run a scenario, the Controller Output window displays information about script execution. You can include statements in a Vuser script to send error and notification messages to Controller. Controller displays these messages in the Output window. For example, you could insert a message that displays the current state of the client application. You can also save these messages to a file.

Note: Do not send messages from within a transaction. Doing so lengthens the transaction execution time and may skew the actual transaction results.

You can use the following message functions in your Vuser script:

lr.debug_message
Sends a debug message to the Output window.
lr.log_message
Sends a message to the Vuser log file.
lr.message
Sends a message to a the Output window.
lr.output_message
Sends a message to the log file and Output window with location information.

In the following example, lr.message sends a message to the output indicating the loop number:

for(int i=0;i<10;i++)
   {
    lr.message("action()"+i);
    lr.think_time(2);
   }

You can instruct the Vusers to redirect the Java standard output and standard error streams to VuGen's Execution log. This is especially helpful when you need to paste existing Java code or use ready-made classes containing System.out and System.err calls in your Vuser scripts. In the execution log, standard output messages are colored blue, while standard errors are shown in red.

The following example shows how to redirect specific messages to the standard output and standard error using lr.enable_redirection:

lr.enable_redirection(true);
System.out.println("This is an informatory message...");  // Redirected
System.err.println("This is an error message...");  // Redirected
lr.enable_redirection(false);
System.out.println("This is an informatory message...");  // Not redirected
System.err.println("This is an error message...");  // Not redirected

Note: When you set lr.enable_redirection to true, it overrides all previous redirections. To restore the former redirections, set this function to false.

Back to top

Emulate user think time

The time that a user waits between performing successive actions is known as the think time. Vusers use the lr.think_time function to emulate user think time. In the following example, the Vuser waits two seconds between loops:

for(int i=0;i<10;i++)
   {
    lr.message("action()"+i);
    lr.think_time(2);
   }

You can use the think time settings as they appear in the script, or a factor of these values. To configure how Vusers handle think time functions, open the runtime settings dialog box. For more information, see Runtime settings.

Back to top

Handle command line arguments

You can pass values to a Vuser script at runtime by specifying command line arguments when you run the script. You insert command line options after the script path and filename in the Controller or Business Process Monitor. There are three functions that allow you to read the command line arguments, and then to pass the values to a Vuser script:

lr.get_attrib_double
Retrieves double precision floating point type arguments
lr.get_attrib_long
Retrieves long integer type arguments
lr.get_attrib_string
Retrieves character strings

Your command line should have the following format, where the arguments and their values are listed in pairs after the script name:

script_name   - argument argument_value   -argument argument_value

The following example shows the command line string used to repeat script1 five times on the machine pc4:

script1  -host pc4  -loop 5 

For more information on how to insert the command line options, see Run a Vuser script from a command prompt.

Back to top

See also: