Skip to content

Commit

Permalink
add jmh test for javassist vs reflect
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanmie committed Oct 14, 2018
1 parent 29ef6c4 commit 0a5eb91
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 9 deletions.
6 changes: 6 additions & 0 deletions core/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package com.alipay.sofa.rpc.filter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.alibaba.dubbo.common.bytecode.Wrapper;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.context.RpcInternalContext;
Expand All @@ -25,9 +29,6 @@
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* 服务端调用业务实现类
*
Expand All @@ -50,6 +51,8 @@ public ProviderInvoker(ProviderConfig<T> providerConfig) {
this.providerConfig = providerConfig;
}

Class<?>[] types = null;
Wrapper wrapper = null;
@Override
public SofaResponse invoke(SofaRequest request) throws SofaRpcException {

Expand All @@ -71,10 +74,27 @@ public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
try {
// 反射 真正调用业务代码
Method method = request.getMethod();
T target = providerConfig.getRef();
String methodName = method.getName();
Object[] arguments = request.getMethodArgs();

if (method == null) {
throw new SofaRpcException(RpcErrorType.SERVER_FILTER, "Need decode method first!");
}
Object result = method.invoke(providerConfig.getRef(), request.getMethodArgs());
Object result = null;
// just for test
if (!"javassist".equals(methodName)) {
result = method.invoke(target, arguments);
} else {
if (types == null) {
types = method.getParameterTypes();
}
if (wrapper == null) {
wrapper = Wrapper
.getWrapper(target.getClass());
}
result = wrapper.invokeMethod(target, methodName, types, arguments);
}

sofaResponse.setAppResponse(result);
} catch (IllegalArgumentException e) { // 非法参数,可能是实现类和接口类不对应)
Expand Down
14 changes: 14 additions & 0 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<!--JMH-->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.21</version>
</dependency>

<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.21</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void main(String[] args) throws InterruptedException {
ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("future-server");

MethodConfig methodConfig = new MethodConfig();
methodConfig.setName("sayHello")
methodConfig.setName("jdkTest8")
.setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK)
.setOnReturn(new SofaResponseCallback() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,11 @@
* Quick Start demo interface
*/
public interface HelloService {
String sayHello(String string);

/** 为了保持方法名长度相同 */
String jdkTest99(String string);
}
interface HelloService2 {

String javassist(String string);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@
*/
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String string) {
System.out.println("Server receive: " + string);
public String jdkTest99(String string) {
return "hello " + string + " !";
}

}

class HelloServiceImpl2 implements HelloService2 {
@Override
public String javassist(String string) {
return "hello " + string + " !";
}
}
69 changes: 69 additions & 0 deletions example/src/test/java/com/alipay/sofa/rpc/quickstart/JmhTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.alipay.sofa.rpc.quickstart;

import java.util.concurrent.TimeUnit;

import com.alipay.sofa.rpc.config.ConsumerConfig;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

/**
*
* @author stateis0
* @date 2018/10/14-下午4:26
*/
@State(Scope.Thread)
@BenchmarkMode({Mode.Throughput, Mode.AverageTime, Mode.SampleTime})
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 2, time = 3, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
@Fork(0)
public class JmhTest {

static ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>()
.setInterfaceId(HelloService.class.getName()) // 指定接口
.setProtocol("bolt") // 指定协议
.setDirectUrl("bolt://127.0.0.1:9696") // 指定直连地址
.setConnectTimeout(10 * 1000);

static HelloService helloService = consumerConfig.refer();

static ConsumerConfig<HelloService2> consumerConfig2 = new ConsumerConfig<HelloService2>()
.setInterfaceId(HelloService2.class.getName()) // 指定接口
.setProtocol("bolt") // 指定协议
.setDirectUrl("bolt://127.0.0.1:9697") // 指定直连地址
.setConnectTimeout(10 * 1000);

static HelloService2 helloService2 = consumerConfig2.refer();



@Benchmark
public void jdk() {
helloService.jdkTest99("jdkTest99");
}

@Benchmark
public void javassist() {
helloService2.javassist("javassist");
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder().include(JmhTest.class.getSimpleName()).build();

new Runner(opt).run();
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.alipay.sofa.rpc.quickstart;

import com.alipay.sofa.rpc.config.ConsumerConfig;

import org.openjdk.jmh.runner.RunnerException;

/**
*
*
* @author 玄灭
* @date 2018/10/14-下午6:42
*/
public class NormalTest {

static ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>()
.setInterfaceId(HelloService.class.getName()) // 指定接口
.setProtocol("bolt") // 指定协议
.setDirectUrl("bolt://127.0.0.1:9696") // 指定直连地址
.setConnectTimeout(10 * 1000);

static HelloService helloService = consumerConfig.refer();

static ConsumerConfig<HelloService2> consumerConfig2 = new ConsumerConfig<HelloService2>()
.setInterfaceId(HelloService2.class.getName()) // 指定接口
.setProtocol("bolt") // 指定协议
.setDirectUrl("bolt://127.0.0.1:9697") // 指定直连地址
.setConnectTimeout(10 * 1000);

static HelloService2 helloService2 = consumerConfig2.refer();

public static void main(String[] args) throws RunnerException {

warp();

long s = System.currentTimeMillis();

for (int i = 0; i < 100000; i++) {
helloService.jdkTest99("jdkTest99");//15321
}
long e = System.currentTimeMillis();
System.out.println(e - s);



s = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
helloService.jdkTest99("jdkTest99");//15321
}
e = System.currentTimeMillis();
System.out.println(e - s);
}

private static void warp() {
for (int i = 0; i < 100000; i++) {
helloService.jdkTest99("jdkTest99");//15321
helloService2.javassist("javassist");//14172
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void main(String[] args) {
HelloService helloService = consumerConfig.refer();

while (true) {
System.out.println(helloService.sayHello("world"));
System.out.println(helloService.jdkTest99("world"));
try {
Thread.sleep(2000);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,26 @@ public static void main(String[] args) {
.setServer(serverConfig); // 指定服务端

providerConfig.export(); // 发布服务



}
}

class Server2{

public static void main(String[] args) {
ServerConfig serverConfig = new ServerConfig()
.setProtocol("bolt") // 设置一个协议,默认bolt
.setPort(9697) // 设置一个端口,默认12200
.setDaemon(false); // 非守护线程

ProviderConfig<HelloService2> providerConfig = new ProviderConfig<HelloService2>()
.setInterfaceId(HelloService2.class.getName()) // 指定接口
.setRef(new HelloServiceImpl2()) // 指定实现
.setServer(serverConfig); // 指定服务端

providerConfig.export(); // 发布服务

}
}

0 comments on commit 0a5eb91

Please sign in to comment.