From 0a92a53f634d50ca4e277344a4c6e79f11180826 Mon Sep 17 00:00:00 2001 From: CalvinKirs Date: Sun, 31 Jan 2021 22:27:51 +0800 Subject: [PATCH] add jmh test --- dolphinscheduler-microbench/pom.xml | 4 + .../microbench/common/IUserService.java | 31 ++++++++ .../microbench/common/RpcTest.java | 77 +++++++++++++++++++ .../microbench/common/UserCallback.java | 30 ++++++++ .../microbench/common/UserService.java | 37 +++++++++ .../remote/codec/NettyDecoder.java | 36 ++++----- .../rpc/config/ServiceBean.java | 5 +- .../rpc/serializer/ProtoStuffSerializer.java | 5 +- .../apache/dolphinscheduler/rpc/RpcTest.java | 1 - 9 files changed, 203 insertions(+), 23 deletions(-) create mode 100644 dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/IUserService.java create mode 100644 dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/RpcTest.java create mode 100644 dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/UserCallback.java create mode 100644 dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/UserService.java diff --git a/dolphinscheduler-microbench/pom.xml b/dolphinscheduler-microbench/pom.xml index 606ecd3c38..c0c095abe7 100644 --- a/dolphinscheduler-microbench/pom.xml +++ b/dolphinscheduler-microbench/pom.xml @@ -61,6 +61,10 @@ org.slf4j slf4j-api + + org.apache.dolphinscheduler + dolphinscheduler-remote + diff --git a/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/IUserService.java b/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/IUserService.java new file mode 100644 index 0000000000..3a77aa8a0d --- /dev/null +++ b/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/IUserService.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.microbench.common; + +import org.apache.dolphinscheduler.rpc.base.Rpc; + +/** + * IUserService + */ +public interface IUserService { + + @Rpc(async = true, serviceCallback = UserCallback.class, retries = 9999) + Boolean say(String s); + + Integer hi(int num); +} diff --git a/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/RpcTest.java b/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/RpcTest.java new file mode 100644 index 0000000000..ecc54f8f26 --- /dev/null +++ b/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/RpcTest.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.microbench.common; + +import org.apache.dolphinscheduler.microbench.base.AbstractBaseBenchmark; +import org.apache.dolphinscheduler.remote.config.NettyServerConfig; +import org.apache.dolphinscheduler.remote.utils.Host; +import org.apache.dolphinscheduler.rpc.client.IRpcClient; +import org.apache.dolphinscheduler.rpc.client.RpcClient; +import org.apache.dolphinscheduler.rpc.remote.NettyClient; +import org.apache.dolphinscheduler.rpc.remote.NettyServer; + +import java.util.concurrent.TimeUnit; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +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.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; + +@Warmup(iterations = 5, time = 1) +@Measurement(iterations = 10, time = 1) +@State(Scope.Benchmark) +@BenchmarkMode({Mode.Throughput, Mode.AverageTime, Mode.SampleTime}) +public class RpcTest extends AbstractBaseBenchmark { + private NettyServer nettyServer; + + private IUserService userService; + + private Host host; + private IRpcClient rpcClient = new RpcClient(); + + @Setup + public void before() throws Exception { + nettyServer = new NettyServer(new NettyServerConfig()); + IRpcClient rpcClient = new RpcClient(); + host = new Host("127.0.0.1", 12346); + userService = rpcClient.create(IUserService.class, host); + + } + + @Benchmark + @BenchmarkMode({Mode.Throughput, Mode.AverageTime, Mode.SampleTime}) + @OutputTimeUnit(TimeUnit.MILLISECONDS) + public void sendTest() throws Exception { + + userService = rpcClient.create(IUserService.class, host); + Integer result = userService.hi(1); + } + + @TearDown + public void after() { + NettyClient.getInstance().close(); + nettyServer.close(); + } + +} diff --git a/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/UserCallback.java b/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/UserCallback.java new file mode 100644 index 0000000000..bb32093f91 --- /dev/null +++ b/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/UserCallback.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.microbench.common; + +import org.apache.dolphinscheduler.rpc.common.AbstractRpcCallBack; + +/** + * UserCallback + */ +public class UserCallback extends AbstractRpcCallBack { + @Override + public void run(Object object) { + + } +} diff --git a/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/UserService.java b/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/UserService.java new file mode 100644 index 0000000000..ad09a34645 --- /dev/null +++ b/dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/UserService.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.microbench.common; + +import org.apache.dolphinscheduler.rpc.base.RpcService; + +/** + * UserService + */ +@RpcService("IUserService") +public class UserService implements IUserService { + + @Override + public Boolean say(String s) { + return true; + } + + @Override + public Integer hi(int num) { + return ++num; + } +} diff --git a/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/codec/NettyDecoder.java b/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/codec/NettyDecoder.java index 343e8c63dd..84b5c1f90b 100644 --- a/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/codec/NettyDecoder.java +++ b/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/codec/NettyDecoder.java @@ -17,26 +17,27 @@ package org.apache.dolphinscheduler.remote.codec; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.ReplayingDecoder; import org.apache.dolphinscheduler.remote.command.Command; import org.apache.dolphinscheduler.remote.command.CommandContext; import org.apache.dolphinscheduler.remote.command.CommandHeader; import org.apache.dolphinscheduler.remote.command.CommandType; + +import java.util.List; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ReplayingDecoder; /** - * netty decoder + * netty decoder */ public class NettyDecoder extends ReplayingDecoder { private static final Logger logger = LoggerFactory.getLogger(NettyDecoder.class); - public NettyDecoder(){ + public NettyDecoder() { super(State.MAGIC); } @@ -48,11 +49,10 @@ public class NettyDecoder extends ReplayingDecoder { * @param ctx channel handler context * @param in byte buffer * @param out out content - * @throws Exception */ @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - switch (state()){ + switch (state()) { case MAGIC: checkMagic(in.readByte()); checkpoint(State.VERSION); @@ -102,13 +102,13 @@ public class NettyDecoder extends ReplayingDecoder { } /** - * get command type + * get command type + * * @param type type - * @return */ - private CommandType commandType(byte type){ - for(CommandType ct : CommandType.values()){ - if(ct.ordinal() == type){ + private CommandType commandType(byte type) { + for (CommandType ct : CommandType.values()) { + if (ct.ordinal() == type) { return ct; } } @@ -116,7 +116,8 @@ public class NettyDecoder extends ReplayingDecoder { } /** - * check magic + * check magic + * * @param magic magic */ private void checkMagic(byte magic) { @@ -126,8 +127,7 @@ public class NettyDecoder extends ReplayingDecoder { } /** - * check version - * @param version + * check version */ private void checkVersion(byte version) { if (version != Command.VERSION) { @@ -135,7 +135,7 @@ public class NettyDecoder extends ReplayingDecoder { } } - enum State{ + enum State { MAGIC, VERSION, COMMAND, diff --git a/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/config/ServiceBean.java b/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/config/ServiceBean.java index 6369d16725..507cacb1d9 100644 --- a/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/config/ServiceBean.java +++ b/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/config/ServiceBean.java @@ -46,7 +46,10 @@ public class ServiceBean { private static synchronized void init() { // todo config - Reflections f = new Reflections("org/apache/dolphinscheduler/rpc"); + if(initialized.get()){ + return; + } + Reflections f = new Reflections("org/apache/dolphinscheduler/"); List> list = new ArrayList<>(f.getTypesAnnotatedWith(RpcService.class)); list.forEach(rpcClass -> { RpcService rpcService = rpcClass.getAnnotation(RpcService.class); diff --git a/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/serializer/ProtoStuffSerializer.java b/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/serializer/ProtoStuffSerializer.java index 3cb3e0a776..a608b08ac3 100644 --- a/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/serializer/ProtoStuffSerializer.java +++ b/dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/serializer/ProtoStuffSerializer.java @@ -24,20 +24,19 @@ import io.protostuff.ProtostuffIOUtil; import io.protostuff.Schema; import io.protostuff.runtime.RuntimeSchema; -public class ProtoStuffSerializer implements Serializer{ +public class ProtoStuffSerializer implements Serializer { private static LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); private static Map, Schema> schemaCache = new ConcurrentHashMap<>(); - @SuppressWarnings("unchecked") private static Schema getSchema(Class clazz) { return (Schema) schemaCache.computeIfAbsent(clazz, RuntimeSchema::createFrom); } @Override - public byte[] serialize(T obj) throws IOException { + public byte[] serialize(T obj) throws IOException { Class clazz = (Class) obj.getClass(); Schema schema = getSchema(clazz); byte[] data; diff --git a/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/rpc/RpcTest.java b/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/rpc/RpcTest.java index 5a10de8d73..bf62e467e9 100644 --- a/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/rpc/RpcTest.java +++ b/dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/rpc/RpcTest.java @@ -42,7 +42,6 @@ public class RpcTest { IRpcClient rpcClient = new RpcClient(); host = new Host("127.0.0.1", 12346); userService = rpcClient.create(IUserService.class, host); - } @Test