您的位置:首页 > 运维架构 > Apache

使用Apache JMeter压測Thrift

2017-08-11 12:06 435 查看
我这里以我的一篇帖子为样例

http://blog.csdn.net/mn960mn/article/details/50476759

这里已经有服务端了,先启动服务端

首先增加maven的依赖

<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>2.13</version>
</dependency>
之后。eclipse提示

The following artifacts could not be resolved: commons-math3:commons-math3:jar:3.4.1, commons-pool2:commons-pool2:jar:2.3: Could not find artifact commons-math3:commons-math3:jar:3.4.1

说是找不到依赖。我去中央仓库找,的确没有。暂时的解决方法是:

先下载

org.apache.commons:commons-math3:3.4.1

org.apache.commons:commons-pool2:2.3

这两个jar到本地。然后,运行以下的命令。安装到本地maven仓库

mvn install:install-file -Dfile=d:/tmp/commons-math3-3.4.1.jar -DgroupId=commons-math3 -DartifactId=commons-math3 -Dversion=3.4.1 -Dpackaging=jar

mvn install:install-file -Dfile=d:/tmp/commons-pool2-2.3.jar -DgroupId=commons-pool2 -DartifactId=commons-pool2 -Dversion=2.3 -Dpackaging=jar

注意,这里要下载上面提示缺失的版本号

安装完之后。再编译。就没有错误提示了

二:写一个类,继承Jmeter的类,在实现方法里面运行thrift的逻辑

package com.pp.client;

import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

import com.pp.calc.Calculator;

/**
* 在用jmeter压測的时候,一个线程会有一个当前类实例
*/
public class ThriftClientTest extends AbstractJavaSamplerClient
{
private Calculator.Client calc;
private TTransport transport;

/**
* init
*/
public void setupTest(JavaSamplerContext context)
{
super.setupTest(context);

transport = new TSocket("127.0.0.1", 9988);
try
{
transport.open();
} catch (TTransportException e)
{
transport.close();
throw new RuntimeException(e);
}

TProtocol protocol = new TCompactProtocol(transport);

calc = new Calculator.Client(protocol);
}

/**
* close
* 关闭相关的资源
*/
public void teardownTest(JavaSamplerContext context)
{
super.teardownTest(context);
transport.close();
}

/**
* 这里就是详细调用的逻辑
*/
public SampleResult runTest(JavaSamplerContext context)
{
SampleResult result = new SampleResult();
result.sampleStart();
try
{
System.out.println(calc.add(4, 5));
result.setSuccessful(true);
} catch (TException e)
{
result.setSuccessful(false);
}

result.sampleEnd();
return result;
}
}


写完之后,运行mvn clean package打包

然后,把打包的jar和libthrift-0.9.3.jar放到 apache-jmeter-2.13\lib\ext 文件夹中去(假设还有其它的依赖,也须要把依赖拷贝过来)

三:启动jmeter

在新建的线程组里面新建Sampler -> Java请求

在出现的窗体中,类名称 右边的下拉框里面选择com.pp.client.ThriftClientTest



最后,配置对应的线程数,循环次数,就能够进行压力測试了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: