Skip to content

Commit

Permalink
Refactor:Javassist动态代理重构为可以提升效率的实现,bug已修复,但性能很差,正在排查问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
songxinjianqwe committed Aug 25, 2018
1 parent 02b96df commit 8921a4f
Show file tree
Hide file tree
Showing 9 changed files with 1,221 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void run(int threads, int requestsTotal, int measurementIterations) {
this.userService = getUserService();
this.executorService = Executors.newFixedThreadPool(threads);
this.measurementIterations = measurementIterations;
// createUser();
// existUser();
// getUser();
createUser();
existUser();
getUser();
listUser();
}

Expand Down Expand Up @@ -164,6 +164,7 @@ private void createUser() {
for (int j = 0; j < requestsPerThread; j++) {
long begin = System.nanoTime();
try {
log.info("request:{},createUser",j);
userService.createUser(user);
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -224,6 +225,7 @@ private void existUser() {
for (int j = 0; j < requestsPerThread; j++) {
long begin = System.nanoTime();
try {
log.info("request:{},existUser",j);
userService.existUser(j + "@gmail.com");
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -281,6 +283,7 @@ private void getUser() {
for (int j = 0; j < requestsPerThread; j++) {
long begin = System.nanoTime();
try {
log.info("request:{},getUser",j);
userService.getUser(j);
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -339,6 +342,7 @@ private void listUser() {
for (int j = 0; j < requestsPerThread; j++) {
long begin = System.nanoTime();
try {
log.info("request:{},listUser",j);
userService.listUser(j);
} catch (Exception e) {
e.printStackTrace();
Expand Down
306 changes: 306 additions & 0 deletions hs_err_pid10420.log

Large diffs are not rendered by default.

306 changes: 306 additions & 0 deletions hs_err_pid11120.log

Large diffs are not rendered by default.

307 changes: 307 additions & 0 deletions hs_err_pid14180.log

Large diffs are not rendered by default.

262 changes: 262 additions & 0 deletions hs_err_pid7536.log

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ public class RPCRequest implements Serializable {
private String methodName;
private String[] parameterTypes;
private Object[] parameters;
private transient Class[] parameterTypeClasses;


public void setParameterTypes(Class[] parameterTypes) {
this.parameterTypeClasses = parameterTypes;
String[] paramTypes = new String[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
paramTypes[i] = parameterTypes[i].getName();
Expand All @@ -42,19 +40,17 @@ public void setParameterTypes(String[] parameterTypes) {
}

public Class[] getParameterTypes() {
if (parameterTypeClasses == null) {
parameterTypeClasses = new Class[parameterTypes.length];
Class[] parameterTypeClasses = new Class[parameterTypes.length];
try {
for (int i = 0; i < parameterTypes.length; i++) {
if (TypeUtil.isPrimitive(parameterTypes[i])) {
parameterTypeClasses[i] = TypeUtil.map(parameterTypes[i]);
}else {
try {
parameterTypeClasses[i] = Class.forName(parameterTypes[i]);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} else {
parameterTypeClasses[i] = Class.forName(parameterTypes[i]);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return parameterTypeClasses;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected <T> T doCreateProxy(Class<T> interfaceClass, Invoker<T> invoker) {
ClassPool pool = ClassPool.getDefault();
// 传入类名,最后生成某种Interface
// 我们保证某个interface只会生成一个代理类
CtClass proxyClass = pool.makeClass(interfaceName + "$proxyInvoker");
CtClass proxyClass = pool.makeClass(interfaceName + "$ProxyInvoker");
// 设置接口类型
proxyClass.setInterfaces(new CtClass[]{pool.getCtClass(interfaceName)});
CtField invokerField = new CtField(this.invokerCtClass, "invoker", proxyClass);
Expand All @@ -50,6 +50,8 @@ protected <T> T doCreateProxy(Class<T> interfaceClass, Invoker<T> invoker) {
addInterfaceMethod(interfaceName, proxyClass, method);
}
addCommonMethods(interfaceName, proxyClass);
String source = proxyClass.toString();
log.info("source:{}",source);
t = interfaceClass.cast(proxyClass.toClass().getConstructor(Invoker.class, AbstractRPCProxyFactory.class).newInstance(invoker, this));
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -107,7 +109,7 @@ private static String generateMethodCode(String interfaceName, Method method) {
//方法声明,由于是实现接口的方法,所以是public
StringBuilder methodDeclare = new StringBuilder();
methodDeclare.append("public ").append(methodReturnType).append(" ").append(methodName).append("(").append(parameterBuffer).append(")").append(exceptionBuffer).append(" {");
// methodDeclare.append("System.out.println(a0);");
// methodDeclare.append("System.out.println(a0);");
// methodDeclare.append("System.out.println(new Object[]{a0});");
// 方法体
methodDeclare.append("return interceptor.invokeProxyMethod(")
Expand All @@ -126,7 +128,7 @@ private static String generateMethodCode(String interfaceName, Method method) {
//传递方法里的参数
methodDeclare.append("new Object[]{");
for (int i = 0; i < parameterTypes.length; i++) {
methodDeclare.append("a").append(i);
methodDeclare.append("($w)a").append(i);
if (i != parameterTypes.length - 1) {
methodDeclare.append(",");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@
@Slf4j
public class ToyEncoder extends MessageToByteEncoder {
private Serializer serializer;

public ToyEncoder(Serializer serializer) {
this.serializer = serializer;
}

@Override
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
Message message = (Message) msg;
out.writeByte((message.getType()));
if (message.getType() == Message.REQUEST) {
out.writeBytes(serializer.serialize(message.getRequest()));
byte[] bytes = serializer.serialize(message.getRequest());
log.info("Message:{},序列化大小为:{}", message,bytes.length);
out.writeBytes(bytes);
message.getRequest().recycle();
} else if (message.getType() == Message.RESPONSE) {
out.writeBytes(serializer.serialize(message.getResponse()));
byte[] bytes = serializer.serialize(message.getResponse());
log.info("Message:{},序列化大小为:{}", message,bytes.length);
out.writeBytes(bytes);
message.getResponse().recycle();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sinjinsong.toy.common.util;

/**
* @author sinjinsong
* @date 2018/8/25
*/
public class Test {
@org.junit.Test
public void test() {
int a0 = 1;
System.out.println(new Object[]{a0});
}
}

0 comments on commit 8921a4f

Please sign in to comment.