【问题标题】:How to read from file in chunks to upload in Jmeter?如何从块中读取文件以上传到Jmeter?
【发布时间】:2019-01-30 11:24:44
【问题描述】:

我正在编写一个 Jmeter 测试计划以将文件分块上传到服务器。我对 Java 知之甚少。

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

我认为理论上是有可能的。因为每当我们上传文件时,我们首先将其放入内存,然后发送到服务器。那么,我们是否可以从字节(1 MB 的块)在内存中创建一个文件,然后将其作为文件上传发送。这是我在 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() );

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

【问题讨论】:

    标签: java file-upload jmeter beanshell chunks


    【解决方案1】:

    我不认为你可以使用HTTP Request 采样器来实现这一点,我会考虑的选项是:

    1. Implement chunked file upload using Java code 不依赖于 JMeter 的 HTTP 请求采样器,类似于

      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属性控制),因此您可以使用像@这样的嗅探器工具记录关联的请求正文数据987654324@ 并稍后以所需的块大小重播它。如果需要,请查看 The JMeter HTTP Raw Request Sampler - When and How to Use It 文章了解更多信息。

    3. 还要注意since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting,因此请考虑在下次有机会时迁移到 Groovy。

    【讨论】:

      猜你喜欢
      • 2019-06-29
      • 1970-01-01
      • 2020-05-04
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多