繁体   English   中英

如何从块中读取文件以在Jmeter中上传?

[英]How to read from file in chunks to upload in Jmeter?

我正在编写一个Jmeter测试计划,以将文件分块上传到服务器。 我对Java不了解。

我在Bean Shell Pre-Processor上的HTTP Request Sampler上使用了while控制器 我编写了一个简短的脚本来从文件中获取字节,现在我面临的问题是:HTTP请求采样器在“文件上传”部分中获取文件路径。 有什么方法可以在Bean Shell预处理器中的内存中创建文件,然后在“文件路径”字段中使用该内存文件变量。

我认为理论上是可能的。 因为无论何时上传文件,我们都会先将其存储到内存中,然后再发送到服务器。 因此,我们能否仅在内存中从字节(1 MB的块)创建文件,然后将其作为文件上传发送。 这是我在Bean Shell预处理器中编写的代码

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() );

预期的 :通过JMeter批量上传文件的替代方法, 或者在JMeter HTTP Request Sampler- > Files Upload部分中创建一个内存变量作为文件上传路径的文件

我认为您无法使用HTTP Request采样器实现此目的,我会考虑以下选项:

  1. 使用Java代码实现分块文件上传,而无需依赖JMeter的HTTP Request采样器,例如

     import org.apache.http.client.methods.HttpPost import org.apache.http.entity.InputStreamEntity import org.apache.http.impl.client.CloseableHttpClient import org.apache.http.impl.client.HttpClients CloseableHttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost("http://example.com"); File file = new File("/path/to/your/file"); InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1, "Your file mime type"); //Alternatively: //FileEntity reqEntity = new FileEntity(file, "Your file mime type"); reqEntity.setChunked(true); client.execute(post) 
  2. 使用HTTP Raw Request采样器,该采样器仅通过网络发送分块的数据(可以通过kg.apc.jmeter.samplers.FileReadChunkSize属性控制分块的大小),因此您可以使用诸如Wireshark的嗅探器工具记录相关的请求主体数据然后以所需的块大小重播。 如果需要,请查看JMeter HTTP原始请求采样器-何时以及如何使用它的文章,以获取更多信息。

  3. 还应注意, 从JMeter 3.1开始,建议使用JSR223测试元素和Groovy语言进行脚本编写,因此请考虑在下一个可用机会时迁移到Groovy。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM