我无法捕获以使用 HTTPSamplerResult 的方法。
我应该如何获取采样时间(毫秒)、字节数、发送字节数、延迟、连接时间和其他参数。
我无法在监听器中获取这些详细信息:在表中查看结果以及其他监听器。
有人可以帮我解决这个问题吗? 我没有得到如何设置所有变量的实现。
下面是我的 java 请求采样器的代码。
public HTTPSampleResult runTest(JavaSamplerContext arg0)
{
HTTPSampleResult result = new HTTPSampleResult();
//Below array contains the user id and password that comes with every chirp
byte[] idpwd = new byte[]{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};
//below we are converting the hex string coming as parameter to Byte Array.
byte arr[] = toByteArray(arg0.getParameter("HEX"));
//Below we are getting the lengths of both the arrays : idpwd & arr
int aLen = arr.length;
int bLen = idpwd.length;
////////////////////////////////////////////////////////////////////
//below we are initializing the byte array to contain both our hex string converted to byte array and the id-pwd.
byte[] actual_Chirp = new byte[aLen+bLen];
//Below we are concatenating both the arrays.
//first we are adding the arr to the actual_chirp.
//then we are adding the idpwd from the alen to blen.
System.arraycopy(arr, 0, actual_Chirp, 0, aLen);
System.arraycopy(idpwd, 0, actual_Chirp, aLen, bLen);
//Here we we actually hit the meter service
try
{
URL obj = new URL(arg0.getParameter("URL"));
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
result.sampleStart();
/////////////////////////////////////////////////////////////////////////
os.write(actual_Chirp);
os.flush();
os.close();
///////////////////////////////////////////////////////////////////////
result.sampleEnd();
result.setContentType(con.getContentType());
result.setSamplerData(POST_URL);
System.out.println(con.getContentType());
System.out.println(result.getHTTPMethod());
System.out.println(result.getRequestHeaders());
System.out.println(Hex.encodeHexString(result.getResponseData()));
result.setSuccessful(true);
System.out.println(con.getResponseMessage());
}
catch(Exception E)
{
}
//
return result;
}
最佳答案
我认为您缺少示例标签,或者可能更多。 HTTPSampleResult
(或一般的SampleResult
)是一种用于累积与执行相关的所有数据的容器。所有可视化人员或记者仅依赖 SampleResult
进行统计,因此如果他们忽略了您的统计信息,则意味着您没有在其中添加足够的数据供他们使用。 HTTPHC4Impl.java 的源代码对您来说是一个很好的引用。 。我可以发现一些缺失的东西:
标记最明显,这最有可能导致您的问题
result.setSampleLabel("your label");
通常这是在
result.sampleStart();
之前和构造函数之后设置的我无法想象这会导致采样器被忽略,但无论如何:通常您应该设置响应代码和响应消息。这是上面提到的源代码的示例:
res.setResponseCode(Integer.toString(statusCode)); res.setResponseMessage(statusLine.getReasonPhrase());
另请查看该代码中的其他字段。例如,对于 HTTP,方法和 URL 具有特殊属性,这使得它们在结果中具有良好的格式
result.setHTTPMethod("POST"); result.setURL(POST_URL);
关于java - 使用 Java Request Sampler 时如何获取不同监听器中使用的延迟、采样时间、字节和其他此类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46198470/