/*
* This software and supporting documentation were developed by
*
* Siemens Corporate Technology
* Competence Center Knowledge Management and Business Transformation
* D-81730 Munich, Germany
*
* Authors (representing a really great team ;-) )
* Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
* This software is Open Source under GNU General Public License (GPL).
* Read the text of this license in LICENSE.TXT
* or look at www.opensource.org/licenses/
*
* Once more we emphasize, that:
* THIS SOFTWARE IS MADE AVAILABLE, AS IS, WITHOUT ANY WARRANTY
* REGARDING THE SOFTWARE, ITS PERFORMANCE OR
* FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
* ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
* PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/
// LoadTest
// ************ package ******************************************************
package mod.UptimeModule;
// ************ imports ******************************************************
import java.util.Date;
import java.io.*;
import java.net.*;
import java.util.*;
import com.oroinc.text.regex.*;
import KFM.HTML.HtmlLoader2;
import KFM.DateTimeServices.KFM_DateTimeService;
import HTTPClient.*;
/****************************************************************************
* LoadTest
*
* Tests periodically the given URL and writes the results in an output file.
* @version 0.1 (00 09 21)
*
****************************************************************************/
public class LoadTest extends Thread
{
// ************************************************************
// Constants
// ************************************************************
protected final String ERR_NOT_LOADED = "Not loaded";
protected final String ERR_LABEL_NOT_FOUND = "Label not found";
protected final String SUCCESS = "Successful";
// ************************************************************
// Variables
// ************************************************************
/** Host */
protected Vector mHost;
/** URL */
protected Vector mUrl;
/** File */
protected Vector mFile;
/** RunsPerInterval */
protected int mRunsPerInterval;
/** PauseBetweenIntervals */
protected int mPauseBetweenIntervals;
/** NumberOfIntervals */
protected int mNumberOfIntervals;
/** TimeoutValue */
protected int mTimeoutValue;
/** Protocol */
protected Vector mProtocol;
/** Port to use */
protected Vector mPort;
/** Label */
protected String mLabel;
/** Logger */
protected Logger mLogger;
/** Thread Id */
protected int mThreadId;
/** UptimeTester */
protected UptimeTester2 mUptimeTester;
/** Test Type */
protected boolean mSerialTest;
protected PatternMatcher mMatcher = new Perl5Matcher();
static protected Pattern mPattern = null;
static {
PatternCompiler tCompiler = new Perl5Compiler();
try {
mPattern = tCompiler.compile("<!--TTP:(.*)ms, CMU:(.*)bytes, MTP:(.*)bytes-->");
} catch(MalformedPatternException e) {
System.err.println("Bad pattern for Perl5Compiler.");
System.err.println(e.getMessage());
System.exit(-1);
}
}
public LoadTest (
UptimeTester2 aUptimeTester,
Vector aUrl,
String aLabel,
int aRunsPerInterval,
int aPauseBetweenIntervals,
int aNumberOfIntervals,
int aTimeoutValue,
Logger aLogger,
int aId,
boolean aSerialTest)
{
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
// create URL and get port, host, file. These values are required for the
// HTTP get
URL tUrl;
mProtocol = new Vector();
mPort = new Vector();
mHost = new Vector();
mFile = new Vector();
mUrl = new Vector();
for (int i=0; i<aUrl.size(); i++){
try{
tUrl= new URL((String)aUrl.elementAt(i));
mProtocol.addElement(tUrl.getProtocol());
mPort.addElement(new Integer(tUrl.getPort()));
mHost.addElement(tUrl.getHost());
mFile.addElement(tUrl.getFile());
mUrl.addElement(aUrl.elementAt(i));
}catch (MalformedURLException me){
System.out.println("LoadTest terminates: "+me+" for URL: "+aUrl.elementAt(i));
System.exit(1);
}
}
mRunsPerInterval = aRunsPerInterval;
mPauseBetweenIntervals = aPauseBetweenIntervals;
mNumberOfIntervals = aNumberOfIntervals;
mTimeoutValue = aTimeoutValue;
mLabel = aLabel;
mUptimeTester = aUptimeTester;
mLogger = aLogger;
mThreadId = aId;
mSerialTest = aSerialTest;
}
protected HtmlLoader2 mHtmlLoad = null;
protected long mRequiredTime=0;
/** Runs ServerCheck
*
*/
public void run ()
{
int i=0;
mHtmlLoad = new HtmlLoader2();
int tCounter=0;
// Loop for mNumberOfIntervals, 0 means unfinite
while((i++ < mNumberOfIntervals) || (mNumberOfIntervals == 0)){
tCounter = 0;
// In every interval load file mRunsPerInterval times
while(tCounter++ < mRunsPerInterval) {
String tContent=null;
// Perform test for every given Url
for (int j=0; j<mHost.size(); j++){
test(
(String) mUrl.elementAt(j),
(String) mHost.elementAt(j),
(String) mFile.elementAt(j),
((Integer) mPort.elementAt(j)).intValue(),
(String) mProtocol.elementAt(j));
}
} // while
// Pause between two intervals
if (i!=mNumberOfIntervals){
try{
sleep(mPauseBetweenIntervals*1000);
} catch (InterruptedException ie){}
}
} // while
long time = mRequiredTime/(mRunsPerInterval*mNumberOfIntervals);
if (mSerialTest){
mUptimeTester.calculateRequiredTime(time);
} else {
mUptimeTester.setReady();
}
} // run
public void test(String aUrl, String aHost, String aFile, int aPort, String aProtocol)
{
String tContent = null;
String tResult=SUCCESS;
long tStartTimeUrl = new Date().getTime();
try{
if (!mHtmlLoad.get(aHost,
aFile,
aPort,
mTimeoutValue, (aProtocol).equals("https"))){
tResult=ERR_NOT_LOADED;
}
}
catch (java.io.InterruptedIOException ioe)
{
tResult=ERR_NOT_LOADED;
}
long tEndTimeUrl = new Date().getTime();
// load was successful
if(tResult.equals(SUCCESS) && !mLabel.equals("unused")){
// Check if page contains given label
tContent = mHtmlLoad.getContent();
if (tContent.indexOf(mLabel) == -1){
tResult=ERR_LABEL_NOT_FOUND;
}
}
long tDiffTime = tEndTimeUrl-tStartTimeUrl;
mRequiredTime += tDiffTime;
long tNetworkTime = -1;
long tServletTime = -1;
long tMemory = -4711;
long tMemoryDiff = -4711;
if(null != tContent) {
/* Pattern matching to extract the different values */
if(mMatcher.contains(tContent, mPattern)) {
MatchResult tMatchResult = mMatcher.getMatch();
tServletTime = Long.parseLong(tMatchResult.group(1));
tMemory = Long.parseLong(tMatchResult.group(2));
tMemoryDiff = Long.parseLong(tMatchResult.group(3));
tNetworkTime = tDiffTime - tServletTime;
}
}
mLogger.write(tStartTimeUrl, mThreadId, aUrl, tDiffTime, tResult,
tNetworkTime, tServletTime, tMemory, tMemoryDiff);
}
}