Browse Source

add jmh test

pull/3/MERGE
CalvinKirs 4 years ago
parent
commit
0a92a53f63
  1. 4
      dolphinscheduler-microbench/pom.xml
  2. 31
      dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/IUserService.java
  3. 77
      dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/RpcTest.java
  4. 30
      dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/UserCallback.java
  5. 37
      dolphinscheduler-microbench/src/main/java/org/apache/dolphinscheduler/microbench/common/UserService.java
  6. 36
      dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/codec/NettyDecoder.java
  7. 5
      dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/config/ServiceBean.java
  8. 5
      dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/serializer/ProtoStuffSerializer.java
  9. 1
      dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/rpc/RpcTest.java

4
dolphinscheduler-microbench/pom.xml

@ -61,6 +61,10 @@
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.dolphinscheduler</groupId>
<artifactId>dolphinscheduler-remote</artifactId>
</dependency>
</dependencies> </dependencies>

31
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);
}

77
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();
}
}

30
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) {
}
}

37
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;
}
}

36
dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/remote/codec/NettyDecoder.java

@ -17,26 +17,27 @@
package org.apache.dolphinscheduler.remote.codec; 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.Command;
import org.apache.dolphinscheduler.remote.command.CommandContext; import org.apache.dolphinscheduler.remote.command.CommandContext;
import org.apache.dolphinscheduler.remote.command.CommandHeader; import org.apache.dolphinscheduler.remote.command.CommandHeader;
import org.apache.dolphinscheduler.remote.command.CommandType; import org.apache.dolphinscheduler.remote.command.CommandType;
import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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<NettyDecoder.State> { public class NettyDecoder extends ReplayingDecoder<NettyDecoder.State> {
private static final Logger logger = LoggerFactory.getLogger(NettyDecoder.class); private static final Logger logger = LoggerFactory.getLogger(NettyDecoder.class);
public NettyDecoder(){ public NettyDecoder() {
super(State.MAGIC); super(State.MAGIC);
} }
@ -48,11 +49,10 @@ public class NettyDecoder extends ReplayingDecoder<NettyDecoder.State> {
* @param ctx channel handler context * @param ctx channel handler context
* @param in byte buffer * @param in byte buffer
* @param out out content * @param out out content
* @throws Exception
*/ */
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
switch (state()){ switch (state()) {
case MAGIC: case MAGIC:
checkMagic(in.readByte()); checkMagic(in.readByte());
checkpoint(State.VERSION); checkpoint(State.VERSION);
@ -102,13 +102,13 @@ public class NettyDecoder extends ReplayingDecoder<NettyDecoder.State> {
} }
/** /**
* get command type * get command type
*
* @param type type * @param type type
* @return
*/ */
private CommandType commandType(byte type){ private CommandType commandType(byte type) {
for(CommandType ct : CommandType.values()){ for (CommandType ct : CommandType.values()) {
if(ct.ordinal() == type){ if (ct.ordinal() == type) {
return ct; return ct;
} }
} }
@ -116,7 +116,8 @@ public class NettyDecoder extends ReplayingDecoder<NettyDecoder.State> {
} }
/** /**
* check magic * check magic
*
* @param magic magic * @param magic magic
*/ */
private void checkMagic(byte magic) { private void checkMagic(byte magic) {
@ -126,8 +127,7 @@ public class NettyDecoder extends ReplayingDecoder<NettyDecoder.State> {
} }
/** /**
* check version * check version
* @param version
*/ */
private void checkVersion(byte version) { private void checkVersion(byte version) {
if (version != Command.VERSION) { if (version != Command.VERSION) {
@ -135,7 +135,7 @@ public class NettyDecoder extends ReplayingDecoder<NettyDecoder.State> {
} }
} }
enum State{ enum State {
MAGIC, MAGIC,
VERSION, VERSION,
COMMAND, COMMAND,

5
dolphinscheduler-remote/src/main/java/org/apache/dolphinscheduler/rpc/config/ServiceBean.java

@ -46,7 +46,10 @@ public class ServiceBean {
private static synchronized void init() { private static synchronized void init() {
// todo config // todo config
Reflections f = new Reflections("org/apache/dolphinscheduler/rpc"); if(initialized.get()){
return;
}
Reflections f = new Reflections("org/apache/dolphinscheduler/");
List<Class<?>> list = new ArrayList<>(f.getTypesAnnotatedWith(RpcService.class)); List<Class<?>> list = new ArrayList<>(f.getTypesAnnotatedWith(RpcService.class));
list.forEach(rpcClass -> { list.forEach(rpcClass -> {
RpcService rpcService = rpcClass.getAnnotation(RpcService.class); RpcService rpcService = rpcClass.getAnnotation(RpcService.class);

5
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.Schema;
import io.protostuff.runtime.RuntimeSchema; 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 LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
private static Map<Class<?>, Schema<?>> schemaCache = new ConcurrentHashMap<>(); private static Map<Class<?>, Schema<?>> schemaCache = new ConcurrentHashMap<>();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static <T> Schema<T> getSchema(Class<T> clazz) { private static <T> Schema<T> getSchema(Class<T> clazz) {
return (Schema<T>) schemaCache.computeIfAbsent(clazz, RuntimeSchema::createFrom); return (Schema<T>) schemaCache.computeIfAbsent(clazz, RuntimeSchema::createFrom);
} }
@Override @Override
public <T> byte[] serialize(T obj) throws IOException { public <T> byte[] serialize(T obj) throws IOException {
Class<T> clazz = (Class<T>) obj.getClass(); Class<T> clazz = (Class<T>) obj.getClass();
Schema<T> schema = getSchema(clazz); Schema<T> schema = getSchema(clazz);
byte[] data; byte[] data;

1
dolphinscheduler-remote/src/test/java/org/apache/dolphinscheduler/rpc/RpcTest.java

@ -42,7 +42,6 @@ public class RpcTest {
IRpcClient rpcClient = new RpcClient(); IRpcClient rpcClient = new RpcClient();
host = new Host("127.0.0.1", 12346); host = new Host("127.0.0.1", 12346);
userService = rpcClient.create(IUserService.class, host); userService = rpcClient.create(IUserService.class, host);
} }
@Test @Test

Loading…
Cancel
Save