Showing posts with label pacing. Show all posts
Showing posts with label pacing. Show all posts

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.

Monday, November 15, 2010

Pacing issue with LoadRunner 9.5

One of my collegue discovered an issue with pacing in LoadRunner 9.5. It seems that if there is a pacing applied to the script then the VU waits for sometime after completing vuser_init function before executing Action function.

To confirm the issue he created following script with a randomized pacing between 90-120sec.
vuser_init()
{
 lr_output_message (lr_eval_string("init: {Timestamp}"));

 return 0;
}
 
Action()
{
 
 lr_output_message (lr_eval_string("Iteration: {Iteration} {Timestamp}"));

 return 0;
}
 
vuser_end()
{
 
 lr_output_message (lr_eval_string("end: {Timestamp}"));

 return 0;
}

Following is the output from Replay Log.



Based on the timestamp between vuser_init and iteration 1 we know that 60 seconds of pacing was applied even though the replay log does not show 'Waiting ## seconds for iteration pacing.' at the end of 'vuser_init'.

What this means is that, if you have a long pacing the vu will not generate load for a while eventhough LR scenario say's that the VU is in run state.

One way to overcome this issue is to write your own pacing code and setting pacing in run-time setting as "As soon as the previous iteration ends". See the Replay log after adding pacing code and changing the runtime setting. In this case, the iteration is expected to finish in 105seconds.



I haven't tested this issue in LR11. Let me know if this issue still exists in LR11.