Performance testing post attachments

Transfer from 51cto LoadRunner performance testing - Upload file script

Detailed LR upload script

脚本
char *fr(char *filename){

    longupfile ;    //定义文件句柄

    intcount ;      //定于文件长度

    intnFileLen;    //定义文件长度

    char*buffer;

    upfile= fopen(filename,"rb"); //以只读方式打开二进制文件,将upfile指向该文件



   fseek(upfile,0,2);          //将文件指针移动到文件尾

    nFileLen= ftell(upfile);      //获得文件尾到文件头的偏移字节数,即文件所包含字节数

    fseek(upfile,0,0);          // 将文件指针移动到文件头

    lr_output_message("nFileLen:%d",nFileLen);    //打印文件字节数



    buffer= (char*)malloc(nFileLen);    //分配长度为nfilelen的内存块

    count= fread(buffer, sizeof(char),nFileLen, upfile); //将upfile指向的文件所包含字节数的所有数据项读入到buffer中,并返回数据项的字节数,及文件长度



    lr_output_message("count:%d",count);  //打印读入文件的字节数

    lr_save_int(count,"fbuff");  //将文件长度赋值给fbuff

    lr_save_int(count-1,"fcurr");  //将文件长度-1赋值给fcurr

    returnbuffer;  //返回读取的文件

}

vuser_init()

{

    lr_save_string(fr("E:\\FS\\testfile\\55.txt"),"fdata");//将读取的文件存到fdata变量中

    return0;

}





Action()

{

/*上传文件*/

    lr_think_time(3);

    lr_start_transaction("hdupfile");



    web_add_header("Content-Disposition","attachment;filename=\"testdata.rar\"");

    web_add_header("X-Content-Range","bytes0-{fcurr}/{fbuff}");

    web_add_header("Session-ID","{userid}");

    web_add_header("Content-Type",  "application/octet-stream");



    web_custom_request("hdup",

    "URL=http://10.255.0.149/upload?userId={userid}&bigmd5={userid}&taskId={userid}&offset=0",

        "Method=POST",

        "Resource=0",

        "Referer=",

        "Mode=HTML",

        "Body={fdata}",

        LAST);

    lr_end_transaction("hdupfile",LR_AUTO);

return 0;

}



vuser_end()

{

    return0;

}

 

Knowledge points:

C language to read and write files

  1. 1.Fopen:

Role: The first parameter is a pointer to a file, if the file does not currently exist, the system creates the file name. The second parameter is the operation of this file. For example, read-only, read-write, write and so on.

Fopen Prototype:

Pointer file name = fopen (filename, used papers)

among them:

File pointer name: it must be specified as the type of the pointer variable FILe

File name: name of the file to be opened, a string constant or an array of strings

Use papers: it refers to the type and mode of operation of the file

For example: in the directory of the current file open file a, run only read operations, when fp and points to the file
the FILE * fp;
fp = ( "File A", "R & lt");

 

Use of papers out of 12 given by the following symbols and their significance.
"Rt": read-only open a text file, allowing only read data
"wt": Write only open or create a text file, allowing only write data
"at": append to open a text file and write data at the end of the file
"rb ": read-only open a binary file, allowing only read data
" wb ": write only open or create a binary file, allowing only write data
" ab ": append to open a binary file and write data at the end of the file
" rt + ": open a text file read and write, to allow reading and writing
"wt +": write or create a text file is opened, to allow the reader
"at +": open a text file read and write, to allow reading or adding at the end of the data file
"rb +" : open reading and writing a binary file that allows reading and writing
"wb +": write or create a binary file open, allowing reading and writing
"ab +": open reading and writing a binary file that allows reading, or additional data at the end of file

 

To read a text file into memory, to ASCII code into binary code, and when the file is written to disk as text, we should also converted into ASCII binary code, so read and write text files to spend more conversion time. Read and write binary files such conversion does not exist.

 

  1. 2.Fseek function

Role: for open files in binary mode, move the file pointer position to read and write, usually after the file is opened, read and write position in chronological order but sometimes you want to change the position of reading and writing, for example, starting again from somewhere, read it again.

 

Fseek function prototype: int fseek (FILE * stream, long offset, int origin)

The first parameter is the stream file pointer
second parameter offset is the offset represents an integer of a positive offset, a negative number indicates a negative offset to
the third parameter set from the origin where the beginning of the file offset value is: SEEK_CUR, SEEK_END or SEEK_SET
SEEK_SET: beginning of the file
SEEK_CUR: current position
SEEK_END: end of file
wherein SEEK_SET, SEEK_CUR SEEK_END and 0, 1 and 2 and in turn
Briefly:
fseek (fp, 100L, 0); fp pointer to the 100 bytes from the beginning of the file;
fseek (fp, 100L,. 1); fp pointer to the 100 byte file from a current position;
fseek (fp, 100L, 2); fp pointer back to the 100 words from the end of file Festival at.

 

  1. 3.Ftell function

Ftell function prototype: long int ftell (FILE * stream);

Action: obtaining a current position of the file pointer location the number of bytes offset with respect to the first document. When taking random file, file location due to frequent movements before and after the program is not easy to determine the current location of the file. Call ftell function can easily determine file location

Using ftell function can easily know the length of the file, such as:

Fsekk (fp, 0,2) // move the file pointer fp to the end of the file

Len = ftell (fp) // get the number of bytes offset from the first end of the file to the file, the number of bytes equal to the number of bytes contained in the file

 

 

 

  1. 4.Malloc function

Function Prototype: extern void * malloc (unsignedint num_bytes);

malloc syntax is: name = pointer (data type *) malloc (length),

 

Action: Blocks of memory allocation length of num_bytes bytes: if allocation is successful pointer points to the allocated memory is returned, otherwise, returns NULL NULL. When the memory is no longer used, should be used free () function to release the memory blocks. 

 

  1. 5.fread function and fwrite functions

Function: Used to read and write a data block.

Prototype:

Fread(buffer,size,count,fp)

Fwrite(buffer,size,count,fp)

Description:

(1) buffer: is a pointer to fread, it is read into the data storage address. For fwrite, the address data is to be output.

Number of bytes to read and write;: (2) size

(3) count: the number of read and write to size bytes of data items;

(4) fp: a pointer file type.

 

LR Functions

  1. 1.lr_save_int(intvalue,const char * param_name)

And a conversion to a string type integer, and the value is stored into the parameter string.

  1. 2.lr_save_string(const char *param_value, const char *param_name)

Function: Null end of the specified string assigned to the parameter

 

  1. 3.web_add_header(constchar * Head, const char * Content)

Function: For the next web request, specified request header

 

  1. 4.web_custom_request(constchar *RequestName, , [EXTRARES, ,] LAST );

Returns:
return LR_PASS (0) on behalf of success, LR_FAIL (1) for failure.

Function: The method allows any HTTP request and the request body to create custom. By default, when the user requests VuGen not explain other functions, this function is generated.

All Web Vusers, WAPVusers run in HTTP mode or Wireless Session Protocol (WSP) playback mode are supported web_custom_request function.

 

parameter:

RequestName : name of the step, the name displayed in the tree view VuGen.

 

Of the Attribute List : Supported attributes are the following:

  • URL: page address.

  • Method: submission, POST or GET page.

  • TargetFrame: contains the name of the current frame links or resources.

  • EncType: encoding type. For example, text / html, the value of the request in advance is designated as the content-type. If you specify a content-tpye in the news in advance, but the content-tpye not match the body cause the server side error. It is recommended not to modify the enctype recording obtained.

For any "EncType" will specify coverage web_add_ [auto_] header function specified Content-Type. When specifying the "EncType =" (null value), no "Content-Type" header request. When omitted "EncType", any web_add_ [auto_] header function will work. If neither nor EncType web_add_ [auto_] header function, and "Method = POST", "application / x-www-form-urlencoded" will be used as the default value. In other cases, no request Content-Type header.

Only when Recording Options - when Recording --HTML-based script-- Recordwithin the current script step option is selected, List of Resource Attributes will be inserted into the code.

  • RecContentType: Content-Type header of the response when recording a script. For example, text / html, application / x-javascript and so on. When the Resource attribute is not set, use it to determine whether the target URL is recorded resources. This property contains the primary and secondary resources. The most frequently used type is text, application, image. Depending on the type of minor changes in many resources. For example: "RecContentType = text / html": indicates html text. "RecContentType = application / msword": indicates the current use is Msword.

  • Referer: page associated with the current page. If you have explicitly specify the url address, this may be omitted.

  • Body: request body.

