lesson3:使用java代码的方式对不能识别的协议进行压力测试
在我们的实际环境中,我们所使用的协议肯定不只是http的方式,对于rpc等调用协议,目前jmeter没有相应的sampler支持,这时就需要通过引入我们自己写的jar包的方式来解决这个问题。例如:当我们的服务方是采用netty+pb、thrift、dubbo等rpc方式时,本文采用thrift的方式来展示。
下载thrift的编译器:https://thrift.apache.org/download
thrfit服务代码:https://github.com/mantuliu/thriftServerDemo
jmeter的sdk代码:https://github.com/mantuliu/jMetterLessons
1.首先,我们先来实现一个非常简单的thrift服务,maven的pom文件如下:需要依赖thrift等开发包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mantu</groupId> <artifactId>thriftServerDemo</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>thriftServerDemo</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.9.0</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> </dependencies> </project>
2.实现thrift的接口定义文件,服务有一个输入参数,完成一个sayHello的动作:
namespace java com.mantu
service  HelloWorldService {
  string sayHello(1:string username)
}
3.使用thrift的编译器编译接口文件后,得到java文件HelloWorldService
4.服务端实现HelloWorldService的实现类,代码如下:
package com.mantu; import org.apache.thrift.TException; /** * blog http://www.cnblogs.com/mantu/ * * @author mantu * */ public class HelloWorldImpl implements HelloWorldService.Iface { public HelloWorldImpl() { } @Override public String sayHello(String username) throws TException { return "Hello," + username + " welcome to http://www.cnblogs.com/mantu/ "; } }
5.在HelloServerDemo中启动thrift服务:
package com.mantu; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; /** * blog http://www.cnblogs.com/mantu/ * * @author mantu * */ public class HelloServerDemo { public static final int SERVER_PORT = 8090; public void startServer() { try { System.out.println("thrift server start ...."); TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>( new HelloWorldImpl()); TServerSocket serverTransport = new TServerSocket(SERVER_PORT); TServer.Args tArgs = new TServer.Args(serverTransport); tArgs.processor(tprocessor); tArgs.protocolFactory(new TBinaryProtocol.Factory()); TServer server = new TSimpleServer(tArgs); server.serve(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { HelloServerDemo server = new HelloServerDemo(); server.startServer(); } }
6.启动thrift服务,监听端口为8090;
7.本文中的jmeter的sdk所使用的项目管理工具为maven,各位在使用的过程中,可能会发生相关的依赖包下载不到的情况,如出现此种情况,请在开发工具中直接引用jmeter相关的开发包,不要使用maven的方式;jMetterLessons的pom文件如下,引用的jmeter的相关包,及thrift的相关包:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mantu</groupId>
  <artifactId>jMeterLessons</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>jMeterLessons</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
    	<groupId>org.apache.jmeter</groupId>
    	<artifactId>ApacheJMeter_java</artifactId>
      	<version>3.0</version>
    </dependency>
     <dependency>
    	<groupId>org.apache.jmeter</groupId>
    	<artifactId>ApacheJMeter_core</artifactId>
      	<version>3.0</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.jmeter</groupId>
    	<artifactId>ApacheJMeter_components</artifactId>
      	<version>3.0</version>
    </dependency>
     <dependency>
		<groupId>org.apache.thrift</groupId>
		<artifactId>libthrift</artifactId>
		<version>0.9.0</version>
	</dependency>
  </dependencies>
</project>
8.sdk的主要代码实现参加类Lesson3,此类继承jmeter的AbstractJavaSamplerClient:
package com.mantu.jmeter;
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.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import com.mantu.HelloWorldService;
/**
 * blog http://www.cnblogs.com/mantu/
 *
 * @author mantu
 *
 */
public class Lesson3 extends AbstractJavaSamplerClient{
    
    public static void main(String [] args){
        
    }
    @Override
    public SampleResult runTest(JavaSamplerContext arg0) {
        // TODO Auto-generated method stub
        String userName = arg0.getParameter("uName");
        SampleResult sr = new SampleResult();
        sr.setSampleLabel("thrift娴嬭瘯");
        try{
            sr.sampleStart();
            HelloClientDemo helloClient = new HelloClientDemo();
            helloClient.startClient(userName);
            sr.setResponseData("success");
            sr.setDataType(SampleResult.TEXT);
            sr.setSuccessful(true);
        }
        catch(Exception ex){
            sr.setSuccessful(false);
            ex.printStackTrace();
        }
        finally{
            sr.sampleEnd();
        }
        return sr;
    }
    
    class HelloClientDemo {
        public static final String SERVER_IP = "localhost";
        public static final int SERVER_PORT = 8090;
        public static final int TIMEOUT = 30000;
        /**
         *
         * @param userName
         */
        public void startClient(String userName) {
            TTransport transport = null;
            try {
                transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
                TProtocol protocol = new TBinaryProtocol(transport);
                HelloWorldService.Client client = new HelloWorldService.Client(
                        protocol);
                transport.open();
                System.out.println(client.sayHello(userName));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != transport) {
                    transport.close();
                }
            }
        }
    }
}
9.在eclipse中导出jMetterLessons工程为jar包到jmeter的\lib\ext目录,并将libthrift-0.9.0.jar拷贝到\lib\ext目录
10.启动jmeter,并添加一个【java请求】的sampler,类名称选择我们刚刚开发的com.mantu.jmeter.Lesson3,并添加一个参数:uName,如图:
 
11.启动测试后,我们能发现测试结果为成功。
本文的协议采用了thrift协议,各位也可以使用其它的协议来模拟实现。
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号