I am writing a Jmeter Test Plan to upload files to server in chunks. I've little knowledge of Java.
I've used while controller on HTTP Request Sampler with Bean Shell Pre-Processor. I have written a short script to get bytes from file, now the problem I am facing is: The HTTP Request Sampler takes file path in File Upload Section. Is there any way to create a file in memory in Bean Shell Pre Processor and then use that memory file variable in File Path field.
What I think is theoretically, It is possible. Because whenever we upload a file, we first take it into memory and then send to server. So, can we just create a file in memory from bytes (chunks of 1 MB) and then send it as file upload. Here is the code I've written in Bean Shell Pre-Processor
Integer maxChunkSize = new Integer(vars.get("FILE_MAX_SIZE"));
String uploadFilePath = vars.get("UPLOAD_FILE");
uploadFileSize = new File(uploadFilePath).length();
InputStream uploadFile = new BufferedInputStream(new FileInputStream(uploadFilePath));
int offset = whileCounter * maxChunkSize;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes;
int size;
/* creating bytes array to hold file data*/
if (offset < uploadFileSize) {
if (((int) offset + (int) maxChunkSize) < uploadFileSize) {
bytes = new byte[ (int) maxChunkSize];
size = maxChunkSize;
} else {
size = (int) (uploadFileSize - offset);
bytes = new byte[ (int) size];
vars.put("WHILE_LOOP", "0");
}
}
/* printing results for debugging */
/*
log.info(" ============================================================== ");
log.info("While counter " + whileCounter.toString() );
log.info("While loop " + vars.get("WHILE_LOOP").toString() );
log.info("The file to upload is : " + uploadFilePath);
log.info("Maximum Chunk size is : " + maxChunkSize.toString());
log.info("Current Offset is : " + offset.toString());
log.info("The file size is " + uploadFileSize.toString());
log.info(" ============================================================== ");
*/
/* here it is giving method invocation on loop counter 2, so skip method is used */
uploadFile.skip(offset);
int bytesRead = uploadFile.read(bytes, 0, size);
/* write to byte output stream to read as a file */
bos.write(bytes, 0, bytesRead);
/* params for next iteration */
uploadFile.close();
whileCounter++;
vars.put("WHILE_COUNTER", whileCounter.toString() );
Expected: An alternative to upload file in chunks via JMeter OR Create a memory variable that will act as file for file upload path in JMeter HTTP Request Sampler -> Files Upload Section.