Socket protocol performance test for self-control LoadRunner

Source: Internet
Author: User
Tags function definition int size mixed socket sprintf strlen
[-] A preface to the two tasks proposed three realization scheme discussion four technical points explain how to start recording a simplest send and receive packet script write log file a line of Read packet file string convert to hex packet send own defined packet receive packet to custom buffer read out data from custom buffer how to release How the custom buffer is calculated as a decimal number five bar based on the packet return

Absrtact: This paper introduces a test method of performance test of socket protocol in LoadRunner, and how to control and transmit packets without relying on LoadRunner established rules.

Keywords: loadrunner,socket, self-control, send and receive data packets

A. Preface

Using the LoadRunner socket protocol for performance testing, the students know that only need to record a few commands, you can implement the socket link, send and receive data packets and close the link, a moment of great cool, but followed by endless torture. Just start parameterized packet send receive all line, slowly discovered, in many cases, the length and content of sending and receiving packets is indeterminate, plus hexadecimal and ASCII, even protocol and encryption and other factors mixed together, is simply a disaster. So the self-control packet sent and received as an option, although LoadRunner provides the relevant functions, but really face the conversion, facing the endless <memory violation:exception Access_violation received Many people can only find another way to complete the task.

Originally wanted to comprehensively analyze LoadRunner socket protocol performance test, found that need to clear the details too much, can only try to clarify the following examples of the various points of knowledge encountered.

two. The making of the task

This performance test is a very common situation, the front of the machine linked to a variety of different hardware device clients, each hardware device client uses a different protocol, the Protocol carries a large number of different services, but the basic structure of the packet is the same, consisting of the header, package body and check code, both TCP links and UDP links, Data is sent using a short link, that is, linked to the server, sent out the data immediately close the link. It is now necessary to loadrunner simulate different hardware devices and test the concurrency of the front-facing machines.

Packet structure:

006.jpg

System Architecture:

001.jpg

three. Implementation Scenario Discussion

This is a very common scenario, but it's more complicated.

If the traditional recording playback, you need to select a few representative types of hardware and focus on business, recording scripts, you can imagine the need to record a lot of scripts, if parameterized, you must understand the various protocols, re-package, this workload is too large.

or development to provide two dynamic link libraries, one for the implementation of various protocols to encode and Decode, another includes the hardware type needs to simulate the key business, the second dynamic link library calls the first, after loading the dynamic link library in the LoadRunner, directly invoke the relevant business operation function can be. This is general enough, but who has the time to talk to you? Besides, if you say this, this article will not be written.

There is a third method, now send and receive the packet in front of the machine has a log file save, you can send a variety of hardware types of packet log files collected, and then do two scripts, a TCP, a UDP, the logic is the same, open the data table log file, read the packet sent, Writes the sent and received packets to the local log file, so that you only need to write two scripts, copy multiple copies, each script into different packet files to simulate different hardware types.

It seems the easiest way to do this is to analyze it and see if it works.

Many protocols will be linked on the server, the server side to provide a unique string to return, as a unique identity of the communication, added to the subsequent packet, where the protocol does not have this problem, or every time before sending a packet, you have to change the data packets to be sent according to the unique string returned, really lucky.

So it seems that after the link is established, the packet can be sent without any modification. However, some businesses, such as increasing the business, the pre-machine received the task may be written to the table, if already exist may conflict, so before testing needs to clear the database, only the initialization data.

This also has the benefit of testing the business and the actual production of the business is exactly the same, regardless of the type or proportion. The disadvantage is that the packet file here will not be large enough to send a short while, it seems that there is also a tool to generate enough packets of files. However, it is loosely coupled.

It is confirmed that there is no business of a certain hardware, mixed use of TCP and UDP cases.

There seems to be no big problem with this plan.

Four. Technical Essentials Explained

1. How to start recording one of the simplest scripts for sending and receiving packets

When the script was started, a green software SocketTool.exe was used, and a TCP server was started on this machine:

002.jpg

Using LoadRunner to record Windows application, start a new SocketTool.exe, create a TCP Client, link the server just started, check the hexadecimal value displayed on the hook, send 313233, don't write the blanks in, Click Send data, and then send the point data back to the client on the server side, finally the client click Disconnect, the script will be recorded completed.

003.jpg

004.jpg

The script has four sentences:

Lrs_create_socket ("Socket0", "TCP", "localhost=0", "remotehost=server:60000", Lrslastarg);

Lrs_send ("Socket0", "buf0", Lrslastarg);

Lrs_receive ("Socket0", "Buf1", Lrslastarg);

Lrs_close_socket ("Socket0");

Data file data.ws:

; Wsrdata 2 1

Send Buf0 3

"123"

Recv BUF1 3

"456"

-1

The following script is modified on this basis.

2. Write log files

Assuming that the script is concurrent with five users, if you are writing to a log file, it is possible for each user log to be intertwined, if you need each user to use their own log files independently, you can create a parameter Vurid

005.jpg

sprintf (creqseqno, "%s%s20d459b3412a2b", Cnow,);

Define variables:

Char clogfile[100]= "n"; Log file

Long filedeslog=0; Log file handle

To open the log file in Vuser_init:

sprintf (Clogfile, "Lrsocket%s.log", Lr_eval_string ("{Vurid}"));

if (Filedeslog = fopen (Clogfile, "A +")) = = = NULL)

{

Lr_output_message ("Open File failed!");

return-1;

}

Write log file contents

Fwrite ("\nopen file:", strlen ("\nopen file:"), 1, filedeslog);

Fwrite (cFileName, strlen (cFileName), 1, filedeslog);

Fwrite ("\ n", 1, 1, filedeslog);

To close the log file in Vuser_end:

