Recently many students are asked JMeter upload, download the file of the script how to do, to pressure test upload, download the function of the file, script how to do, on-line check all said very vague, this time, we will be good jmeter upload download file good strands, all the whole understand, how a process, how to do.
First, upload/download process.
The process of uploading is the process of throwing your local files to the server.
Download, it is to bring the files on the server, and then saved to your local process.
Summarize, upload is you to the server a file, download it is you get a file from the server.
Second, jmeter do upload script.
Let's start with this process, and then we begin to make the script, which says, the upload is a server that sends data, typically sends data with a POST request.
The jmeter used here is the 3.2 version.
2.1 Preparatory work
There is an interface for uploading files, interface documents such as:
coding is the domain name of our company interface, I am here to start locally, IP is the 127.0.0.1 port number is 8888
With this upload file interface, but also know his parameters, we can make a script.
2.2 Writing the script process
First add a thread group, and then add an HTTP request to the threads group, because it is sending data, all the post requests, writing the uploaded address, and then writing the file path.
1. Add thread Group: Right-click Test Plan, add-threads (Users)-thread group;
2. Add an HTTP request: Right-click the thread group and add the-sampler-http request;
3. Write the URL and path in the HTTP request, and the file to upload
4. Add a view result tree to view the results of the interface call. Right-click Thread Group, add-listener-view result tree;
2.3 Script Instances
Protocol: HTTP
Server name: 127.0.0.1
Port number: 8888
Request Type: Post
Path:/file/upload
body: To choose files_upload this tab page, write file path, and parameter name
Here's the script:
2.4 Run the script to see the results
Look at the diagram below, run the results, the interface returned to success, and then go to the server upload directory, to see if there is no success.
Look at the figure below, the files directory on the server has already been uploaded by us.
JMeter uploading files It's so easy, just try it.
Third, JMeter download the file.
3.1 Preparatory work:
Download the file, in fact, is more simple, is to save a file to the local can, I now find a picture on Baidu URL, we download to the local.
The URL of the picture is: http://a.hiphotos.baidu.com/image/pic/item/4b90f603738da977b1e8c6c7ba51f8198718e313.jpg
3.2 Writing the script process
First, add a thread group, and then add an HTTP request to the threads group, because it is getting the data, all the get requests, and the downloaded address is written.
1. Add thread Group: Right-click Test Plan, add-threads (Users)-thread group;
2. Add an HTTP request: Right-click the thread group and add the-sampler-http request;
3. Write the URL and path in the HTTP request;
But this is the download script, download, there is a step to save the file to our local, this jmeter no ready-made things can be used, we have to write BeanShell to achieve, save the file to the local.
Of course, if you want to write BeanShell, you have to have its syntax, which is written in Java. But also don't worry not to write Java, this save file is dead format so write, remember on the line, need to download the time to copy the past on the line.
4, add BeanShell, get to the contents of the returned file, save to a file inside. Right-click Thread Group, add-sampler- The BeanShell Sampler,beanshell code is as follows:
12345678910111213 |
import java. Io. *;byte[] result = prev. Getresponsedata(); //This is the data that gets returned to the request,Prev is the return of the last request String file_name = "c:\\users\\bjniuhanyang\\desktop\\baidu.jpg"; //represents the location and file name where the files are stored file file = new file(file_name); FileOutputStream out = new fileoutputstream(file); out. Write(result); out. Close(); |
5. Add a view result tree to view the results of the interface call. Right-click Thread Group, add-listener-view result tree;
3.3 Script Instances
Protocol: HTTP
Server name:a.hiphotos.baidu.com
Port number: 80 (hint, the port number is the default is 80, can not write OH)
Request Type: Get
Path:/image/pic/item/4b90f603738da977b1e8c6c7ba51f8198718e313.jpg
The script is as follows:
The following is the HTTP request inside, that is, we get the image of the
The following is the contents of BeanShell, that is, we save the file operation.
2.4 Run the script to see the results
Look at the figure below, run the result, you can see the picture just now, and the request is successful.
Let's go to the desktop and see that the file already exists on the desktop.
Upload files and download files is the operation is so simple, move small deft on hand to try it.
JMeter action-file upload, download