Package mod.UptimeModule

Source Code of mod.UptimeModule.UptimeTester2

/*
*  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.
*
*/


// UptimeTester2

// ************ package ******************************************************
package mod.UptimeModule;

// ************ imports ******************************************************
import KFM.Smtp;
import KFM.DateTimeServices.KFM_DateTimeService;
import java.io.*;
import java.util.*;


/****************************************************************************
* UptimeTester2
*
* Module which can be used either as watchdog or as loadtest.
* It tests periodically a given URL and reports the test results.
* @version 0.1 (00 09 21)
*
****************************************************************************/
public class UptimeTester2
{

    // ************************************************************
    // Variables
    // ************************************************************

    String mMailServer;
    String mReceiversMailAddress;
    String mSenderMailAddress;
    int    mCounter;
    long   mTime;
    int    mNumberOfThreads;
    Logger mLogger;
    Vector mUrls;
    long   mStartTime;
    String mMailSubject;

    // ************************************************************
    // Methods
    // ************************************************************

    /** Constructor for the UrlWatchdog
     * One Watchdog thread will be started, it invokes periodically the given URL and
     * sends a mail in case of problems.
     *
     * @param String aUrl
     * @param String aLabel
     * @param int aRunsPerInterval
     * @param int aPauseBetweenIntervals
     * @param int mNumberOfIntervals
     * @param int aTimeoutValue
     * @param String aMailServer
     * @param String aReceiversMailAddress
     * @param String aSenderMailAddress
     * @param long aObserverTimeout
     * @param String aMailSubject
     */
    public UptimeTester2 (
        String aUrl,
        String aLabel,
        int    aRunsPerInterval,
        int    aPauseBetweenIntervals,
        int    aNumberOfIntervals,
        int    aTimeoutValue,
        String aMailServer,
        String aReceiversMailAddress,
        String aSenderMailAddress,
        long    aObserverTimeout,
        String aMailSubject)
    {

        mMailServer = aMailServer;
        mReceiversMailAddress = aReceiversMailAddress;
        mSenderMailAddress = aSenderMailAddress;
        mMailSubject = aMailSubject;

        // display status
        System.out.println("\n\nUptimeTester2 status:\n"
            + "------------------------\n"
            + "Testing mode is UrlWatchdog.\n"
            + "Url to be tested is set to: " +aUrl + "\n"
            + "Label which has to be in result page is set to: " +aLabel + "\n"
            + "UrlWatchdog loads "+aRunsPerInterval+" times the Url, "
            + "then waits for "+aPauseBetweenIntervals+" seconds.\n");
        if (aNumberOfIntervals != 0){
            System.out.println("This will be done "+ aNumberOfIntervals + " times.\n");
        }
        if (aTimeoutValue != 0){
            System.out.println("The server has " + aTimeoutValue
            + " ms to send data, before timeout occurs.\n");
        }
        System.out.println("All alert mails will be send to " + aReceiversMailAddress
            + " on mailserver " + aMailServer+ ".\n"
            + "UrlWatchdog mailaddress is " + aSenderMailAddress + ".\n\n"
            + "Observer stops UrlWatchdog after "+aObserverTimeout +" seconds without sign of life.\n");


        // Create and start watchdog thread
        UrlWatchdog mWatchdog = new UrlWatchdog(this, aUrl, aLabel, aRunsPerInterval,
                                          aPauseBetweenIntervals, aNumberOfIntervals,
                                          aTimeoutValue, aObserverTimeout);
        mWatchdog.start();

    }

    /** Constructor for the DbWatchdog
     * One Watchdog thread will be started, it periodically tests the given DB accessibility
     * and sends a mail in case of problems.
     *
     * @param String aJdbcDriver
     * @param String aJdbcConnectionURL
     * @param String aJdbcUser
     * @param String aJdbcPassword
     * @param int aRunsPerInterval
     * @param int aPauseBetweenIntervals
     * @param int mNumberOfIntervals
     * @param int aTimeoutValue
     * @param String aMailServer
     * @param String aReceiversMailAddress
     * @param String aSenderMailAddress
     * @param long aObserverTimeout
     * @param String aMailSubject
     */
    public UptimeTester2 (
        String aJdbcDriver,
        String aJdbcConnectionURL,
        String aJdbcUser,
        String aJdbcPassword,
        int    aRunsPerInterval,
        int    aPauseBetweenIntervals,
        int    aNumberOfIntervals,
        int    aTimeoutValue,
        String aMailServer,
        String aReceiversMailAddress,
        String aSenderMailAddress,
        long    aObserverTimeout,
        String aMailSubject)
    {

        mMailServer = aMailServer;
        mReceiversMailAddress = aReceiversMailAddress;
        mSenderMailAddress = aSenderMailAddress;
        mMailSubject = aMailSubject;

        // display status
        System.out.println("\n\nUptimeTester2 status:\n"
            + "------------------------\n"
            + "Testing mode is DbWatchdog.\n"
            + "DB to be checked is set to: " + aJdbcConnectionURL + "\n"
            + "DbWatchdog checks " + aRunsPerInterval + " times the DB, "
            + "then waits for " + aPauseBetweenIntervals + " seconds.\n");
        if (aNumberOfIntervals != 0){
            System.out.println("This will be done " + aNumberOfIntervals + " times.\n");
        }
        if (aTimeoutValue != 0){
            System.out.println("The server has " + aTimeoutValue
            + " ms to send data, before timeout occurs.\n");
        }
        System.out.println("All alert mails will be send to " + aReceiversMailAddress
            + " on mailserver " + aMailServer+ ".\n"
            + "DbWatchdog mailaddress is " + aSenderMailAddress + ".\n\n"
            + "Observer stops DbWatchdog after " + aObserverTimeout + " seconds without sign of life.\n");


        // Create and start watchdog thread
        DbWatchdog mWatchdog = new DbWatchdog(this, aJdbcDriver, aJdbcConnectionURL, aJdbcUser, aJdbcPassword,
                                            aRunsPerInterval, aPauseBetweenIntervals, aNumberOfIntervals,
                                            aTimeoutValue, aObserverTimeout);
        mWatchdog.start();

    }

    /** Constructor for the LoadTest
     * aNumberOfThreads LoadTest threads will be started. Each loadTest thread invokes
     * periodically the given URLs and reports the result in an output file.
     *
     * @param String aUrl (could contain one Url or File with list of URLs)
     * @param String aLabel
     * @param int aRunsPerInterval
     * @param int aPauseBetweenIntervals
     * @param int mNumberOfIntervals
     * @param int aTimeoutValue
     * @param int aNumberOfThreads
     * @param String aDirectoryForResultFile
     * @param String aLoadTestType
     * @param Logger aLogger specify a logfile instead of using the default logging mechanism
     */
    public UptimeTester2 (
        String aUrl,
        String aLabel,
        int    aRunsPerInterval,
        int    aPauseBetweenIntervals,
        int    aNumberOfIntervals,
        int    aTimeoutValue,
        int    aNumberOfThreads,
        String aDirectoryForResultFile,
        String aLoadTestType,
        Logger aLogger)
    {

        boolean tManyUrl = false;
        mUrls = new Vector();;

        // check if given parameter aUrl contains files with URLs to test
        try {
            FileReader tReader = new FileReader(aUrl);
            BufferedReader tBufReader = new BufferedReader(tReader);
            while (tBufReader.ready()){
                mUrls.addElement(tBufReader.readLine());
            }
            tManyUrl=true;
        } catch (Exception e){
        }

        if (!tManyUrl){
            mUrls.addElement(aUrl);
        }


        // display status
        System.out.println("\n\nUptimeTester2 status:\n"
            + "------------------------\n"
            + "Testing mode is Loadtest.\n");

        if (!tManyUrl){
            System.out.println("Url to be tested is set to: " +aUrl + "\n");
        }
        System.out.println(
            "Label which has to be in result page is set to: " +aLabel + "\n"
            + "Every Loader Thread loads "+aRunsPerInterval+" times the Url, "
            + "then waits for "+aPauseBetweenIntervals+" seconds.\n");

        if (aNumberOfIntervals != 0){
            System.out.println("We have "+ aNumberOfIntervals + " runs.\n");
        }

        if (aTimeoutValue != 0){
            System.out.println("The server has " + aTimeoutValue
            + " ms to send data, before timeout occurs.\n");
        }

        System.out.println("Number of Threads is set to: "+ aNumberOfThreads +".\n");

        mTime=0;
        mCounter=0;
        mNumberOfThreads = aNumberOfThreads;

        if(aLogger != null) {
            mLogger = aLogger;
        } else {
            // Create Logger object responsible for writing the results on stdout and in a file
            mLogger = new Logger(aDirectoryForResultFile, false);
        }

        mStartTime = new Date().getTime();

        if (aLoadTestType.equals("s")){
            // Serial Test: One thread invokes all Urls in the file
            // Create and start load threads
            for (int i=0; i<aNumberOfThreads; i++){
                LoadTest tLoader = new LoadTest(this, mUrls, aLabel, aRunsPerInterval, aPauseBetweenIntervals,
                                                aNumberOfIntervals, aTimeoutValue, mLogger, i+1, true);
                tLoader.start();
            }
        } else {
            // Parallel Test: For every Url in the file a thread will be started
            for (int j=0; j<mUrls.size(); j++){
                for (int i=0; i<aNumberOfThreads; i++){
                    Vector v = new Vector();
                    v.addElement(mUrls.elementAt(j));
                    LoadTest tLoader = new LoadTest(this, v, aLabel, aRunsPerInterval, aPauseBetweenIntervals,
                                                    aNumberOfIntervals, aTimeoutValue, mLogger, i+1, false);
                    tLoader.start();
                }
            }
        }
    }

    /** Constructor for the LoadTest2
     * aNumberOfThreads LoadTest threads will be started. Each loadTest thread invokes
     * periodically the given URLs and reports the result in an output file.
     *
     * @param String aUrl (could contain one Url or File with list of URLs)
     * @param String aLabel
     * @param int aRunsPerInterval
     * @param int aPauseBetweenIntervals
     * @param int mNumberOfIntervals
     * @param int aTimeoutValue
     * @param int aNumberOfThreads
     * @param String aDirectoryForResultFile
     * @param String aLoadTestType
     * @param int aGetRequests number of get requests to perform after a login event
     * @param String aGetFile the file which should be retrieved by the get event
     * @param Logger aLogger specify a logfile instead of using the default logging mechanism
     */
    public UptimeTester2 (
        String aUrl,
        String aLabel,
        int    aRunsPerInterval,
        int    aPauseBetweenIntervals,
        int    aNumberOfIntervals,
        int    aTimeoutValue,
        int    aNumberOfThreads,
        String aDirectoryForResultFile,
        String aLoadTestType,
        int    aGetRequests,
        String aGetFile,
        Logger aLogger)
    {

        boolean tManyUrl = false;
        mUrls = new Vector();;

        // check if given parameter aUrl contains files with URLs to test
        try {
            FileReader tReader = new FileReader(aUrl);
            BufferedReader tBufReader = new BufferedReader(tReader);
            while (tBufReader.ready()){
                mUrls.addElement(tBufReader.readLine());
            }
            tManyUrl=true;
        } catch (Exception e){
        }

        if (!tManyUrl){
            mUrls.addElement(aUrl);
        }


        // display status
        System.out.println("\n\nUptimeTester2 status:\n"
            + "------------------------\n"
            + "Testing mode is Loadtest2 (Hack for Personalize).\n");

        if (!tManyUrl){
            System.out.println("Url to be tested is set to: " +aUrl + "\n");
        }
        System.out.println(
            "Label which has to be in result page is set to: " +aLabel + "\n"
            + "Every Loader Thread loads "+aRunsPerInterval+" times the Url, "
            + "then waits for "+aPauseBetweenIntervals+" seconds.\n");

        if (aNumberOfIntervals != 0){
            System.out.println("We have "+ aNumberOfIntervals + " runs.\n");
        }

        if (aTimeoutValue != 0){
            System.out.println("The server has " + aTimeoutValue
            + " ms to send data, before timeout occurs.\n");
        }

        System.out.println("Number of Threads is set to: "+ aNumberOfThreads +".\n");

        mTime=0;
        mCounter=0;
        mNumberOfThreads = aNumberOfThreads;

        if(aLogger != null) {
            mLogger = aLogger;
        } else {
            // Create Logger object responsible for writing the results on stdout and in a file
            mLogger = new Logger(aDirectoryForResultFile, false);
        }

        mStartTime = new Date().getTime();

        if (aLoadTestType.equals("s")){
            // Serial Test: One thread invokes all Urls in the file
            // Create and start load threads
            for (int i=0; i<aNumberOfThreads; i++){
                LoadTest2 tLoader = new LoadTest2(this, mUrls, aLabel, aRunsPerInterval, aPauseBetweenIntervals,
                                                aNumberOfIntervals, aTimeoutValue, mLogger, i+1, true,
                                                aGetRequests, aGetFile);
                tLoader.start();
            }
        } else {
            // Parallel Test: For every Url in the file a thread will be started
            for (int j=0; j<mUrls.size(); j++){
                for (int i=0; i<aNumberOfThreads; i++){
                    Vector v = new Vector();
                    v.addElement(mUrls.elementAt(j));
                    LoadTest2 tLoader = new LoadTest2(this, v, aLabel, aRunsPerInterval, aPauseBetweenIntervals,
                                                    aNumberOfIntervals, aTimeoutValue, mLogger, i+1, false,
                                                    aGetRequests, aGetFile);
                    tLoader.start();
                }
            }
        }
    }

    /** Calculates the average loading time (for loading ALL URLs) for all load test threads
     *  and writes it to stdout. Every load thread invokes this method when
     * it finished the work.
     *
     * Will be used for the serial loadtest!
     */
    public void calculateRequiredTime(long aTime){
        // number of load threads already reported the result
        mCounter++;
        // sum of time required
        mTime+=aTime;

        // writes average time (for loading all URLs)
        if (mCounter==mNumberOfThreads){
            System.out.println("The average loading time was: "+mTime/mNumberOfThreads+" ms.");
        }
    }

    /** Waits until ALL threads finished the work and displays the required time (total time)
     *
     *  Will be used for the parallel loadtest!
     */

    public void setReady(){
        // number of load threads already reported the result
        mCounter++;

        // writes average time
        if (mCounter==(mNumberOfThreads*mUrls.size())){
            long tTime = new Date().getTime()-mStartTime;
            // After this time ALL threads are finished
            System.out.println(tTime);
        }
    }

    /** Sends mail to receivers.
     */
    protected void sendMail (
        String aEvent,
        String aMessage)
    {
        aMessage = "Subject: "+mMailSubject + ": "+ aEvent+"\n\n"
            + KFM_DateTimeService.createTimeStamp() + ": " + aMessage;

        // *Send* mail via Smtp class. Yes, really, the constructor *sends* a mail.
        Smtp tMail = new Smtp(mMailServer,
            mReceiversMailAddress,
            mSenderMailAddress,
            aMessage,
            true /*several recipients*/);
    }



    /**
     * UrlWatchdog, DbWatchdog and LoadTest (refactor this !!!).
     * Which one is called depends on the first command line argument (the type) which can
     * be w (for UrlWatchdog), db (for DbWatchdog) or l (for LoadTest).
     * The meaning and order of the following command line arguments varies from type to type.
     * See method usage() for further detail.
     */
    public static void main(String[] aArgv)
    {
        try {
            if(12 == aArgv.length && aArgv[0].equals("w")) {
                // UrlWatchdog
                UptimeTester2 tUpTester = new UptimeTester2(
                    aArgv[1], aArgv[2], Integer.parseInt(aArgv[3]), Integer.parseInt(aArgv[4]),
                    Integer.parseInt(aArgv[5]), Integer.parseInt(aArgv[6]),
                    aArgv[7], aArgv[8], aArgv[9], Long.parseLong(aArgv[10]),
                    aArgv[11]);
            } else
            if(14 == aArgv.length && aArgv[0].equals("db")) {
                // DbWatchdog
                UptimeTester2 tUpTester = new UptimeTester2(
                    aArgv[1], aArgv[2], aArgv[3], aArgv[4], Integer.parseInt(aArgv[5]),
                    Integer.parseInt(aArgv[6]), Integer.parseInt(aArgv[7]),
                    Integer.parseInt(aArgv[8]), aArgv[9], aArgv[10], aArgv[11],
                    Long.parseLong(aArgv[12]), aArgv[13]);
            } else
            if(10 == aArgv.length && aArgv[0].equals("l")) {
                // Loadtest
                UptimeTester2 tUpTester = new UptimeTester2(
                    aArgv[1], aArgv[2], Integer.parseInt(aArgv[3]), Integer.parseInt(aArgv[4]),
                    Integer.parseInt(aArgv[5]), Integer.parseInt(aArgv[6]), Integer.parseInt(aArgv[7]),
                    aArgv[8], aArgv[9], null);
            } else
            if(12 == aArgv.length && aArgv[0].equals("L2")) { // l looks so like a 1
                // Loadtest2
                UptimeTester2 tUpTester = new UptimeTester2(
                    aArgv[1], aArgv[2], Integer.parseInt(aArgv[3]), Integer.parseInt(aArgv[4]),
                    Integer.parseInt(aArgv[5]), Integer.parseInt(aArgv[6]), Integer.parseInt(aArgv[7]),
                    aArgv[8], aArgv[9], Integer.parseInt(aArgv[10]), aArgv[11], null);
            } else {
                usage();
            }
        } catch (Exception e){
            System.out.println("UptimeTester2 terminates: " + e);
        }
    }

    static private void usage ()
    {
        System.out.println(
            "Wrong Usage. Call UptimeTester2 this way:\n\n" +
            "UrlWatchdog:\n" +
            "============\n" +
            "UptimeTester2 w\n" +
            "    <UrlToLoad> <Label> <NumberOfRunsPerInterval> <PauseBetweenIntervals>\n" +
            "    <NumberOfIntervals> <TimeoutValueForLoadRequest> <Mailserver>\n" +
            "    <ReceiversMailAddresses> <SenderMailAddress> <ObserverTimeout> <MailSubject>\n\n" +
            "DbWatchdog:\n" +
            "===========\n" +
            "UptimeTester2 db\n" +
            "    <JdbcDriver> <JdbcConnectionUrl> <JdbcUser> <JdbcPassword>\n" +
            "    <NumberOfRunsPerInterval> <PauseBetweenIntervals> <NumberOfIntervals>\n" +
            "    <TimeoutValueForLoadRequest> <Mailserver> <ReceiversMailAddresses>\n" +
            "    <SenderMailAddress> <ObserverTimeout> <MailSubject>\n\n" +
            "LoadTester:\n" +
            "===========\n" +
            "UptimeTester2 l\n" +
            "    <UrlsToLoad/FileWithUrls> <Label> <NumberOfRunsPerInterval>\n" +
            "    <PauseBetweenIntervals> <NumberOfIntervals> <TimeoutValueForLoadRequest>\n" +
            "    <NumberOfThreads> <DirectoryForResultFile> <loadTestType>\n");

        System.exit(1);
    }
}
TOP

Related Classes of mod.UptimeModule.UptimeTester2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.