Fclose (Filedeslog);

3. Read the packet file one line at a time

Definition section:

Char cfilename[100]= "n"; Packet file name

Long filedes=0; Packet file handle

Char cline[2048]= "n"; A row in the file

Read File Method:

sprintf (cFileName, "%s", "Data.txt");

if ((Filedes = fopen (cFileName, "R")) = = = NULL)

{

Lr_output_message ("Open File failed!");

return-1;

}

while (!feof (filedes)) {

FSCANF (Filedes, "%s", cLine);

Lr_output_message ("read:%s", cLine);

}

Fclose (Filedes);

4. String conversion to hex packet

Defined:

unsigned char cout[1024]= "n"; Record the converted packets, send out the packets

Although the surface is a character array here, please do not treat cout[as a string, but should be understood as an array that holds a series of hexadecimal data. What difference does it have? Of course.

For example, you're going to send out a packet. 16 binary is: 31 32 00 33 34, which is stored (in decimal) in this array:

cout[0]=49

Cout[1]=50

cout[2]= 0

cout[3]=51

cout[4]=52

When sending a packet, it should send a length of 5, if processing in order to string, send strlen (cOut), you can imagine, every 0 stopped, only sent to the first two bytes. It is also not possible to use strcpy (Cout,bufval) when receiving it, since encountering 0 stops and if there are 00 bytes in the packet, the data is incomplete.

Binary conversion

m=0;

memset (cout,0,sizeof (COut));

For (K=0;k<strlen (cLine); k++) {

if (k% 2==1) {

CTMP[0]=CLINE[K-1];

CTMP[1]=CLINE[K];

ctmp[2]=0;

SSCANF (CTMP, "%x", &lngtrans);

Cout[m]=lngtrans;

m++;

}

}

The first initialization of all bytes of cout is 0;

Reads a row taken from the file;

Each encounter even digit character, read out two characters, put cTmp string, use SSCANF (CTMP, "%x", &lngtrans);

For example, ctmp "31", understood as 16 binary conversion out, lngtrans=0x31;

And then put the converted data into the cout and get the packets to send.

If you want to see what's inside cout:

unsigned char *p;

P=cout;

For (I=0;i<strlen (cLine)/2;i++) {

Lr_output_message ("Package ready:%x,%d,%x", p,*p,*p);

p++;

}

You cannot print a value directly in LoadRunner by referencing cout[0], you need to use a pointer. Even the address of the pointer has been called to you, this is clear enough.

5. Send a packet of your own definition

Create a link I won't write it, send my own definition of the packet:

Lrs_set_send_buffer ("Socket0", (char *) COut, strlen (cLine)/2);

Lrs_send ("Socket0", "buf0", Lrslastarg);

Description

1. (char *) COut is because of the parameter definition of the function
int Lrs_set_send_buffer (char *s_desc,char *buffer, int size);

2. strlen (CLine)/2 cannot be written as strlen (COut), be sure to keep in mind that this is not a string to send, but a binary packet;

6. Receiving packets to a custom buffer

Code:

Char *bufval; Record the received packets

int intgetlen=0; Record the length of the received packet

LRS_RECEIVE_EX ("Socket0", "Buf1", "numberofbytestorecv=4", Lrslastarg);

Lrs_get_last_received_buffer ("Socket0", &bufval, &intgetlen);

Description

1. Intgetlen must be defined as an int, not a long, why. The function definition determines:
int Lrs_get_last_received_buffer (char *s_desc, char **data,int *size );

2. "Numberofbytestorecv=4" Here LoadRunner The example of the help in the wrong, when I pasted down, alive and dead reported that horrible <memory violation:exception Access_violatio N Received>, later looked carefully, understand, the example of NUMBEROFBYTESTORECV in front of a space, deleted on it can be;

3. Define the received packet length, this parameter only adapts to the TCP protocol, UDP is not

7. Reading data from a custom buffer

Code:

Char cgetlen[5]= "n"; Record the first four bytes received

memset (cgetlen,0,sizeof (Cgetlen));

for (j=0;j<intgetlen;j++) {

sprintf (cT1, "%02x", (unsigned char) *bufval);

strcat (CGETLEN,CT1);

bufval++;

}

Description

1. Initialize the receive array Cgetlen all bytes to 0;

2. (unsigned char) *bufval will bufval point to the value of a byte read out, according to the unsigned number interpreted as 16 and decimal, if not set to unsigned number, encountered such as 0XA0, the conversion to a decimal string is not "160", will become a negative "95" , the high level is interpreted in order to sign;

3. Cgetlen does not need to be defined as unsigned, he is only used to convert the 16 binary string to write to the log, not the stored packet

8. How to free up custom buffers

Code:

for (j=0;j<intgetlen;j++) {

bufval--;

}

Lrs_free_buffer (Bufval);

After the buffer bufval need to be released, or Bufval constantly get back, will be more and more long, positioning becomes troublesome, use up inconvenient. The initial release was also met with <memory violation:exception access_violation received>. Looking at the example, think for a long time, finally understand that I read the buffer operation before the pointer, while the release needs to be the initial head pointer, so wrote a piece of dog blood code, through the loop, back to the initial state to release. -_-|||

9. How to calculate the decimal number based on the packet return

When the data is received is divided into two steps, first get four bytes, calculate the length of subsequent packets, and then specify the length to receive. So after getting the returned four bytes, you need to calculate the length. Here I am a byte a byte is converted to a decimal value, for example:

0x11 0x22 0x33 0x44=0d17 0d34 0d51 0d68=256^3*17+256^2*34+256^1*51+256^0*68

Code:

Defined:

unsigned char ct2[3]= "n"; Record the received 10 binary string

L

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.