LR

loadrunner12以上版本支持websocket,在http/html协议录制时可以直接录制websocket相关内容信息。

网上找的一个测试websocket网址:http://www.blue-zero.com/WebSocket/,在线websocket测试地址。以这个地址来学习。

  

录制时注意事项:

1)选择协议开始录制,步骤:打开网址-》点击连接-》输入数据1,发送-》输入数据2,发送-》断开连接

2)录制过程中建立连接后一直等待服务器返回应答,未返回建立连接数据。此时,连接为黑色,表示在连接状态。此时,可随意发送数据,断开连接后,右侧栏中会显示全部的与服务器交互信息。

 录制完脚本:

lr12  websocket_redislr12  websocket_redis_02
Action()
{

    web_add_cookie("user=7; DOMAIN=www.blue-zero.com");

    web_url("WebSocket", 
        "URL=http://www.blue-zero.com/WebSocket/", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t18.inf", 
        "Mode=HTML", 
        EXTRARES, 
        "Url=PLUG/WebSocketMain.swf", ENDITEM, 
        "Url=http://img.t.sinajs.cn/t4/appstyle/expression/ext/normal/f2/wg_thumb.gif", ENDITEM, 
        LAST);

    web_custom_request("q.cgi", 
        "URL=http://masterconn.qq.com/q.cgi", 
        "Method=POST", 
        "Resource=1", 
        "Referer=", 
        "Snapshot=t19.inf", 
        "EncType=", 
        "BodyBinary=\\x00\\x02\\x01@\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\xE2\\x00\\x00\\x07\\xDA\\x00\\x00\\x00\\x00\\x08;\\xA4\n\\xA4,\\xD5\\xB4d\\x92@\\x9A8V3\r\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\t\\x95\\xD41\\x95s\\xD9\\xF5Cc\\xCD\\xD5V\\xDD5\\x96\\x1ED\\xFDk5)\\x15T\\x9Ed\\xECvt\\xDC\\x04\\xDE&\\xF2\\xA5\\xBE\\xCB\\xEC\\xAD\\x0BH\\xF2+\\xF7Y\\xEA\\xE5,"
        "\\x02\\xCF\\xB2\\xA5\\x1D\\xF5\\xD5Gy\\xE7\\x1E<\\xE3\\xDE\\xB2y\\xB2\\xCA\r\\xAD\\xCA\\xCFk\\x1E\\xB5\\xF0l\\xC0T\\xA0\\x88\\xCB\\xBE\\xF2\\x1B\\xBAs\\x01\\x8C\\xD5\\xA9\\xDD\\xF34NJ\\x92\\xA4\\x85\\xA4V5q*\\x16Q_\\\\\\x86\\x02>\\xE1\\x95\\xF0=\\x02^\\xA5\\xE4U\\x83y\\x99\\x84>\\xBD%-\\xA1\\x9C\\xFE\\xB6S\\x0E\\x1AZi\\x8C\\xF5v\\x1F\\xD1\\xF1A\\x9C\\xD6\\xDD\\xC3p\\xC9\t\\xAF\\xB4\\x1D,D\\xA6\\xF5U\\x05\\xCD\\xC4\\x1E", 
        LAST);

    web_url("glyphicons-halflings-regular.eot", 
        "URL=http://www.blue-zero.com/WebSocket/CSS/FONTS/glyphicons-halflings-regular.eot?", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://www.blue-zero.com/WebSocket/", 
        "Snapshot=t20.inf", 
        "Mode=HTML", 
        EXTRARES, 
        "Url=http://www.bing.com/favicon.ico", "Referer=", ENDITEM, 
        LAST);

    lr_think_time(8);

    web_url("favicon.ico", 
        "URL=http://www.blue-zero.com/favicon.ico", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t21.inf", 
        "Mode=HTML", 
        LAST);

    lr_think_time(15);

    web_websocket_connect("ID=0", 
        "URI=ws://121.40.165.18:8088/", 
        "Origin=http://www.blue-zero.com", 
        "OnOpenCB=OnOpenCB0", 
        "OnMessageCB=OnMessageCB0", 
        "OnErrorCB=OnErrorCB0", 
        "OnCloseCB=OnCloseCB0", 
        LAST);

    web_websocket_send("ID=0", 
        "Buffer=#ah#8", 
        "IsBinary=0", 
        LAST);

    /*Connection ID 0 received buffer WebSocketReceive0*/

    lr_think_time(16);
    //input value : wait100
    web_websocket_send("ID=0", 
        "Buffer=wait100#ah#8", 
        "IsBinary=0", 
        LAST);

    /*Connection ID 0 received buffer WebSocketReceive1*/

    lr_think_time(11);
    //input value :let me see
    web_websocket_send("ID=0", 
        "Buffer=let me see#ah#8", 
        "IsBinary=0", 
        LAST);

    /*Connection ID 0 received buffer WebSocketReceive2*/

    web_websocket_close("ID=0", 
        "Code=1000", 
        LAST);

    return 0;
}
View Code

 

脚本结构:

lr12  websocket_LR_03

WebSocketBuffer.h

个人理解:这个文件是记录的录制过程中与服务器交互历次交互信息。

注意这里显示的值是录制过程中的,比如发送数据包含wait100,对应receive中也会包含此值。并不是实际运行时,显示的服务器返回值。

问题:脚本参数化时要将这里的值,也做参数化么?

lr12  websocket_ico_04

WebSocketCB.c

这里有几个重要的事件,OnOpenCB0、OnErrorCB0、OnMessageCB0、OnCloseCB0。在哪用?查看脚本中的建立连接的函数。

每件事件默认的触发信息都被注释掉了。当然,这里可以将其取消注释,也可以根据实际需求编写。

1)比如,OnMessageCB0事件中只输出了connectionID与长度,如果想查看该事件返回值,则把data值也输出。

2)比如,OnMessageCB0事件,在文中有示例说明,有心跳及服务器返回值处理,像保存成一个新参数,对结果成功与否判断处理等。

文中示例:

lr12  websocket_redislr12  websocket_redis_02
void OnMessageCB0 (
const char* connectionID,
        int isbinary,
        const char * data,
        int length)
{
    int isactivesession;
    int isheartbeat;
    int isupdate;

    lr_output_message("WebSocket ID = %s got message=%s length = %d, ", connectionID, data, length);

    isactivesession = strncmp(data,"5:::{\"name\":\"message\"",21);
    isupdate = strncmp(data,"5:::{\"name\":\"update\"",20);
    isheartbeat = strncmp(data,"2::",3);

    if(isheartbeat==0) {
        lr_user_data_point("Heartbeat message",1);
        web_websocket_send("ID=0","Buffer=2::","IsBinary=false",LAST);
        lr_output_message("WebSocket ID = %s heartbeat message sent back", connectionID);
    }

    if(isactivesession==0) {
        lr_save_param_regexp (
            data,
            length,
            "RegExp=5:::{\"name\":\"message\",\"args\":.(.+).}",                
            "ResultParam=myActiveSessions",
            LAST );

lr_user_data_point_ex("Active Sessions", atoi(lr_eval_string("{myActiveSessions}"))-1, DP_FLAGS_EXTENDED_LOG);
        lr_output_message(">>>> got visits counter!!!");
    }

    if(isupdate==0) {
        lr_save_param_regexp (
            data,
            length,
            "RegExp=LoadRunner\\\\\",\\\\\"Price\\\\\":([0-9]*\.?[0-9]+)},{",
            "ResultParam=myLRPrice",
            LAST );

        lr_user_data_point_ex("LoadRunner price", atof(lr_eval_string("{myLRPrice}")), DP_FLAGS_EXTENDED_LOG);
        lr_output_message(">>>> got price update!!!");
    }

    if (counter++ > 10) lr_save_string("OK","ready");

}
View Code

LR中默认的WebSocketCB.c

lr12  websocket_redislr12  websocket_redis_02
void OnOpenCB0 (const char* connectionID,
                  const char * AccumulatedHeadersStr,
                  int AccumulatedHeadersLen)
{
    
//        lr_output_message("WebSocket ID = %s connected", connectionID);
//        lr_save_param_regexp (AccumulatedHeadersStr,
//                              AccumulatedHeadersLen,
//                              "RegExp=Sec-WebSocket-Accept: (.+)\\r\\n",
//                              "ResultParam=Accept",
//                              LAST );
//        lr_output_message("Sec-WebSocket-Accept = [%s]",
//                          lr_eval_string("{Accept}"));

}

void OnMessageCB0 (const char* connectionID,
                  int isbinary,
                  const char * data,
                  int length)
{
    
//        if (isbinary) {
//            lr_output_message("WebSocket ID = %s. [%d] bytes binary message received.", connectionID, length);
//        }
//        else {
//            lr_output_message("WebSocket ID = %s. [%d] bytes text message received.", connectionID, length);
//        }
}

void OnErrorCB0 (const char* connectionID,
                  const char * message,
                  int length)
{
    
//        lr_output_message("WebSocket ID = %s error occured. Error message = %s", connectionID, message);
}

void OnCloseCB0 (const char* connectionID,
                  int isClosedByClient,
                  int code,
                  const char* reason,
                  int length)
{
    
//        lr_output_message("WebSocket ID = %s closed. CloseCode= %d, CloseReason=%s", connectionID, code, reason);
}
View Code

 

录制完的脚本将WebSocketCB.c中注释取取消,直接执行脚本,提示连接未成功,触发OnErrorCB0提示。这可能是测试服务器的事。

 

Action

因为是基于http协议录制的,所以大部分还是常见的信息。只是多个几个websocket函数。从LR帮助手册里,可以搜到这三个函数。

lr12  websocket_f5_09

三个函数实现连接请求、发送数据、关闭请求。

     web_websocket_connect("ID=0", 
        "URI=ws://121.40.165.18:8088/", 
        "Origin=http://www.blue-zero.com", 
        "OnOpenCB=OnOpenCB0", 
        "OnMessageCB=OnMessageCB0", 
        "OnErrorCB=OnErrorCB0", 
        "OnCloseCB=OnCloseCB0", 
        LAST); 

This function creates a WebSocket connection upon which to send the message.

Headers that were detected during recording will be generated automatically, except for SecWebSocketKey. To add additional headers to the handshake request, place web_add_header or web_add_cookie steps before this function.

Callbacks will be generated with the default implementation commented out. For a description of the callback functions, see WebSocket Callback Functions.

 

LR帮助里关于三个函数的示例:

Action()

{

web_websocket_connect("ID=0", "URI=ws://pumpkin:9876/", "Origin=http://pumpkin:9876", "OnOpenCB=OnOpenCB0", "OnMessageCB=OnMessageCB0", "OnErrorCB=OnErrorCB0", "OnCloseCB=OnCloseCB0", LAST);

web_websocket_send("ID=0", "Buffer=sample text message", "IsBinary=0", LAST);

web_websocket_send("ID=0", "Buffer/File={myfile}", "IsBinary=0", LAST);

/*Connection ID 0 received buffer WebSocketReceive0*/

lr_think_time(7);

web_websocket_close("ID=0", "Code=1000", "Reason=OK", LAST);

return 0;

}

建立连接后,发送数据,对于数据做参数化等操作就可以了。

 

 

对于websocket接口类型的测试,当然使用jmeter工具也可以。参考文章:Websocket接口性能测试方法

 

 

 


 

参考资料:

1、loadrunner录制websocket

2、http://community.hpe.com/t5/LoadRunner-and-Performance/Async-Communications-The-WebSocket-protocol-made-easy-in/ba-p/6469242#.V7EogLh95hH