jmter sampleEnd called twice异常

解决:

将 SampleResult 定义成局部变量即可解决

DB---------------------------------

package test;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

import com.alibaba.fastjson.JSONObject;

import dao.UserAcountDaoImpl;
import entity.UserAcount;

public class PerformenceTestDB implements JavaSamplerClient {
    
    //private SampleResult results;
    private String url;

    // 设置传入的参数,可以设置多个,已设置的参数会显示到Jmeter的参数列表中 
    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        
        return params;
    }

    // 测试执行的循环体,根据线程数和循环次数的不同可执行多次 
    @Override
    public SampleResult runTest(JavaSamplerContext arg0) {
        SampleResult results = new SampleResult();    
        results.sampleStart();
        try {
            UserAcountDaoImpl impl = new UserAcountDaoImpl();
            List<UserAcount> list = impl.getUserAcountByPwd("25d55ad283aa400af464c76d713c07ad");
            /*for(UserAcount user : list){
                System.out.println(JSONObject.toJSON(user));
                //System.out.println(SecretExtension.asDecryptByAes(user.getAccount()));;
                System.out.println("################################################################");
                
            }*/
            results.setResponseData(JSONObject.toJSONString(list).getBytes());
            results.setSuccessful(true);
        } catch (Exception e) {
            results.setSuccessful(false);
            e.printStackTrace();
        }finally{
            results.sampleEnd();
        }
        return results;
    }


    // 初始化方法,实际运行时每个线程仅执行一次,在测试方法运行前执行
    @Override
    public void setupTest(JavaSamplerContext arg0) {
        //results = new SampleResult();
    }

    // 结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行 
    @Override
    public void teardownTest(JavaSamplerContext arg0) {
        
    }
    
    /*public static void main(String[] args) {
        Arguments params = new Arguments();
        JavaSamplerContext context = new JavaSamplerContext(params);
        PerformenceTestDB test = new PerformenceTestDB();
        test.setupTest(context);
        test.runTest(context);
        test.teardownTest(context);
    }*/

}
 

 

----------------------HTTP

package test;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

public class PerformenceTest implements JavaSamplerClient {
    
    //private SampleResult results;
    private String url;

    // 设置传入的参数,可以设置多个,已设置的参数会显示到Jmeter的参数列表中 
    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("url", "http://192.168.1.86/#/Web/Home");
        return params;
    }

    // 测试执行的循环体,根据线程数和循环次数的不同可执行多次 
    @Override
    public SampleResult runTest(JavaSamplerContext arg0) {
        SampleResult results = new SampleResult();
        url = arg0.getParameter("url");
        results.sampleStart();
        
        
    /*    try {
            OutputService test = new OutputService();
            test.out(filename, Integer.parseInt(a), Integer.parseInt(b));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            results.sampleEnd();
        }*/
        
        HttpGet get = new HttpGet(url);
        try {
            CloseableHttpResponse response = HttpClients.createDefault().execute(get);
            HttpEntity httpEntity = response.getEntity();
            if(httpEntity != null){
                String resultData = EntityUtils.toString(httpEntity);
                results.setResponseData(resultData.getBytes());
                results.setDataEncoding("UTF-8");
                results.setSuccessful(true);
                response.close();
            }
        } catch (ClientProtocolException e) {
            results.setSuccessful(false);
            e.printStackTrace();
        } catch (IOException e) {
            results.setSuccessful(false);
            e.printStackTrace();
        }finally {
            results.sampleEnd();
        }
        //System.out.println("--------response= "+results.getResponseDataAsString());
        
        return results;
    }


    // 初始化方法,实际运行时每个线程仅执行一次,在测试方法运行前执行
    @Override
    public void setupTest(JavaSamplerContext arg0) {
        //results = new SampleResult();
    }

    // 结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行 
    @Override
    public void teardownTest(JavaSamplerContext arg0) {
        
    }
    
/*    public static void main(String[] args) {
        Arguments params = new Arguments();
        params.addArgument("url", "http://192.168.1.86/#/Web/Home");
        JavaSamplerContext context = new JavaSamplerContext(params);
        PerformenceTest test = new PerformenceTest();
        test.setupTest(context);
        test.runTest(context);
        test.teardownTest(context);
    }*/

}

转载于:https://my.oschina.net/pEHq2WMa5p5/blog/863909

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值