This Blog Post and the related YouTube Video describes how to integrate a simple Java API (JAR file) into LoadRunner (VuGen script). This post explores selecting the JDK version, sample API, Setup VuGen, Create the VuGen Script and running the script. All of the source code can be downloaded from my GitHub account.
I am currently looking around for my next challenge and am responding to many opportunities in the market. One selection criteria I was responding to triggered this blog post “Demonstrated Script Development experience in Windows Sockets protocol” The customer appears to be using Win Socket instead of scripting at the API layer. Scripting at the API layer will reduce script creation time, increase script readability and maintainability.
Yes, I have recommended the use of Windows Socket on a site with a very simple UDP protocol – however, I have always avoided recreating protocols at the network layer and used native API (java or windows DLL) to generate application load.
This blog post demonstrates how I integrate java APIs into my LoadRunner VuGen scripts. As I will use a blank windows image I will start by the selecting the JDK Version, Sample API, setup VuGen, Create VuGen Script and Run the VuGen Script.
Selecting the JDK version
Select the correct version of Java – getting this wrong can result in the VuGen script not working. As I am using LoadRunner 12.55 – the highest Java version can be found in the LoadRunner 12.55 What’s New Page. LoadRunner 12.55 is comparable with Java 8, 32 bit. For this blog post, I will be using Java SE Development Kit 8u161, 32 bit.
The Sample Java API
For the purpose of demonstration, I have created
- A very simple server that prints the request to standard out and responds to a connection with this website
- A very simple API that connects to the server and sends a request
- A very simple demonstration application that invokes the API and sends my LinkedIn page and prints the response to standard out
All of the API code can be downloaded from my GitHub account.
The server can be started by the provided scripts: JAR/startServer.bat or JAR/startServer.sh
The demonstration program can be started provided scripts: JAR/demo.bat or JAR/demo.sh
Most APIs are supplied with a simple sample demo application utilizing the API. In this case, it is a simple static function call com.performancetestgurus.SimpleClient.sendMessage(IP address, port, message);
/** * Example use of API for LoadRunner Java API example * * @author Reanrd Vardy * * Copyright (C) 2018 Renard Vardy, www.performancetestgururus.com/copyright * */ public class App { public static void main(String args[]) { System.out.println( SimpleClient.sendMessage( "127.0.0.1", 8081, "linkedin.com/in/renard-vardy")); } }
We will use this call later in our VuGen Java script. Up next is setting up the VuGen Java runtime environment.
Setup VuGen
This section is setup the VuGen script java environment and the creating of the sample script. First, create a new Single Protocol Java User.
In the new VuUser to our java installation. This is achieved by going to
Replay->Runtime Settings (F4) or double-clicking on Runtime Settings in the solution explorer
Select Java VM
Enter your JDK location – mine is located at C:\Program Files (x86)\Java\jdk1.8.0_161
Next set the Classpath – this tells LoadRunner where the API is located and gives your script access to the demo API. I have copied the SimpleServer.jar file from my GitHub account to C:\SimpleServer. I then added SimpleServer.jar to my classpath by selecting Classpath in the Runtime Settings, Add File, navigated to SimpleServer.jar.
Create VuGen Script
Now that the VuGen Java environment is setup we can now create our script.
init() is executed ONCE at the start of execution
action() is executed multiple times through the test
end() is executed ONCE at the end of script execution
We will add our call to the API to the action function. Adding a call to the Java API in the action function. This can be achieved by adding:
com.performancetestgurus.SimpleServer.SimpleClient.sendMessage(“127.0.0.1”, 8081, “www.linkedin.com/in/renard-vardy”);
After adding assertions, transactions and logging the return value the VuGen script looks something like this:
/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: Sample API VuGen Script * * @author Reanrd Vardy * * Copyright (C) 2018 Renard Vardy, www.performancetestgururus.com/copyright * */ import lrapi.lr; public class Actions { public int init() throws Throwable { return 0; }//end of init public int action() throws Throwable { lr.start_transaction("API Call"); String returnValue = com.performancetestgurus.SimpleServer.SimpleClient. sendMessage("127.0.0.1", 8081, "www.linkedin.com/in/renard-vardy"); lr.log_message(returnValue); if(returnValue.contains("performancetestgurus.com")){ lr.end_transaction("API Call", lr.AUTO); } else{ lr.end_transaction("API Call", lr.FAIL); } return 0; }//end of action public int end() throws Throwable { return 0; }//end of end }
Run the VuGen Script
Before running the script – start the Simple Server by running JAR/simpleServer.bat or JAR/simpleServer.sh. Then select the replay (play) button on the VuGen toolbar. The output of a successful run should look something like this.
You must be logged in to post a comment.