构建 thrift 简单项目, 使用 jmeter 去压测
项目代码 https://github.com/xtj-yiluo/thriftJmeterTest
1、创建一个 thrift idl文件
namespace java com.syscom.thrift service lprocessData{ string processData(1:string para); }
2、生成接口文件
thrift -r --gen java processData.thrift
3、将接口文件拷贝 项目工程
4、 撰写服务端
先实现 接口方法
package com.syscom.server; import com.syscom.thrift.lprocessData; import org.apache.thrift.TException; /** * @author xtj * @date 2021/2/23 14:14 */ public class lprocessDataLmp implements lprocessData.Iface{ // 实现接口 ,自定义逻辑 @Override public String processData(String para) throws TException { System.out.println(para); return "success"; } }
5、 撰写服务端方法
package com.syscom.server; import com.syscom.thrift.lprocessData; import org.apache.thrift.TProcessor; import org.apache.thrift.TProcessorFactory; import org.apache.thrift.protocol.TCompactProtocol; import org.apache.thrift.server.THsHaServer; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TNonblockingServerSocket; /** * @author xtj * @date 2021/2/23 14:19 */ public class lprocessDataServer { public static int SERVER_PORT = 8899 ; public static String SERVER_IP = "localhost" ; public void start(){ try { //定义服务传输服务方式,及接受前端请求的服务方式,顺带把端口定义好 //多线程服务器端使用非阻塞式 I/O 使用方法 TNonblockingServerSocket socket = new TNonblockingServerSocket(SERVER_PORT) ; //定义服务 THsHaServer.Args args = new THsHaServer.Args(socket) ; //处理进程->定义处理进程,将关联你自己写的your core code TProcessor process = new lprocessData.Processor<lprocessDataLmp>(new lprocessDataLmp()) ; //ILogin.Processor process = new ILogin.Processor(new LoginImp()) ; args.protocolFactory(new TCompactProtocol.Factory()) ; //压缩传输协议 args.transportFactory(new TFramedTransport.Factory()) ; //文件块的传输方式 args.processorFactory(new TProcessorFactory(process)) ; //定义传输服务->传输方法->传输进程—>指向你实现的具体的方法 THsHaServer server = new THsHaServer(args) ; // 一定要与TFramedTransport 同时使用 server.serve() ; } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args){ lprocessDataServer server = new lprocessDataServer() ; System.out.println("----------服务端程序-start-ok-------------") ; server.start() ; } }
启动服务端
6、撰写客户端程序
package com.syscom.client; import com.syscom.thrift.lprocessData; import org.apache.thrift.protocol.TCompactProtocol; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TSocket; /** * @author xtj * @date 2021/2/23 14:25 */ public class sendDataClient { private static int SERVER_PORT = 8899; private static String SERVER_IP = "localhost" ; /** * * 发送数据方法, 发送到服务器,调用服务器方法 * * @param para */ public String sendData(String para){ String rtn = "" ; TFramedTransport transport = null ; try { //定义传输入方式,要与后台保持一持,也即是这儿定义 传到什么 ip 哪个端口 transport = new TFramedTransport(new TSocket(SERVER_IP,SERVER_PORT),1000) ; //定义传输入协议,压缩方式 TCompactProtocol protocol = new TCompactProtocol(transport) ; //调用后台的方法 Client 方法了,将协议和传输方式,传入到 后台的 Client 方法 注册 //注册后,那么前台就把后台实现的类已经实例化,拿到前台了 //把后台的方法拿过来 lprocessData.Client client = new lprocessData.Client(protocol) ; //开始传输,就是socket 传输,打开流 transport.open(); //现在就可以直接调用后台你的方法了,像调用本地的方法一样 rtn = client.processData(para) ; } catch (Exception e) { e.printStackTrace(); rtn = "" ; } finally { transport.clear() ; transport.close() ; } return rtn ; } public static void main(String[] args){ sendDataClient dataClient = new sendDataClient() ; String data = "data" ; for (int i=1;i<10;i++){ System.out.println("*************开始发送*************"); System.out.println("----------连接后台----------"); System.out.println(); System.out.println("----------等待后台响应----------"); System.out.println(); String rtn = dataClient.sendData(data); System.out.println("---------后台返回结果--------"); System.out.println(); System.out.println(data+i+rtn); System.out.println("*************发送结束*************"); try { System.out.println(); Thread.sleep(1000); System.out.println(); } catch (InterruptedException e) { System.out.println("*************发送异常*************"); e.printStackTrace(); } } } }
启动客户端测试 服务端联通性
此时已经联通
7、撰写jmeter 发送程序
package com.syscom.jmter; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import java.util.Random; import com.syscom.client.sendDataClient; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; public class JmeterSendThrift extends AbstractJavaSamplerClient { private SampleResult results; private int count1 = 0; private int count2 = 0; // private int timestep = 0 ; @Override public void setupTest(JavaSamplerContext jsc) { results = new SampleResult(); } @Override public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument("msg", ""); params.addArgument("NOWDATE", ""); return params; } @Override public SampleResult runTest(JavaSamplerContext arg0) { String msg = arg0.getParameter("msg"); String nowdate = arg0.getParameter("NOWDATE"); try { results.sampleStart(); Random r = new Random(); byte[] byteArrMsgGBK = msg.getBytes("GBK"); StringBuffer msgs = new StringBuffer(); String dateString = ""; if (nowdate.equals("Y") || nowdate.equals("y")) { dateString = new SimpleDateFormat("yyyyMMddHHmmssSSS") .format(new Date(System.currentTimeMillis())).substring(0, 16); } for(int a = 0; a<byteArrMsgGBK.length;a++){ msgs.append((char)byteArrMsgGBK[a]); } String str = msgs.toString(); sendDataClient dataClient = new sendDataClient() ; String rtn = dataClient.sendData(str); System.out.println(rtn); results.setSuccessful(true); } catch (Exception e) { // TODO: handle exception results.setSuccessful(false); e.printStackTrace(); } finally { results.sampleEnd(); } return results; } @Override public void teardownTest(JavaSamplerContext arg0) { System.out.println(count1); System.out.println(count2); //producer.close(); } public static void main(String[] args) { // TODO Auto-generated method stub // Arguments param=new Arguments(); // // param.addArgument("msg","F:\\tmp\\test.csv"); // param.addArgument("NOWDATE","Y"); // // JavaSamplerContext arg0=new JavaSamplerContext(param); // // JmeterSendThrift test=new JmeterSendThrift(); // // test.setupTest(arg0); // test.runTest(arg0); // test.teardownTest(arg0); } }
打包程序 jmeter 程序
启动jmeter 面板测试 , 随便读取 任意一本地文件
测试成功