我必须从包含10000多条记录的csv文件中读取数据。 我想在JMeter中使用这些数据来访问Web服务。
我用硬编码值编写了代码。但我想让它充满活力。
如何在自定义Java Request Sampler中访问 CSV数据集配置? 如何在我的java请求采样器中访问CSV数据集配置中声明的变量。?
这是我的完整代码:
@Override
public SampleResult runTest(JavaSamplerContext arg0)
{
SampleResult result = new SampleResult();
boolean success = true;
byte arr[] = new byte[] {1,49,45,1,2,(byte)214,1,1,98,0,6,0,0,9,24,0,0,0,0,0,0,0,0,0,0,0,0,0,127,(byte)255,0,21,0,16,0,75,1,0,0,58,32,2,7,0,0,4,4,0,85,81,98,0,5,14,(byte)158,0,2,0,0,0,0,0,88,82,50,69,49,83,49,86,48,67,48,0,0,1,97,75,0,84,30,12,7,17,5,7,50,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,4,6,0,0,48,49,48,48,49,48,51,48,0,0,0,0,0,0,0,0,0,0,70,48,10,29,22,85,0,1,(byte)134,(byte)160,(byte)255,(byte)255,(byte)158,(byte)170,0,0,0,67,0,0,0,0,2,0,0,12,0,12,0,12,0,12,0,13,0,12,0,13,0,12,0,13,0,12,0,13,0,13,0,12,0,12,0,12,0,12,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,13,0,12,0,13,0,13,0,13,0,14,0,13,0,12,0,13,0,13,0,13,0,13,0,12,0,13,0,13,0,13,0,14,0,13,0,13,0,13,0,12,0,13,0,13,2,(byte)158,2,(byte)159,2,(byte)241,2,(byte)234,5,48,5,68,8,90,7,(byte)193,6,15,4,10,3,100,4,(byte)224,7,47,6,72,4,(byte)170,4,4,4,7,5,16,6,107,6,114,5,(byte)195,4,(byte)179,2,(byte)198,0,13,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,13,0,101,0,99,0,(byte)129,0,(byte)129,2,81,2,(byte)224,1,(byte)153,0,(byte)30,0,31,0,14,0,13,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,14,0,14,0,21,0,86,0,98,0,51,0,72,0,104,0,(byte)144,0,(byte)175,0,(byte)174,0,(byte)174,2,20,4,(byte)132,4,103,5,96,0,126,0,14,0,14,0,14,0,14,0,14,0,15,0,14,0,14,0,14,0,14,0,14,0,85,1,41,1,104,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,12,0,13,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,(byte)226,0,(byte)223,0,(byte)223,0,15,0,14,0,13,0,115,0,(byte) 223,(byte)162,40,38,85,115,101,114,78,97,109,101,61,101,82,101,103,38,85,115,101,114,80,97,115,115,119,111,114,100,61,97,98,99,49,50,51};
try
{
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
result.sampleStart();
OutputStream os = con.getOutputStream();
os.write(arr);
os.flush();
os.close();
result.sampleEnd();
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK)
{ //success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString().getBytes());
}
else
{
System.out.println("POST request not worked");
}
}
catch(Exception E)
{
}
//
result.setSuccessful(success);
return result;
}
@Override
public Arguments getDefaultParameters()
{
Arguments dp=new Arguments();
return dp;
}
@Override
public void setupTest(JavaSamplerContext context) {}
@Override
public void teardownTest(JavaSamplerContext context) {
}
答案 0 :(得分:2)
通常你应该可以访问JMeter变量,如:
String myVar = JMeterContextService.getContext().getVariables().get("your_variable_name_here");
但是,如果您不想对其进行硬编码,可以考虑将配置移至Java Request Sampler GUI,如:
String valueFromCsv = "";
String defaultValue = "insert_jmeter_variable_here";
@Override
public Arguments getDefaultParameters() {
Arguments dp = new Arguments();
dp.addArgument("hexData", "insert_jmeter_variable_here");
return dp;
}
@Override
public void setupTest(JavaSamplerContext context) {
valueFromCsv = context.getParameter("hexData", defaultValue );
}
这样您就可以直接从JMeter GUI控制参数值。
参考文献:
答案 1 :(得分:0)
带有CSV数据的完整Java采样器解决方案: 礼貌-https://dzone.com/articles/implement-custom-jmeter-samplers 创建了一个课程
public class VDCSampler extends AbstractJavaSamplerClient implements Serializable {
private static final String ARG1_IDATE = "idate";
private String attrib1;
@Override
public Arguments getDefaultParameters() {
Arguments defaultParameters = new Arguments();
defaultParameters.addArgument(ARG1_IDATE, attrib1);
return defaultParameters;
}
@Override
public void setupTest(JavaSamplerContext javaSamplerContext) {
attrib1 = javaSamplerContext.getParameter(ARG1_IDATE, attrib1);
}
@Override
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
VDCFunctionalitySampling functionalityForSampling = new VDCFunctionalitySampling();
SampleResult sampleResult = new SampleResult();
sampleResult.sampleStart();
try {
String message = functionalityForSampling.testFunction(attrib1);
}
}