如何从Jmeter中的http后处理器中的Sample结果中删除原始后文?

时间:2018-07-11 19:19:42

标签: jmeter

我有一个上载1.5 Gb文件的场景,并且我使用了分布式jmeter测试。即请求数据对我的测试没有意义,所以我不希望将后数据从从(代理)jmeter传输到主(服务器),但是,在beanshell后处理器中我没有找到任何用于从http采样器中删除原始帖子数据的API。

https://jmeter.apache.org/api/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.html#setPostBodyRaw-boolean-似乎并非如此。那么,如何才能从Jmeter中的采样器中删除大量的后期数据,以使分布式测试工作更可靠?

2 个答案:

答案 0 :(得分:0)

  1. 默认情况下,JMeter不应将POST数据从从机传输到主机。如果确实如此,请仔细检查您的Results File Configuration并确保将var path = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) + "\\Resources\\script\\myjavascript.js"; var raw = File.ReadAllText(path); var script = raw.Replace("{0}", "my_username"); Console.WriteLine(script); 设置为jmeter.save.saveservice.output_format,并且仅存储绝对必需的那些指标

  1. 仅供参考:您正在查看错误的功能,如果要以编程方式从“文件上传”部分中删除文件,则需要执行类似sampler.setHTTPFiles(new HTTPFileArg\[0\]);

    的操作

    请注意,此更改后,您的文件上传将停止工作。

    1. Starting from JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language用于脚本编写的主要原因是Groovy performs much better than other scripting languages,因此请确保使用Groovy。

答案 1 :(得分:0)

所以,我不知道如何从jmeter从到主通信中排除发布数据;我必须编写自己的JSR-223 Sampler,它使用HttpConnection类执行称为表单Java代码的http请求,然后操纵Java代码中的示例数据。 Http Sampler不适用于我。

这种采样器是好的,因为它还允许使用Input / OutputStreams和缓冲区读取文件并将其发送到http正文,因此我们不再需要HttpSampler用来分配要上传的整个文件的内存。 / p>

示例采样器代码为:

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