1
votes

I have to read data from a csv file which contains 10000+ records. I want to use this data in JMeter to hit a web service.

I had written my code with the hard coded value. But I want to make it dynamic.

How can I access CSV data set config in my custom Java Request Sampler...? How can I access the variable i declared in the CSV data set config in my java request sampler..?

Here is my full code :

@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) {
}
2
Can you add your current code? - user7294900
In http sampler we directly read the variables declared in the csv data set config. But how can we do that when using a custom java request sampler - Peter
The hex data string in the byte array is what i want to send from a file. - Peter

2 Answers

2
votes

Normally you should be able to access JMeter Variables like:

String myVar = JMeterContextService.getContext().getVariables().get("your_variable_name_here");

However if you don't want to have it hard-coded you might consider moving the configuration to Java Request Sampler GUI like:

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

This way you will be able to control the parameter value directly from JMeter GUI.

References:

0
votes

A full java sampler solution with CSV data: courtesy - https://dzone.com/articles/implement-custom-jmeter-samplers Created a class

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

enter image description here