Friday, November 2, 2012
Amdocs response time calculator
import lrapi.lr;
import java.io.File;
import java.util.Date;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import java.sql.*;
import java.util.*;
import java.math.*;
import java.text.*;
public class Actions
{
int NetTimeDiff = 0;
int TimeDiff = 0;
long totalTime = 0;
SqlReader Sqlr_orch;
SqlReader Sqlr_router;
private static String CF1 = "com.clarify.jms.bridge.connectionFactory.boss.LBCF";
private static String RouterInQ = "com.clarify.jms.boss.router.in";
private static int sleepTime = 100;
public int init()
{
String sattrValue = null;
try
{
Sqlr_orch = new SqlReader(lr.eval_string("jdbc:oracle:thin:@localhost:1522:VTOR"), "sa", "sa");
Sqlr_router = new SqlReader(lr.eval_string("jdbc:oracle:thin:@localhost:1523:VTRO"), "sa", "sa");
}
catch(Exception e)
{
e.printStackTrace();
lr.exit(lr.EXIT_VUSER, lr.FAIL);
return 1;
}
return 0;
}
public int action()
{
String sqlResRouter [] = null;
String sqlResOrch [] = null;
String OrderId;
String OrderIdSubString;
String subHHRot1 = null;
String subHHRot = null;
String subHHOrc = null;
String subMMRot = null;
String subMMOrc = null;
String subSSRot = null;
String subSSOrc = null;
int intsubHHRot = 0;
int intsubHHOrc = 0;
OrderId = lr.eval_string("<OrderId>");
try{
sqlResRouter = Sqlr_router.RunQuery("select MIN(OSS_RECEIVED_DATE) from TABLE_OSS_ROUTER_MSG "+
"WHERE OSS_RECEIVED_DATE > SYSDATE - 1 "+
"AND OSS_CONVERSATION_ID like ? ",
"OSS_RECEIVED_DATE",
OrderId);
sqlResOrch = Sqlr_orch.RunQuery("SELECT LAST_UPDATE FROM TABLE_CONTRACT "+
"WHERE LAST_UPDATE > SYSDATE - 1 " +
"AND STATUS = 'Complete' " +
"AND OSS_CMP_ORDER_ID LIKE ? ",
"LAST_UPDATE",
OrderId);
String rotTime = sqlResRouter[0];
String orcTime = sqlResOrch[0];
//
System.out.println("rotTime: " + rotTime);
System.out.println("orcTime: " + orcTime);
long timeDiffinSec = timeDiff(rotTime, orcTime);
// System.out.println("timeDiffinSec: " + timeDiffinSec);
// System.out.println("orderid:" + OrderId + " timeDiffinSec " + timeDiffinSec);
totalTime = totalTime + timeDiffinSec;
}
catch (Exception e)
{
//System.out.print("Error: " + e.getMessage());
}
return 0;
}
public int end()
{
// System.out.println("\nNet: " + NetTimeDiff);
int iter = lr.eval_int("<Iterations>");
System.out.println("\nIter: " + iter);
long avg = totalTime/iter;
System.out.println("\nAverage: " + avg);
System.out.println("*****: ");
return 0;
}
public long timeDiff(String initialTime ,String finalTime )
{
long timeDiff=0;
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
//String now = new String( df.format( ) );
try
{
Date time1 = df.parse(initialTime);
Date time2= df.parse(finalTime);
//System.out.println("Today = " + df.format(today));
// System.out.println("time1 in millisecs after parsing : " + time1.getTime());
// System.out.println("time2 in millisecs after parsing : " + time2.getTime());
timeDiff=((time2.getTime()-time1.getTime())/1000);
// System.out.println(" Time Diff in seconds: " + (timeDiff));
} catch (ParseException e)
{
System.out.println(" Exception occurs in parsing date");
e.printStackTrace();
}
return timeDiff;
}
}
--------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class SqlReader
{
private Connection con;
private PreparedStatement ps;
protected String DbName;
protected String DbUser;
protected String DbPass;
public SqlReader(String DbName, String DbUser, String DbPass) throws Exception
{
this.DbName = DbName;
this.DbUser = DbUser;
this.DbPass = DbPass;
connect();
}
public Connection connect()throws Exception
{
try
{
System.out.println("conecting DB..."+DbUser+","+DbPass+"@"+DbName);
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection(DbName,DbUser,DbPass);
System.out.println("returning con");
return con;
}
catch (SQLException e)
{
throw new Exception("Error init sql reader" + e.getMessage(), e);
}
}
public String[] RunQuery(String str_query, String FieldSelector, String BindParameters)
throws Exception
{
System.out.print("str_query" + str_query);
System.out.print("FieldSelector " + FieldSelector);
System.out.print("BindParameters " + BindParameters);
String[] results = new String[100];
String res = null;
String [] temp = null;
temp = BindParameters.split(";");
String timerecv = null;
try
{
ResultSet rs = null;
ps = con.prepareStatement(str_query,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ps.setString(1, BindParameters);
rs = ps.executeQuery();
while (rs.next()) {
res = rs.getString(1);
results[0] = res;
}
}
catch (SQLException e)
{
throw new Exception("Error in sql run query" + e.getMessage(), e);
}
ps.close();
return results;
}
public void close() throws Exception
{
try
{
con.close();
}
catch (SQLException e)
{
throw new Exception("Error closing sql connection : " + e.getMessage(),e);
}
}
}
---------------------------
Random String generator
import lrapi.lr;
public class Actions
{
randomGen randgen;
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
randgen = new randomGen();
String randStringAct = randgen.randomGen(5);
System.out.println("returned in Action:" + randStringAct);
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
----------------
public class randomGen
{
String randomString = "";
public String randomGen ( int length)
{
for (int count = 0; count < length; count ++) {
char randomChar = (char)((int)'A'+Math.random()*((int)'Z'-(int)'A'+1));
randomString = randomChar + randomString;
}
System.out.println("RandomString in Funct: " + randomString);
return randomString;
}
}
Calculate the Stub Delay
/*
* LoadRunner Java script. (Build: 946)
*
* Script Description:
*
*/
import lrapi.lr;
import java.util.Date;
public class Actions
{
private Date date = null;
private Date date1 = null;
private long sendTime;
private long receiveTime;
private long timeDiff;
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
//lr.start_transaction("A");
//lr.start_transaction("A1");
date = new Date();
sendTime = date.getTime();
System.out.println("SentTime: " + sendTime);
// System.out.println("Time: " + date.getDate());
// for(int i=0; i < 10; i++)
// {
//System.out.println("*");
//lr.think_time(1);
// }
date1 = new Date();
receiveTime = date1.getTime();
System.out.println("receiveTime: " + receiveTime);
timeDiff= (receiveTime - sendTime);
System.out.println("Timediff: " + timeDiff);
// System.out.println("Time: " + (receiveTime - sendTime));
// System.out.println("Time*: " + (receiveTime - sendTime)/1000);
// lr.set_transaction("A1", timeDiff, lr.PASS);
lr.set_transaction("A1", 2.5, lr.PASS);
//lr.end_transaction("A", lr.PASS);
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
Print a triangle with characters
import lrapi.lr;
public class Actions
{
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
int N = 10;
for (int i = 0; i < N; i++) {
// print j periods
for (int j = 0; j < i; j++)
System.out.print(" ");
// print N-i asterisks
for (int j = 0; j < N-i; j++)
System.out.print("\\");
// print N-i -
for (int k = 0; k < N-i; k++)
System.out.print("-");
// print N-i -
for (int k = 0; k < N-i; k++)
System.out.print("-");
// print N-i +
for (int l = 0; l < N-i; l++)
System.out.print("/");
// print N-i /
// for (int m = 0; m < N-i; m++)
// System.out.print("/");
// print a new line
System.out.println();
}
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
Sample XML creator
//this is using the HTTP/HTML protocol
long file;
long i = 30000;
vuser_init()
{
char * filename = "c:\\test\\sunlogfile.txt";
int id;
char * groupname;
// Create a new file
if ((file = fopen(filename, "w+" )) == NULL) {
lr_output_message("Unable to create %s", filename);
return -1;
}
// Write the Vuser id and group to the log file
fprintf(file,"<?xml version=\"1.0\" encoding=\"utf-8\"?>");
fprintf(file,"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:tns=\"http://Med_Lib_Service/ManageServiceParametersAllocationExtV2\" xmlns:man=\"http://telstra.oss.fal/ManageServiceParametersAllocationV2.xsd\">\n");
fprintf(file," <SOAP-ENV:Body>\n");
fprintf(file," <tns:UnReserveResources>\n");
fprintf(file," <UnreserveResourcesRequest>\n");
fprintf(file," <man:InternationalFormat>false</man:InternationalFormat>\n");
fprintf(file," <man:ResourceType>\n");
fprintf(file," <man:CommonName>IMSI</man:CommonName>\n");
fprintf(file," </man:ResourceType>\n");
fprintf(file," <man:Activity>USOS Unreserve</man:Activity>\n");
return 0;
}
------------------
Action()
{
//long i ;
// for(i=30000, i<39999, i++)
// {
fprintf(file," <man:Resource>\n");
fprintf(file," <man:ObjectID>5050100068");
fprintf(file,"%d",i);
fprintf(file,"</man:ObjectID>\n");
fprintf(file," </man:Resource>\n");
// }
i=i+1;
return 0;
}
--------------------
vuser_end()
{
fprintf(file,"</UnreserveResourcesRequest>\n");
fprintf(file,"</tns:UnReserveResources>\n");
fprintf(file," </SOAP-ENV:Body>\n");
fprintf(file," </SOAP-ENV:Envelope>\n");
fclose(file);
return 0;
}
Delete all files in a folder
/*
* LoadRunner Java script. (Build: 946)
*
* Script Description:
*
*/
import lrapi.lr;
import java.io.*;
public class Actions
{
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
String dirPath="D:\\OrderIDs\\Copyof20090407\\"; //change the Directory accordingly
File dir_path = new File(dirPath);
File[] files = dir_path.listFiles();
for(int i=0; i < files.length; i++){
String fileName = files[i]+"";
File f1 = new File(fileName);
boolean success = f1.delete();
if (!success){
System.out.println("Deletion failed.");
System.exit(0);
}else{
System.out.println(fileName + " - deleted.");
}
}
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
Delete files from a folder
/*
* LoadRunner Java script. (Build: 946)
*
* Script Description:
*
*/
import lrapi.lr;
import java.io.*;
public class Actions
{
public int flagFile = 0;
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
String dirPath="D:\\OrderIDs\\Copyof20090407\\"; //change the Directory accordingly
String ext = "log";
// File dir_path = new File(dirPath);
// File[] fileList = dir_path.listFiles();
File[] fileList = new File(dirPath).listFiles();
for(int j = 0; j < fileList.length; j++)
{
try
{
if(fileList[j].getName().endsWith(ext)){
//System.out.println("fileList[j] : " + fileList[j]);
String fileName = fileList[j] + "";
//System.out.println("fileName : "+ fileName);
File f1 = new File(fileName);
boolean success = f1.delete();
if (!success){
System.out.println("Deletion failed.");
//System.exit(0);
}else{
System.out.println(fileName + " - File deleted.");
}
//System.exit(0);
}
if (!fileList[j].getName().endsWith("log")) {
flagFile = 1;
}
}
catch(Exception e)
{
System.out.println("Error deleting file." + fileList[j]);
continue;
}
}
if (flagFile == 1) {
System.out.println("No " + ext + " file exists");
}
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
Count order Ids from files in a folder
/*--
This program counts the no of order and the displays the total orders
--*/
import java.io.*;
import java.io.FileWriter;
import lrapi.lr;
import lrapi.web;
public class Actions
{
private String file_Contents;
public int sum;
int totalOrderCount = 0;
public int init() {
sum = 0;
return 0;
}
public int action() throws FileNotFoundException, IOException {
String dirPath="D:\\OrderIDs\\20090407\\"; //change the Directory accordingly
File dir_path = new File(dirPath);
File[] files = dir_path.listFiles();
for(int i=0; i < files.length; i++){
String fileName = files[i]+"";
//System.out.println("Files: "+ files[i]); // displays all the file names
int orderCount=0;
FileReader fr = new FileReader(fileName);
LineNumberReader ln = new LineNumberReader(fr);
while (ln.readLine() != null){
orderCount++;
totalOrderCount++;
} // end while
System.out.println(""+ files[i] + ": " + orderCount);
} // end for
System.out.println("\n");
System.out.println("Total OrderIds: "+ totalOrderCount);
System.out.println("No of files:"+ files.length); // displays no of files
return 0;
}
public int end() {
return 0;
}
}
System out using Debug level
import lrapi.lr;
public class Actions
{
public int init() throws Throwable {
int debugLvl=0;
String StrdebugLvl = null;
String argument = null;
StrdebugLvl = lr.eval_string("<debugLevel>");
debugLvl = Integer.parseInt(StrdebugLvl.trim());
argument = lr.get_attrib_string("sunArgs");
System.out.println("SunArgs: "+argument);
System.out.println("SunArgs:"+ lr.get_attrib_string("sunArgs"));
return 0;
}//end of init
public int action() throws Throwable {
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
Read from DB
import lrapi.lr;
import java.io.File;
import java.util.Date;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import java.sql.*;
import java.util.*;
import java.math.*;
import java.text.*;
public class Actions
{
SqlReader Sqlr_orch;
private static String CF1 = "com.clarify.jms.bridge.connectionFactory.boss.LBCF";
private static String RouterInQ = "com.clarify.jms.boss.router.in";
private static int sleepTime = 100;
String OrderId = null;
public int init()
{
try
{
Sqlr_orch = new SqlReader(lr.eval_string("jdbc:oracle:thin:@localhost:1522:SVTORC"), "sax", "sax");
System.out.print("**** ");
}
catch(Exception e)
{
System.out.print("in Exp");
e.printStackTrace();
lr.exit(lr.EXIT_VUSER, lr.FAIL);
return 1;
}
return 0;
}
public int action()
{
String sqlResOrch [] = null;
OrderId = lr.eval_string("<OrderId>");
System.out.print("OrderId " + OrderId);
try{
sqlResOrch = Sqlr_orch.RunQuery("SELECT C.OSS_CMP_ORDER_ID "+
"FROM TABLE_ORDER_ACTION oa , TABLE_CONTRACT c, TABLE_ORDER_LINE ol "+
"WHERE c.objid = oa.ORDER_ACTION2CONTRACT and ol.order_line2order_action=oa.objid "+
"and oa.X_FAMILY = 'Mobile' "+
"and c.STATUS = 'Complete' "+
"and C.OSS_CMP_ORDER_ID like ? "+
"and c.START_DATE> to_char('+<date>+') "+
"order by c.START_DATE desc",
"C.OSS_CMP_ORDER_ID",
OrderId);
System.out.print(" " + sqlResOrch);
System.out.print("\n" );
}
catch (Exception e)
{
System.out.print("Error: " + e.getMessage());
}
return 0;
}
public int end()
{
return 0;
}
}
-----------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class SqlReader
{
private Connection con;
private PreparedStatement ps;
protected String DbName;
protected String DbUser;
protected String DbPass;
public SqlReader(String DbName, String DbUser, String DbPass) throws Exception
{
this.DbName = DbName;
this.DbUser = DbUser;
this.DbPass = DbPass;
connect();
}
public Connection connect()throws Exception
{
try
{
System.out.println("conecting DB..."+DbUser+","+DbPass+"@"+DbName);
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection(DbName,DbUser,DbPass);
System.out.println("returning con");
return con;
}
catch (SQLException e)
{
throw new Exception("Error init sql reader" + e.getMessage(), e);
}
}
public String[] RunQuery(String str_query, String FieldSelector, String BindParameters)
throws Exception
{
System.out.print("str_query" + str_query);
System.out.print("FieldSelector " + FieldSelector);
System.out.print("BindParameters " + BindParameters);
String[] results = new String[100];
String res = null;
String [] temp = null;
temp = BindParameters.split(";");
String timerecv = null;
try
{
ResultSet rs = null;
ps = con.prepareStatement(str_query,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ps.setString(1, BindParameters);
rs = ps.executeQuery();
while (rs.next()) {
res = rs.getString(1);
results[0] = res;
}
}
catch (SQLException e)
{
throw new Exception("Error in sql run query" + e.getMessage(), e);
}
ps.close();
return results;
}
public void close() throws Exception
{
try
{
con.close();
}
catch (SQLException e)
{
throw new Exception("Error closing sql connection : " + e.getMessage(),e);
}
}
}
--------
System Time
import lrapi.lr;
import java.util.Date;
public class Actions
{
private Date date = null;
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
long time1 = System.currentTimeMillis();
System.out.println("Time1:" + time1);
long sendTime;
date = new Date();
sendTime = date.getTime();
System.out.println("Time2:" + sendTime);
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
SSH - Read from Unix
import lrapi.lr;
import java.io.IOException;
import org.twdata.enchanter.SSH;
import org.twdata.enchanter.impl.DefaultSSH;
public class Actions
{
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
SSH ssh = new DefaultSSH();
ssh.connect("asde12nus135pf", 22, "asdd254515", "akhfdasd");
String readline = null;
readline = ssh.getLine();
System.out.println("readline"+readline);
while (! readline.equals("au10qap3f0tels2 $")) {
readline = ssh.getLine();
System.out.println("readline"+readline);
readline = ssh.getLine();
System.out.println("readline"+readline);
}
System.out.println("Out of loop - While");
System.out.println("15:"+ssh.lastLine());
System.out.println("3"+ssh.lastLine());
System.out.println("4"+ssh.getLine());
System.out.println("date "+ssh.getLine());
System.out.println("ls "+ssh.getLine());
System.out.println("ls "+ssh.lastLine());
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
Read an XML
import lrapi.lr;
public class Actions
{
String xmlStr = null;
String finalFileName = null;
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
xmlStr = lr.read_xml("Created_MCO_Connect.xml");
finalFileName=xmlStr.replace("SUNILOZZIE", "<name>");
System.out.println("XML: " + finalFileName);
finalFileName = null;
xmlStr = null;
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
Subscribe to:
Comments (Atom)