我有一个上传1.5 Gb文件的场景,我使用分布式jmeter测试。我认为请求数据对我的测试没有意义,所以我不希望将post数据从从(代理)jmeter传输到主(服务器),但是,在beanshell post处理器中,我没有找到任何API来从http采样器中删除原始post数据。
https://jmeter . Apache . org/API/org/Apache/jmeter/protocol/http/sampler/httpsamplerbase . html # setPostBodyRaw-boolean-好像不是这样。那么,如何从Jmeter的采样器中移除大量的post数据,以使分布式测试工作更加健壮呢?
jmeter.save.saveservice.output_format
设置为 csv
,并且您只存储那些绝对需要的指标仅供参考:您正在查看错误的函数,如果您想以编程方式从“文件上传”部分删除文件,您需要执行类似 sampler.setHTTPFiles(新 HTTPFileArg\[0\]);
请注意,在此更改后,您的文件上传将停止工作。
所以,我不知道如何排除后数据从jeter从到主通信;我不得不编写自己的JSR-223采样器,它使用HttpConnection类执行称为表单 Java 代码的超文本传输协议请求,然后操作来自 Java 代码的样本数据。Http采样器不适合我。
这种采样器很好,因为它还允许读取文件并使用Input/OutputStreams和缓冲区将它们发送到http post body,因此我们不再需要内存来上传HttpSampler用来分配的整个文件。
示例采样器代码如下:
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();
}