嗨,我在 Jmeter 中更新鲜
我写了一个 Java 采样器代码(复制和粘贴),运行此代码后获得成功消息但请求和响应数据没有显示..我怎样才能获得请求和响应数据,我也想传递一些参数,是否可以
我的 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:");
}
}