Prerequisites:

  • Java 1.8+

  • GenRocket Runtime

  • GenRocket UserProfile


Sample Independent Java Code that use Engine API to modify the Scenario at Runtime


Test.java

import com.genRocket.GenRocketException;
import com.genRocket.engine.EngineAPI;
import com.genRocket.engine.EngineManual;

public class Test {
 Test() {}

 public void execute(String scenarioPath) {
   EngineAPI api = new EngineManual(); // Initializing the EngineAPI

   api.scenarioLoad(scenarioPath); // Loading the scenario file

   String domainName = api.primaryDomain();
   api.domainSetLoopCount(domainName, "100000");

   api.scenarioRun(); // Running the scenario
 }

 public static void main(String[] args) {
   Test test = new Test();
   String scenarioPath = args.length > 0 ? args[0] : null;

   if (scenarioPath == null) {
     throw new GenRocketException("Please specify scenario path in command."); 
   }

   test.execute(scenarioPath); // Calling of execute method
   System.out.println("All Done!");
 }
}



Directory Structure:


Suppose the file Test.java is placed under the directory: C:\Users\ali\javaSource, the GenRocket runtime jars are placed under the directory: %GENROCKET_HOME%/lib


genrocket-3.5.16/lib/

-- genrocket-3.5.16.1.jar

-- gr-engine-3.5.16.3.jar

-- gr-generators-3.5.16.6.jar

-- gr-receivers-3.5.16.1.jar

--

--


Compilation of Java Source Code using External Jars:


Next step is to compile the java source code using the GenRocket jars which are present in GENROCKET_HOME/lib directory.

In order to do so, we need to execute the following command to compile the source code to create the class file.


By using the defined GenRocket Environment Variable in Linux/Mac

javac -cp $GENROCKET_HOME/lib/*: /home/ali/javaSource/Test.java



By using the defined GenRocket Environment Variable in Windows

javac -cp %GENROCKET_HOME%\lib\*; C:\Users\ali\javaSource\Test.java


Execution of Java Source Code using External Jars to Run GenRocket Scenario:


After the compilation, change the directory where the compiled class file is created:


cd C:\Users\ali\javaSource


Then, run the java code by providing the classpath of the external jar files to run the GenRocket Scenarios like:


The syntax for Windows:

java -cp <Jar_File_Full_Path>; <ClassName> <GenRocket_Scenario_Full_Path>


The syntax for Linux:


java -cp <Jar_File_Full_Path>: <ClassName> <GenRocket_Scenario_Full_Path>



e.g for Windows. 


java -cp %GENROCKET_HOME%\lib\*; Test "C:\Users\ali\Downloads\UserScenario.grs"



By following the above steps, you’ll be able to run the GenRocket Scenario using Java.