Monitoring Tomcat with LoadRunner

来源:互联网 发布:明是非 知善恶 知良莠 编辑:程序博客网 时间:2024/05/17 09:06

出自:http://www.jds.net.au/tech-tips/monitoring-tomcat/


LoadRunner does not come with a monitor for Tomcat. Fortunately, you can easily create one in about 5 minutes…

Tomcat exposes metrics related to JVM memory and Servlet container threads (and some other useful information) on a Status page at <Server Name>/manager (ask your Tomcat admin to enable it).

Tomcat Status page

Create a standard web vuser script which loads the Tomcat Status page, and capture all the metrics you want using web_reg_save_param. Then log these values using lr_user_data_point. The metrics will be visible in the LoadRunner Controller and also in LoadRunner Analysis on the User-Defined Data Points Graph.

// This script collects server metrics from the Tomcat Status page (http://10.110.30.50:8080/manager/status).
// Runtime settings are set to run this script once every 5 seconds.

double atof (const char *string); /* Explicit declaration */
extern char* strtok(char *token, const char *delimiter);

CollectMetrics()
{
    int countP, countS, countF, countR, countK;
    int numValues;
    static int loggedVersionInfo = FALSE;

    lr_save_string("10.110.30.50:8080", "ServerName");
    web_set_max_html_param_len("65536"); // Note: this may need to be increased.

    web_set_user("admin",
        "admin",
        "{ServerName}");

    lr_start_transaction("monitor tomcat");

    /*
    <h1>JVM</h1><p> Free memory: 130.99 MB Total memory: 254.18 MB Max memory: 1016.12 MB</p>
    */

    web_reg_save_param("JVMFreeMemory",
        "LB=Free memory: ",
        "RB= MB",
        "Ord=1",
        LAST);

    web_reg_save_param("JVMTotalMemory",
        "LB=Total memory: ",
        "RB= MB",
        "Ord=1",
        LAST);

    web_reg_save_param("JVMMaxMemory",
        "LB=Max memory: ",
        "RB= MB",
        "Ord=1",
        LAST);

    /*
    <h1>http-8080</h1><p> Max threads: 150 Min spare threads: 25 Max spare threads: 75 Current
    thread count: 25 Current thread busy: 2<br>
    Max processing time: 78 ms Processing time: 0.124 s
    Request count: 11 Error count: 3 Bytes received: 0.00 MB Bytes sent: 0.03 MB</p>
    */

    web_reg_save_param("HTTPMaxThreads",
        "LB=Max threads: ",
        "RB= ",
        "Ord=1",
        LAST);

    web_reg_save_param("HTTPMinSpareThreads",
        "LB=Min spare threads: ",
        "RB= ",
        "Ord=1",
        LAST);

    web_reg_save_param("HTTPMaxSpareThreads",
        "LB=Max spare threads: ",
        "RB= ",
        "Ord=1",
        LAST);

    web_reg_save_param("HTTPCurrentSpareThreads",
        "LB=Current thread count: ",
        "RB= ",
        "Ord=1",
        LAST);

    web_reg_save_param("HTTPCurrentThreadBusy",
        "LB=Current thread busy: ",
        "RB= ",
        "Ord=1",
        LAST);

    web_reg_save_param("HTTPMaxProcessingTime",
        "LB=Max processing time: ",
        "RB= ",
        "Ord=1",
        LAST);

    web_reg_save_param("HTTPRequestCount",
        "LB=Request count: ",
        "RB= ",
        "Ord=1",
        LAST);

    web_reg_save_param("HTTPErrorCount",
        "LB=Error count: ",
        "RB= ",
        "Ord=1",
        LAST);

    web_reg_save_param("HTTPBytesReceived",
        "LB=Bytes received: ",
        "RB= ",
        "Ord=1",
        LAST);

    web_reg_save_param("HTTPBytesSent",
        "LB=Bytes sent: ",
        "RB= ",
        "Ord=1",
        LAST);

    /*
    <h1>jk-8009</h1><p> Max threads: 200 Min spare threads: 4 Max spare threads: 50 Current thread count: 8 Current thread busy: 6
    <br> Max processing time: 8031 ms Processing time: 199.1 s Request count: 11279 Error count: 426 Bytes received: 0.00 MB Bytes sent: 100.88 MB</p>
    */

    web_reg_save_param("JKMaxThreads",
        "LB=Max threads: ",
        "RB= ",
        "Ord=2",
        LAST);

    web_reg_save_param("JKMinSpareThreads",
        "LB=Min spare threads: ",
        "RB= ",
        "Ord=2",
        LAST);

    web_reg_save_param("JKMaxSpareThreads",
        "LB=Max spare threads: ",
        "RB= ",
        "Ord=2",
        LAST);

    web_reg_save_param("JKCurrentSpareThreads",
        "LB=Current thread count: ",
        "RB= ",
        "Ord=2",
        LAST);

    web_reg_save_param("JKCurrentThreadBusy",
        "LB=Current thread busy: ",
        "RB= ",
        "Ord=2",
        LAST);

    web_reg_save_param("JKMaxProcessingTime",
        "LB=Max processing time: ",
        "RB= ",
        "Ord=2",
        LAST);

    web_reg_save_param("JKRequestCount",
        "LB=Request count: ",
        "RB= ",
        "Ord=2",
        LAST);

    web_reg_save_param("JKErrorCount",
        "LB=Error count: ",
        "RB= ",
        "Ord=2",
        LAST);

    web_reg_save_param("JKBytesReceived",
        "LB=Bytes received: ",
        "RB= ",
        "Ord=2",
        LAST);

    web_reg_save_param("JKBytesSent",
        "LB=Bytes sent: ",
        "RB= ",
        "Ord=2",
        LAST);

    // Version Information
    /*
    <table border="1" cellspacing="0" cellpadding="3">
    <tr>
     <td colspan="6" class="title">Server Information</td>
    </tr>
    <tr>
     <td class="header-center"><small>Tomcat Version</small></td>
     <td class="header-center"><small>JVM Version</small></td>
     <td class="header-center"><small>JVM Vendor</small></td>
     <td class="header-center"><small>OS Name</small></td>
     <td class="header-center"><small>OS Version</small></td>
     <td class="header-center"><small>OS Architecture</small></td>
    </tr>
    <tr>
     <td class="row-center"><small>Apache Tomcat/5.5.23</small></td>
     <td class="row-center"><small>1.5.0_15-b04</small></td>
     <td class="row-center"><small>Sun Microsystems Inc.</small></td>
     <td class="row-center"><small>Windows 2003</small></td>
     <td class="row-center"><small>5.2</small></td>
     <td class="row-center"><small>x86</small></td>
    </tr>
    </table>
    */
    web_reg_save_param("ServerTomcatVersion",
        "LB=<td class=\"row-center\"><small>",
        "RB=</small></td>",
        "Ord=1",
        LAST);

    web_reg_save_param("ServerJVMVersion",
        "LB=<td class=\"row-center\"><small>",
        "RB=</small></td>",
        "Ord=2",
        LAST);

    web_reg_save_param("ServerJVMVendor",
        "LB=<td class=\"row-center\"><small>",
        "RB=</small></td>",
        "Ord=3",
        LAST);

    web_reg_save_param("ServerOSName",
        "LB=<td class=\"row-center\"><small>",
        "RB=</small></td>",
        "Ord=4",
        LAST);

    web_reg_save_param("ServerOSVersione",
        "LB=<td class=\"row-center\"><small>",
        "RB=</small></td>",
        "Ord=5",
        LAST);

    web_reg_save_param("ServerOSArchitecture",
        "LB=<td class=\"row-center\"><small>",
        "RB=</small></td>",
        "Ord=6",
        LAST);

/*
    // Determine number of threads in each state.
    // P: Parse and prepare request S: Service F: Finishing R: Ready K: Keepalive

    web_reg_save_param("StageTable",
        "LB=<table border=\"0\"><tr><th>Stage</th><th>Time</th><th>B Sent</th><th>B Recv</th><th>Client</th><th>VHost</th><th>Request</th></tr>", //<tr><th>Stage", //</th><th>Time</th><th>B Sent</th><th>B Recv</th><th>Client</th><th>VHost</th><th>Request</th></tr><tr>",
        "RB=</table>",
        "Ord=All",    // note that there should only be 2 tables in this test environment.
        LAST);
*/
    web_reg_find("Text=/manager",
        LAST);

    web_url("status",
        "URL=http://{ServerName}/manager/status",
        "Resource=0",
        "RecContentType=text/html",
        "Referer=",
        "Snapshot=t1.inf",
        "Mode=HTTP",
        LAST);

    lr_end_transaction("monitor tomcat", LR_AUTO);

    // Tomcat JVM metrics
    lr_user_data_point("Tomcat JVM Free memory", atof(lr_eval_string("{JVMFreeMemory}")));
    lr_user_data_point("Tomcat JVM Total memory", atof(lr_eval_string("{JVMTotalMemory}")));
    lr_user_data_point("Tomcat JVM Max memory", atof(lr_eval_string("{JVMMaxMemory}")));

    // Tomcat web server metrics
    lr_user_data_point("Tomcat HTTP Max threads", atof(lr_eval_string("{HTTPMaxThreads}")));
    lr_user_data_point("Tomcat HTTP Min spare threads", atof(lr_eval_string("{HTTPMinSpareThreads}")));
    lr_user_data_point("Tomcat HTTP Max spare threads", atof(lr_eval_string("{HTTPMaxSpareThreads}")));
    lr_user_data_point("Tomcat HTTP Current spare threads", atof(lr_eval_string("{HTTPCurrentSpareThreads}")));
    lr_user_data_point("Tomcat HTTP Current thread busy", atof(lr_eval_string("{HTTPCurrentThreadBusy}")));
    lr_user_data_point("Tomcat HTTP Max processing time", atof(lr_eval_string("{HTTPMaxProcessingTime}")));
    lr_user_data_point("Tomcat HTTP Request count", atof(lr_eval_string("{HTTPRequestCount}")));
    lr_user_data_point("Tomcat HTTP Error count", atof(lr_eval_string("{HTTPErrorCount}")));
    lr_user_data_point("Tomcat HTTP Bytes received", atof(lr_eval_string("{HTTPBytesReceived}")));
    lr_user_data_point("Tomcat HTTP Bytes sent", atof(lr_eval_string("{HTTPBytesSent}")));

    // Tomcat servlet container metrics
    lr_user_data_point("Tomcat JK Max threads", atof(lr_eval_string("{JKMaxThreads}")));
    lr_user_data_point("Tomcat JK Min spare threads", atof(lr_eval_string("{JKMinSpareThreads}")));
    lr_user_data_point("Tomcat JK Max spare threads", atof(lr_eval_string("{JKMaxSpareThreads}")));
    lr_user_data_point("Tomcat JK Current spare threads", atof(lr_eval_string("{JKCurrentSpareThreads}")));
    lr_user_data_point("Tomcat JK Current thread busy", atof(lr_eval_string("{JKCurrentThreadBusy}")));
    lr_user_data_point("Tomcat JK Max processing time", atof(lr_eval_string("{JKMaxProcessingTime}")));
    lr_user_data_point("Tomcat JK Request count", atof(lr_eval_string("{JKRequestCount}")));
    lr_user_data_point("Tomcat JK Error count", atof(lr_eval_string("{JKErrorCount}")));
    lr_user_data_point("Tomcat JK Bytes received", atof(lr_eval_string("{JKBytesReceived}")));
    lr_user_data_point("Tomcat JK Bytes sent", atof(lr_eval_string("{JKBytesSent}")));
/*
    // Determine number of threads in each state.
    // Note that there is some complexity in doing this (see code below)...
    // P: Parse and prepare request S: Service F: Finishing R: Ready K: Keepalive

    if (strcmp(lr_eval_string("{StageTable_count}"), "2") != 0) { // Verify that there are only two tables.
        lr_error_message("Monitoring script expected 2 tables of thread metrics, but instead found %s", lr_eval_string("{StageTable_count}"));
    } else {    // extract the P, S, F, R, and K values from both the tables.

        // check that there are only 2 tables. Raise an error if there is not...
        //lr_save_searched_string(const char *buffer, long buf_size, unsigned int occurrence, const char *search_string, int offset, unsigned int string_len, const char *parm_name );
        // maybe should use strtok for this.
        //strtok
        // lr_xml_get_values
        lr_output_message("%s", lr_eval_string("{StageTable_1}"));
        numValues= lr_xml_get_values("XML={StageTable_1}",
            "ValueParam=OutputParam",
            "Query=/tr/td/strong/*",
            "SelectAll=yes", LAST);

    }
*/

    // Log Tomcat/JVM version info on the first iteration only.
    if (loggedVersionInfo != TRUE) {
        lr_log_message("****** Test Environment Information ******");
        lr_log_message("%s", lr_eval_string("Tomcat: {ServerTomcatVersion}"));
        lr_log_message("%s", lr_eval_string("JVM: {ServerJVMVersion} {ServerJVMVendor}"));
        lr_log_message("%s", lr_eval_string("Operating System: {ServerOSName} {ServerOSVersione} {ServerOSArchitecture}"));
        lr_log_message("******************************************");
        loggedVersionInfo = TRUE;
    }
    

    return 0;
}