Body: representation rule, printable strings. It can not be represented null byte. All the characters are represented by a backslash. Note: In the old script, you can see the non-printable characters are encoded in the request body in hexadecimal format. (E.g., "\\ x5c"), in this case, you must use the "Binary = 1" is identified. Null byte using the " File: //0.0.0.0/ " to represent. Instead, the request to the new script will be placed into different body parameters ( "Body = ...", " BodyBinary = ...", Body = ... ").

BodyBinary: represent a binary code. Nonprintable characters in hexadecimal in the request body file: // xhh / encoded. Where HH is the hexadecimal value. Null byte using the " File: //0.0.0.0/ " to represent.

BodyUnicode: American English, especially Latin UTF-16LE (little-endian) encoding. This encoding a 0 byte is appended at the end of each character, so that the characters are easier to read. However, in VuGen actual parameters are all zero bytes removed. But before being sent to the Web server, web_custom_request function will be added back 0 bytes. For non-printable characters, use single backslash said they could not express null byte.

Note: If the request body is greater than 100K, instead use a variable parameter Body. Variable is defined in lrw_custom_body.h in.

  • RAW BODY :( function is currently available only web_custom_request): request body is passed as a pointer, a pointer to this data string. Binary request may be sent using BodyBinary body attribute (or attributes using Body be passed, the prerequisite is to set "Binary = 1"). In any case, this method requires the use of the backslash escape character unprintable characters into ASCII characters. In order to have a more convenient way to show the raw data, Raw Body property came into being, can pass a pointer pointing to the binary data.

  • BodyFilePath: a file path as the transmitted request body. It can not be used with the following attributes: Body, or other properties or Raw Body Body property comprises BodyBinary, BodyUnicode, RAW_BODY_START or Binary = 1.

  • Resource: URL indicating whether resources. It is 1; 0 not. After setting this parameter, RecContentType parameter is ignored. "Resource = 1", means that the current operation is successful and where the script or not has little to do. If an error occurs while downloading resources, as warnings rather than errors are handled; whether the URL is downloaded by - impact "Run-Time Setting-BrowserEmulation Download non-HTML resources" for this option. Response information of this operation is not as HTML to parse. "Resource = 0", indicates that this URL is important, not affected by the request to send (RTS), will parse it when needed.

 

  • ResourceByteLimit: not available in HTTP mode, (a district Vuser script, all functions in this region concurrently) in the Concurrent Groups area can not be used. Sockets applies only to playback, WinInet is not applicable.

 

  • Snapshot: Snapshot file name, use association.

 

  • Mode: two types of recording levels of HTML, HTTP.

HTML Level: Recording intuitive HTML action on the current Web interface. With a step by step web_url, web_link, web_image, web_submit_form to record these actions. VuGen recorded only returns the requested HTML page, scripts and applications are not processed.

HTTP Level: VuGen recording all requests to web_url instruction is not generated web_link, web_image, web_submit_form these functions. This method is more flexible, but the resulting script is not intuitive.

 

  • ExtraResBaseDir :( currently available and web_custom_request function only): root URL, put EXTRARES group. It is used to resolve relative URL (Translator added: Similar to the relative and absolute paths for Windows).

URL can be an absolute path (eg http://weather.abc.com/weather/forecast.jsp?locCode=LFPO ), it can also be a relative path (for example, "forecast.jsp? LocCode = LFPO") .

The real download URL is made by absolute path, so you must use a relative URL path to resolve the root URL path. For example, using http://weather.abc.com/weather/ as a root path to resolve "forecast.jsp locCode = LFPO?", The final URL is: http://weather.abc.com/weather/forecast. JSP? locCode = LFPO . If no "ExtraResBaseDir", the default root URL is the URL of the main page.

 

  • UserAgent: user agent, it is a name HTTP header that identifies the application, usually a web browser, it presents the interactive user and the server.

For example: header "User-Agent: Mozilla / 4.0 (compatible; MSIE 6.0; WindowsNT 5.0)" is identified in the IE browser Window NT 6.0. Other User-Agent value used to describe other browsers, or a non-browser programs. Typically, an application requests all use the same user agent, as recorded by a run-time parameter to specify (Run-Time Setting-Browser Emulation-UserAgent). In any case, even in a simple browser process, it may still be used in non-browser components (such as ActiveX controls) that interact directly with the server, they have usually different from the browser's user agent properties. Designated "UserAgent" indicates that this is a non-browser request. The string is specified HTTP header "User-Agent:" use, in some cases, it will also affect the behavior of the script playback. For example, do not use the browser cache, assuming that resources belong to the specified URL, and so on.

LoadRunner itself does not check the specified string value with the browser itself are the same.

  • Binary: "Binary = 1" denotes a page request to each body file: // x / value ## of the form (here, "##" represents two hexadecimal digits), it is replaced with a single byte hexadecimal value.

If "Binary = 0" (the default), all character sequences according to the value transfer only literally.

Note the use of double slashes. In the C compiler are interpreted as a single double slash slash. If no zero bytes, a single slash Binary may not be equal in the case of using a (e.g., using \ instead of on the x20 File: // on the x20 / ). If you need a zero byte, you can only use the file: // x00 / and set "Binary = 1", \ x00 will be truncated logically

 

  • ContentEncoding: Specifies the use specified in the request body (gzip or the deflate) is encoded (e.g., compressed), the corresponding "ContentEncoding:" HTTP header and the request will be transmitted together. This parameter applies to web_custom_request and web_submit_data.

  • EXTRARES: The following parameters will show is the List Of Resource Attributes.

  • LAST: end identifier.

The Attributes of Resource List
Web page mechanism produces non-HTML resource list that contains the resource Javascript, ActiveX, Java applets andFlash requested. VuGen's the Recording option, you can set these resources recorded in the current operation (this is the default setting) or as a separate step to record

Published 30 original articles · won praise 21 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44078196/article/details/104795356