Actions.java 4.57 KB
/*
 * LoadRunner Java script. (Build: _build_number_)
 * 
 * Script Description: 
 *                     
 */

import java.util.ArrayList;
import lrapi.lr;
import se.lil.om.*;

public class Actions
{
	
	ArrayList<OraMon> oraList = new ArrayList<OraMon>();

	boolean firstIteration = true;
	String empty = "";

	public int init() throws Throwable 
	{
	    if(lr.get_attrib_string("ConnectionString") == null || lr.get_attrib_string("User") == null || lr.get_attrib_string("Password") == null ||
			lr.get_attrib_string("ConnectionString").equals(empty) || lr.get_attrib_string("User").equals(empty) || lr.get_attrib_string("Password").equals(empty)) 
		{
			lr.error_message("Attributes are missing for Oracle: ConnectionString, User or Password are null. Nothing to do, aborting vuser.");
			lr.exit(lr.EXIT_VUSER, lr.FAIL);
	    }

		String[] conS = lr.get_attrib_string("ConnectionString").split(",");
		String[] usrS = lr.get_attrib_string("User").split(",");
		String[] pasS = lr.get_attrib_string("Password").split(",");
		
		if(conS.length != usrS.length || conS.length != pasS.length) 
		{
		    lr.error_message("Oracle has multiple comma separated connection strings but not the same number of multiple user or password strings. Cannot continue, abort.");
		    lr.exit(lr.EXIT_VUSER, lr.FAIL);
		}
		
		for(int x=0; x<conS.length; x++) 
		{
		    OraMon mon = new OraMon();
		    lr.debug_message (lr.MSG_CLASS_EXTENDED_LOG , " Using " + conS[x].trim() + " User " + usrS[x].trim() + " Pass " + pasS[x].trim());
		    mon.open(conS[x].trim(), usrS[x].trim(), pasS[x].trim());
		    oraList.add(mon);
		}
	    return 0;
	}//end of init


	public int action() throws Throwable 
	{
		
		for(OraMon oracle : oraList) 
		{
		    lr.debug_message (lr.MSG_CLASS_EXTENDED_LOG , "#### ORACLE COUNTERS ####");
		    lr.debug_message (lr.MSG_CLASS_EXTENDED_LOG, "Using: " + oracle.getConString());
		    oracle.getData();
		    lr.debug_message (lr.MSG_CLASS_EXTENDED_LOG, "Returned DB Name: " + oracle.getDBName());
		    
		    if(firstIteration == false) 
		    {
			    String instance = oracle.getDBName();
			    sendDataPoint("ORA Cpus (#) " + instance, oracle.getNumberOfCPUs());
			    sendDataPoint("ORA Cpu Time (ms/s) " + instance, oracle.getCPUTimePerSecond());
			    sendDataPoint("ORA Cpu Usage (%) " + instance, oracle.getCPUPercent());
			    sendDataPoint("ORA Logical Reads (#/s) " + instance, oracle.getLogicalReadsPerSecond());
			    sendDataPoint("ORA Consistent Gets (#/s) " + instance, oracle.getPerSecondValue("consistent gets"));
			    sendDataPoint("ORA DB Block Gets (#/s) " + instance, oracle.getPerSecondValue("db block gets"));
			    sendDataPoint("ORA Cache Hit Ratio (%) " + instance, oracle.getCacheHitRatioPercent());
			    sendDataPoint("ORA Buffer Cache Hit Ratio (%) " + instance, oracle.getBufferCacheHitRatioPercent());
			    sendDataPoint("ORA DB Block Changes (#/s) " + instance, oracle.getPerSecondValue("db block changes"));
			    sendDataPoint("ORA Redo Size (#/s) " + instance, oracle.getPerSecondValue("redo size"));
			    sendDataPoint("ORA Physical Reads (#/s) " + instance, oracle.getPerSecondValue("physical reads"));
			    sendDataPoint("ORA Physical Writes (#/s) " + instance, oracle.getPerSecondValue("physical writes"));
			    sendDataPoint("ORA Redo Writes (#/s) " + instance, oracle.getPerSecondValue("redo writes"));
			    sendDataPoint("ORA Non-idle Wait Time (ms/s) " + instance, oracle.getPerSecondValue("non-idle wait time"));
			    sendDataPoint("ORA File I/O Wait Time (ms/s) " + instance, oracle.getPerSecondValue("file io wait time"));
			    sendDataPoint("ORA Executes (#/s) " + instance, oracle.getPerSecondValue("execute count"));
			    sendDataPoint("ORA User Calls (#/s) " + instance, oracle.getPerSecondValue("user calls"));
			    sendDataPoint("ORA User Commits (#/s) " + instance, oracle.getPerSecondValue("user commits"));
			    sendDataPoint("ORA User Rollbacks (#/s) " + instance, oracle.getPerSecondValue("user rollbacks"));
			    sendDataPoint("ORA Parse Count Total (#/s) " + instance, oracle.getPerSecondValue("parse count (total)"));
			    sendDataPoint("ORA Parse Count Hard (#/s) " + instance, oracle.getPerSecondValue("parse count (hard)"));
		    } else {
		    	firstIteration = false;
		    }
		}
		
	    return 0;
	}//end of action


	public int end() throws Throwable 
	{
		for(OraMon oracle : oraList) 
		{
		    oracle.close();
		}
	    
	    return 0;
	}//end of end
	
	// Needed to avoid LR12 VuGen from hanging in a CPU loop (later fixed by a patch but just in case)
	public void sendDataPoint(String name, double value) 
	{
		lr.user_data_point(name, value);
	}
}