Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

LoadRunner performance Test-upload File script

2025-03-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

LR upload file script detailed explanation script

Char * fr (char * filename) {

Longupfile; / / define file handle

Intcount; / / set the file length

IntnFileLen; / / define the file length

Char*buffer

Upfile= fopen (filename, "rb"); / / Open the binary file read-only and point the upfile to the file

Fseek (upfile,0,2); / / move the file pointer to the end of the file

NFileLen= ftell (upfile); / / get the number of bytes offset from the end of the file to the head of the file, that 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 bytes of the printed file

Buffer= (char*) malloc (nFileLen); / / allocate memory blocks of length nfilelen

Count= fread (buffer, sizeof (char), nFileLen, upfile); / / reads all the data items in the number of bytes contained in the file pointed to by upfile into buffer, and returns the number of bytes of the data items, and the length of the file

Lr_output_message ("count:%d", count); / / print the number of bytes read into the file

Lr_save_int (count, "fbuff"); / / assign the file length to fbuff

Lr_save_int (count-1, "fcurr"); / / assign the file length-1 to fcurr

Returnbuffer; / / returns the read file

}

Vuser_init ()

{

Lr_save_string (fr ("E:\ FS\\ testfile\\ 55.txt"), "fdata"); / / Save the read file in the fdata variable

Return0

}

Action ()

{

/ * upload files * /

Lr_think_time (3)

Lr_start_transaction ("hdupfile")

Web_add_header ("Content-Disposition", "paired testdata.rar\" filename =\ "filename\")

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. Fopen:

What it does: the first parameter is to point to a file that will be created if the current file does not exist. The second parameter is the operation on this file. For example, read-only, read-write, write, etc.

Fopen function prototype:

File pointer name = fopen (file name, using file method)

Where:

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

File name: the file name of the opened file, which is a string constant or an array of strings

Use of files: refers to the type of files and the way they are manipulated

For example: open the file file an in the current directory, only run for read operation, and when fp points to the file

FILE * fp

Fp= ("file a", "r")

There are 12 ways to use files, and their symbols and meanings are given below.

"rt": read-only opens a text file, and only data is allowed to be read

"wt": only write to open or create a text file, only data is allowed to be written

"at": append to open a text file and write data at the end of the file

"rb": read-only opens a binary file, and only data is allowed to be read

"wb": only write to open or create a binary file, only data is allowed to be written

"ab": append to open a binary file and write data at the end of the file

"rt+": read and write opens a text file that allows reading and writing

"wt+": read and write to open or create a text file that allows reading and writing

"at+": read-write opens a text file, allows reading, or appends data to the end of the file

"rb+": read and write opens a binary file that allows reading and writing

"wb+": read and write to open or create a binary file that allows reading and writing

"ab+": read-write opens a binary file, allows reading, or appends data to the end of the file

When a text file is read into memory, the ASCII code is converted into binary code, and when the file is written to disk in text form, the binary code is also converted into ASCII code, so it takes more time to read and write the text file. There is no such conversion for reading and writing binaries.

2. Fseek function

Function: for files opened in binary mode, move the reading and writing pointer position of the file, usually after the file is opened, the reading and writing positions are in order. But sometimes you want to change the position of reading and writing, such as starting somewhere and reading again.

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

The first parameter stream is the file pointer

The second parameter offset is offset, an integer indicates a positive offset and a negative number indicates a negative offset

The third parameter, origin, sets where the offset starts in the file. The values are: SEEK_CUR, SEEK_END, or SEEK_SET.

SEEK_SET: the beginning of the file

SEEK_CUR: current location

SEEK_END: end of file

Among them, SEEK_SET,SEEK_CUR and SEEK_END are 0BI 1 and 2, respectively.

In short:

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 location 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: used to obtain the number of bytes of offset of the current position of the file location pointer relative to the beginning of the file. When fetching files at random, due to the frequent forward and backward movement of the file location, it is not easy for the program to determine the current location of the file. The location of the file can be easily determined by calling the ftell function.

You can also easily know the length of a file by using the ftell function, such as:

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

Len=ftell (fp) / / gets the number of bytes offset from the end of the file to the beginning of the file, which is equal to the number of bytes contained in the file

4. Malloc function

Function prototype: extern void * malloc (unsignedint num_bytes)

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

Function: allocate num_bytes bytes of memory block description: if the allocation is successful, it returns a pointer to the allocated memory, otherwise a null pointer NULL is returned. When memory is no longer in use, you should use the free () function to free the memory block.

5. Fread function and fwrite function

Function: used to read and write a data block.

Function prototype:

Fread (buffer,size,count,fp)

Fwrite (buffer,size,count,fp)

Description:

(1) buffer: it is a pointer, and for fread, it is the address where the read data is stored. For fwrite, the address to which the data is to be output.

(2) size: the number of bytes to read and write

(3) count: how many size bytes of data items to read and write

(4) fp: file pointer.

LR function

1. Lr_save_int (intvalue,const char * param_name)

Function to convert an integer type to a string type and store the string value in the parameter.

2. Lr_save_string (const char * param_value, const char * param_name)

Function: assign a specified string ending with Null to a parameter

3. Web_add_header (constchar * Head, constchar * 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) for success and LR_FAIL (1) for failure.

Function: 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, WAPVusers running in HTTP mode or Wireless Session Protocol (WSP) playback mode support the web_custom_request function.

Parameters:

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

List of Attribute: the following attributes are supported:

URL: page address.

Method: the way the page is submitted, POST or GET.

TargetFrame: the name of the frame that contains the current link or resource.

EncType: encoding type. For example, text/html, the value of content-type specified in the request header. If content-tpye is specified in the header, but the content-tpye does not match the body, it will result in a server-side error. Therefore, it is recommended not to modify the recorded enctype.

Any assignment to "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. Any web_add_ [auto_] header function works when "EncType" is omitted. If neither EncType nor web_add_ [auto_] header function is specified, and "Method=POST", "application/x-www-form-urlencoded" is used as the default. In other cases, no Content-Type request header is generated.

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

RecContentType: the content type of the response header when recording the script. For example, text/html, application/x-javascript, etc. When the Resource property is not set, it is used to determine whether the target URL is a recordable resource. This property contains primary and secondary resources. The most frequently used types are text, application, and p_w_picpath. Secondary types vary a lot from resource to resource. For example: "RecContentType=text/html": represents html text. "RecContentType=application/msword": indicates that Msword is currently being used.

Referer: the page associated with the current page. This item can be omitted if the address of the url has been explicitly specified.

Body: request body.

Body: a regular, printable string. Cannot represent null bytes. All characters are represented by a backslash. Note: in the old script, you can see that non-printable characters are encoded in hexadecimal form in the request body. (for example, "\ x5c"), in this case, it must be identified with "Binary=1". Null bytes are represented by "file://0.0.0.0/"". In contrast, the new script will expose the request body in different parameters ("Body=...", "BodyBinary=...", Body=... ").

BodyBinary: represents binary code. Non-printable characters are encoded in hexadecimal file://xhh/ in the request body. Here HH represents a hexadecimal value. Null bytes are represented by "file://0.0.0.0/"".

BodyUnicode: American English, especially the Latin UTF-16LE (little-endian) code. This encoding appends a 0 byte to the end of each character to make the character more readable. But in VuGen, the actual parameter removes all 0 bytes. But before sending it to the Web server, the web_custom_request function adds 0 bytes again. For non-printable characters, a single backslash is used and null bytes cannot be represented.

Note: if the request body is greater than 100K, a variable will be used instead of the Body parameter. Variables are defined in lrw_custom_body.h.

RAW BODY: (currently available only with the web_custom_request function): the request body is passed as a pointer, which points to a string of data. A binary request body can be sent using the BodyBinary attribute (or passed using the Body attribute, provided that "Binary=1" must be set). In any case, this approach requires the use of escape character backslashes to convert non-printable characters into ASCII characters. In order to have an easier way to represent raw data, the Raw Body attribute arises at the historic moment, which can pass pointers to binary data.

BodyFilePath: the path to the file transferred as the request body. It cannot be used with the following properties: Body, or other Body properties or Raw Body properties including BodyBinary,BodyUnicode, RAW_BODY_START, or Binary=1.

Resource: indicates whether URL belongs to a resource. 1 Yes; 0 No. 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. If an error occurs when downloading a resource, it is treated as a warning rather than an error; whether URL is downloaded or not is affected by the "Run-Time Setting-BrowserEmulation--Download non-HTML resources" option. The response information for this operation is not parsed as a HTML. "Resource=0" indicates that this URL is important, is not affected by the sending request (RTS), and will be parsed when needed.

ResourceByteLimit: cannot be used in HTTP mode, nor in the Concurrent Groups (a zone in a Vuser script where all functions execute concurrently). Only for Sockets playback, WinInet is not applicable.

Snapshot: the file name of the snapshot, used when associating.

Mode: two recording levels, HTML and HTTP.

HTML level: record intuitive HTML actions on the current Web interface. Record these actions with step-by-step web_url, web_link, web_p_w_picpath, web_submit_form. VuGen only records requests that return to HTML pages and does not process scripts and applications.

HTTP level: VuGen records all requests as web_url instructions and does not generate functions such as web_link, web_p_w_picpath and web_submit_form. This approach is more flexible, but the generated script is not intuitive enough.

ExtraResBaseDir: (currently available only with the web_custom_request function): root URL, put in the EXTRARES group. It is used to analyze relative URL (relative path and absolute path similar to Windows).

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

The real URL is downloaded through an absolute path, so the relative URL path must be parsed using the root path URL. For example, use http://weather.abc.com/weather/ as the root path to parse "forecast.jsp?locCode=LFPO", and the final URL is: http://weather.abc.com/weather/forecast.jsp?locCode=LFPO. If "ExtraResBaseDir" is not specified, the default root URL is the URL of the main page.

UserAgent: user agent, which is the name of a HTTP header that identifies the application, usually the browser, and represents the interaction between the user and the server.

For example, the header message "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; WindowsNT 5.0)" identifies 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 runtime 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 different user agent properties than browsers. Specifying "UserAgent" indicates that this is a non-browser request. The specified string is used by the HTTP header "User-Agent:", which in some cases also affects the behavior of the playback script. For example, do not use browser caching, assume 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 browser itself.

Binary: "Binary=1" means that each value in the page request body in the form of file://x/## (where "# #" represents two hexadecimal digits) is replaced with a single-byte hexadecimal value.

If "Binary=0" (the default), all character sequences are passed literally.

You need to pay attention to the use of double slashes. A double slash is interpreted as a single slash in the C compiler. If zero bytes are not needed, a slash can be used if Binary is not equal to 1 (for example, use\ x20 instead of file://x20/). If zero bytes are required, you can only use file://x00/ and set "Binary=1".\ x00 will be logically truncated.

ContentEncoding: specifies that the request body is encoded in the specified way (gzip or deflate) (for example, compression), and the corresponding "Content-Encoding:" HTTP header is sent with this request. This parameter applies to web_custom_request and web_submit_data.

EXTRARES: indicates that the following parameter will be 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 that are requested by Javascript, ActiveX, and Java applets andFlash. In the Recording option of VuGen's, you can set whether to record these resources in the current operation (the default is this setting) or as a separate step.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report