Thursday, November 18, 2010

Pacing code using Openscript application

OATS(Oracle application testing suite) comes with an OpenScript application, which is used for creating the load scripts. The application is based on Eclipse IDE and uses Java for scripting. During my analysis of the tool, I could not find an option to set pacing and therefore wrote a Java code to do so. 

In load testing tool such as LoadRunner, you can set the passing through Run-Time Setting option.

//Import these file for date, random number etc
import java.util.Random;
import java.util.Date;
import java.util.*;
import java.text.*;
import java.io.*;

import oracle.oats.scripting.modules.basic.api.internal.*;
import oracle.oats.scripting.modules.basic.api.*;
import oracle.oats.scripting.modules.http.api.*;
import oracle.oats.scripting.modules.http.api.HTTPService.*;
import oracle.oats.scripting.modules.utilities.api.*;
import oracle.oats.scripting.modules.utilities.api.sql.*;
import oracle.oats.scripting.modules.utilities.api.xml.*;
import oracle.oats.scripting.modules.utilities.api.file.*;


public class script extends IteratingVUserScript {
@ScriptService oracle.oats.scripting.modules.utilities.api.UtilitiesService utilities;
@ScriptService oracle.oats.scripting.modules.http.api.HTTPService http;
/************** LOCAL VARIABLES FOR THE SCRIPT************************/
static int MIN=90; //min percentage think time 90%
static int MAX=110; //max percentage think time 110%
static float Pacing=(float) 100.0; //expected iteration completion time


/************this function generates a random time between 90% and 110% of the recorded time**************/
long calRandtime(int Rectime, int minPerc,int maxPerc)
{
Random aRandom= new Random();
int range = (int)(Rectime*(maxPerc-minPerc)/100) + 1;
// compute a fraction of the range, 0 <= frac < range
int fraction = (int)(range * aRandom.nextDouble());
long randomNumber = (long)((fraction + (Rectime*maxPerc/100))*1000);
System.out.println("The calculated think time is " + randomNumber/1000);
return (randomNumber);
}

public void initialize() throws Exception {

}

public void run() throws Exception
{
beginStep("ALL");
{
long now = System.currentTimeMillis(); //Get current time in milliseconds
System.out.println(now); //print out time in milliseconds

Thread.sleep(calRandtime(20,MIN,MAX)); //sleep the thread for random calculated time
beginStep("Step1");
{
System.out.println("Transaction 1 completed");
}
endStep();

Thread.sleep(calRandtime(10,MIN,MAX));
beginStep("Step2");
{
System.out.println("Transaction 2 completed");
}
endStep();

Thread.sleep(calRandtime(5,MIN,MAX));
beginStep("Step3");
{
System.out.println("Transaction 3 completed");
}
endStep();

long diff = System.currentTimeMillis()- now; // calculate how long it took to execute the code
float seconds= diff/1000.0f; //convert milliseconds into seconds
System.out.println("It took " + seconds+" seconds to execute the code"); //print out how long it took ti execute the code

if(seconds<=Pacing) //check if execution time is less than expected pacing time
{
System.out.println("Going to sleep for");
System.out.println(Pacing-seconds); //calculated second for which the code needs to sleep
Thread.sleep((long)(Pacing-seconds)*1000);
System.out.println("Finished sleeping");
}
}
endStep();
}

public void finish() throws Exception {
}
}
The code is self explanatory. Following is an execution of the above code as capture in console window of this tool.

No comments: