Manually Create a Java Script

This task describes how to manually create and edit a custom Java script using the Java Vuser protocol.

Note: To work with Java scripts, make sure that your Java environment is set up. For details, see Set up the environment for Java protocols.

  1. Create a new script:

    1. Open VuGen.

    2. Select File > New or click the New button. The New Virtual User dialog box opens.

    3. From the Select Vuser type list, select Custom > Java Vuser and click OK. VuGen displays a blank Java Vuser script.

    4. Click the Actions section in the left frame to display the Actions class.

  2. Insert your code into the script

    After generating an empty template, you can insert the desired Java code. When working with this type of Vuser script, you place all your code in the Actions class. To view the Actions class, click Actions in the left pane. VuGen displays its contents in the right pane.

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

    The Actions class contains three methods: init, action, and end. 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, cleaning up the environment, and so forth.

    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;
        }
    
  3. Insert additional API functions

    VuGen provides Java-specific functions for Java Vuser scripts. These functions are all static methods of the lrapi.lr class.

    The Java API functions are classified into several categories: Transaction, Command Line Parsing, Informational, String, Message, and Runtimefunctions.

    For more information about each of these functions, see the Function Reference. Note that when you create a new Java Vuser script, the import lrapi.* is already inserted into the script.

  4. Insert additional Java functions

    To use additional Java classes, import them at the beginning of the script as shown below.

    Make sure that the additional classes are thread-safe and scalable.

    import java.io.*;
    import lrapi.*;
    public class Actions
    {
    ...
    }
    
  5. Set the classpath

    Set the classpath to the Java classes in Runtime Settings > Java Environment > Classpath. These files are then used as a reference, but are not contained in the script's folder.

  6. Add script enhancements

    Enhance your script with rendezvous points, transactions, output messages, and so on. For more information, see Enhance a Java script.

Note: If additional files are required during replay, add them as follows: In the Solution Explorer pane, right click the Extra Files node and select Add Files to Script. The files are copied to the script's folder.

Back to top

See also: