LoadRunner測試J2ME的Socket程式

TIB發表於2010-02-24

LoadRunnerWindows Sockets協議用於支援Socket程式的測試指令碼開發,但是有時候會錄製不了或錄製不全Socket指令碼,而且Socket指令碼的修改和引數化等方面都比較困難。

 

J2ME Wireless Toolkit 2.0附帶的NetworkDemo程式中的Socket Demo為例,用LoadRunner錄製下來的指令碼如下所示:

/*********************************************************************

 * Created by Mercury Interactive Windows Sockets Recorder

 *

 * Created on: Wed Feb 24 13:35:17

 *********************************************************************/

 

#include "lrs.h"

 

 

Action()

{

    lrs_create_socket("socket0", "TCP", "LocalHost=0", "Backlog=50",  LrsLastArg);

 

    lrs_create_socket("socket1", "TCP", "LocalHost=2663", "Backlog=50",  LrsLastArg);

 

    lrs_create_socket("socket2", "TCP", "RemoteHost=192.168.1.100:57328",  LrsLastArg);

 

    lrs_send("socket2", "buf0", LrsLastArg);

 

    lrs_receive("socket2", "buf1", LrsLastArg);

 

    lrs_close_socket("socket2");

 

    lr_think_time(13);

 

    lrs_create_socket("socket3", "TCP", "LocalHost=0", "Backlog=50",  LrsLastArg);

 

    lrs_create_socket("socket4", "TCP", "LocalHost=2669", "Backlog=50",  LrsLastArg);

 

    lrs_create_socket("socket5", "TCP", "LocalHost=0", "Backlog=50",  LrsLastArg);

 

    lrs_create_socket("socket6", "TCP", "LocalHost=2670", "Backlog=50",  LrsLastArg);

 

    lrs_create_socket("socket7", "TCP", "RemoteHost=192.168.1.100:57328",  LrsLastArg);

 

    lrs_send("socket7", "buf2", LrsLastArg);

 

    lrs_receive("socket7", "buf3", LrsLastArg);

 

    lrs_close_socket("socket7");

 

    lrs_create_socket("socket8", "TCP", "RemoteHost=192.168.1.100:57328",  LrsLastArg);

 

    lrs_send("socket8", "buf4", LrsLastArg);

 

    lrs_receive("socket8", "buf5", LrsLastArg);

 

    lrs_send("socket8", "buf6", LrsLastArg);

 

    lrs_receive("socket8", "buf7", LrsLastArg);

 

    lrs_send("socket8", "buf8", LrsLastArg);

 

    lrs_create_socket("socket9", "TCP", "LocalHost=0", "Backlog=50",  LrsLastArg);

 

    lrs_create_socket("socket10", "TCP", "LocalHost=2675", "Backlog=50",  LrsLastArg);

 

       /*

        * 傳送訊息

        */

 

    return 0;

}

 

傳送和接收的Socket包資料可以在data.ws檔案中找到,Socket Demo的客戶端傳送訊息的操作並沒有錄製下來。

 

轉變一下思路,可以採用Java Vuser來開發JavaSocket指令碼,直接連線Socket Demo的伺服器端併傳送訊息:

 

/*

 * LoadRunner Java script. (Build: 3020)

 *

 * Script Description:

 *                    

 */

 

import lrapi.lr;

 

import java.io.*;

import java.net.*;

 

public class Actions

{

 

       Socket socket;

       BufferedReader in;

       PrintWriter out;

 

       public int init() throws Throwable {

 

           try

           {

                socket = new Socket("127.0.0.1", 5000);

 

           }

           catch (IOException e){}

 

           return 0;

       }//end of init

 

       public int action() throws Throwable {

 

           try

           {

 

              in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

              //lr.output_message(in.readLine());

              out = new PrintWriter(socket.getOutputStream(),true);

              out.println("World!");

              //lr.output_message(in.readLine());

           }

           catch (IOException e){}

 

           return 0;

       }//end of action

 

 

       public int end() throws Throwable {

 

           try

           {    

              out.close();

              in.close();

              socket.close();

           }

           catch (IOException e){}

 

           return 0;

       }//end of end

}

 

 

 

相關文章