Turn from 51cto loadrunner performance test-upload file script
LR Upload File script detailed script
Char *fr (char *filename) {
Longupfile; Defining file Handles
intcount; Set to File length
Intnfilelen; Define file length
Char*buffer;
upfile= fopen (filename, "RB"); Opens a binary file as read-only, pointing upfile to the file
Fseek (upfile,0,2); Move the file pointer to the end of the file
nfilelen= Ftell (upfile); gets the number of offset bytes from the end of the file to the file header, which is the number of bytes contained in the file
Fseek (upfile,0,0); Move the file pointer to the file header
Lr_output_message ("nfilelen:%d", Nfilelen); Number of print file bytes
Buffer= (char*) malloc (Nfilelen); Allocating a block of memory with a length of Nfilelen
Count= fread (buffer, sizeof (char), Nfilelen, upfile); reads all data items that contain the number of bytes of the file that the Upfile points to into buffer, and returns the number of bytes of data items, and the file length
Lr_output_message ("count:%d", count); Print the number of bytes read into the file
Lr_save_int (Count, "Fbuff"); Assigning a file length to Fbuff
Lr_save_int (count-1, "Fcurr"); Assigns a file length of 1 to Fcurr
Returnbuffer; Returns the Read file
}
Vuser_init ()
{
Lr_save_string (FR ("E:\\fs\\testfile\\55.txt"), "Fdata");//Save the Read file to the Fdata variable
Return0;
}
Action ()
{
/* Upload file */
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 Point: C language Reading and writing files
1.Fopen:
Function: The first parameter is a pointer to a file, and if the current file does not exist, the system creates the file name. The second parameter is the operation on this file. For example, read-only, read-write, write, etc.
fopen function Prototypes:
File pointer name =fopen (filename, using file mode)
which
File pointer name: A pointer variable that must be indicated as a file type
FileName: File name of the file being opened, is a string constant or an array of strings
How to use file: Refers to the type of file and how the file is manipulated
For example: Open file A in the current directory, run only for read operations, and when FP points to the file
FILE *FP;
fp= ("File A", "R");
There are 12 ways to use the file, and the symbols and meanings are given below.
"RT": Read-only open a text file, allowing only read data
"WT": Write-only to open or create a text file, allowing only write data
"At": Append 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 open a binary file and write data at the end of the file
"rt+": Read and write open a text file that allows read and write
"wt+": Read/write open or create a text file that allows read and write
"at+": Read and write open a text file, allow reading, or append data at the end of the file
"rb+": Read and write open a binary file that allows read and write
"wb+": Read-write open or create a binary file that allows read and write
"ab+": Read and write open a binary file, allow reading, or append data at the end of the file
When a text file is read into memory, the ASCII code is converted to binary code, and the file is written in text to the disk, the binary code is converted to ASCII code, so the text file read and write to spend more time to convert. There is no such conversion to read and write binary files.
2.Fseek function
Function: Used for binary open file, move the file read and write pointer position, usually open, read and write position in order. But sometimes you want to change the reading and writing position, for example, to re-read it from somewhere.
Fseek function prototype: int Fseek (FILE *stream,long offset,int origin)
The first parameter stream is a file pointer
The second parameter offset is the offset, the integer represents the forward offset, and the negative number represents the negative offset
The third parameter, origin, sets the offset from where the file begins, with the value: Seek_cur, Seek_end, or Seek_set
Seek_set: File Start
Seek_cur: Current Location
Seek_end: End of File
where Seek_set,seek_cur and Seek_end and sequentially 0, 1 and 2.
Simply:
Fseek (fp,100l,0); Move the fp pointer to 100 bytes from the beginning of the file;
Fseek (fp,100l,1); Move the fp pointer to 100 bytes from the current position of the file;
Fseek (fp,100l,2); Returns the fp pointer to 100 bytes from the end of the file.
3.Ftell function
Ftell function prototype:long int ftell (FILE * stream);
Function: The number of offset bytes used to obtain the current position of the file position pointer relative to the top of the file. When fetching a file randomly, the program is not easy to determine the current location of the file due to the frequent and backward movement of the file location. Calling the Ftell function makes it very easy to determine the file location
The Ftell function also makes it easy to know the length of a file, such as:
Fsekk (fp,0,2)//Move the FP file pointer to the end of the file
Len=ftell (FP)//Gets the number of offset bytes from the end of the file to the beginning of the file, which corresponds to the number of bytes contained in the file
4.Malloc function
Function prototype:extern void *malloc (Unsignedint num_bytes);
The syntax for malloc is: pointer name = (data type *) malloc (length),
Function: allocates a memory block with a length of num_bytes bytes Description: Returns a pointer to the allocated memory if the assignment succeeds, otherwise returns null pointer. Use the free () function to release memory blocks when memory is no longer in use.
5.fread functions and Fwrite functions
Function: Used to read and write a data block.
Function Prototypes:
Fread (BUFFER,SIZE,COUNT,FP)
Fwrite (BUFFER,SIZE,COUNT,FP)
Description
(1) Buffer: is a pointer to Fread, which is the storage address of the read-in data. For Fwrite, the address of the data to be output.
(2) Size: The number of bytes to read and write;
(3) Count: The number of bytes of data to read and write;
(4) FP: file-type pointer.
LR function
1.lr_save_int (intvalue,const char * param_name)
function, converts an integer to a string type, and saves the string value to the parameter.
2.lr_save_string (const char *param_value, const char *param_name)
Function: Assigns the specified null-terminated string to the parameter
3.web_add_header (Constchar * Head, const char * Content)
Function: For the next Web request, specify the request header
4.web_custom_request (Constchar *requestname,, [Extrares,,] last);
return value:
Return Lr_pass (0) represents success, Lr_fail (1) represents failure.
Functionality: Allows you to create custom HTTP requests using any method and request body. By default, this function is generated when Vugen cannot use other functions to interpret user requests.
All Web Vusers, web_custom_request functions are supported by wapvusers that run in HTTP mode or wireless Session Protocol (WSP) playback mode.
Parameters:
Requestname : The name of the step, the name that is displayed in the tree view in Vugen.
List of Attribute : The following properties are supported:
-
URL: page address.
-
method: How pages are submitted, Post or get.
-
Targetframe
-
enctype: Encoding type. For example,,text/html, is designated as the content-type content-tpye does not match Body
Any designation for "EncType" overrides the Content-type specified by the Web_add_[auto_]header function. When "enctype=" (null value) is specified, the "Content-type" request header is not generated. When "EncType" is omitted, any web_add_[auto_]header function will work. If neither enctype nor web_add_[auto_]header function is specified, and "Method=post", "application/x-www-form-urlencoded" will be used as the default value. In other cases, the Content-type request header is not generated.
List of Recordwithin Att only when recording options--recording--html-based script--Resource The current script step option is selected Ributes will be inserted into the code.
-
reccontenttype: The content type of the response header when the script is recorded. such as text/html, application/x-javascript and so on. When the resource property is not set, it is used to determine whether the target Url "reccontenttype=text/html": Represents HTML text. "Reccontenttype=application/msword": Indicates that MSWord is currently in use.
-
referer : the page associated with the current page. If the address of the URL has been explicitly specified, this entry can be omitted.
-
body: the request body.
Body: A printable string that represents a rule. Cannot represent empty bytes. All characters are represented by a backslash. Note: In the old script, you can see that non-printable characters are encoded in the request body as 16 binary. (For example, "\\x5c"), in which case you must use "Binary=1" to identify it. Null bytes are expressed using "file://0.0.0.0/". instead, the new script places the request body in separate parameters ("body= ...", "bodybinary= ...", body= ...).
Bodybinary: Represents a binary code. Non-printable characters are encoded in the request body in a 16-file://xhh/manner. Here HH represents the hexadecimal value. Null bytes are expressed using "file://0.0.0.0/".
Bodyunicode: American English, especially Latin utf-16le (Little-endian) encoding. This encoding appends a 0 byte to the end of each character to make the character more readable. But the actual parameters in the Vugen remove all 0 bytes. But before sending it to the Web server, the Web_custom_request function will re-add 0 bytes. For non-printable characters, the use of a DSLR slash indicates that an empty byte cannot be represented.
Note: If the request body is larger than 100K, a variable is used instead of the body parameter. The variables are defined in Lrw_custom_body.h.
RAW Body: (currently only available with the web_custom_request function): The request body is passed as a pointer, which points to a string of data. The binary request body can be sent using the Bodybinary property (or by using the Body property, provided that "Binary=1" must be set). In any case, this approach requires the use of an escape character backslash to convert non-printable characters to ASCII characters. In order to have a simpler way of showing the raw data, the raw body property comes into being, passing pointers to binary data.
Bodyfilepath: The path of the file to be transferred as the request body. It cannot be used with the following attributes: Body, or other body property or RAW Body property including Bodybinary,bodyunicode,raw_body_start or Binary=1.
Resource: Indicates whether the URL belongs to a resource. 1 Yes; 0 is not. When this parameter is set, the Reccontenttype parameter is ignored. "Resource=1" means that the current operation has little to do with the success of the script in which it resides. If an error occurs while downloading a resource, it is treated as a warning rather than an error, and the URL is downloaded by the "Run-time setting-browseremulation--download non-html resources" option. The response information for this operation is not parsed as HTML. "Resource=0", which indicates that this URL is important, is not affected by the Send request (RTS) and resolves it when needed.
Resourcebytelimit: cannot be used in HTTP mode, and is not available in concurrent Groups (a zone in the VUser script where all functions in the zone are executed concurrently). Only applies to sockets playback, WinInet is also not applicable.
HTML level: Record intuitive HTML actions on the current web interface. Record these actions in step-by-step Web_url, Web_link, Web_image, and Web_submit_form. Vugen only records requests that return an HTML page, and does not process scripts and applications.
HTTP level: Vugen all requests are recorded as Web_url instructions and do not generate Web_link, web_image, web_submit_form functions. This method is more flexible, but the resulting script is not intuitive.
The URL can be an absolute path (for example, Http://weather.abc.com/weather/forecast.jsp?locCode=LFPO), or it can be a relative path (for example, "Forecast.jsp?loccode=lfpo").
The true URL is downloaded through an absolute path, so the relative URL path must be parsed using the root path URL. For example, using http://weather.abc.com/weather/as the root path to resolve "Forecast.jsp?loccode=lfpo", the final URL is: http://weather.abc.com/weather /forecast.jsp?loccode=lfpo. If you do not specify "Extraresbasedir", the default root URL is the URL of the main page.
UserAgent: User agent, which is the name of an HTTP header used to identify an application, usually a browser, that renders the interaction between the user and the server.
For example: Head Information "user-agent:mozilla/4.0" (compatible; MSIE 6.0; WindowsNT 5.0) "Identifies the IE Browser 6.0 under Window NT. Other user-agent values are used to describe other browsers, or non-browser programs. Typically, all requests in an application use the same user agent, and the recorder is specified as a run-time parameter (run-time setting-browser emulation-useragent). In any case, even in a simple browser process, it is possible to use non-browser components (such as ActiveX controls) that interact directly with the server, usually with user-agent properties that are different from the browser. Specify "useragent" to indicate that this is a non-browser request. The specified string is used by the HTTP header "User-agent:" and in some cases it affects the behavior when the script is played back. For example, do not use browser caching, assuming that the specified URL belongs to a resource, and so on.
LoadRunner itself does not check whether the specified string is the same as the value of the browser itself.
If "binary=0" (the default value), all character sequences are simply passed by literal values.
Pay attention to the use of double slashes. In the C compiler, a double slash is interpreted as a single slash. If you do not need 0 bytes, a single slash can be used in cases where binary is not equal to 1 (for example, using \x20 instead of file://x20/). If you need 0 bytes, you can only use File://x00/and set "Binary=1", \x00 is logically truncated
ContentEncoding: Specifies that the request body is encoded using the specified method (gzip or deflate), and the corresponding "content-encoding:" HTTP header is sent along with this request. This parameter applies to web_custom_request and Web_submit_data.
Extrares: Indicates that the following parameter will be the list of Resource attributes.
Last: The marker at the end.
List of Resource Attributes
The non-HTML mechanism in the Web page produces a list of resources, including JavaScript, ActiveX, and Java applets Andflash the requested resource. Vugen's recording option, you can set these resources to be recorded in the current operation (this setting by default) or as a separate step to record
Upload Attachment Performance test