2

嗨,我在 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:");
    }
}
4

1 回答 1

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

这三个都包含对“我也想传递一些参数,是否可能”的回答

源代码可从JMeter 下载页面获得

于 2013-11-09T16:27:32.643 回答