0
votes

I have a scenario with upload of 1.5 Gb file and I use distributed jmeter testing. I.e the request data don't make a sense for my tests, so I don't want the post data being transferred from slave (agent) jmeter to master (server), However, in the beanshell post-processor I haven't fount any API for removing the raw post data from the http sampler.

https://jmeter.apache.org/api/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.html#setPostBodyRaw-boolean- seems not be the case. So, how one can remove the large post data from the sampler in Jmeter in order to make distributed test work more robust?

2

2 Answers

0
votes
  1. By default JMeter should not transfer the POST data from slave to master. If it does double check your Results File Configuration and make sure that jmeter.save.saveservice.output_format is set to csv and you're storing only those metrics which are absolutely required

  1. Just FYI: you're looking at wrong function, if you want to remove the file from the "Files Upload" section programmatically you need to do something like sampler.setHTTPFiles(new HTTPFileArg\[0\]);

    Be aware that your file upload will stop working after this change.

    1. Starting from JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for scripting mainly because Groovy performs much better than other scripting languages so going forward make sure to use Groovy.
0
votes

SO, i couldn't figure out how to exclude post data from jmeter slave-to-master communication; I had to write own JSR-223 Sampler which does the http request called form Java code using HttpConnection class and then manipulates the sample data from Java code. Http Sampler didn't worked for me.

such kind of sampler are good because it also allows to read files and send them to http post body using Input/OutputStreams and buffers, thus we no longer require memory for the whole file to be uploaded that HttpSampler uses to allocate.

Example sampler code is:

    import java.net.HttpURLConnection;

    HttpURLConnection httpConn = null;
    String line = null;
    try {
       URL url = new URL("http://${url}/api/v2.0/datasets");
           URLConnection urlConn = url.openConnection();               
        if (!(urlConn instanceof HttpURLConnection)) {
            throw new IOException ("URL is not an Https URL");
        }               
        httpConn = (HttpURLConnection)urlConn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Connection", "keep-alive");
        httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=boundary");
        httpConn.setRequestProperty("User-Agent", "Apache-HttpClient/4.5.5 (Java/1.8.0_161)");
        httpConn.setReadTimeout(500 * 1000);
        httpConn.setDoOutput(true);

        OutputStreamWriter out = new OutputStreamWriter(
                                         httpConn.getOutputStream());


        out.write("--boundary\r\n");
        out.write("Content-Disposition: form-data; name=\"name\"\r\n");
        out.write("Content-Type: text/plain; charset=US-ASCII\r\n");
        out.write("Content-Transfer-Encoding: 8bit\r\n");
        out.write("\r\n");
        out.write("dataset${__time}\r\n");
        out.write("--boundary\r\n");
        out.write("Content-Disposition: form-data; name=\"token\"\r\n");
        out.write("Content-Type: text/plain; charset=US-ASCII\r\n");
        out.write("Content-Transfer-Encoding: 8bit\r\n");
        out.write("\r\n");
        out.write("${user_token}\r\n");
        out.write("--boundary\r\n");
        out.write("Content-Disposition: form-data; name=\"sep\"\r\n");
        out.write("Content-Type: text/plain; charset=US-ASCII\r\n");
        out.write("Content-Transfer-Encoding: 8bit\r\n");
        out.write("\r\n");
        out.write("${separator}\r\n");
        out.write("--boundary\r\n");
        out.write("Content-Disposition: form-data; name=\"csv_file\"; filename=\"filename.csv\"\r\n");
        out.write("Content-Type: text/plain\r\n");
        out.write("Content-Transfer-Encoding: binary\r\n");
        out.write("\r\n");
        out.write("\r\n");
        String filepath="files//${datasetFileName}";
        File file = new File(filepath);
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fileInputStream);
        BufferedReader fin =
                new BufferedReader(isr); 
       String bufString;                  
       while ((bufString = fin.readLine()) != null) {
             out.write(bufString+"r\n");
        }  
        out.write("--boundary--");
        out.flush();
        out.close();

       InputStream _is;
       log.info(""+httpConn.getResponseCode());
       SampleResult.setResponseCode(httpConn.getResponseCode()+"");
       if (httpConn.getResponseCode() >= 400) {         
       _is = httpConn.getErrorStream();
       SampleResult.setSuccessful(false);
       } else {
       /* error from server */
       _is = httpConn.getInputStream();
       }
       if (_is!=null)
       {
       BufferedReader in =
                new BufferedReader(
                    new InputStreamReader(_is)
                    );                   

        String decodedString;
        String accumulate="";
        while ((decodedString = in.readLine()) != null) {
             accumulate=accumulate+"\n"+decodedString;
           log.info(decodedString);
        }       
        SampleResult.setResponseData(accumulate);
       }
       else
       {
        SampleResult.setResponseData("No data from server");
       }
       }
      catch (MalformedURLException e) {
       e.printStackTrace();
       log.info(e.getMessage());
    } catch( SocketTimeoutException e){
       e.printStackTrace();
       log.info(e.getMessage());
     }    
    finally {//httpConn.disconnect();
        }