环境:mac os
一 Jmeter安装
下载Jmeter最新版本,地址 http://jmeter.apache.org/download_jmeter.cgi
解压之后就可以直接用了。
启动Jmeter:
wangwenqians-MacBook-Pro:bin wangwenqian$ pwd
/Users/wangwenqian/Tools/apache-jmeter-2.13/bin
wangwenqians-MacBook-Pro:bin wangwenqian$ ./jmeter.sh
二 Thrift安装
下载thrift-0.9.3, libevent-2.0.22-stable, boost_1_59_0, bison-3.0.4.
安装boost_1_59_0.
$tar xzvf boost_1_59_0.tar.gz
$cd boost_1_59_0
$./bootstrap.sh -prefix=PATH_TO_BOOSTsudo $./b2 threading=multi address-model=64 variant=release stage install
安装libevent-2.0.22-stable
$tar xzvf libevent-2.0.22-stable.tar.gz
$./configure --prefix=/usr/local/ --disable-static
$make
$sudo make install
安装thrift-0.9.3
$cd thrift-0.9.3
$./configure --prefix=/usr/local/ --disable-static --with-boost=/usr/local/ --with-libevent=/usr/local/
$make
$sudo make install
$tar -xvf bison-3.0.4.tar
$./configure --prefix=/usr/
$make
$sudo make install
三 Thrift实例——java
新建一个thrift文件,文件名为add.thrift。
namespace java com.test.samples.thrift.server
typedef i32 int
service AdditionService{
int add(1:int n1, 2:int n2),
}
执行完成后,生成gen-java目录。新建一个java工程,将生成的java文件全部copy到java工程里面。
下面在工程中实现server端代码。
package com.test.samples.thrift.server;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.server.TSimpleServer;
public class MyServer {
public static void StartsimpleServer(AdditionService.Processor processor) {
try {
TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TSimpleServer(
new Args(serverTransport).processor(processor));
System.out.println("Starting the simple server...");
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StartsimpleServer(new AdditionService.Processor(new AdditionServiceHandler()));
}
}
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting the simple server...
新建java工程,add external libraries:
JMeter中的jar包 avalon-framework-4.1.4.jar, jorphan.jar, logkit-2.0.jar, ApachekJMeter_core.jar, ApacjeJMeter_java.jar
Thrift中的jar包 libthrift-0.9.3.jar, slf4j-api-1.7.12.jar
另外,把thrift的两个jar包放到apache-jmeter-2.13/lib/ext下。
将生成的AdditionService.java拷贝到java工程中。新建AdditionClient.java.
package jmeter.sampler;
import org.apache.thrift.TException;
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 org.apache.thrift.transport.TTransportException;
public class AdditionClient {
public static void send() {
System.out.println("enter...");
try {
TTransport transport;
transport = new TSocket("localhost", 9090);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
AdditionService.Client client = new AdditionService.Client(protocol);
System.out.println(client.add(100, 200));
transport.close();
System.out.println("exit...");
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException x) {
x.printStackTrace();
}
}
}
package jmeter.sampler;
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 MyJavaSampler extends AbstractJavaSamplerClient{
@Override
public SampleResult runTest(JavaSamplerContext arg0) {
// TODO Auto-generated method stub
SampleResult result = new SampleResult();
result.sampleStart();
System.out.println("enter....");
System.out.println("[runTest]");
AdditionClient.send();
result.setSuccessful(true);
result.sampleEnd();
return null;
}
@Override
public Arguments getDefaultParameters() {
// TODO Auto-generated method stub
System.out.println("Get Parameter name![getDefaultParameters]");
return super.getDefaultParameters();
}
@Override
public void setupTest(JavaSamplerContext context) {
// TODO Auto-generated method stub
System.out.println("[setupTest]");
super.setupTest(context);
}
@Override
public void teardownTest(JavaSamplerContext context) {
// TODO Auto-generated method stub
System.out.println("[teardownTest]");
super.teardownTest(context);
}
}
Export出jar包,放到apache-jmeter-2.13/lib/ext。 重新启动Jmeter。新建Thread Group, 在Thread Group下,Thread Group -> Add -> Sampler -> Java Request,在Classname下选在新做的plugin的类名。
大功告成了,保证server处于启动状态,点击Jmeter上面的run,可以在控制台看到输出结果。