Java Vuser protocol
You can manually create a Java-compatible Vuser script using the Java Vuser protocol. The Vuser script is compiled by a Java compiler and supports all of the standard Java conventions.
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.
Manually create a Java script
To create a Java-compatible Vuser script, you create a new Java Vuser protocol script and insert your Java code. You can add Java Vuser functions to enhance the script, and parameterize the arguments to use different values during iterations.
To manually create a Java Vuser script:
-
Create a new Java Vuser script. For details, see Create and open Vuser scripts.
-
Insert your code into the script, in the Actions class, which is displayed in the right pane when the script template opens. For details, see Classes and methods.
-
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 Runtime functions.
For more information about each of these functions, see the Function Reference (select the relevant version). Note that when you create a new Java Vuser script, the import lrapi.* is already inserted into the script.
-
Import additional Java classes into the beginning of the script. Make sure that the additional classes are thread-safe and scalable.
import java.io.*; import lrapi.*; public class Actions { ... }
-
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.
-
Enhance your script with rendezvous points, transactions, output messages, and other elements. For more information, see Enhance 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. You can then integrate the script into your scenarios and performance tests.
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.
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 method |
a login to a server
|
the Vuser is initialized (loaded)
|
Action method |
client activity
|
the Vuser is in "Running" status
|
End method |
a log off procedure
|
the Vuser finishes or is stopped
|
Place all the login procedures and one-time configuration settings in the init method. The init method is executed once only, when the Vuser begins running the script. The following sample init method initializes an applet.
Import the org.omg.CORBA.ORB function into this section, so that it is not 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); ...
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; }
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 executed once only, 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:
- Java protocols
- Java Record Replay protocol for recording and replaying Java scripts
- Java over HTTP protocol for recording and replaying Java remote calls over HTTP