Java Vuser (manual) protocol

To prepare Vuser scripts in Java, use the Java type Vusers. This Vuser type supports Java on a protocol level. The Vuser script is compiled by a Java compiler and supports all of the standard Java conventions.

To create a Java-compatible Vuser script, you first create a new Vuser script and select the Java Vuser protocol. Then, insert your Java code into the script. You can add Java Vuser functions to enhance the script and parameterize the arguments to use different values during iterations. For details, see Manually Create a Java Script.

After you prepare a script, run it as a standalone test from VuGen. A Java compiler (javac) checks it for errors and compiles the script.

Then, you can integrate the script into your environment: a LoadRunner Professional scenario, LoadRunner Enterprise performance test, or Business Process Monitor configuration.

Classes and methods

When working with this type of Vuser script, you place all your code in the Actions class. The Actions class contains three methods: init, action, and end.

import lrapi.*;
public class Actions
{
    public int init() {
        return 0;
    }
    public int action() {
        return 0;
    }
    public int end() {
        return 0;
    }
}

The following table shows what to include in each method and when each method is executed.

Script method
Used to emulate...
Is executed when...
init
a login to a server
the Vuser is initialized (loaded)
action
client activity
the Vuser is in "Running" status
end
a log off procedure
the Vuser finishes or is stopped

Init Method

Place all the login procedures and one-time configuration settings in the init method. The init method is only executed once—when the Vuser begins running the script. The following sample init method initializes an applet. Make sure to import the org.omg.CORBA.ORB function into this section, so that it will not be repeated for each iteration.

import org.omg.CORBA.*;
import org.omg.CORBA.ORB.*;
import lrapi.lr;
// Public function: init 
    public int init() throws Throwable {
    // Initialize Orb instance...
    MApplet mapplet = new MApplet("http://chaos/classes/", null);
    orb = org.omg.CORBA.ORB.init(mapplet, null);
...

Action Method

Place all Vuser actions in the action method. The action method is executed according to the number of iterations you set in the runtime settings. For more information on the iteration settings, see Runtime settings. The following sample action method retrieves and prints the Vuser ID.

 public int action() {
    lr.message("vuser: " + lr.get_vuser_id() + " xxx");
         return 0;
   }

End Method

In the end method, place the code you want the Vuser to execute at the end of the script, such as logging off from a server, or cleaning up the environment.

The end method is only executed once—when the Vuser finishes running the script. In the following example, the end method closes and prints the end message to the execution log.

 public int end() {
        lr.message("End");
        return 0;
    }

The Java Vuser script runs as a scalable multi-threaded application. If you include a custom class in your script, make sure that the code is thread-safe. For details, see Java protocol programming tips.

See also: