如何在JMeter中将数据从JSR223采样器传递到Http请求采样器

wkyowqbh  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(340)

之前我们使用CSV数据集配置和我们的Jmeter脚本读取此种子数据并准备HTTP请求,它运行良好。
但是根据当前的需求,我们需要从mongodb获取数据,并将这些动态值传递给HTTP请求采样器,我的意思是,我们需要根据数据库中的动态值构建HTTP请求采样器?
你能请谁帮我一下吗?

hi3rlvi2

hi3rlvi21#

在JSR 223采样器中有vars的缩写,它代表JMeterVariables类示例,它提供对线程范围中所有JMeter变量的读/写访问。
因此,如果您在JSR 223采样器中执行以下操作:

vars.put('foo', 'bar')

它将创建值为barfoo JMeter变量,您可以在HTTP请求采样器中将其称为{foo}

关于MongoDB连接设置,您可以参考How to Load Test MongoDB with JMeter文章,为了获得提到的测试元素(MongoBD源配置和MongoDB脚本),将下一行添加到 user.properties 文件:

not_in_menu=org.apache.jmeter.protocol.mongodb.sampler.MongoScriptSampler,org.apache.jmeter.protocol.mongodb.config.MongoSourceElement

需要重新启动JMeter才能提取属性。

2lpgd968

2lpgd9682#

不确定使用JSR223 Sampler进行计算,然后将值传递到HTTP Request Sampler中有多大好处。
使用JSR223 PreprocessorJSR223 Sampler肯定有更好的主意。从我的Angular 来看,有两种选择:
1.直接使用JSR223 Sampler进行所有计算,从数据库中提取数据,并进行HTTP调用
1.在JSR223 Sampler中使用JSR223 Preprocessor,并访问JSR223 Preprocessor中的值。当为不同请求计算一些公共值时,此方法非常有用,例如,S3的验证签名,其中为下载、上载、更新文件/标记等不同操作计算签名。
对于第二种方法,可以创建一个JSR223 Preprocessor并编写代码来计算数据,将这些数据设置为vars并访问JSR223 Sampler。可以参考用于S3签名计算的预处理器示例:JMeter throws error 'Signature doesn't match' while uploading file to s3
这很简单,下面是将JSR223 Preprocessor中计算的值访问到HTTP Request Sampler中的步骤:
1.计算值并添加到vars中,varsJSR223可用的键值Map,例如:'vars.put(“key”,“value”)
1.通过使用字符串变量模板(例如${key})访问HTTP Request SamplerHTTP Header Manager中的值或任何位置。它从varsMap中获取给定键的值。
虽然从JSR223 Preprocessor获取baked数据很容易,但我的建议是使用JSR223 Sampler代替您的用例。
使用JSR223 Sampler,您可以编写代码从数据库中提取数据,并直接发出HTTP请求。
提供代码片段以供参考:

import org.apache.http.HttpHeaders;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;
import java.util.Map;
import java.util.HashMap;
import java.io.ByteArrayInputStream;
import org.apache.http.entity.InputStreamEntity;

def x_amz_date = vars.get("x_amz_date");
def authorization = vars.get("aws_authorization");
def content_type = "plain/text";
def x_amz_security_token = <session token for s3>;
def x_amz_content_sha256 = vars.get("aws_sha256");
def host = <host name for s3 service>;
def content_length = vars.get("aws_content_length");

def url = "https://" + host + "/" + <bucket> + "/" + <path or directory> + "/" + <file name in s3>

log.info("x_amz_date : " + x_amz_date);
log.info("authorization : " + authorization);
log.info("content_type : " + content_type);
log.info("x_amz_security_token : " + x_amz_security_token);
log.info("x_amz_content_sha256 : " + x_amz_content_sha256);
log.info("host : " + host);
log.info("content_length : " + content_length);
log.info("url : " + url);

    Map<String, String> result = new HashMap<>();

    RequestConfig requestConfig = RequestConfig
        .custom()
        .setConnectTimeout(10000)
        .setSocketTimeout(10000)
        .build();

    //byte array for the file which need to be uploaded in s3
    byte[] data = ("X5O!P%@AP<insert>[4\\PZX54(P^)7CC)7}\044EICAR-STANDARD-ANTIVIRUS-TEST-FILE!\044H+H*".replace("<insert>", "")).getBytes();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
    InputStreamEntity entity = new InputStreamEntity(inputStream);

    RequestBuilder requestBuilder = RequestBuilder
        .put()
        .setConfig(requestConfig)
        .setUri(url)
        .setHeader("X-Amz-Date", x_amz_date)
        .setHeader("Authorization", authorization)
        .setHeader("Content-Type", content_type)
        .setHeader("X-Amz-Security-Token", x_amz_security_token)
        .setHeader("X-Amz-Content-Sha256", x_amz_content_sha256)
        .setHeader("Host", host)
        //.setHeader("Content-Length", content_length)
        .setEntity(entity);

    HttpUriRequest request = requestBuilder.build();

    HttpClientBuilder
        .create()
        .build()
        .withCloseable { 
            httpClient -> 
            httpClient.execute(request)
                .withCloseable{ response -> 
                    String res = "RESPONSE:" + "\n"+response.getStatusLine()+"\n"+"Headers: "+response.getAllHeaders();
                    log.info("--------Result: " + res)
                    result.put("result", res)
        }
    }

result

相关问题