嗨Iam Fresher in Jmeter
我编写了一个Java Sampler代码(Copy& Paste),运行此代码后获取成功消息但是请求和响应数据没有显示..如何获取请求和响应数据,我也想传递一些参数,是否有可能
我的Jsampler代码是
package com.wordpress.newspaint.jmeter.java.demo;
import java.io.Serializable;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
public class ExampleJavaTestClassRR extends AbstractJavaSamplerClient implements Serializable {
private static final long serialVersionUID = 1L;
// set up default arguments for the JMeter GUI
@Override
public Arguments getDefaultParameters() {
Arguments defaultParameters = new Arguments();
defaultParameters.addArgument("URL", "http://www.google.com/");
defaultParameters.addArgument("SEARCHFOR", "newspaint");
// defaultParameters.addArgument("ENTITY","BANK");
return defaultParameters;
}
public SampleResult runTest(JavaSamplerContext context) {
String urlString = context.getParameter( "URL" );
String searchFor = context.getParameter( "SEARCHFOR" );
// String ENTITY = context.getParameter( "ENTITY" );
SampleResult result = new SampleResult();
result.sampleStart(); // start stopwatch
try {
java.net.URL url = new java.net.URL(urlString + "?q=" + searchFor);
java.net.HttpURLConnection connection = (java.net.HttpURLConnection)url.openConnection(); // have to cast connection
connection.setRequestMethod("POST");
connection.connect();
result.setSuccessful( true );
result.setResponseMessage("URL IS"+ url);
result.setResponseMessage( "Successfully performed actionSSSSn" );
result.setResponseCodeOK(); // 200 code
} catch (Exception e) {
// result.sampleEnd(); // stop stopwatch
result.setSuccessful( false );
result.setResponseMessage( "Exception: " + e );
// get stack trace as a String to return as document data
java.io.StringWriter stringWriter = new java.io.StringWriter();
e.printStackTrace( new java.io.PrintWriter( stringWriter ) );
result.setResponseData( stringWriter.toString() );
result.setDataType( org.apache.jmeter.samplers.SampleResult.TEXT );
result.setResponseCode( "500" );
}
result.sampleEnd();
return result;
}
void teardownTest() {
System.out.println("inside tear Down:");
}
}
答案 0 :(得分:4)
<强>第一强>:
您的代码示例不是很正确。
这一行:
result.setResponseData( stringWriter.toString() );
根据JavaDoc,它已被弃用
/**
* Sets the responseData attribute of the SampleResult object.
* Should only be called after setting the dataEncoding (if necessary)
*
* @param response
* the new responseData value (String)
*
* @deprecated - only intended for use from BeanShell code
*/
因此,如果您想以这种方式设置响应数据,则需要使用ie __Beanshell函数
调用setResponseData的正确方法是
result.setDataEncoding("UTF-8");
result.setResponseData("Everything is fine".getBytes());
或
result.setResponseData("Everything is fine", "UTF-8);
<强>第二强>
您的代码假设结果仅在失败的情况下返回。将默认http://www.google.com/ URL更改为某个不存在的URL,您将获得异常堆栈跟踪作为响应。
如果您需要在成功时返回某些内容,则需要在try块的末尾添加上面的任何行。
<强>第三强>
据我所知,您无法为Java Request设置请求数据。您需要开发一个能够设置它的采样器。我建议查看ExampleSampler的来源
/src/examples/org/apache/jmeter/examples/sampler/ExampleSampler.java
以及现有的JavaTest和SleepTest Java Request采样器:
/src/protocol/java/org/apache/jmeter/protocol/java/test/JavaTest.java
/src/protocol/java/org/apache/jmeter/protocol/java/test/SleepTest.java
所有三个都包含答案“我也想传递一些参数,是否可能”片
获取