java程序员SuperTestman_博客已迁移

Apache Jmeter进阶-RPC服务压测

2017-03-10  本文已影响391人  唐影若凡

声明:原创文章,转载请注明出处。http://www.jianshu.com/u/e02df63eaa87

在上一节Apache Jmeter入门中,我们简单介绍了Jmeter的使用方法。
本节中,我们将着重介绍Jmeter压测,以Thrift RPC为例子进行讲解。

1、准备

我们依然采用Thrift RPC这节中的RPC服务作为服务端程序并运行。

服务端

2、Jmeter测试类编写

2.1 Jmeter测试类简介
<dependency>
    <groupId>org.apache.jmeter</groupId>
    <artifactId>ApacheJMeter_core</artifactId>
    <version>2.13</version>
 </dependency>
 <dependency>
    <groupId>org.apache.jmeter</groupId>
    <artifactId>ApacheJMeter_java</artifactId>
    <version>2.13</version>
</dependency>
2.2 RPC Thrift测试类编写

ThriftClient

public class ThriftClient {
    private ComputeServer.Client client = null;
    private TTransport transport = null;

    protected ThriftClient(String ip, int port) {
        try {
            TTransport transport = new TFramedTransport(new TSocket(ip, port));
            transport.open();
            TProtocol protocol = new TBinaryProtocol(transport);
            client = new ComputeServer.Client(protocol);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected ComputeResponse getResponse(ComputeRequest request) {
        try {
            ComputeResponse response = client.getComputeResult(request);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    protected void close() {
        if (transport != null && transport.isOpen()) {
            transport.close();
        }
    }
}

TestThriftByJmeter

public class TestThriftByJmeter extends AbstractJavaSamplerClient {
    private ThriftClient client;
    private ComputeRequest request;
    private ComputeResponse response;

    @Override
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
        arguments.addArgument("ip", "127.0.0.1");
        arguments.addArgument("port", "9000");
        arguments.addArgument("X", "0");
        arguments.addArgument("Y", "0");
        arguments.addArgument("type", "0");
        return arguments;
    }

    @Override
    public void setupTest(JavaSamplerContext context) {
        try {
            // 获取参数
            String ip = context.getParameter("ip");
            int port = context.getIntParameter("port");
            int x = context.getIntParameter("X");
            int y = context.getIntParameter("Y");
            ComputeType type = getComputeType(context.getIntParameter("type"));

            // 创建客户端
            client = new ThriftClient(ip, port);
            // 设置request请求
            request = new ComputeRequest(x, y, type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.setupTest(context);
    }
    
    public SampleResult runTest(JavaSamplerContext context) {
        SampleResult result = new SampleResult();
        result.sampleStart();
        try {
            long begin = System. currentTimeMillis();
            response = client.getResponse(request);
            long cost = (System.currentTimeMillis() - begin);
            System.out.println(response.toString() + " cost:[" + cost + "ms]");

            if (response == null) {
                result.setSuccessful(false);
                return result;
            }
            if (response.getErrorNo() == 0) {
                result.setSuccessful(true);
            } else {
                result.setSuccessful(false);
            }
        } catch (Exception e) {
            result.setSuccessful(false);
            e.printStackTrace();
        }  finally {
            result.sampleEnd();
        }
        return result;
    }
    
    public void tearDownTest(JavaSamplerContext context) {
        if (client != null) {
            client.close();
        }
        super.teardownTest(context);
    }

    ComputeType getComputeType(int type) {
        ComputeType computeType;
        switch (type) {
            case 0: computeType = ComputeType.ADD; break;
            case 1: computeType = ComputeType.SUB; break;
            case 2: computeType = ComputeType.MUL; break;
            case 3: computeType = ComputeType.DIV; break;
            default: computeType = ComputeType.ADD; break;
        }
        return computeType;
    }
}

代码结构:

代码目录结构

3、Jmeter测试

3.1 添加依赖

首先将2.2节中的代码编译打包,并使用命令mvn dependency:copy-dependencies -DoutputDirectory=lib将该代码的Jar包和所依赖的包,添加到Jmeter的apache-jmeter-2.13\lib\ext目录下。

3.2 Jmeter设置

打开Jmeter,设置如下信息。

Jmeter设置

在添加Java请求时,注意要选择2.2节中的Jmeter测试类,下面的列表中可以看到参数和默认值

添加Java请求
3.3 测试

下面我们将进行持续一分钟的的性能压测,首先设置线程组,设置10个并发线程。

设置线程组

之后进行测试,测试结束后,可以在聚合报告中看到此次压测的结果。

聚合报告

服务端日志:

服务端
上一篇下一篇

猜你喜欢

热点阅读