├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── armeria-client ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ └── rpc │ └── Client.java ├── armeria-server ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ ├── rpc │ └── Server.java │ └── service │ └── ArmeriaUserServiceServerImpl.java ├── benchmark-base ├── .java-version ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ ├── bean │ ├── Page.java │ └── User.java │ ├── pool │ ├── ConcurrentObjectPool.java │ ├── LockObjectPool.java │ ├── UnsafeUtils.java │ ├── ViberObjectPool.java │ └── WaitStrategy.java │ ├── rpc │ ├── AbstractClient.java │ ├── protocol │ │ ├── Request.java │ │ └── Response.java │ ├── route │ │ └── RouteService.java │ └── util │ │ ├── ByteBufferUtils.java │ │ ├── HttpClientUtils.java │ │ └── JsonUtils.java │ └── service │ ├── ServiceRegister.java │ ├── UserService.java │ ├── UserServiceJsonHttpClientImpl.java │ └── UserServiceServerImpl.java ├── benchmark.java ├── brpc-client ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Client.java │ └── resources │ └── logback.xml ├── brpc-server ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Server.java │ └── resources │ └── logback.xml ├── dubbo-client ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Client.java │ └── resources │ ├── consumer.xml │ └── logback.xml ├── dubbo-kryo-client ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Client.java │ └── resources │ ├── consumer.xml │ └── logback.xml ├── dubbo-kryo-server ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Server.java │ └── resources │ ├── logback.xml │ └── provider.xml ├── dubbo-server ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Server.java │ └── resources │ ├── logback.xml │ └── provider.xml ├── grpc-client ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Client.java │ │ └── grpc │ │ ├── GrpcUserServiceClient.java │ │ └── UserServiceGrpcClientImpl.java │ ├── proto │ └── UserService.proto │ └── resources │ └── logback.xml ├── grpc-server ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Server.java │ │ └── grpc │ │ └── server │ │ └── UserServiceGrpcServerImpl.java │ ├── proto │ └── UserService.proto │ └── resources │ └── logback.xml ├── hprose-client ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Client.java │ └── resources │ └── consumer.xml ├── hprose-server ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Server.java │ └── resources │ └── provider.xml ├── jupiter-client ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ ├── rpc │ │ └── Client.java │ │ └── service │ │ └── JupiterUserService.java │ └── resources │ ├── META-INF │ └── services │ │ └── org.jupiter.rpc.consumer.processor.ConsumerExecutorFactory │ ├── logback.xml │ └── spring-consumer.xml ├── jupiter-server ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ ├── rpc │ │ └── Server.java │ │ └── service │ │ ├── JupiterUserService.java │ │ └── JupiterUserServiceServerImpl.java │ └── resources │ ├── META-INF │ └── services │ │ └── org.jupiter.rpc.provider.processor.ProviderExecutorFactory │ ├── logback.xml │ └── spring-provider.xml ├── motan-client ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ ├── bean │ │ └── MotanUser.java │ │ ├── rpc │ │ └── Client.java │ │ └── service │ │ ├── MotanUserService.java │ │ └── MotanUserServiceServerImpl.java │ └── resources │ └── motan_client.xml ├── motan-server ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ ├── bean │ │ └── MotanUser.java │ │ ├── rpc │ │ └── Server.java │ │ └── service │ │ ├── MotanUserService.java │ │ └── MotanUserServiceServerImpl.java │ └── resources │ └── motan_server.xml ├── netty-client ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Client.java │ │ └── netty │ │ ├── client │ │ ├── NettyClientConnector.java │ │ ├── UserServiceNettyClientImpl.java │ │ ├── codec │ │ │ ├── ProtocolDecoder.java │ │ │ └── ProtocolEncoder.java │ │ ├── future │ │ │ └── FutureContainer.java │ │ └── handler │ │ │ ├── BenchmarkChannelInitializer.java │ │ │ └── BenchmarkClientHandler.java │ │ └── serializer │ │ ├── BooleanSerializer.java │ │ ├── FastestSerializer.java │ │ ├── IntegerSerializer.java │ │ ├── LocalDateSerializer.java │ │ ├── LocalDateTimeSerializer.java │ │ ├── LocalTimeSerializer.java │ │ ├── LongSerializer.java │ │ ├── ObjectSerializer.java │ │ ├── Register.java │ │ ├── RequestSerializer.java │ │ ├── ResponseSerializer.java │ │ ├── Serializer.java │ │ ├── StringSerializer.java │ │ ├── UserPageSerializer.java │ │ ├── UserSerializer.java │ │ └── VoidSerializer.java │ └── resources │ └── logback.xml ├── netty-server ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ └── rpc │ ├── Server.java │ └── netty │ ├── serializer │ ├── BooleanSerializer.java │ ├── FastestSerializer.java │ ├── IntegerSerializer.java │ ├── LocalDateSerializer.java │ ├── LocalDateTimeSerializer.java │ ├── LocalTimeSerializer.java │ ├── LongSerializer.java │ ├── ObjectSerializer.java │ ├── Register.java │ ├── RequestSerializer.java │ ├── ResponseSerializer.java │ ├── Serializer.java │ ├── StringSerializer.java │ ├── UserPageSerializer.java │ ├── UserSerializer.java │ └── VoidSerializer.java │ └── server │ ├── codec │ ├── ProtocolDecoder.java │ └── ProtocolEncoder.java │ └── handler │ ├── BenchmarkChannelInitializer.java │ └── BenchmarkServerHandler.java ├── pom.xml ├── rapidoid-client ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ └── rpc │ └── Client.java ├── rapidoid-server ├── .gitignore ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ └── rpc │ ├── Server.java │ └── rapidoid │ └── server │ ├── CreateUserController.java │ ├── GetUserController.java │ ├── ListUserController.java │ └── UserExistController.java ├── rpcBenchmark.jpg ├── rsocket-client ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Client.java │ │ └── rsocket │ │ └── UserServiceRsocketClientImpl.java │ └── proto │ └── UserService.proto ├── rsocket-server ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Server.java │ │ └── rsocket │ │ └── server │ │ └── UserServiceRsocketServerImpl.java │ └── proto │ └── UserService.proto ├── servicecomb-client ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Client.java │ └── resources │ ├── META-INF │ └── spring │ │ └── benchmark.consumer.bean.xml │ ├── config │ └── log4j.demo.properties │ ├── log4j.properties │ └── microservice.yaml ├── servicecomb-server ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ ├── rpc │ │ └── Server.java │ │ └── service │ │ └── ServiceCombUserServiceServerImpl.java │ └── resources │ ├── META-INF │ └── spring │ │ └── benchmark.provider.bean.xml │ ├── config │ └── log4j.demo.properties │ ├── log4j.properties │ └── microservice.yaml ├── sofa-client ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Client.java │ └── resources │ └── logback.xml ├── sofa-server ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ └── Server.java │ └── resources │ └── logback.xml ├── springboot-client ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ └── rpc │ └── Client.java ├── springboot-server ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Server.java │ │ └── springboot │ │ └── server │ │ ├── CreateUserController.java │ │ ├── GetUserController.java │ │ ├── ListUserController.java │ │ └── UserExistController.java │ └── resources │ └── application.yml ├── springboot-undertow-client ├── .gitignore ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ └── rpc │ └── Client.java ├── springboot-undertow-server ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Server.java │ │ └── springboot │ │ └── server │ │ ├── CreateUserController.java │ │ ├── GetUserController.java │ │ ├── ListUserController.java │ │ └── UserExistController.java │ └── resources │ └── application.yml ├── springwebflux-client ├── .gitignore ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ └── rpc │ └── Client.java ├── springwebflux-server ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Server.java │ │ └── webflux │ │ └── server │ │ ├── CreateUserController.java │ │ ├── GetUserController.java │ │ ├── ListUserController.java │ │ └── UserExistController.java │ └── resources │ └── application.yml ├── thrift-client ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Client.java │ │ └── thrift │ │ ├── Converter.java │ │ ├── TServiceClientNoPrint.java │ │ ├── ThriftUserServiceClient.java │ │ ├── User.java │ │ ├── UserPage.java │ │ ├── UserService.java │ │ └── UserServiceThriftClientImpl.java │ ├── resources │ └── logback.xml │ └── thrift │ └── UserService.thrift ├── thrift-server ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Server.java │ │ └── thrift │ │ ├── Converter.java │ │ ├── User.java │ │ ├── UserPage.java │ │ ├── UserService.java │ │ └── UserServiceThriftServerImpl.java │ └── thrift │ ├── UserService.thrift │ └── gen-java │ └── benchmark │ └── rpc │ └── thrift │ ├── User.java │ ├── UserPage.java │ └── UserService.java ├── turbo-rest-client ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ ├── rpc │ └── Client.java │ └── service │ └── TurboUserServiceJsonHttpClientImpl.java ├── turbo-rest-server ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Server.java │ │ └── service │ │ ├── TurboUserService.java │ │ └── TurboUserServiceServerImpl.java │ └── resources │ └── logback.xml ├── turbo-rpc-client ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Client.java │ │ └── service │ │ └── TurboUserService.java │ └── resources │ ├── logback.xml │ └── turbo-client.conf ├── turbo-rpc-server ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── benchmark │ │ └── rpc │ │ ├── Server.java │ │ └── service │ │ ├── TurboUserService.java │ │ └── TurboUserServiceServerImpl.java │ └── resources │ ├── logback.xml │ └── turbo-server.conf ├── undertow-async-client ├── .gitignore ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ └── rpc │ └── Client.java ├── undertow-async-server ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ ├── benchmark │ │ └── rpc │ │ │ ├── Server.java │ │ │ └── undertow │ │ │ └── server │ │ │ ├── CreateUserHandler.java │ │ │ ├── GetUserHandler.java │ │ │ ├── ListUserHandler.java │ │ │ └── UserExistHandler.java │ └── io │ │ └── undertow │ │ └── async │ │ ├── handler │ │ └── AsyncHttpHandler.java │ │ ├── io │ │ ├── PooledByteBufferInputStream.java │ │ └── PooledByteBufferOutputStream.java │ │ └── util │ │ └── UnsafeUtils.java │ └── resources │ └── logback.xml ├── undertow-client ├── .gitignore ├── pom.xml └── src │ └── main │ └── java │ └── benchmark │ └── rpc │ └── Client.java └── undertow-server ├── .gitignore ├── pom.xml └── src └── main ├── java └── benchmark │ └── rpc │ ├── Server.java │ └── undertow │ └── server │ ├── CreateUserHandler.java │ ├── GetUserHandler.java │ ├── ListUserHandler.java │ └── UserExistHandler.java └── resources └── logback.xml /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | *.gz 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | 26 | 27 | */.classpath 28 | */.factorypath 29 | */.project 30 | */.settings 31 | */.idea 32 | */target 33 | *.bak 34 | *.iml 35 | .idea 36 | .settings 37 | .project 38 | 39 | *.iml 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RPC Benchmark 2 | 几乎所有的 RPC 框架都宣称自己是“高性能”的, 那么实际结果到底如何呢, 让我们来做一个性能测试吧. 3 | 4 | ## 测试结果 5 | - Round-5 2019-02-11 https://www.jianshu.com/p/cdd94d0853c3 6 | - Round-4 2018-08-05 https://www.jianshu.com/p/72b98dc67d9d 7 | - Round-3 2018-05-12 https://www.jianshu.com/p/caf51f5cfbaa 8 | - Round-2 2018-03-25 https://www.jianshu.com/p/f0f494cfce94 9 | - Round-1 2018-01-28 https://www.jianshu.com/p/18c95649b1a4 10 | 11 | ## 测试说明 12 | - 仅限于Java. 13 | - 客户端使用JMH进行压测, 32 线程, 3 轮预热 3 轮测试 每轮 10s 14 | - 每次运行前都会执行 ***killall java***, 但没有在每轮测试时重启操作系统 15 | - 所有类库版本在发布时都是最新的, 除非存在bug 16 | - 所有框架都尽量参考该项目自带的Benchmark实现 17 | - 将会一直持续, 不定期发布测试结果 18 | - 更多说明请移步: [怎样对 RPC 进行有效的性能测试](https://www.jianshu.com/p/cbcdf05eaa5c) 19 | 20 | ## 测试用例 21 | 1. boolean existUser(String email), 判断某个 email 是否存在 22 | 2. boolean createUser(User user), 添加一个 User 23 | 3. User getUser(long id), 根据 id 获取一个用户 24 | 4. Page listUser(int pageNo), 获取用户列表 25 | 26 | ## 运行说明 27 | 1. 需要两台机器,一台作为客户端,一台作为服务端 28 | 2. 系统要求为 linux x64, 至少 4GB ram 29 | 3. 客户端需要安装 jdk 11, maven 3 30 | 4. 服务端需要安装 jdk 11 31 | 5. 客户端服务端均需要设置 hosts 32 | > 10.0.0.88 benchmark-client
33 | > 10.0.0.99 benchmark-server 34 | 35 | 6. 服务端需要添加用户 benchmark, 需要配置成客户端免密登录, 也就是客户端可以通过如下方式访问服务端 36 | > ssh benchmark@benchmark-server "ls -lh" 37 | 38 | 7. 客户端执行如下命令, 结果输出到 benchmark/benchmark-result 39 | > git clone https://github.com/hank-whu/rpc-benchmark.git
40 | > cd rpc-benchmark
41 | > java benchmark.java 42 | 43 | ## 开发者必读 44 | 1. cd benchmark-base && mvn install 45 | 2. 配置好 hosts: benchmark-client benchmark-server 46 | 3. 修改或者实现 xxx-server xxx-client 47 | 4. 启动 Server, 然后启动 Client, 确保能不出错跑完所有测试项目 48 | 5. 提交 Pull Request 49 | 50 | ## 免责声明 51 | - 能力所限错误在所难免, 本测试用例及测试结果仅供参考. 52 | - 如果你认为xx框架的代码或配置存在问题,那么欢迎发起Pull Request. 53 | - 利益相关: 本测试用例作者同时为 [turbo](https://github.com/hank-whu/turbo-rpc), [undertow-async](https://github.com/hank-whu/undertow-async) 的作者. 54 | 55 | ## 关注微信公众号: rpcBenchmark 56 | ![rpcBenchmark](https://github.com/hank-whu/rpc-benchmark/raw/master/rpcBenchmark.jpg) -------------------------------------------------------------------------------- /armeria-client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | benchmark.rpc 6 | armeria-client 7 | round-5 8 | jar 9 | 10 | armeria-client 11 | http://maven.apache.org 12 | 13 | 14 | 11 15 | 11 16 | UTF-8 17 | round-5 18 | 19 | 20 | 21 | 22 | benchmark.rpc 23 | benchmark-base 24 | ${version.benchmark-base} 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.12 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 3.8.0 43 | 44 | ${maven.compiler.source} 45 | ${maven.compiler.target} 46 | UTF-8 47 | 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-resources-plugin 52 | 3.1.0 53 | 54 | UTF-8 55 | 56 | 57 | 58 | 59 | maven-assembly-plugin 60 | 3.1.0 61 | 62 | 63 | jar-with-dependencies 64 | 65 | 66 | 67 | benchmark.rpc.Client 68 | 69 | 70 | 71 | 72 | 73 | make-assembly 74 | package 75 | 76 | single 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | src/main/resources 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /armeria-client/src/main/java/benchmark/rpc/Client.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import org.openjdk.jmh.annotations.Benchmark; 6 | import org.openjdk.jmh.annotations.BenchmarkMode; 7 | import org.openjdk.jmh.annotations.Mode; 8 | import org.openjdk.jmh.annotations.OutputTimeUnit; 9 | import org.openjdk.jmh.annotations.Scope; 10 | import org.openjdk.jmh.annotations.State; 11 | import org.openjdk.jmh.runner.Runner; 12 | import org.openjdk.jmh.runner.RunnerException; 13 | import org.openjdk.jmh.runner.options.Options; 14 | import org.openjdk.jmh.runner.options.OptionsBuilder; 15 | import org.openjdk.jmh.runner.options.TimeValue; 16 | 17 | import benchmark.bean.Page; 18 | import benchmark.bean.User; 19 | import benchmark.service.UserService; 20 | import benchmark.service.UserServiceJsonHttpClientImpl; 21 | 22 | @State(Scope.Benchmark) 23 | public class Client extends AbstractClient { 24 | public static final int CONCURRENCY = 32; 25 | 26 | private final UserService userService = new UserServiceJsonHttpClientImpl(CONCURRENCY); 27 | 28 | @Override 29 | protected UserService getUserService() { 30 | return userService; 31 | } 32 | 33 | @Benchmark 34 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 35 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 36 | @Override 37 | public boolean existUser() throws Exception { 38 | return super.existUser(); 39 | } 40 | 41 | @Benchmark 42 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 43 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 44 | @Override 45 | public boolean createUser() throws Exception { 46 | return super.createUser(); 47 | } 48 | 49 | @Benchmark 50 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 51 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 52 | @Override 53 | public User getUser() throws Exception { 54 | return super.getUser(); 55 | } 56 | 57 | @Benchmark 58 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 59 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 60 | @Override 61 | public Page listUser() throws Exception { 62 | return super.listUser(); 63 | } 64 | 65 | public static void main(String[] args) throws RunnerException { 66 | Options opt = new OptionsBuilder()// 67 | .include(Client.class.getSimpleName())// 68 | .warmupIterations(3)// 69 | .warmupTime(TimeValue.seconds(10))// 70 | .measurementIterations(3)// 71 | .measurementTime(TimeValue.seconds(10))// 72 | .threads(CONCURRENCY)// 73 | .forks(1)// 74 | .build(); 75 | 76 | new Runner(opt).run(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /armeria-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import java.net.InetSocketAddress; 4 | 5 | import com.linecorp.armeria.server.ServerBuilder; 6 | 7 | import benchmark.service.ArmeriaUserServiceServerImpl; 8 | 9 | public class Server { 10 | 11 | public static void main(String[] args) throws Exception { 12 | ServerBuilder sb = new ServerBuilder(); 13 | 14 | // Configure an HTTP port. 15 | sb.http(new InetSocketAddress("benchmark-server", 8080)); 16 | 17 | // Using an annotated service object: 18 | sb.annotatedService(new ArmeriaUserServiceServerImpl()); 19 | 20 | sb.build().start().join(); 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /benchmark-base/.java-version: -------------------------------------------------------------------------------- 1 | 11 2 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/bean/Page.java: -------------------------------------------------------------------------------- 1 | package benchmark.bean; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | public class Page implements Serializable { 7 | private static final long serialVersionUID = -7529237188686406553L; 8 | 9 | private int pageNo; 10 | private int total; 11 | private List result; 12 | 13 | public int getPageNo() { 14 | return pageNo; 15 | } 16 | 17 | public void setPageNo(int pageNo) { 18 | this.pageNo = pageNo; 19 | } 20 | 21 | public int getTotal() { 22 | return total; 23 | } 24 | 25 | public void setTotal(int total) { 26 | this.total = total; 27 | } 28 | 29 | public List getResult() { 30 | return result; 31 | } 32 | 33 | public void setResult(List result) { 34 | this.result = result; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "Page [pageNo=" + pageNo + ", total=" + total + ", result=" + result + "]"; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/pool/LockObjectPool.java: -------------------------------------------------------------------------------- 1 | package benchmark.pool; 2 | 3 | import java.io.Closeable; 4 | import java.io.IOException; 5 | import java.util.IdentityHashMap; 6 | import java.util.concurrent.ThreadLocalRandom; 7 | import java.util.concurrent.locks.Lock; 8 | import java.util.concurrent.locks.ReentrantLock; 9 | import java.util.function.Supplier; 10 | 11 | /** 12 | * @author Hank 13 | * 14 | * @param 15 | */ 16 | @SuppressWarnings("unchecked") 17 | public class LockObjectPool implements Closeable { 18 | 19 | static class ObjectWithLock { 20 | public final Object obj; 21 | public final Lock lock; 22 | 23 | public ObjectWithLock(Object obj) { 24 | this.obj = obj; 25 | this.lock = new ReentrantLock(); 26 | } 27 | } 28 | 29 | private final ObjectWithLock[] array; 30 | private final IdentityHashMap lockMap; 31 | private final int poolSize; 32 | 33 | public LockObjectPool(int poolSize, Supplier producer) { 34 | this.poolSize = poolSize; 35 | 36 | lockMap = new IdentityHashMap<>(poolSize * 2); 37 | 38 | array = new ObjectWithLock[poolSize]; 39 | for (int i = 0; i < poolSize; i++) { 40 | T t = producer.get(); 41 | ObjectWithLock objectWithLock = new ObjectWithLock(t); 42 | 43 | array[i] = objectWithLock; 44 | lockMap.put(t, objectWithLock); 45 | } 46 | } 47 | 48 | public T borrow() { 49 | int index = ThreadLocalRandom.current().nextInt(poolSize); 50 | ObjectWithLock objectWithLock = array[index]; 51 | 52 | objectWithLock.lock.lock(); 53 | 54 | return (T) objectWithLock.obj; 55 | } 56 | 57 | public void release(T t) { 58 | if (t == null) { 59 | return; 60 | } 61 | 62 | lockMap.get(t).lock.unlock(); 63 | } 64 | 65 | @Override 66 | public void close() throws IOException { 67 | 68 | for (int i = 0; i < array.length; i++) { 69 | Object obj = array[i].obj; 70 | 71 | if (obj instanceof AutoCloseable) { 72 | try { 73 | ((AutoCloseable) obj).close(); 74 | } catch (Exception e) { 75 | e.printStackTrace(); 76 | } 77 | } 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/pool/UnsafeUtils.java: -------------------------------------------------------------------------------- 1 | package benchmark.pool; 2 | 3 | import sun.misc.Unsafe; 4 | 5 | public class UnsafeUtils { 6 | final static private Unsafe _unsafe; 7 | 8 | static { 9 | Unsafe tmpUnsafe = null; 10 | 11 | try { 12 | java.lang.reflect.Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); 13 | field.setAccessible(true); 14 | tmpUnsafe = (sun.misc.Unsafe) field.get(null); 15 | } catch (java.lang.Exception e) { 16 | throw new Error(e); 17 | } 18 | 19 | _unsafe = tmpUnsafe; 20 | } 21 | 22 | public static final Unsafe unsafe() { 23 | return _unsafe; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/pool/ViberObjectPool.java: -------------------------------------------------------------------------------- 1 | package benchmark.pool; 2 | 3 | import java.io.Closeable; 4 | import java.io.IOException; 5 | import java.util.function.Supplier; 6 | 7 | import org.vibur.objectpool.ConcurrentPool; 8 | import org.vibur.objectpool.PoolObjectFactory; 9 | import org.vibur.objectpool.PoolService; 10 | import org.vibur.objectpool.util.ConcurrentCollection; 11 | import org.vibur.objectpool.util.MultithreadConcurrentQueueCollection; 12 | 13 | public class ViberObjectPool implements Closeable { 14 | 15 | private final PoolService pool; 16 | 17 | public ViberObjectPool(int poolSize, Supplier producer) { 18 | PoolObjectFactory poolObjectFactory = new PoolObjectFactory() { 19 | 20 | @Override 21 | public T create() { 22 | return producer.get(); 23 | } 24 | 25 | @Override 26 | public boolean readyToTake(T obj) { 27 | return true; 28 | } 29 | 30 | @Override 31 | public boolean readyToRestore(T obj) { 32 | return true; 33 | } 34 | 35 | @Override 36 | public void destroy(T obj) { 37 | if (obj instanceof AutoCloseable) { 38 | try { 39 | ((AutoCloseable) obj).close(); 40 | } catch (Exception e) { 41 | e.printStackTrace(); 42 | } 43 | } 44 | } 45 | }; 46 | 47 | ConcurrentCollection concurrentCollection = new MultithreadConcurrentQueueCollection<>(poolSize); 48 | pool = new ConcurrentPool<>(concurrentCollection, poolObjectFactory, poolSize, poolSize, false); 49 | } 50 | 51 | public T borrow() { 52 | return pool.take(); 53 | } 54 | 55 | public void release(T t) { 56 | if (t == null) { 57 | return; 58 | } 59 | 60 | pool.restore(t); 61 | } 62 | 63 | @Override 64 | public void close() throws IOException { 65 | pool.close(); 66 | } 67 | 68 | } -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/pool/WaitStrategy.java: -------------------------------------------------------------------------------- 1 | package benchmark.pool; 2 | 3 | import java.util.concurrent.locks.LockSupport; 4 | 5 | public final class WaitStrategy { 6 | 7 | public final int idle(final int idleCounter) { 8 | 9 | final int idled = idleCounter + 1; 10 | 11 | if (idleCounter < 10) { 12 | Thread.onSpinWait(); 13 | return idled; 14 | } 15 | 16 | if (idleCounter < 10 + 10) { 17 | Thread.yield(); 18 | return idled; 19 | } 20 | 21 | LockSupport.parkNanos(1L); 22 | 23 | return idled; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/rpc/AbstractClient.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | 5 | import benchmark.bean.Page; 6 | import benchmark.bean.User; 7 | import benchmark.service.UserService; 8 | import benchmark.service.UserServiceServerImpl; 9 | 10 | public abstract class AbstractClient { 11 | private final AtomicInteger counter = new AtomicInteger(0); 12 | private final UserService _serviceUserService = new UserServiceServerImpl(); 13 | 14 | protected abstract UserService getUserService(); 15 | 16 | public boolean existUser() throws Exception { 17 | String email = String.valueOf(counter.getAndIncrement()); 18 | return getUserService().existUser(email); 19 | } 20 | 21 | public boolean createUser() throws Exception { 22 | int id = counter.getAndIncrement(); 23 | User user = _serviceUserService.getUser(id); 24 | return getUserService().createUser(user); 25 | } 26 | 27 | public User getUser() throws Exception { 28 | int id = counter.getAndIncrement(); 29 | return getUserService().getUser(id); 30 | } 31 | 32 | public Page listUser() throws Exception { 33 | int pageNo = counter.getAndIncrement(); 34 | return getUserService().listUser(pageNo); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/rpc/protocol/Request.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.protocol; 2 | 3 | import java.io.Serializable; 4 | import java.util.Arrays; 5 | 6 | public class Request implements Serializable { 7 | private static final long serialVersionUID = 7798556948864269597L; 8 | 9 | private long requestId; 10 | private int serviceId; 11 | private Object[] params; 12 | 13 | public long getRequestId() { 14 | return requestId; 15 | } 16 | 17 | public void setRequestId(long requestId) { 18 | this.requestId = requestId; 19 | } 20 | 21 | public int getServiceId() { 22 | return serviceId; 23 | } 24 | 25 | public void setServiceId(int serviceId) { 26 | this.serviceId = serviceId; 27 | } 28 | 29 | public Object[] getParams() { 30 | return params; 31 | } 32 | 33 | public void setParams(Object[] params) { 34 | this.params = params; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "Request [requestId=" + requestId + ", serviceId=" + serviceId + ", params=" + Arrays.toString(params) 40 | + "]"; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/rpc/protocol/Response.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.protocol; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Response implements Serializable { 6 | private static final long serialVersionUID = -2827803061483152127L; 7 | 8 | private long requestId; 9 | private byte statusCode; 10 | private Object result; 11 | 12 | public long getRequestId() { 13 | return requestId; 14 | } 15 | 16 | public void setRequestId(long requestId) { 17 | this.requestId = requestId; 18 | } 19 | 20 | public byte getStatusCode() { 21 | return statusCode; 22 | } 23 | 24 | public void setStatusCode(byte statusCode) { 25 | this.statusCode = statusCode; 26 | } 27 | 28 | public Object getResult() { 29 | return result; 30 | } 31 | 32 | public void setResult(Object result) { 33 | this.result = result; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "Response [requestId=" + requestId + ", statusCode=" + statusCode + ", result=" + result + "]"; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/rpc/route/RouteService.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.route; 2 | 3 | import static benchmark.service.ServiceRegister.CREATE_USER; 4 | import static benchmark.service.ServiceRegister.EXIST_USER; 5 | import static benchmark.service.ServiceRegister.GET_USER; 6 | import static benchmark.service.ServiceRegister.LIST_USER; 7 | 8 | import benchmark.bean.User; 9 | import benchmark.service.UserService; 10 | import benchmark.service.UserServiceServerImpl; 11 | 12 | public class RouteService { 13 | 14 | private final UserService userService = new UserServiceServerImpl(); 15 | 16 | public Object invoke(int serviceId, Object[] params) { 17 | 18 | switch (serviceId) { 19 | case EXIST_USER: 20 | return userService.existUser((String) params[0]); 21 | 22 | case CREATE_USER: 23 | return userService.createUser((User) params[0]); 24 | 25 | case GET_USER: 26 | return userService.getUser((Long) params[0]); 27 | 28 | case LIST_USER: 29 | return userService.listUser((Integer) params[0]); 30 | 31 | default: 32 | break; 33 | } 34 | 35 | return null; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/rpc/util/ByteBufferUtils.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.util; 2 | 3 | import static java.nio.charset.StandardCharsets.UTF_8; 4 | 5 | import java.nio.ByteBuffer; 6 | 7 | public class ByteBufferUtils { 8 | 9 | public static ByteBuffer allocateDirect(String str) { 10 | return _allocateByteBuffer(str.getBytes(UTF_8), true); 11 | } 12 | 13 | public static ByteBuffer allocate(String str) { 14 | return _allocateByteBuffer(str.getBytes(UTF_8), false); 15 | } 16 | 17 | public static ByteBuffer allocate(byte[] bytes) { 18 | return _allocateByteBuffer(bytes, false); 19 | } 20 | 21 | private static ByteBuffer _allocateByteBuffer(byte[] bytes, boolean isDirect) { 22 | final ByteBuffer buffer = isDirect ? // 23 | ByteBuffer.allocateDirect(bytes.length) : // 24 | ByteBuffer.allocate(bytes.length); 25 | 26 | buffer.put(bytes); 27 | buffer.flip(); 28 | 29 | return buffer; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/rpc/util/HttpClientUtils.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.util; 2 | 3 | import org.apache.http.client.config.RequestConfig; 4 | import org.apache.http.config.SocketConfig; 5 | import org.apache.http.impl.client.CloseableHttpClient; 6 | import org.apache.http.impl.client.HttpClientBuilder; 7 | import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 8 | 9 | public class HttpClientUtils { 10 | 11 | public static final int SOCKET_TIMEOUT = 60000; 12 | public static final int CONNECTION_REQUEST_TIMEOUT = 60000; 13 | public static final int CONNECT_TIMEOUT = 60000; 14 | 15 | public static CloseableHttpClient createHttpClient(int concurrency) { 16 | HttpClientBuilder builder = HttpClientBuilder.create(); 17 | 18 | PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); 19 | connManager.setDefaultMaxPerRoute(concurrency); 20 | connManager.setMaxTotal(concurrency); 21 | 22 | RequestConfig requestConfig = RequestConfig.custom()// 23 | .setAuthenticationEnabled(true)// 24 | .setSocketTimeout(SOCKET_TIMEOUT)// 25 | .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)// 26 | .setConnectTimeout(CONNECT_TIMEOUT)// 27 | .setRedirectsEnabled(true)// 28 | .setRelativeRedirectsAllowed(true)// 29 | .setMaxRedirects(15)// 30 | .build(); 31 | 32 | SocketConfig socketConfig = SocketConfig.custom()// 33 | .setSoKeepAlive(true)// 34 | .setSoReuseAddress(true)// 35 | .build(); 36 | 37 | builder.setConnectionManager(connManager); 38 | builder.setDefaultSocketConfig(socketConfig); 39 | builder.setDefaultRequestConfig(requestConfig); 40 | 41 | return builder.build(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/rpc/util/JsonUtils.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.util; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; 5 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 6 | import com.fasterxml.jackson.module.afterburner.AfterburnerModule; 7 | 8 | public class JsonUtils { 9 | public static final ObjectMapper objectMapper = new ObjectMapper(); 10 | 11 | static { 12 | objectMapper.registerModule(new Jdk8Module()); 13 | objectMapper.registerModule(new JavaTimeModule()); 14 | objectMapper.registerModule(new AfterburnerModule()); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/service/ServiceRegister.java: -------------------------------------------------------------------------------- 1 | package benchmark.service; 2 | 3 | public interface ServiceRegister { 4 | public static final int EXIST_USER = 0; 5 | public static final int CREATE_USER = 1; 6 | public static final int GET_USER = 2; 7 | public static final int LIST_USER = 3; 8 | } 9 | -------------------------------------------------------------------------------- /benchmark-base/src/main/java/benchmark/service/UserService.java: -------------------------------------------------------------------------------- 1 | package benchmark.service; 2 | 3 | import benchmark.bean.Page; 4 | import benchmark.bean.User; 5 | 6 | public interface UserService { 7 | public boolean existUser(String email); 8 | 9 | public boolean createUser(User user); 10 | 11 | public User getUser(long id); 12 | 13 | public Page listUser(int pageNo); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /brpc-client/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /brpc-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import benchmark.service.UserServiceServerImpl; 4 | import com.baidu.brpc.protocol.Options; 5 | import com.baidu.brpc.server.RpcServer; 6 | import com.baidu.brpc.server.RpcServerOptions; 7 | 8 | public class Server { 9 | 10 | public static void main(String[] args) throws InterruptedException { 11 | int port = 8002; 12 | 13 | RpcServerOptions options = new RpcServerOptions(); 14 | options.setReceiveBufferSize(64 * 1024 * 1024); 15 | options.setSendBufferSize(64 * 1024 * 1024); 16 | options.setKeepAliveTime(20); 17 | options.setProtocolType(Options.ProtocolType.PROTOCOL_HTTP_JSON_VALUE); 18 | 19 | final RpcServer rpcServer = new RpcServer(port, options); 20 | rpcServer.registerService(new UserServiceServerImpl()); 21 | rpcServer.start(); 22 | 23 | System.out.println("START"); 24 | Thread.sleep(Long.MAX_VALUE); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /brpc-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /dubbo-client/src/main/resources/consumer.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /dubbo-client/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /dubbo-kryo-client/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /dubbo-kryo-client/src/main/resources/consumer.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /dubbo-kryo-client/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /dubbo-kryo-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import org.springframework.context.support.ClassPathXmlApplicationContext; 4 | 5 | public class Server { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");) { 9 | context.start(); 10 | Thread.sleep(Integer.MAX_VALUE); 11 | } 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /dubbo-kryo-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /dubbo-kryo-server/src/main/resources/provider.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /dubbo-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import org.springframework.context.support.ClassPathXmlApplicationContext; 4 | 5 | public class Server { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");) { 9 | context.start(); 10 | Thread.sleep(Integer.MAX_VALUE); 11 | } 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /dubbo-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /dubbo-server/src/main/resources/provider.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /grpc-client/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /grpc-client/src/main/java/benchmark/rpc/grpc/GrpcUserServiceClient.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.grpc; 2 | 3 | import java.io.Closeable; 4 | import java.io.IOException; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import io.grpc.ManagedChannel; 8 | import io.grpc.ManagedChannelBuilder; 9 | 10 | public class GrpcUserServiceClient implements Closeable { 11 | 12 | public final ManagedChannel channel; 13 | public final UserServiceGrpc.UserServiceBlockingStub userServiceBlockingStub; 14 | 15 | public GrpcUserServiceClient(String host, int port) { 16 | ManagedChannelBuilder channelBuilder = ManagedChannelBuilder// 17 | .forAddress(host, port)// 18 | .idleTimeout(5, TimeUnit.SECONDS)// 19 | .usePlaintext(true); 20 | 21 | channel = channelBuilder.build(); 22 | userServiceBlockingStub = UserServiceGrpc.newBlockingStub(channel); 23 | } 24 | 25 | @Override 26 | public void close() throws IOException { 27 | try { 28 | channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); 29 | } catch (InterruptedException e) { 30 | throw new IOException(e); 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /grpc-client/src/main/proto/UserService.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package grpc; 3 | 4 | import public "google/protobuf/timestamp.proto"; 5 | 6 | option java_package = "benchmark.rpc.grpc"; 7 | 8 | service UserService { 9 | rpc userExist (UserExistRequest) returns (UserExistResponse) {} 10 | 11 | rpc createUser (User) returns (CreateUserResponse) {} 12 | 13 | rpc getUser (GetUserRequest) returns (User) {} 14 | 15 | rpc listUser (ListUserRequest) returns (UserPage) {} 16 | } 17 | 18 | message UserExistRequest { 19 | string email = 1; 20 | } 21 | 22 | message UserExistResponse { 23 | bool exist = 1; 24 | } 25 | 26 | message GetUserRequest { 27 | int64 id = 1; 28 | } 29 | 30 | message User { 31 | int64 id = 1; 32 | string name = 2; 33 | int32 sex = 3; 34 | int32 birthday = 4; 35 | string email = 5; 36 | string mobile = 6; 37 | string address = 7; 38 | string icon = 8; 39 | repeated int32 permissions = 9; 40 | int32 status = 10; 41 | int64 createTime = 11; 42 | int64 updateTime = 12; 43 | } 44 | 45 | message CreateUserResponse { 46 | bool success = 1; 47 | } 48 | 49 | message ListUserRequest { 50 | int32 pageNo = 1; 51 | } 52 | 53 | message UserPage { 54 | int32 pageNo = 1; 55 | int32 total = 2; 56 | repeated User result = 3; 57 | } -------------------------------------------------------------------------------- /grpc-client/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /grpc-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import benchmark.rpc.grpc.server.UserServiceGrpcServerImpl; 4 | import io.grpc.ServerBuilder; 5 | 6 | public class Server { 7 | 8 | public static final String host = "benchmark-server"; 9 | public static final int port = 8080; 10 | 11 | public static void main(String[] args) throws Exception { 12 | ServerBuilder// 13 | .forPort(port)// 14 | .addService(new UserServiceGrpcServerImpl())// 15 | .build()// 16 | .start()// 17 | .awaitTermination(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /grpc-server/src/main/proto/UserService.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package grpc; 3 | 4 | import public "google/protobuf/timestamp.proto"; 5 | 6 | option java_package = "benchmark.rpc.grpc"; 7 | 8 | service UserService { 9 | rpc userExist (UserExistRequest) returns (UserExistResponse) {} 10 | 11 | rpc createUser (User) returns (CreateUserResponse) {} 12 | 13 | rpc getUser (GetUserRequest) returns (User) {} 14 | 15 | rpc listUser (ListUserRequest) returns (UserPage) {} 16 | } 17 | 18 | message UserExistRequest { 19 | string email = 1; 20 | } 21 | 22 | message UserExistResponse { 23 | bool exist = 1; 24 | } 25 | 26 | message GetUserRequest { 27 | int64 id = 1; 28 | } 29 | 30 | message User { 31 | int64 id = 1; 32 | string name = 2; 33 | int32 sex = 3; 34 | int32 birthday = 4; 35 | string email = 5; 36 | string mobile = 6; 37 | string address = 7; 38 | string icon = 8; 39 | repeated int32 permissions = 9; 40 | int32 status = 10; 41 | int64 createTime = 11; 42 | int64 updateTime = 12; 43 | } 44 | 45 | message CreateUserResponse { 46 | bool success = 1; 47 | } 48 | 49 | message ListUserRequest { 50 | int32 pageNo = 1; 51 | } 52 | 53 | message UserPage { 54 | int32 pageNo = 1; 55 | int32 total = 2; 56 | repeated User result = 3; 57 | } -------------------------------------------------------------------------------- /grpc-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-server.log 15 | 16 | logs/benchmark-rpc-server.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /hprose-client/src/main/resources/consumer.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 12 | 13 | -------------------------------------------------------------------------------- /hprose-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import benchmark.service.UserServiceServerImpl; 4 | import hprose.server.HproseTcpServer; 5 | 6 | public class Server { 7 | 8 | public static void main(String[] args) throws Exception { 9 | HproseTcpServer server = new HproseTcpServer("benchmark-server", 8080); 10 | server.add(new UserServiceServerImpl()); 11 | server.start(); 12 | System.out.println("START"); 13 | Thread.sleep(Long.MAX_VALUE); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /hprose-server/src/main/resources/provider.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /jupiter-client/src/main/java/benchmark/service/JupiterUserService.java: -------------------------------------------------------------------------------- 1 | package benchmark.service; 2 | 3 | import java.util.concurrent.CompletableFuture; 4 | 5 | import org.jupiter.rpc.ServiceProvider; 6 | 7 | import benchmark.bean.Page; 8 | import benchmark.bean.User; 9 | 10 | @ServiceProvider(group = "test", name = "jupiterUserService") 11 | public interface JupiterUserService { 12 | CompletableFuture existUser(String email); 13 | 14 | CompletableFuture createUser(User user); 15 | 16 | CompletableFuture getUser(long id); 17 | 18 | CompletableFuture> listUser(int pageNo); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /jupiter-client/src/main/resources/META-INF/services/org.jupiter.rpc.consumer.processor.ConsumerExecutorFactory: -------------------------------------------------------------------------------- 1 | org.jupiter.rpc.executor.CallerRunsExecutorFactory 2 | org.jupiter.rpc.executor.DisruptorExecutorFactory -------------------------------------------------------------------------------- /jupiter-client/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-server.log 15 | 16 | logs/benchmark-rpc-server.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /jupiter-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /jupiter-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import benchmark.service.JupiterUserServiceServerImpl; 4 | import io.netty.util.ResourceLeakDetector; 5 | 6 | import org.jupiter.common.util.SystemPropertyUtil; 7 | import org.jupiter.rpc.DefaultServer; 8 | import org.jupiter.rpc.JServer; 9 | import org.jupiter.transport.JConfig; 10 | import org.jupiter.transport.JOption; 11 | import org.jupiter.transport.netty.JNettyTcpAcceptor; 12 | 13 | public class Server { 14 | 15 | public static void main(String[] args) throws InterruptedException { 16 | ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.DISABLED); 17 | try { 18 | SystemPropertyUtil.setProperty("jupiter.executor.factory.provider.factory_name", "callerRuns"); 19 | SystemPropertyUtil.setProperty("jupiter.executor.factory.affinity.thread", "true"); 20 | SystemPropertyUtil.setProperty("jupiter.tracing.needed", "false"); 21 | JServer server = new DefaultServer().withAcceptor(new JNettyTcpAcceptor(18090, true)); 22 | JConfig config = server.acceptor().configGroup().child(); 23 | config.setOption(JOption.WRITE_BUFFER_HIGH_WATER_MARK, 2048 * 1024); 24 | config.setOption(JOption.WRITE_BUFFER_LOW_WATER_MARK, 1024 * 1024); 25 | config.setOption(JOption.SO_RCVBUF, 256 * 1024); 26 | config.setOption(JOption.SO_SNDBUF, 256 * 1024); 27 | 28 | server.serviceRegistry().provider(new JupiterUserServiceServerImpl()).register(); 29 | 30 | server.start(false); 31 | System.out.println("Jupiter started"); 32 | Thread.sleep(java.lang.Integer.MAX_VALUE); 33 | } catch (Throwable t) { 34 | t.printStackTrace(); 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /jupiter-server/src/main/java/benchmark/service/JupiterUserService.java: -------------------------------------------------------------------------------- 1 | package benchmark.service; 2 | 3 | import java.util.concurrent.CompletableFuture; 4 | 5 | import org.jupiter.rpc.ServiceProvider; 6 | 7 | import benchmark.bean.Page; 8 | import benchmark.bean.User; 9 | 10 | @ServiceProvider(group = "test", name = "jupiterUserService") 11 | public interface JupiterUserService { 12 | CompletableFuture existUser(String email); 13 | 14 | CompletableFuture createUser(User user); 15 | 16 | CompletableFuture getUser(long id); 17 | 18 | CompletableFuture> listUser(int pageNo); 19 | 20 | } 21 | -------------------------------------------------------------------------------- /jupiter-server/src/main/resources/META-INF/services/org.jupiter.rpc.provider.processor.ProviderExecutorFactory: -------------------------------------------------------------------------------- 1 | org.jupiter.rpc.executor.DisruptorExecutorFactory 2 | org.jupiter.rpc.executor.CallerRunsExecutorFactory 3 | org.jupiter.rpc.executor.ForkJoinPoolExecutorFactory -------------------------------------------------------------------------------- /jupiter-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-server.log 15 | 16 | logs/benchmark-rpc-server.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /jupiter-server/src/main/resources/spring-provider.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 17 | 18 | 19 | 20 | 21 | 24 | 25 | 26 | 27 | 28 | 29 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /motan-client/src/main/java/benchmark/service/MotanUserService.java: -------------------------------------------------------------------------------- 1 | package benchmark.service; 2 | 3 | import benchmark.bean.MotanUser; 4 | import benchmark.bean.Page; 5 | 6 | public interface MotanUserService { 7 | public boolean existUser(String email); 8 | 9 | public boolean createUser(MotanUser user); 10 | 11 | public MotanUser getUser(long id); 12 | 13 | public Page listUser(int pageNo); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /motan-client/src/main/resources/motan_client.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 15 | 16 | 17 | 21 | 22 | 23 | 26 | -------------------------------------------------------------------------------- /motan-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /motan-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import org.springframework.context.support.ClassPathXmlApplicationContext; 4 | 5 | public class Server { 6 | 7 | public static void main(String[] args) throws InterruptedException { 8 | try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("motan_server.xml");) { 9 | context.start(); 10 | Thread.sleep(Integer.MAX_VALUE); 11 | } 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /motan-server/src/main/java/benchmark/service/MotanUserService.java: -------------------------------------------------------------------------------- 1 | package benchmark.service; 2 | 3 | import benchmark.bean.MotanUser; 4 | import benchmark.bean.Page; 5 | 6 | public interface MotanUserService { 7 | public boolean existUser(String email); 8 | 9 | public boolean createUser(MotanUser user); 10 | 11 | public MotanUser getUser(long id); 12 | 13 | public Page listUser(int pageNo); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /motan-server/src/main/resources/motan_server.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 19 | 20 | 21 | 25 | 26 | 27 | 30 | 31 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/client/UserServiceNettyClientImpl.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.client; 2 | 3 | import java.io.Closeable; 4 | import java.io.IOException; 5 | import java.util.concurrent.atomic.AtomicLong; 6 | 7 | import benchmark.bean.Page; 8 | import benchmark.bean.User; 9 | import benchmark.rpc.protocol.Request; 10 | import benchmark.rpc.protocol.Response; 11 | import benchmark.service.ServiceRegister; 12 | import benchmark.service.UserService; 13 | 14 | public class UserServiceNettyClientImpl implements UserService, Closeable { 15 | 16 | public static final String host = "benchmark-server"; 17 | public static final int port = 8080; 18 | 19 | private final NettyClientConnector connector = new NettyClientConnector(host, port); 20 | private final AtomicLong requestCounter = new AtomicLong(); 21 | 22 | public UserServiceNettyClientImpl() { 23 | connect(); 24 | } 25 | 26 | @Override 27 | public void close() throws IOException { 28 | connector.close(); 29 | } 30 | 31 | private void connect() { 32 | try { 33 | connector.connect(); 34 | } catch (InterruptedException e) { 35 | throw new RuntimeException(e); 36 | } 37 | } 38 | 39 | @SuppressWarnings("unchecked") 40 | private T execute(int serviceId, Object... params) { 41 | long requestId = requestCounter.getAndIncrement(); 42 | 43 | try { 44 | Request request = new Request(); 45 | request.setRequestId(requestId); 46 | request.setServiceId(serviceId); 47 | request.setParams(params); 48 | 49 | Response response = connector.execute(request); 50 | return (T) response.getResult(); 51 | } catch (Exception e) { 52 | throw new RuntimeException(e); 53 | } 54 | 55 | } 56 | 57 | @Override 58 | public boolean existUser(String email) { 59 | return execute(ServiceRegister.EXIST_USER, email); 60 | } 61 | 62 | @Override 63 | public boolean createUser(User user) { 64 | return execute(ServiceRegister.CREATE_USER, user); 65 | } 66 | 67 | @Override 68 | public User getUser(long id) { 69 | return execute(ServiceRegister.GET_USER, id); 70 | } 71 | 72 | @Override 73 | public Page listUser(int pageNo) { 74 | return execute(ServiceRegister.LIST_USER, pageNo); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/client/codec/ProtocolDecoder.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.client.codec; 2 | 3 | import benchmark.rpc.netty.serializer.FastestSerializer; 4 | import io.netty.buffer.ByteBuf; 5 | import io.netty.channel.ChannelHandlerContext; 6 | import io.netty.handler.codec.LengthFieldBasedFrameDecoder; 7 | 8 | public class ProtocolDecoder extends LengthFieldBasedFrameDecoder { 9 | 10 | private static final int HEADER_FIELD_LENGTH = 4; 11 | 12 | public ProtocolDecoder(int maxFrameLength) { 13 | super(maxFrameLength, 0, HEADER_FIELD_LENGTH, 0, HEADER_FIELD_LENGTH); 14 | } 15 | 16 | @Override 17 | protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { 18 | ByteBuf buffer = (ByteBuf) super.decode(ctx, in); 19 | 20 | if (buffer != null) { 21 | 22 | try { 23 | return FastestSerializer.readResponse(buffer); 24 | } finally { 25 | buffer.release(); 26 | } 27 | } 28 | 29 | return null; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/client/codec/ProtocolEncoder.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.client.codec; 2 | 3 | import benchmark.rpc.netty.serializer.FastestSerializer; 4 | import benchmark.rpc.protocol.Request; 5 | import io.netty.buffer.ByteBuf; 6 | import io.netty.channel.ChannelHandlerContext; 7 | import io.netty.handler.codec.MessageToByteEncoder; 8 | 9 | public class ProtocolEncoder extends MessageToByteEncoder { 10 | 11 | protected void encode(ChannelHandlerContext ctx, Request request, ByteBuf buffer) throws Exception { 12 | FastestSerializer.writeRequest(buffer, request); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/client/future/FutureContainer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.client.future; 2 | 3 | import java.util.concurrent.CompletableFuture; 4 | import java.util.concurrent.ConcurrentHashMap; 5 | 6 | import benchmark.rpc.protocol.Response; 7 | 8 | /** 9 | * 10 | * @author Hank 11 | * 12 | */ 13 | public final class FutureContainer { 14 | private final ConcurrentHashMap> futureMap = new ConcurrentHashMap<>(); 15 | 16 | public void addFuture(long requestId, CompletableFuture future) { 17 | futureMap.put(requestId, future); 18 | } 19 | 20 | public void remove(long requestId) { 21 | futureMap.remove(requestId); 22 | } 23 | 24 | public void notifyResponse(Response response) { 25 | CompletableFuture future = futureMap.remove(response.getRequestId()); 26 | 27 | if (future == null) { 28 | return; 29 | } 30 | 31 | future.complete(response); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/client/handler/BenchmarkChannelInitializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.client.handler; 2 | 3 | import benchmark.rpc.netty.client.codec.ProtocolDecoder; 4 | import benchmark.rpc.netty.client.codec.ProtocolEncoder; 5 | import benchmark.rpc.netty.client.future.FutureContainer; 6 | import io.netty.channel.ChannelInitializer; 7 | import io.netty.channel.socket.SocketChannel; 8 | 9 | public class BenchmarkChannelInitializer extends ChannelInitializer { 10 | 11 | public static final int MAX_FRAME_LENGTH = 8 * 1024 * 1024; 12 | 13 | private final FutureContainer futureContainer; 14 | 15 | public BenchmarkChannelInitializer(FutureContainer futureContainer) { 16 | this.futureContainer = futureContainer; 17 | } 18 | 19 | @Override 20 | public void initChannel(SocketChannel ch) throws Exception { 21 | ch.pipeline()// 22 | .addLast("encoder", new ProtocolEncoder())// 23 | .addLast("decoder", new ProtocolDecoder(MAX_FRAME_LENGTH))// 24 | .addLast("handler", new BenchmarkClientHandler(futureContainer)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/client/handler/BenchmarkClientHandler.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.client.handler; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import benchmark.rpc.netty.client.future.FutureContainer; 7 | import benchmark.rpc.protocol.Response; 8 | import io.netty.channel.ChannelHandlerContext; 9 | import io.netty.channel.SimpleChannelInboundHandler; 10 | 11 | public class BenchmarkClientHandler extends SimpleChannelInboundHandler { 12 | private static final Logger LOGGER = LoggerFactory.getLogger(BenchmarkClientHandler.class); 13 | 14 | private final FutureContainer futureContainer; 15 | 16 | public BenchmarkClientHandler(FutureContainer futureContainer) { 17 | this.futureContainer = futureContainer; 18 | } 19 | 20 | protected void channelRead0(ChannelHandlerContext ctx, Response response) throws Exception { 21 | futureContainer.notifyResponse(response); 22 | } 23 | 24 | @Override 25 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 26 | LOGGER.error("Exception caught on {}, ", ctx.channel(), cause); 27 | ctx.channel().close(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/BooleanSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | public class BooleanSerializer implements Serializer { 6 | 7 | @Override 8 | public void write(ByteBuf byteBuf, Boolean value) { 9 | byteBuf.writeBoolean(value); 10 | } 11 | 12 | @Override 13 | public Boolean read(ByteBuf byteBuf) { 14 | return byteBuf.readBoolean(); 15 | } 16 | 17 | @Override 18 | public Class typeClass() { 19 | return Boolean.class; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/FastestSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.io.IOException; 4 | 5 | import benchmark.rpc.protocol.Request; 6 | import benchmark.rpc.protocol.Response; 7 | import io.netty.buffer.ByteBuf; 8 | 9 | public class FastestSerializer { 10 | 11 | private static final RequestSerializer requestSerializer = new RequestSerializer(); 12 | private static final ResponseSerializer responseSerializer = new ResponseSerializer(); 13 | 14 | public static void writeRequest(ByteBuf byteBuf, Request request) throws IOException { 15 | int beginWriterIndex = byteBuf.writerIndex(); 16 | byteBuf.writeInt(0); 17 | 18 | requestSerializer.write(byteBuf, request); 19 | 20 | int finishWriterIndex = byteBuf.writerIndex(); 21 | int length = finishWriterIndex - beginWriterIndex - 4; 22 | 23 | byteBuf.setInt(beginWriterIndex, length); 24 | } 25 | 26 | public static Request readRequest(ByteBuf byteBuf) throws IOException { 27 | return requestSerializer.read(byteBuf); 28 | } 29 | 30 | public static void writeResponse(ByteBuf byteBuf, Response response) throws IOException { 31 | int beginWriterIndex = byteBuf.writerIndex(); 32 | byteBuf.writeInt(0); 33 | 34 | responseSerializer.write(byteBuf, response); 35 | 36 | int finishWriterIndex = byteBuf.writerIndex(); 37 | int length = finishWriterIndex - beginWriterIndex - 4; 38 | 39 | byteBuf.setInt(beginWriterIndex, length); 40 | } 41 | 42 | public static Response readResponse(ByteBuf byteBuf) throws IOException { 43 | return responseSerializer.read(byteBuf); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/IntegerSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | public class IntegerSerializer implements Serializer { 6 | 7 | @Override 8 | public void write(ByteBuf byteBuf, Integer value) { 9 | byteBuf.writeInt(value); 10 | } 11 | 12 | @Override 13 | public Integer read(ByteBuf byteBuf) { 14 | return byteBuf.readInt(); 15 | } 16 | 17 | @Override 18 | public Class typeClass() { 19 | return Integer.class; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/LocalDateSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.time.LocalDate; 4 | 5 | import io.netty.buffer.ByteBuf; 6 | 7 | public class LocalDateSerializer implements Serializer { 8 | 9 | @Override 10 | public void write(ByteBuf byteBuf, LocalDate localDate) { 11 | byteBuf.writeShort(localDate.getYear()); 12 | byteBuf.writeByte(localDate.getMonthValue()); 13 | byteBuf.writeByte(localDate.getDayOfMonth()); 14 | } 15 | 16 | @Override 17 | public LocalDate read(ByteBuf byteBuf) { 18 | int year = byteBuf.readShort(); 19 | int month = byteBuf.readByte(); 20 | int dayOfMonth = byteBuf.readByte(); 21 | 22 | return LocalDate.of(year, month, dayOfMonth); 23 | } 24 | 25 | @Override 26 | public Class typeClass() { 27 | return LocalDate.class; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/LocalDateTimeSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.time.LocalDate; 4 | import java.time.LocalDateTime; 5 | import java.time.LocalTime; 6 | 7 | import io.netty.buffer.ByteBuf; 8 | 9 | public class LocalDateTimeSerializer implements Serializer { 10 | 11 | private final LocalDateSerializer localDateSerializer = new LocalDateSerializer(); 12 | private final LocalTimeSerializer localTimeSerializer = new LocalTimeSerializer(); 13 | 14 | @Override 15 | public void write(ByteBuf byteBuf, LocalDateTime localDateTime) { 16 | localDateSerializer.write(byteBuf, localDateTime.toLocalDate()); 17 | localTimeSerializer.write(byteBuf, localDateTime.toLocalTime()); 18 | } 19 | 20 | @Override 21 | public LocalDateTime read(ByteBuf byteBuf) { 22 | LocalDate localDate = localDateSerializer.read(byteBuf); 23 | LocalTime localTime = localTimeSerializer.read(byteBuf); 24 | 25 | return LocalDateTime.of(localDate, localTime); 26 | } 27 | 28 | @Override 29 | public Class typeClass() { 30 | return LocalDateTime.class; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/LocalTimeSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.time.LocalTime; 4 | 5 | import io.netty.buffer.ByteBuf; 6 | 7 | public class LocalTimeSerializer implements Serializer { 8 | 9 | @Override 10 | public void write(ByteBuf byteBuf, LocalTime localTime) { 11 | byteBuf.writeByte(localTime.getHour()); 12 | byteBuf.writeByte(localTime.getMinute()); 13 | byteBuf.writeByte(localTime.getSecond()); 14 | byteBuf.writeInt(localTime.getNano()); 15 | } 16 | 17 | @Override 18 | public LocalTime read(ByteBuf byteBuf) { 19 | int hour = byteBuf.readByte(); 20 | int minute = byteBuf.readByte(); 21 | int second = byteBuf.readByte(); 22 | int nanoOfSecond = byteBuf.readInt(); 23 | 24 | return LocalTime.of(hour, minute, second, nanoOfSecond); 25 | } 26 | 27 | @Override 28 | public Class typeClass() { 29 | return LocalTime.class; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/LongSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | public class LongSerializer implements Serializer { 6 | 7 | @Override 8 | public void write(ByteBuf byteBuf, Long value) { 9 | byteBuf.writeLong(value); 10 | } 11 | 12 | @Override 13 | public Long read(ByteBuf byteBuf) { 14 | return byteBuf.readLong(); 15 | } 16 | 17 | @Override 18 | public Class typeClass() { 19 | return Long.class; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/ObjectSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | @SuppressWarnings("unchecked") 6 | public class ObjectSerializer implements Serializer { 7 | 8 | @Override 9 | public void write(ByteBuf byteBuf, Object object) { 10 | if (object == null) { 11 | byteBuf.writeInt(0); 12 | return; 13 | } 14 | 15 | Class clazz = object.getClass(); 16 | 17 | int id = Register.id(clazz); 18 | Serializer serializer = (Serializer) Register.serializer(id); 19 | 20 | byteBuf.writeInt(id); 21 | serializer.write(byteBuf, object); 22 | } 23 | 24 | @Override 25 | public Object read(ByteBuf byteBuf) { 26 | int id = byteBuf.readInt(); 27 | Serializer serializer = (Serializer) Register.serializer(id); 28 | return serializer.read(byteBuf); 29 | } 30 | 31 | @Override 32 | public Class typeClass() { 33 | return Object.class; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/Register.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.util.IdentityHashMap; 4 | 5 | public class Register { 6 | private final static Serializer[] idToSerializer = new Serializer[16]; 7 | private final static IdentityHashMap, Integer> classToId = new IdentityHashMap<>(); 8 | private final static IdentityHashMap, Serializer> classToSerializer = new IdentityHashMap<>(); 9 | 10 | static { 11 | int index = 0; 12 | 13 | idToSerializer[index++] = new VoidSerializer(); 14 | idToSerializer[index++] = new IntegerSerializer(); 15 | idToSerializer[index++] = new LongSerializer(); 16 | idToSerializer[index++] = new BooleanSerializer(); 17 | idToSerializer[index++] = new StringSerializer(); 18 | idToSerializer[index++] = new LocalDateSerializer(); 19 | idToSerializer[index++] = new LocalTimeSerializer(); 20 | idToSerializer[index++] = new LocalDateTimeSerializer(); 21 | idToSerializer[index++] = new UserSerializer(); 22 | idToSerializer[index++] = new UserPageSerializer(); 23 | idToSerializer[index++] = new RequestSerializer(); 24 | 25 | for (int i = 0; i < index; i++) { 26 | Serializer serializer = idToSerializer[i]; 27 | Class clazz = serializer.typeClass(); 28 | 29 | classToSerializer.put(clazz, serializer); 30 | classToId.put(clazz, i); 31 | } 32 | } 33 | 34 | public static Serializer serializer(int id) { 35 | return idToSerializer[id]; 36 | } 37 | 38 | public static Serializer serializer(Class clazz) { 39 | return classToSerializer.get(clazz); 40 | } 41 | 42 | public static int id(Class clazz) { 43 | return classToId.get(clazz); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/RequestSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import benchmark.rpc.protocol.Request; 4 | import io.netty.buffer.ByteBuf; 5 | 6 | public class RequestSerializer implements Serializer { 7 | 8 | private final ObjectSerializer objectSerializer = new ObjectSerializer(); 9 | 10 | @Override 11 | public void write(ByteBuf byteBuf, Request request) { 12 | Object[] params = request.getParams(); 13 | 14 | byteBuf.writeLong(request.getRequestId()); 15 | byteBuf.writeInt(request.getServiceId()); 16 | 17 | byteBuf.writeInt(params.length); 18 | for (int i = 0; i < params.length; i++) { 19 | objectSerializer.write(byteBuf, params[i]); 20 | } 21 | } 22 | 23 | @Override 24 | public Request read(ByteBuf byteBuf) { 25 | long requestId = byteBuf.readLong(); 26 | int serviceId = byteBuf.readInt(); 27 | 28 | int size = byteBuf.readInt(); 29 | Object[] params = new Object[size]; 30 | for (int i = 0; i < size; i++) { 31 | params[i] = objectSerializer.read(byteBuf); 32 | } 33 | 34 | Request request = new Request(); 35 | request.setRequestId(requestId); 36 | request.setServiceId(serviceId); 37 | request.setParams(params); 38 | 39 | return request; 40 | } 41 | 42 | @Override 43 | public Class typeClass() { 44 | return Request.class; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/ResponseSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import benchmark.rpc.protocol.Response; 4 | import io.netty.buffer.ByteBuf; 5 | 6 | public class ResponseSerializer implements Serializer { 7 | 8 | private final ObjectSerializer objectSerializer = new ObjectSerializer(); 9 | 10 | @Override 11 | public void write(ByteBuf byteBuf, Response response) { 12 | byteBuf.writeLong(response.getRequestId()); 13 | byteBuf.writeByte(response.getStatusCode()); 14 | objectSerializer.write(byteBuf, response.getResult()); 15 | } 16 | 17 | @Override 18 | public Response read(ByteBuf byteBuf) { 19 | long requestId = byteBuf.readLong(); 20 | byte statusCode = byteBuf.readByte(); 21 | Object result = objectSerializer.read(byteBuf); 22 | 23 | Response response = new Response(); 24 | response.setRequestId(requestId); 25 | response.setStatusCode(statusCode); 26 | response.setResult(result); 27 | 28 | return response; 29 | } 30 | 31 | @Override 32 | public Class typeClass() { 33 | return Response.class; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/Serializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | public interface Serializer { 6 | 7 | public void write(ByteBuf byteBuf, T object); 8 | 9 | public T read(ByteBuf byteBuf); 10 | 11 | public Class typeClass(); 12 | } 13 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/StringSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.nio.charset.StandardCharsets; 4 | 5 | import io.netty.buffer.ByteBuf; 6 | 7 | public class StringSerializer implements Serializer { 8 | 9 | @Override 10 | public void write(ByteBuf byteBuf, String str) { 11 | byte[] bytes = str.getBytes(StandardCharsets.UTF_8); 12 | byteBuf.writeInt(bytes.length); 13 | byteBuf.writeBytes(bytes); 14 | } 15 | 16 | @Override 17 | public String read(ByteBuf byteBuf) { 18 | int length = byteBuf.readInt(); 19 | byte[] bytes = new byte[length]; 20 | byteBuf.readBytes(bytes); 21 | 22 | return new String(bytes, StandardCharsets.UTF_8); 23 | } 24 | 25 | @Override 26 | public Class typeClass() { 27 | return String.class; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/UserPageSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.RandomAccess; 6 | 7 | import benchmark.bean.Page; 8 | import benchmark.bean.User; 9 | import io.netty.buffer.ByteBuf; 10 | 11 | public class UserPageSerializer implements Serializer> { 12 | 13 | private final UserSerializer userSerializer = new UserSerializer(); 14 | 15 | @Override 16 | public void write(ByteBuf byteBuf, Page userPage) { 17 | List userList = userPage.getResult(); 18 | 19 | byteBuf.writeInt(userPage.getPageNo()); 20 | byteBuf.writeInt(userPage.getTotal()); 21 | byteBuf.writeInt(userList.size()); 22 | 23 | if (userList instanceof RandomAccess) { 24 | for (int i = 0; i < userList.size(); i++) { 25 | userSerializer.write(byteBuf, userList.get(i)); 26 | } 27 | } else { 28 | for (User user : userList) { 29 | userSerializer.write(byteBuf, user); 30 | } 31 | } 32 | } 33 | 34 | @Override 35 | public Page read(ByteBuf byteBuf) { 36 | int pageNo = byteBuf.readInt(); 37 | int total = byteBuf.readInt(); 38 | int size = byteBuf.readInt(); 39 | 40 | List userList = new ArrayList<>(size); 41 | for (int i = 0; i < size; i++) { 42 | userList.add(userSerializer.read(byteBuf)); 43 | } 44 | 45 | Page userPage = new Page<>(); 46 | userPage.setPageNo(pageNo); 47 | userPage.setTotal(total); 48 | userPage.setResult(userList); 49 | 50 | return userPage; 51 | } 52 | 53 | @Override 54 | @SuppressWarnings("rawtypes") 55 | public Class typeClass() { 56 | return Page.class; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /netty-client/src/main/java/benchmark/rpc/netty/serializer/VoidSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | public class VoidSerializer implements Serializer { 6 | 7 | @Override 8 | public void write(ByteBuf byteBuf, Void value) { 9 | } 10 | 11 | @Override 12 | public Void read(ByteBuf byteBuf) { 13 | return null; 14 | } 15 | 16 | @Override 17 | public Class typeClass() { 18 | return Void.class; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /netty-client/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 9 | 10 | 11 | 12 | 13 | 15 | logs/benchmark-rpc-client.log 16 | 18 | logs/benchmark-netty-client.%d{yyyy-MM-dd}.log.gz 19 | 20 | 21 | 22 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import java.net.InetSocketAddress; 4 | 5 | import benchmark.rpc.netty.server.handler.BenchmarkChannelInitializer; 6 | import io.netty.bootstrap.ServerBootstrap; 7 | import io.netty.channel.Channel; 8 | import io.netty.channel.ChannelOption; 9 | import io.netty.channel.EventLoopGroup; 10 | import io.netty.channel.ServerChannel; 11 | import io.netty.channel.WriteBufferWaterMark; 12 | import io.netty.channel.epoll.Epoll; 13 | import io.netty.channel.epoll.EpollChannelOption; 14 | import io.netty.channel.epoll.EpollEventLoopGroup; 15 | import io.netty.channel.epoll.EpollServerSocketChannel; 16 | import io.netty.channel.nio.NioEventLoopGroup; 17 | import io.netty.channel.socket.nio.NioServerSocketChannel; 18 | import io.netty.util.ResourceLeakDetector; 19 | import io.netty.util.ResourceLeakDetector.Level; 20 | 21 | public class Server { 22 | 23 | public static final String host = "benchmark-server"; 24 | public static final int port = 8080; 25 | 26 | static { 27 | ResourceLeakDetector.setLevel(Level.DISABLED); 28 | } 29 | 30 | public static void main(String[] args) throws InterruptedException { 31 | if (Epoll.isAvailable()) { 32 | doRun(// 33 | new EpollEventLoopGroup(), // 34 | EpollServerSocketChannel.class, // 35 | true); 36 | } else { 37 | doRun(// 38 | new NioEventLoopGroup(), // 39 | NioServerSocketChannel.class, // 40 | false); 41 | } 42 | } 43 | 44 | private static void doRun(EventLoopGroup loupGroup, Class serverChannelClass, 45 | boolean isEpoll) throws InterruptedException { 46 | try { 47 | InetSocketAddress inet = new InetSocketAddress(port); 48 | 49 | ServerBootstrap b = new ServerBootstrap(); 50 | 51 | if (isEpoll) { 52 | b.option(EpollChannelOption.SO_REUSEPORT, true); 53 | } 54 | 55 | b.option(ChannelOption.SO_BACKLOG, 1024 * 8); 56 | b.option(ChannelOption.SO_REUSEADDR, true); 57 | b.option(ChannelOption.SO_RCVBUF, 256 * 1024); 58 | b.group(loupGroup).channel(serverChannelClass).childHandler(new BenchmarkChannelInitializer()); 59 | b.childOption(ChannelOption.SO_REUSEADDR, true); 60 | 61 | b.childOption(ChannelOption.SO_REUSEADDR, true); 62 | b.childOption(ChannelOption.SO_RCVBUF, 256 * 1024); 63 | b.childOption(ChannelOption.SO_SNDBUF, 256 * 1024); 64 | b.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, // 65 | new WriteBufferWaterMark(1024 * 1024, 2048 * 1024)); 66 | 67 | Channel ch = b.bind(inet).sync().channel(); 68 | 69 | System.out.printf("Httpd started. Listening on: %s%n", inet.toString()); 70 | 71 | ch.closeFuture().sync(); 72 | } finally { 73 | loupGroup.shutdownGracefully().sync(); 74 | } 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/BooleanSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | public class BooleanSerializer implements Serializer { 6 | 7 | @Override 8 | public void write(ByteBuf byteBuf, Boolean value) { 9 | byteBuf.writeBoolean(value); 10 | } 11 | 12 | @Override 13 | public Boolean read(ByteBuf byteBuf) { 14 | return byteBuf.readBoolean(); 15 | } 16 | 17 | @Override 18 | public Class typeClass() { 19 | return Boolean.class; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/FastestSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.io.IOException; 4 | 5 | import benchmark.rpc.protocol.Request; 6 | import benchmark.rpc.protocol.Response; 7 | import io.netty.buffer.ByteBuf; 8 | 9 | public class FastestSerializer { 10 | 11 | private static final RequestSerializer requestSerializer = new RequestSerializer(); 12 | private static final ResponseSerializer responseSerializer = new ResponseSerializer(); 13 | 14 | public static void writeRequest(ByteBuf byteBuf, Request request) throws IOException { 15 | int beginWriterIndex = byteBuf.writerIndex(); 16 | byteBuf.writeInt(0); 17 | 18 | requestSerializer.write(byteBuf, request); 19 | 20 | int finishWriterIndex = byteBuf.writerIndex(); 21 | int length = finishWriterIndex - beginWriterIndex - 4; 22 | 23 | byteBuf.setInt(beginWriterIndex, length); 24 | } 25 | 26 | public static Request readRequest(ByteBuf byteBuf) throws IOException { 27 | return requestSerializer.read(byteBuf); 28 | } 29 | 30 | public static void writeResponse(ByteBuf byteBuf, Response response) throws IOException { 31 | int beginWriterIndex = byteBuf.writerIndex(); 32 | byteBuf.writeInt(0); 33 | 34 | responseSerializer.write(byteBuf, response); 35 | 36 | int finishWriterIndex = byteBuf.writerIndex(); 37 | int length = finishWriterIndex - beginWriterIndex - 4; 38 | 39 | byteBuf.setInt(beginWriterIndex, length); 40 | } 41 | 42 | public static Response readResponse(ByteBuf byteBuf) throws IOException { 43 | return responseSerializer.read(byteBuf); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/IntegerSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | public class IntegerSerializer implements Serializer { 6 | 7 | @Override 8 | public void write(ByteBuf byteBuf, Integer value) { 9 | byteBuf.writeInt(value); 10 | } 11 | 12 | @Override 13 | public Integer read(ByteBuf byteBuf) { 14 | return byteBuf.readInt(); 15 | } 16 | 17 | @Override 18 | public Class typeClass() { 19 | return Integer.class; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/LocalDateSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.time.LocalDate; 4 | 5 | import io.netty.buffer.ByteBuf; 6 | 7 | public class LocalDateSerializer implements Serializer { 8 | 9 | @Override 10 | public void write(ByteBuf byteBuf, LocalDate localDate) { 11 | byteBuf.writeShort(localDate.getYear()); 12 | byteBuf.writeByte(localDate.getMonthValue()); 13 | byteBuf.writeByte(localDate.getDayOfMonth()); 14 | } 15 | 16 | @Override 17 | public LocalDate read(ByteBuf byteBuf) { 18 | int year = byteBuf.readShort(); 19 | int month = byteBuf.readByte(); 20 | int dayOfMonth = byteBuf.readByte(); 21 | 22 | return LocalDate.of(year, month, dayOfMonth); 23 | } 24 | 25 | @Override 26 | public Class typeClass() { 27 | return LocalDate.class; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/LocalDateTimeSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.time.LocalDate; 4 | import java.time.LocalDateTime; 5 | import java.time.LocalTime; 6 | 7 | import io.netty.buffer.ByteBuf; 8 | 9 | public class LocalDateTimeSerializer implements Serializer { 10 | 11 | private final LocalDateSerializer localDateSerializer = new LocalDateSerializer(); 12 | private final LocalTimeSerializer localTimeSerializer = new LocalTimeSerializer(); 13 | 14 | @Override 15 | public void write(ByteBuf byteBuf, LocalDateTime localDateTime) { 16 | localDateSerializer.write(byteBuf, localDateTime.toLocalDate()); 17 | localTimeSerializer.write(byteBuf, localDateTime.toLocalTime()); 18 | } 19 | 20 | @Override 21 | public LocalDateTime read(ByteBuf byteBuf) { 22 | LocalDate localDate = localDateSerializer.read(byteBuf); 23 | LocalTime localTime = localTimeSerializer.read(byteBuf); 24 | 25 | return LocalDateTime.of(localDate, localTime); 26 | } 27 | 28 | @Override 29 | public Class typeClass() { 30 | return LocalDateTime.class; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/LocalTimeSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.time.LocalTime; 4 | 5 | import io.netty.buffer.ByteBuf; 6 | 7 | public class LocalTimeSerializer implements Serializer { 8 | 9 | @Override 10 | public void write(ByteBuf byteBuf, LocalTime localTime) { 11 | byteBuf.writeByte(localTime.getHour()); 12 | byteBuf.writeByte(localTime.getMinute()); 13 | byteBuf.writeByte(localTime.getSecond()); 14 | byteBuf.writeInt(localTime.getNano()); 15 | } 16 | 17 | @Override 18 | public LocalTime read(ByteBuf byteBuf) { 19 | int hour = byteBuf.readByte(); 20 | int minute = byteBuf.readByte(); 21 | int second = byteBuf.readByte(); 22 | int nanoOfSecond = byteBuf.readInt(); 23 | 24 | return LocalTime.of(hour, minute, second, nanoOfSecond); 25 | } 26 | 27 | @Override 28 | public Class typeClass() { 29 | return LocalTime.class; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/LongSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | public class LongSerializer implements Serializer { 6 | 7 | @Override 8 | public void write(ByteBuf byteBuf, Long value) { 9 | byteBuf.writeLong(value); 10 | } 11 | 12 | @Override 13 | public Long read(ByteBuf byteBuf) { 14 | return byteBuf.readLong(); 15 | } 16 | 17 | @Override 18 | public Class typeClass() { 19 | return Long.class; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/ObjectSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | @SuppressWarnings("unchecked") 6 | public class ObjectSerializer implements Serializer { 7 | 8 | @Override 9 | public void write(ByteBuf byteBuf, Object object) { 10 | if (object == null) { 11 | byteBuf.writeInt(0); 12 | return; 13 | } 14 | 15 | Class clazz = object.getClass(); 16 | 17 | int id = Register.id(clazz); 18 | Serializer serializer = (Serializer) Register.serializer(id); 19 | 20 | byteBuf.writeInt(id); 21 | serializer.write(byteBuf, object); 22 | } 23 | 24 | @Override 25 | public Object read(ByteBuf byteBuf) { 26 | int id = byteBuf.readInt(); 27 | Serializer serializer = (Serializer) Register.serializer(id); 28 | return serializer.read(byteBuf); 29 | } 30 | 31 | @Override 32 | public Class typeClass() { 33 | return Object.class; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/Register.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.util.IdentityHashMap; 4 | 5 | public class Register { 6 | private final static Serializer[] idToSerializer = new Serializer[16]; 7 | private final static IdentityHashMap, Integer> classToId = new IdentityHashMap<>(); 8 | private final static IdentityHashMap, Serializer> classToSerializer = new IdentityHashMap<>(); 9 | 10 | static { 11 | int index = 0; 12 | 13 | idToSerializer[index++] = new VoidSerializer(); 14 | idToSerializer[index++] = new IntegerSerializer(); 15 | idToSerializer[index++] = new LongSerializer(); 16 | idToSerializer[index++] = new BooleanSerializer(); 17 | idToSerializer[index++] = new StringSerializer(); 18 | idToSerializer[index++] = new LocalDateSerializer(); 19 | idToSerializer[index++] = new LocalTimeSerializer(); 20 | idToSerializer[index++] = new LocalDateTimeSerializer(); 21 | idToSerializer[index++] = new UserSerializer(); 22 | idToSerializer[index++] = new UserPageSerializer(); 23 | idToSerializer[index++] = new RequestSerializer(); 24 | 25 | for (int i = 0; i < index; i++) { 26 | Serializer serializer = idToSerializer[i]; 27 | Class clazz = serializer.typeClass(); 28 | 29 | classToSerializer.put(clazz, serializer); 30 | classToId.put(clazz, i); 31 | } 32 | } 33 | 34 | public static Serializer serializer(int id) { 35 | return idToSerializer[id]; 36 | } 37 | 38 | public static Serializer serializer(Class clazz) { 39 | return classToSerializer.get(clazz); 40 | } 41 | 42 | public static int id(Class clazz) { 43 | return classToId.get(clazz); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/RequestSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import benchmark.rpc.protocol.Request; 4 | import io.netty.buffer.ByteBuf; 5 | 6 | public class RequestSerializer implements Serializer { 7 | 8 | private final ObjectSerializer objectSerializer = new ObjectSerializer(); 9 | 10 | @Override 11 | public void write(ByteBuf byteBuf, Request request) { 12 | Object[] params = request.getParams(); 13 | 14 | byteBuf.writeLong(request.getRequestId()); 15 | byteBuf.writeInt(request.getServiceId()); 16 | 17 | byteBuf.writeInt(params.length); 18 | for (int i = 0; i < params.length; i++) { 19 | objectSerializer.write(byteBuf, params[i]); 20 | } 21 | } 22 | 23 | @Override 24 | public Request read(ByteBuf byteBuf) { 25 | long requestId = byteBuf.readLong(); 26 | int serviceId = byteBuf.readInt(); 27 | 28 | int size = byteBuf.readInt(); 29 | Object[] params = new Object[size]; 30 | for (int i = 0; i < size; i++) { 31 | params[i] = objectSerializer.read(byteBuf); 32 | } 33 | 34 | Request request = new Request(); 35 | request.setRequestId(requestId); 36 | request.setServiceId(serviceId); 37 | request.setParams(params); 38 | 39 | return request; 40 | } 41 | 42 | @Override 43 | public Class typeClass() { 44 | return Request.class; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/ResponseSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import benchmark.rpc.protocol.Response; 4 | import io.netty.buffer.ByteBuf; 5 | 6 | public class ResponseSerializer implements Serializer { 7 | 8 | private final ObjectSerializer objectSerializer = new ObjectSerializer(); 9 | 10 | @Override 11 | public void write(ByteBuf byteBuf, Response response) { 12 | byteBuf.writeLong(response.getRequestId()); 13 | byteBuf.writeByte(response.getStatusCode()); 14 | objectSerializer.write(byteBuf, response.getResult()); 15 | } 16 | 17 | @Override 18 | public Response read(ByteBuf byteBuf) { 19 | long requestId = byteBuf.readLong(); 20 | byte statusCode = byteBuf.readByte(); 21 | Object result = objectSerializer.read(byteBuf); 22 | 23 | Response response = new Response(); 24 | response.setRequestId(requestId); 25 | response.setStatusCode(statusCode); 26 | response.setResult(result); 27 | 28 | return response; 29 | } 30 | 31 | @Override 32 | public Class typeClass() { 33 | return Response.class; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/Serializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | public interface Serializer { 6 | 7 | public void write(ByteBuf byteBuf, T object); 8 | 9 | public T read(ByteBuf byteBuf); 10 | 11 | public Class typeClass(); 12 | } 13 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/StringSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.nio.charset.StandardCharsets; 4 | 5 | import io.netty.buffer.ByteBuf; 6 | 7 | public class StringSerializer implements Serializer { 8 | 9 | @Override 10 | public void write(ByteBuf byteBuf, String str) { 11 | byte[] bytes = str.getBytes(StandardCharsets.UTF_8); 12 | byteBuf.writeInt(bytes.length); 13 | byteBuf.writeBytes(bytes); 14 | } 15 | 16 | @Override 17 | public String read(ByteBuf byteBuf) { 18 | int length = byteBuf.readInt(); 19 | byte[] bytes = new byte[length]; 20 | byteBuf.readBytes(bytes); 21 | 22 | return new String(bytes, StandardCharsets.UTF_8); 23 | } 24 | 25 | @Override 26 | public Class typeClass() { 27 | return String.class; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/UserPageSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.RandomAccess; 6 | 7 | import benchmark.bean.Page; 8 | import benchmark.bean.User; 9 | import io.netty.buffer.ByteBuf; 10 | 11 | public class UserPageSerializer implements Serializer> { 12 | 13 | private final UserSerializer userSerializer = new UserSerializer(); 14 | 15 | @Override 16 | public void write(ByteBuf byteBuf, Page userPage) { 17 | List userList = userPage.getResult(); 18 | 19 | byteBuf.writeInt(userPage.getPageNo()); 20 | byteBuf.writeInt(userPage.getTotal()); 21 | byteBuf.writeInt(userList.size()); 22 | 23 | if (userList instanceof RandomAccess) { 24 | for (int i = 0; i < userList.size(); i++) { 25 | userSerializer.write(byteBuf, userList.get(i)); 26 | } 27 | } else { 28 | for (User user : userList) { 29 | userSerializer.write(byteBuf, user); 30 | } 31 | } 32 | } 33 | 34 | @Override 35 | public Page read(ByteBuf byteBuf) { 36 | int pageNo = byteBuf.readInt(); 37 | int total = byteBuf.readInt(); 38 | int size = byteBuf.readInt(); 39 | 40 | List userList = new ArrayList<>(size); 41 | for (int i = 0; i < size; i++) { 42 | userList.add(userSerializer.read(byteBuf)); 43 | } 44 | 45 | Page userPage = new Page<>(); 46 | userPage.setPageNo(pageNo); 47 | userPage.setTotal(total); 48 | userPage.setResult(userList); 49 | 50 | return userPage; 51 | } 52 | 53 | @Override 54 | @SuppressWarnings("rawtypes") 55 | public Class typeClass() { 56 | return Page.class; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/serializer/VoidSerializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.serializer; 2 | 3 | import io.netty.buffer.ByteBuf; 4 | 5 | public class VoidSerializer implements Serializer { 6 | 7 | @Override 8 | public void write(ByteBuf byteBuf, Void value) { 9 | } 10 | 11 | @Override 12 | public Void read(ByteBuf byteBuf) { 13 | return null; 14 | } 15 | 16 | @Override 17 | public Class typeClass() { 18 | return Void.class; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/server/codec/ProtocolDecoder.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.server.codec; 2 | 3 | import benchmark.rpc.netty.serializer.FastestSerializer; 4 | import io.netty.buffer.ByteBuf; 5 | import io.netty.channel.ChannelHandlerContext; 6 | import io.netty.handler.codec.LengthFieldBasedFrameDecoder; 7 | 8 | public class ProtocolDecoder extends LengthFieldBasedFrameDecoder { 9 | 10 | private static final int HEADER_FIELD_LENGTH = 4; 11 | 12 | public ProtocolDecoder(int maxFrameLength) { 13 | super(maxFrameLength, 0, HEADER_FIELD_LENGTH, 0, HEADER_FIELD_LENGTH); 14 | } 15 | 16 | @Override 17 | protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { 18 | ByteBuf buffer = (ByteBuf) super.decode(ctx, in); 19 | 20 | if (buffer != null) { 21 | 22 | try { 23 | return FastestSerializer.readRequest(buffer); 24 | } finally { 25 | buffer.release(); 26 | } 27 | } 28 | 29 | return null; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/server/codec/ProtocolEncoder.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.server.codec; 2 | 3 | import benchmark.rpc.netty.serializer.FastestSerializer; 4 | import benchmark.rpc.protocol.Response; 5 | import io.netty.buffer.ByteBuf; 6 | import io.netty.channel.ChannelHandlerContext; 7 | import io.netty.handler.codec.MessageToByteEncoder; 8 | 9 | public class ProtocolEncoder extends MessageToByteEncoder { 10 | 11 | protected void encode(ChannelHandlerContext ctx, Response response, ByteBuf buffer) throws Exception { 12 | FastestSerializer.writeResponse(buffer, response); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/server/handler/BenchmarkChannelInitializer.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.server.handler; 2 | 3 | import benchmark.rpc.netty.server.codec.ProtocolDecoder; 4 | import benchmark.rpc.netty.server.codec.ProtocolEncoder; 5 | import io.netty.channel.ChannelInitializer; 6 | import io.netty.channel.socket.SocketChannel; 7 | 8 | public class BenchmarkChannelInitializer extends ChannelInitializer { 9 | 10 | public static final int MAX_FRAME_LENGTH = 8 * 1024 * 1024; 11 | 12 | @Override 13 | public void initChannel(SocketChannel ch) throws Exception { 14 | ch.pipeline()// 15 | .addLast("encoder", new ProtocolEncoder())// 16 | .addLast("decoder", new ProtocolDecoder(MAX_FRAME_LENGTH))// 17 | .addLast("handler", new BenchmarkServerHandler()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /netty-server/src/main/java/benchmark/rpc/netty/server/handler/BenchmarkServerHandler.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.netty.server.handler; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import benchmark.rpc.protocol.Request; 7 | import benchmark.rpc.protocol.Response; 8 | import benchmark.rpc.route.RouteService; 9 | import io.netty.channel.ChannelHandlerContext; 10 | import io.netty.channel.SimpleChannelInboundHandler; 11 | 12 | public class BenchmarkServerHandler extends SimpleChannelInboundHandler { 13 | private static final Logger LOGGER = LoggerFactory.getLogger(BenchmarkServerHandler.class); 14 | 15 | private final RouteService routeService = new RouteService(); 16 | 17 | protected void channelRead0(ChannelHandlerContext ctx, Request msg) throws Exception { 18 | final long requestId = msg.getRequestId(); 19 | final int serviceId = msg.getServiceId(); 20 | final Object[] params = msg.getParams(); 21 | 22 | Object result = routeService.invoke(serviceId, params); 23 | 24 | Response response = new Response(); 25 | 26 | response.setRequestId(requestId); 27 | response.setStatusCode((byte) 1); 28 | response.setResult(result); 29 | 30 | ctx.writeAndFlush(response, ctx.voidPromise()); 31 | } 32 | 33 | 34 | 35 | @Override 36 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 37 | LOGGER.error("Exception caught on {}, ", ctx.channel(), cause); 38 | ctx.channel().close(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /rapidoid-client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | benchmark.rpc 6 | rapidoid-client 7 | round-5 8 | jar 9 | 10 | rapidoid-client 11 | http://maven.apache.org 12 | 13 | 14 | 11 15 | 11 16 | UTF-8 17 | round-5 18 | 19 | 20 | 21 | 22 | benchmark.rpc 23 | benchmark-base 24 | ${version.benchmark-base} 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.12 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 3.8.0 43 | 44 | 45 | ${maven.compiler.source} 46 | ${maven.compiler.target} 47 | UTF-8 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-resources-plugin 53 | 3.1.0 54 | 55 | UTF-8 56 | 57 | 58 | 59 | 60 | maven-assembly-plugin 61 | 3.1.0 62 | 63 | 64 | jar-with-dependencies 65 | 66 | 67 | 68 | benchmark.rpc.Client 69 | 70 | 71 | 72 | 73 | 74 | make-assembly 75 | package 76 | 77 | single 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | src/main/resources 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /rapidoid-client/src/main/java/benchmark/rpc/Client.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import org.openjdk.jmh.annotations.Benchmark; 6 | import org.openjdk.jmh.annotations.BenchmarkMode; 7 | import org.openjdk.jmh.annotations.Mode; 8 | import org.openjdk.jmh.annotations.OutputTimeUnit; 9 | import org.openjdk.jmh.annotations.Scope; 10 | import org.openjdk.jmh.annotations.State; 11 | import org.openjdk.jmh.runner.Runner; 12 | import org.openjdk.jmh.runner.RunnerException; 13 | import org.openjdk.jmh.runner.options.Options; 14 | import org.openjdk.jmh.runner.options.OptionsBuilder; 15 | import org.openjdk.jmh.runner.options.TimeValue; 16 | 17 | import benchmark.bean.Page; 18 | import benchmark.bean.User; 19 | import benchmark.service.UserService; 20 | import benchmark.service.UserServiceJsonHttpClientImpl; 21 | 22 | @State(Scope.Benchmark) 23 | public class Client extends AbstractClient { 24 | public static final int CONCURRENCY = 32; 25 | 26 | private final UserService userService = new UserServiceJsonHttpClientImpl(CONCURRENCY); 27 | 28 | @Override 29 | protected UserService getUserService() { 30 | return userService; 31 | } 32 | 33 | @Benchmark 34 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 35 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 36 | @Override 37 | public boolean existUser() throws Exception { 38 | return super.existUser(); 39 | } 40 | 41 | @Benchmark 42 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 43 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 44 | @Override 45 | public boolean createUser() throws Exception { 46 | return super.createUser(); 47 | } 48 | 49 | @Benchmark 50 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 51 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 52 | @Override 53 | public User getUser() throws Exception { 54 | return super.getUser(); 55 | } 56 | 57 | @Benchmark 58 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 59 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 60 | @Override 61 | public Page listUser() throws Exception { 62 | return super.listUser(); 63 | } 64 | 65 | public static void main(String[] args) throws RunnerException { 66 | Options opt = new OptionsBuilder()// 67 | .include(Client.class.getSimpleName())// 68 | .warmupIterations(3)// 69 | .warmupTime(TimeValue.seconds(10))// 70 | .measurementIterations(3)// 71 | .measurementTime(TimeValue.seconds(10))// 72 | .threads(CONCURRENCY)// 73 | .forks(1)// 74 | .build(); 75 | 76 | new Runner(opt).run(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /rapidoid-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /rapidoid-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import org.rapidoid.config.Conf; 4 | import org.rapidoid.setup.App; 5 | import org.rapidoid.setup.My; 6 | import org.rapidoid.setup.On; 7 | 8 | import benchmark.rpc.util.JsonUtils; 9 | 10 | public class Server { 11 | 12 | public static final String host = "benchmark-server"; 13 | public static final int port = 8080; 14 | 15 | public static void main(String[] args) { 16 | 17 | App.bootstrap(args); 18 | 19 | My.custom().objectMapper(JsonUtils.objectMapper); 20 | 21 | Conf.HTTP.set("maxPipeline", 256); 22 | Conf.HTTP.set("timeout", 0); 23 | Conf.HTTP.sub("mandatoryHeaders").set("connection", false); 24 | 25 | On.port(port); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /rapidoid-server/src/main/java/benchmark/rpc/rapidoid/server/CreateUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.rapidoid.server; 2 | 3 | import org.rapidoid.annotation.Controller; 4 | import org.rapidoid.annotation.GET; 5 | 6 | import benchmark.bean.User; 7 | import benchmark.service.UserService; 8 | import benchmark.service.UserServiceServerImpl; 9 | 10 | @Controller 11 | public class CreateUserController { 12 | private final UserService userService = new UserServiceServerImpl(); 13 | 14 | @GET("create-user") 15 | public boolean createUser(User user) { 16 | return userService.createUser(user); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /rapidoid-server/src/main/java/benchmark/rpc/rapidoid/server/GetUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.rapidoid.server; 2 | 3 | import org.rapidoid.annotation.Controller; 4 | import org.rapidoid.annotation.GET; 5 | 6 | import benchmark.bean.User; 7 | import benchmark.service.UserService; 8 | import benchmark.service.UserServiceServerImpl; 9 | 10 | @Controller 11 | public class GetUserController { 12 | private final UserService userService = new UserServiceServerImpl(); 13 | 14 | @GET("get-user") 15 | public User getUser(int id) { 16 | User user = userService.getUser(id); 17 | return user; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /rapidoid-server/src/main/java/benchmark/rpc/rapidoid/server/ListUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.rapidoid.server; 2 | 3 | import org.rapidoid.annotation.Controller; 4 | import org.rapidoid.annotation.GET; 5 | 6 | import benchmark.bean.Page; 7 | import benchmark.bean.User; 8 | import benchmark.service.UserService; 9 | import benchmark.service.UserServiceServerImpl; 10 | 11 | @Controller 12 | public class ListUserController { 13 | private final UserService userService = new UserServiceServerImpl(); 14 | 15 | @GET("list-user") 16 | public Page getUser(int pageNo) { 17 | Page userList = userService.listUser(pageNo); 18 | return userList; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /rapidoid-server/src/main/java/benchmark/rpc/rapidoid/server/UserExistController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.rapidoid.server; 2 | 3 | import org.rapidoid.annotation.Controller; 4 | import org.rapidoid.annotation.GET; 5 | 6 | import benchmark.service.UserService; 7 | import benchmark.service.UserServiceServerImpl; 8 | 9 | @Controller 10 | public class UserExistController { 11 | 12 | private final UserService userService = new UserServiceServerImpl(); 13 | 14 | @GET("user-exist") 15 | public boolean emailExist(String email) { 16 | return userService.existUser(email); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /rpcBenchmark.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hank-whu/rpc-benchmark/1fd6fda3aae72aca8297998866925cc15b2d9632/rpcBenchmark.jpg -------------------------------------------------------------------------------- /rsocket-client/src/main/proto/UserService.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package grpc; 3 | 4 | import public "google/protobuf/timestamp.proto"; 5 | 6 | option java_package = "benchmark.rpc.grpc"; 7 | 8 | service UserService { 9 | rpc userExist (UserExistRequest) returns (UserExistResponse) {} 10 | 11 | rpc createUser (User) returns (CreateUserResponse) {} 12 | 13 | rpc getUser (GetUserRequest) returns (User) {} 14 | 15 | rpc listUser (ListUserRequest) returns (UserPage) {} 16 | } 17 | 18 | message UserExistRequest { 19 | string email = 1; 20 | } 21 | 22 | message UserExistResponse { 23 | bool exist = 1; 24 | } 25 | 26 | message GetUserRequest { 27 | int64 id = 1; 28 | } 29 | 30 | message User { 31 | int64 id = 1; 32 | string name = 2; 33 | int32 sex = 3; 34 | int32 birthday = 4; 35 | string email = 5; 36 | string mobile = 6; 37 | string address = 7; 38 | string icon = 8; 39 | repeated int32 permissions = 9; 40 | int32 status = 10; 41 | int64 createTime = 11; 42 | int64 updateTime = 12; 43 | } 44 | 45 | message CreateUserResponse { 46 | bool success = 1; 47 | } 48 | 49 | message ListUserRequest { 50 | int32 pageNo = 1; 51 | } 52 | 53 | message UserPage { 54 | int32 pageNo = 1; 55 | int32 total = 2; 56 | repeated User result = 3; 57 | } -------------------------------------------------------------------------------- /rsocket-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import benchmark.rpc.grpc.UserServiceServer; 4 | import benchmark.rpc.rsocket.server.UserServiceRsocketServerImpl; 5 | import io.rsocket.RSocketFactory; 6 | import io.rsocket.rpc.rsocket.RequestHandlingRSocket; 7 | import io.rsocket.transport.netty.server.CloseableChannel; 8 | import io.rsocket.transport.netty.server.TcpServerTransport; 9 | import reactor.core.publisher.Mono; 10 | 11 | import java.util.Optional; 12 | 13 | public class Server { 14 | 15 | public static void main(String[] args){ 16 | UserServiceServer userServiceServer = new UserServiceServer(new UserServiceRsocketServerImpl(), Optional.empty(), Optional.empty()); 17 | CloseableChannel closeableChannel = 18 | RSocketFactory.receive() 19 | .acceptor( 20 | (setup, sendingSocket) -> Mono.just(new RequestHandlingRSocket(userServiceServer))) 21 | .transport(TcpServerTransport.create(8080)) 22 | .start() 23 | .block(); 24 | 25 | // Block so we don't exit 26 | closeableChannel.onClose().block(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /rsocket-server/src/main/proto/UserService.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package grpc; 3 | 4 | import public "google/protobuf/timestamp.proto"; 5 | 6 | option java_package = "benchmark.rpc.grpc"; 7 | 8 | service UserService { 9 | rpc userExist (UserExistRequest) returns (UserExistResponse) {} 10 | 11 | rpc createUser (User) returns (CreateUserResponse) {} 12 | 13 | rpc getUser (GetUserRequest) returns (User) {} 14 | 15 | rpc listUser (ListUserRequest) returns (UserPage) {} 16 | } 17 | 18 | message UserExistRequest { 19 | string email = 1; 20 | } 21 | 22 | message UserExistResponse { 23 | bool exist = 1; 24 | } 25 | 26 | message GetUserRequest { 27 | int64 id = 1; 28 | } 29 | 30 | message User { 31 | int64 id = 1; 32 | string name = 2; 33 | int32 sex = 3; 34 | int32 birthday = 4; 35 | string email = 5; 36 | string mobile = 6; 37 | string address = 7; 38 | string icon = 8; 39 | repeated int32 permissions = 9; 40 | int32 status = 10; 41 | int64 createTime = 11; 42 | int64 updateTime = 12; 43 | } 44 | 45 | message CreateUserResponse { 46 | bool success = 1; 47 | } 48 | 49 | message ListUserRequest { 50 | int32 pageNo = 1; 51 | } 52 | 53 | message UserPage { 54 | int32 pageNo = 1; 55 | int32 total = 2; 56 | repeated User result = 3; 57 | } -------------------------------------------------------------------------------- /servicecomb-client/src/main/resources/META-INF/spring/benchmark.consumer.bean.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /servicecomb-client/src/main/resources/config/log4j.demo.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Licensed to the Apache Software Foundation (ASF) under one or more 3 | # contributor license agreements. See the NOTICE file distributed with 4 | # this work for additional information regarding copyright ownership. 5 | # The ASF licenses this file to You under the Apache License, Version 2.0 6 | # (the "License"); you may not use this file except in compliance with 7 | # the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | paas.logs.dir=target/logs/ 19 | paas.logs.file=benchmark.log 20 | 21 | log4j.rootLogger=WARN,paas,stdout 22 | -------------------------------------------------------------------------------- /servicecomb-client/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Licensed to the Apache Software Foundation (ASF) under one or more 3 | # contributor license agreements. See the NOTICE file distributed with 4 | # this work for additional information regarding copyright ownership. 5 | # The ASF licenses this file to You under the Apache License, Version 2.0 6 | # (the "License"); you may not use this file except in compliance with 7 | # the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | paas.logs.dir=target/logs/ 19 | paas.logs.file=benchmark.log 20 | 21 | log4j.rootLogger=WARN,paas,stdout 22 | -------------------------------------------------------------------------------- /servicecomb-client/src/main/resources/microservice.yaml: -------------------------------------------------------------------------------- 1 | ## --------------------------------------------------------------------------- 2 | ## Licensed to the Apache Software Foundation (ASF) under one or more 3 | ## contributor license agreements. See the NOTICE file distributed with 4 | ## this work for additional information regarding copyright ownership. 5 | ## The ASF licenses this file to You under the Apache License, Version 2.0 6 | ## (the "License"); you may not use this file except in compliance with 7 | ## the License. You may obtain a copy of the License at 8 | ## 9 | ## http://www.apache.org/licenses/LICENSE-2.0 10 | ## 11 | ## Unless required by applicable law or agreed to in writing, software 12 | ## distributed under the License is distributed on an "AS IS" BASIS, 13 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | ## See the License for the specific language governing permissions and 15 | ## limitations under the License. 16 | ## --------------------------------------------------------------------------- 17 | 18 | APPLICATION_ID: rpc-benchmark 19 | service_description: 20 | name: benchmarkClient 21 | version: 0.0.1 22 | servicecomb: 23 | service: 24 | registry: 25 | address: http://benchmark-server:30100 26 | highway: 27 | client: 28 | thread-count: 2 29 | isolation: 30 | Consumer: 31 | enabled: false 32 | references: 33 | benchmark: 34 | version-rule: 0.0.1 35 | -------------------------------------------------------------------------------- /servicecomb-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import org.apache.servicecomb.foundation.common.utils.BeanUtils; 4 | import org.apache.servicecomb.foundation.common.utils.Log4jUtils; 5 | 6 | import io.netty.util.concurrent.GlobalEventExecutor; 7 | 8 | public class Server { 9 | 10 | public static void main(String[] args) throws Exception { 11 | Object exec = GlobalEventExecutor.INSTANCE; 12 | System.out.println(exec); 13 | 14 | Log4jUtils.init(); 15 | BeanUtils.init(); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /servicecomb-server/src/main/resources/META-INF/spring/benchmark.provider.bean.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /servicecomb-server/src/main/resources/config/log4j.demo.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Licensed to the Apache Software Foundation (ASF) under one or more 3 | # contributor license agreements. See the NOTICE file distributed with 4 | # this work for additional information regarding copyright ownership. 5 | # The ASF licenses this file to You under the Apache License, Version 2.0 6 | # (the "License"); you may not use this file except in compliance with 7 | # the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | paas.logs.dir=target/logs/ 19 | paas.logs.file=benchmark.log 20 | 21 | log4j.rootLogger=WARN,paas,stdout 22 | -------------------------------------------------------------------------------- /servicecomb-server/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Licensed to the Apache Software Foundation (ASF) under one or more 3 | # contributor license agreements. See the NOTICE file distributed with 4 | # this work for additional information regarding copyright ownership. 5 | # The ASF licenses this file to You under the Apache License, Version 2.0 6 | # (the "License"); you may not use this file except in compliance with 7 | # the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | paas.logs.dir=target/logs/ 19 | paas.logs.file=benchmark.log 20 | 21 | log4j.rootLogger=WARN,paas,stdout 22 | -------------------------------------------------------------------------------- /servicecomb-server/src/main/resources/microservice.yaml: -------------------------------------------------------------------------------- 1 | ## --------------------------------------------------------------------------- 2 | ## Licensed to the Apache Software Foundation (ASF) under one or more 3 | ## contributor license agreements. See the NOTICE file distributed with 4 | ## this work for additional information regarding copyright ownership. 5 | ## The ASF licenses this file to You under the Apache License, Version 2.0 6 | ## (the "License"); you may not use this file except in compliance with 7 | ## the License. You may obtain a copy of the License at 8 | ## 9 | ## http://www.apache.org/licenses/LICENSE-2.0 10 | ## 11 | ## Unless required by applicable law or agreed to in writing, software 12 | ## distributed under the License is distributed on an "AS IS" BASIS, 13 | ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | ## See the License for the specific language governing permissions and 15 | ## limitations under the License. 16 | ## --------------------------------------------------------------------------- 17 | 18 | APPLICATION_ID: rpc-benchmark 19 | service_description: 20 | name: benchmark 21 | version: 0.0.1 22 | servicecomb: 23 | service: 24 | registry: 25 | address: http://benchmark-server:30100 26 | highway: 27 | address: benchmark-server:8080 28 | server: 29 | thread-count: 2 30 | -------------------------------------------------------------------------------- /sofa-client/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /sofa-server/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /sofa-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import benchmark.service.UserService; 4 | import benchmark.service.UserServiceServerImpl; 5 | import com.alipay.sofa.rpc.config.ProviderConfig; 6 | import com.alipay.sofa.rpc.config.ServerConfig; 7 | 8 | public class Server { 9 | 10 | public static void main(String[] args) { 11 | ServerConfig serverConfig = new ServerConfig() 12 | .setProtocol("bolt") 13 | .setPort(12200) 14 | .setDaemon(false); 15 | 16 | ProviderConfig providerConfig = new ProviderConfig() 17 | .setInterfaceId(UserService.class.getName()) 18 | .setRef(new UserServiceServerImpl()) 19 | .setServer(serverConfig); 20 | 21 | providerConfig.export(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /sofa-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /springboot-client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | benchmark.rpc 6 | springboot-client 7 | round-5 8 | jar 9 | 10 | springboot-client 11 | http://maven.apache.org 12 | 13 | 14 | 11 15 | 11 16 | UTF-8 17 | round-5 18 | 19 | 20 | 21 | 22 | benchmark.rpc 23 | benchmark-base 24 | ${version.benchmark-base} 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.12 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 3.8.0 43 | 44 | 45 | ${maven.compiler.source} 46 | ${maven.compiler.target} 47 | UTF-8 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-resources-plugin 53 | 3.1.0 54 | 55 | UTF-8 56 | 57 | 58 | 59 | 60 | maven-assembly-plugin 61 | 3.1.0 62 | 63 | 64 | jar-with-dependencies 65 | 66 | 67 | 68 | benchmark.rpc.Client 69 | 70 | 71 | 72 | 73 | 74 | make-assembly 75 | package 76 | 77 | single 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | src/main/resources 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /springboot-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /springboot-server/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | benchmark.rpc 6 | springboot-server 7 | round-5 8 | jar 9 | 10 | springboot-server 11 | http://maven.apache.org 12 | 13 | 14 | 11 15 | 11 16 | UTF-8 17 | round-5 18 | 2.1.2.RELEASE 19 | 20 | 21 | 22 | 23 | benchmark.rpc 24 | benchmark-base 25 | ${version.benchmark-base} 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-web 30 | ${version.spring-boot} 31 | 32 | 33 | 34 | junit 35 | junit 36 | 4.12 37 | test 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.apache.maven.plugins 47 | maven-compiler-plugin 48 | 3.8.0 49 | 50 | 51 | ${maven.compiler.source} 52 | ${maven.compiler.target} 53 | UTF-8 54 | 55 | 56 | 57 | org.apache.maven.plugins 58 | maven-resources-plugin 59 | 3.1.0 60 | 61 | UTF-8 62 | 63 | 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-maven-plugin 68 | 69 | true 70 | benchmark.rpc.Server 71 | 72 | 73 | 74 | 75 | repackage 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /springboot-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = { "benchmark.rpc.springboot" }) 7 | public class Server { 8 | public static void main(String[] args) { 9 | SpringApplication.run(Server.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /springboot-server/src/main/java/benchmark/rpc/springboot/server/CreateUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.springboot.server; 2 | 3 | import org.springframework.web.bind.annotation.PostMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.bean.User; 7 | import benchmark.service.UserService; 8 | import benchmark.service.UserServiceServerImpl; 9 | 10 | @RestController 11 | public class CreateUserController { 12 | private final UserService userService = new UserServiceServerImpl(); 13 | 14 | @PostMapping("create-user") 15 | public boolean createUser(User user) { 16 | return userService.createUser(user); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /springboot-server/src/main/java/benchmark/rpc/springboot/server/GetUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.springboot.server; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.bean.User; 7 | import benchmark.service.UserService; 8 | import benchmark.service.UserServiceServerImpl; 9 | 10 | @RestController 11 | public class GetUserController { 12 | private final UserService userService = new UserServiceServerImpl(); 13 | 14 | @GetMapping("get-user") 15 | public User getUser(int id) { 16 | User user = userService.getUser(id); 17 | return user; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /springboot-server/src/main/java/benchmark/rpc/springboot/server/ListUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.springboot.server; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.bean.Page; 7 | import benchmark.bean.User; 8 | import benchmark.service.UserService; 9 | import benchmark.service.UserServiceServerImpl; 10 | 11 | @RestController 12 | public class ListUserController { 13 | private final UserService userService = new UserServiceServerImpl(); 14 | 15 | @GetMapping("list-user") 16 | public Page getUser(int pageNo) { 17 | Page userList = userService.listUser(pageNo); 18 | return userList; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /springboot-server/src/main/java/benchmark/rpc/springboot/server/UserExistController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.springboot.server; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.service.UserService; 7 | import benchmark.service.UserServiceServerImpl; 8 | 9 | @RestController 10 | public class UserExistController { 11 | 12 | private final UserService userService = new UserServiceServerImpl(); 13 | 14 | @GetMapping("user-exist") 15 | public boolean emailExist(String email) { 16 | return userService.existUser(email); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /springboot-server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | address: benchmark-server 3 | port: 8080 -------------------------------------------------------------------------------- /springboot-undertow-client/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /springboot-undertow-client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | benchmark.rpc 6 | springboot-undertow-client 7 | round-5 8 | jar 9 | 10 | springboot-undertow-client 11 | http://maven.apache.org 12 | 13 | 14 | 11 15 | 11 16 | UTF-8 17 | round-5 18 | 19 | 20 | 21 | 22 | benchmark.rpc 23 | benchmark-base 24 | ${version.benchmark-base} 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.12 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 3.8.0 43 | 44 | 45 | ${maven.compiler.source} 46 | ${maven.compiler.target} 47 | UTF-8 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-resources-plugin 53 | 3.1.0 54 | 55 | UTF-8 56 | 57 | 58 | 59 | 60 | maven-assembly-plugin 61 | 3.1.0 62 | 63 | 64 | jar-with-dependencies 65 | 66 | 67 | 68 | benchmark.rpc.Client 69 | 70 | 71 | 72 | 73 | 74 | make-assembly 75 | package 76 | 77 | single 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | src/main/resources 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /springboot-undertow-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /springboot-undertow-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication(scanBasePackages = { "benchmark.rpc.springboot" }) 7 | public class Server { 8 | public static void main(String[] args) { 9 | SpringApplication.run(Server.class, args); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /springboot-undertow-server/src/main/java/benchmark/rpc/springboot/server/CreateUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.springboot.server; 2 | 3 | import org.springframework.web.bind.annotation.PostMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.bean.User; 7 | import benchmark.service.UserService; 8 | import benchmark.service.UserServiceServerImpl; 9 | 10 | @RestController 11 | public class CreateUserController { 12 | private final UserService userService = new UserServiceServerImpl(); 13 | 14 | @PostMapping("create-user") 15 | public boolean createUser(User user) { 16 | return userService.createUser(user); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /springboot-undertow-server/src/main/java/benchmark/rpc/springboot/server/GetUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.springboot.server; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.bean.User; 7 | import benchmark.service.UserService; 8 | import benchmark.service.UserServiceServerImpl; 9 | 10 | @RestController 11 | public class GetUserController { 12 | private final UserService userService = new UserServiceServerImpl(); 13 | 14 | @GetMapping("get-user") 15 | public User getUser(int id) { 16 | User user = userService.getUser(id); 17 | return user; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /springboot-undertow-server/src/main/java/benchmark/rpc/springboot/server/ListUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.springboot.server; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.bean.Page; 7 | import benchmark.bean.User; 8 | import benchmark.service.UserService; 9 | import benchmark.service.UserServiceServerImpl; 10 | 11 | @RestController 12 | public class ListUserController { 13 | private final UserService userService = new UserServiceServerImpl(); 14 | 15 | @GetMapping("list-user") 16 | public Page getUser(int pageNo) { 17 | Page userList = userService.listUser(pageNo); 18 | return userList; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /springboot-undertow-server/src/main/java/benchmark/rpc/springboot/server/UserExistController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.springboot.server; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.service.UserService; 7 | import benchmark.service.UserServiceServerImpl; 8 | 9 | @RestController 10 | public class UserExistController { 11 | 12 | private final UserService userService = new UserServiceServerImpl(); 13 | 14 | @GetMapping("user-exist") 15 | public boolean emailExist(String email) { 16 | return userService.existUser(email); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /springboot-undertow-server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | address: benchmark-server 3 | port: 8080 -------------------------------------------------------------------------------- /springwebflux-client/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /springwebflux-client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | benchmark.rpc 6 | springwebflux-client 7 | round-5 8 | jar 9 | 10 | springwebflux-client 11 | http://maven.apache.org 12 | 13 | 14 | 11 15 | 11 16 | UTF-8 17 | round-5 18 | 19 | 20 | 21 | 22 | benchmark.rpc 23 | benchmark-base 24 | ${version.benchmark-base} 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.12 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 3.8.0 43 | 44 | 45 | ${maven.compiler.source} 46 | ${maven.compiler.target} 47 | UTF-8 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-resources-plugin 53 | 3.1.0 54 | 55 | UTF-8 56 | 57 | 58 | 59 | 60 | maven-assembly-plugin 61 | 3.1.0 62 | 63 | 64 | jar-with-dependencies 65 | 66 | 67 | 68 | benchmark.rpc.Client 69 | 70 | 71 | 72 | 73 | 74 | make-assembly 75 | package 76 | 77 | single 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | src/main/resources 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /springwebflux-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /springwebflux-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Primary; 7 | import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; 8 | import org.springframework.web.reactive.config.EnableWebFlux; 9 | 10 | import com.fasterxml.jackson.databind.ObjectMapper; 11 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; 12 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 13 | import com.fasterxml.jackson.module.afterburner.AfterburnerModule; 14 | 15 | @SpringBootApplication(scanBasePackages = { "benchmark.rpc.webflux" }) 16 | @EnableWebFlux 17 | public class Server { 18 | public static void main(String[] args) { 19 | SpringApplication.run(Server.class, args); 20 | } 21 | 22 | @Bean 23 | @Primary 24 | public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { 25 | ObjectMapper objectMapper = builder.createXmlMapper(false).build(); 26 | 27 | objectMapper.registerModule(new Jdk8Module()); 28 | objectMapper.registerModule(new JavaTimeModule()); 29 | objectMapper.registerModule(new AfterburnerModule()); 30 | 31 | return objectMapper; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /springwebflux-server/src/main/java/benchmark/rpc/webflux/server/CreateUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.webflux.server; 2 | 3 | import org.springframework.web.bind.annotation.PostMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.bean.User; 7 | import benchmark.service.UserService; 8 | import benchmark.service.UserServiceServerImpl; 9 | import reactor.core.publisher.Mono; 10 | 11 | @RestController 12 | public class CreateUserController { 13 | private final UserService userService = new UserServiceServerImpl(); 14 | 15 | @PostMapping("create-user") 16 | public Mono createUser(User user) { 17 | return Mono.fromCallable(() -> userService.createUser(user)); 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /springwebflux-server/src/main/java/benchmark/rpc/webflux/server/GetUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.webflux.server; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.bean.User; 7 | import benchmark.service.UserService; 8 | import benchmark.service.UserServiceServerImpl; 9 | import reactor.core.publisher.Mono; 10 | 11 | @RestController 12 | public class GetUserController { 13 | private final UserService userService = new UserServiceServerImpl(); 14 | 15 | @GetMapping("get-user") 16 | public Mono getUser(int id) { 17 | return Mono.fromCallable(() -> userService.getUser(id)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /springwebflux-server/src/main/java/benchmark/rpc/webflux/server/ListUserController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.webflux.server; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.bean.Page; 7 | import benchmark.bean.User; 8 | import benchmark.service.UserService; 9 | import benchmark.service.UserServiceServerImpl; 10 | import reactor.core.publisher.Mono; 11 | 12 | @RestController 13 | public class ListUserController { 14 | private final UserService userService = new UserServiceServerImpl(); 15 | 16 | @GetMapping("list-user") 17 | public Mono> getUser(int pageNo) { 18 | return Mono.fromCallable(() -> userService.listUser(pageNo)); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /springwebflux-server/src/main/java/benchmark/rpc/webflux/server/UserExistController.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.webflux.server; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | import benchmark.service.UserService; 7 | import benchmark.service.UserServiceServerImpl; 8 | import reactor.core.publisher.Mono; 9 | 10 | @RestController 11 | public class UserExistController { 12 | 13 | private final UserService userService = new UserServiceServerImpl(); 14 | 15 | @GetMapping("user-exist") 16 | public Mono emailExist(String email) { 17 | return Mono.fromCallable(() -> userService.existUser(email)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /springwebflux-server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | address: benchmark-server 3 | port: 8080 -------------------------------------------------------------------------------- /thrift-client/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /thrift-client/src/main/java/benchmark/rpc/thrift/Converter.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.thrift; 2 | 3 | import java.time.LocalDate; 4 | import java.time.LocalDateTime; 5 | import java.time.ZoneOffset; 6 | 7 | public class Converter { 8 | 9 | public static benchmark.rpc.thrift.User toThrift(benchmark.bean.User user) { 10 | benchmark.rpc.thrift.User thriftUser = new benchmark.rpc.thrift.User(); 11 | 12 | thriftUser.setId(user.getId()); 13 | thriftUser.setName(user.getName()); 14 | thriftUser.setSex(user.getSex()); 15 | thriftUser.setBirthday((int) (user.getBirthday().toEpochDay())); 16 | thriftUser.setEmail(user.getEmail()); 17 | thriftUser.setMobile(user.getMobile()); 18 | thriftUser.setAddress(user.getAddress()); 19 | thriftUser.setIcon(user.getIcon()); 20 | thriftUser.setPermissions(user.getPermissions()); 21 | thriftUser.setStatus(user.getStatus()); 22 | thriftUser.setCreateTime(user.getCreateTime().toEpochSecond(ZoneOffset.UTC)); 23 | thriftUser.setUpdateTime(user.getUpdateTime().toEpochSecond(ZoneOffset.UTC)); 24 | 25 | return thriftUser; 26 | } 27 | 28 | public static benchmark.bean.User toRaw(benchmark.rpc.thrift.User user) { 29 | benchmark.bean.User rawUser = new benchmark.bean.User(); 30 | 31 | rawUser.setId(user.getId()); 32 | rawUser.setName(user.getName()); 33 | rawUser.setSex(user.getSex()); 34 | rawUser.setBirthday(LocalDate.ofEpochDay(user.getBirthday())); 35 | rawUser.setEmail(user.getEmail()); 36 | rawUser.setMobile(user.getMobile()); 37 | rawUser.setAddress(user.getAddress()); 38 | rawUser.setIcon(user.getIcon()); 39 | rawUser.setPermissions(user.getPermissions()); 40 | rawUser.setStatus(user.getStatus()); 41 | rawUser.setCreateTime(LocalDateTime.ofEpochSecond(user.getCreateTime(), 0, ZoneOffset.UTC)); 42 | rawUser.setUpdateTime(LocalDateTime.ofEpochSecond(user.getUpdateTime(), 0, ZoneOffset.UTC)); 43 | 44 | return rawUser; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /thrift-client/src/main/java/benchmark/rpc/thrift/TServiceClientNoPrint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. The ASF licenses this file 6 | * to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package benchmark.rpc.thrift; 21 | 22 | import org.apache.thrift.TApplicationException; 23 | import org.apache.thrift.TBase; 24 | import org.apache.thrift.TException; 25 | import org.apache.thrift.protocol.TMessage; 26 | import org.apache.thrift.protocol.TMessageType; 27 | import org.apache.thrift.protocol.TProtocol; 28 | 29 | /** 30 | * A TServiceClient is used to communicate with a TService implementation across 31 | * protocols and transports. 32 | */ 33 | public abstract class TServiceClientNoPrint extends org.apache.thrift.TServiceClient { 34 | 35 | public TServiceClientNoPrint(TProtocol prot) { 36 | super(prot); 37 | } 38 | 39 | public TServiceClientNoPrint(TProtocol iprot, TProtocol oprot) { 40 | super(iprot, oprot); 41 | } 42 | 43 | @Override 44 | protected void receiveBase(TBase result, String methodName) throws TException { 45 | TMessage msg = iprot_.readMessageBegin(); 46 | if (msg.type == TMessageType.EXCEPTION) { 47 | TApplicationException x = new TApplicationException(); 48 | x.read(iprot_); 49 | iprot_.readMessageEnd(); 50 | throw x; 51 | } 52 | // System.out.format("Received %d%n", msg.seqid); 53 | if (msg.seqid != seqid_) { 54 | throw new TApplicationException(TApplicationException.BAD_SEQUENCE_ID, String.format( 55 | "%s failed: out of sequence response: expected %d but got %d", methodName, seqid_, msg.seqid)); 56 | } 57 | result.read(iprot_); 58 | iprot_.readMessageEnd(); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /thrift-client/src/main/java/benchmark/rpc/thrift/ThriftUserServiceClient.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.thrift; 2 | 3 | import java.io.Closeable; 4 | import java.io.IOException; 5 | 6 | import org.apache.thrift.protocol.TBinaryProtocol; 7 | import org.apache.thrift.protocol.TProtocol; 8 | import org.apache.thrift.transport.TFramedTransport; 9 | import org.apache.thrift.transport.TSocket; 10 | import org.apache.thrift.transport.TTransport; 11 | import org.apache.thrift.transport.TTransportException; 12 | 13 | public class ThriftUserServiceClient implements Closeable { 14 | 15 | // not thread safe 16 | public final TTransport transport; 17 | public final TProtocol protocol; 18 | public final UserService.Client client; 19 | 20 | public ThriftUserServiceClient(String host, int port) { 21 | transport = new TFramedTransport(new TSocket(host, port)); 22 | protocol = new TBinaryProtocol(transport); 23 | client = new UserService.Client(protocol); 24 | 25 | try { 26 | transport.open(); 27 | } catch (TTransportException e) { 28 | throw new Error(e); 29 | } 30 | } 31 | 32 | @Override 33 | public void close() throws IOException { 34 | transport.close(); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /thrift-client/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /thrift-client/src/main/thrift/UserService.thrift: -------------------------------------------------------------------------------- 1 | namespace java benchmark.rpc.thrift 2 | 3 | struct User { 4 | 1: i64 id, 5 | 2: string name, 6 | 3: i32 sex, 7 | 4: i32 birthday, 8 | 5: string email, 9 | 6: string mobile, 10 | 7: string address, 11 | 8: string icon, 12 | 9: list permissions, 13 | 10: i32 status, 14 | 11: i64 createTime, 15 | 12: i64 updateTime, 16 | } 17 | 18 | struct UserPage { 19 | 1: i32 pageNo, 20 | 2: i32 total, 21 | 3: list result, 22 | } 23 | 24 | service UserService { 25 | 26 | bool userExist(1: string email), 27 | 28 | bool createUser(1: User user), 29 | 30 | User getUser(1: i64 id), 31 | 32 | UserPage listUser(1: i32 pageNo) 33 | } -------------------------------------------------------------------------------- /thrift-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /thrift-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import java.net.InetSocketAddress; 4 | 5 | import org.apache.thrift.protocol.TBinaryProtocol; 6 | import org.apache.thrift.server.TServer; 7 | import org.apache.thrift.server.TThreadedSelectorServer; 8 | import org.apache.thrift.transport.TNonblockingServerSocket; 9 | import org.apache.thrift.transport.TNonblockingServerTransport; 10 | import org.apache.thrift.transport.TTransportException; 11 | 12 | import benchmark.rpc.thrift.UserService; 13 | import benchmark.rpc.thrift.UserService.Iface; 14 | import benchmark.rpc.thrift.UserServiceThriftServerImpl; 15 | 16 | public class Server { 17 | 18 | public static void main(String[] args) throws TTransportException { 19 | InetSocketAddress serverAddress = new InetSocketAddress("benchmark-server", 8080); 20 | 21 | TNonblockingServerTransport serverSocket = new TNonblockingServerSocket(serverAddress); 22 | TThreadedSelectorServer.Args serverParams = new TThreadedSelectorServer.Args(serverSocket); 23 | serverParams.protocolFactory(new TBinaryProtocol.Factory()); 24 | serverParams.processor(new UserService.Processor(new UserServiceThriftServerImpl())); 25 | TServer server = new TThreadedSelectorServer(serverParams); 26 | server.serve(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /thrift-server/src/main/java/benchmark/rpc/thrift/Converter.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.thrift; 2 | 3 | import java.time.LocalDate; 4 | import java.time.LocalDateTime; 5 | import java.time.ZoneOffset; 6 | 7 | public class Converter { 8 | 9 | public static benchmark.rpc.thrift.User toThrift(benchmark.bean.User user) { 10 | benchmark.rpc.thrift.User thriftUser = new benchmark.rpc.thrift.User(); 11 | 12 | thriftUser.setId(user.getId()); 13 | thriftUser.setName(user.getName()); 14 | thriftUser.setSex(user.getSex()); 15 | thriftUser.setBirthday((int) (user.getBirthday().toEpochDay())); 16 | thriftUser.setEmail(user.getEmail()); 17 | thriftUser.setMobile(user.getMobile()); 18 | thriftUser.setAddress(user.getAddress()); 19 | thriftUser.setIcon(user.getIcon()); 20 | thriftUser.setPermissions(user.getPermissions()); 21 | thriftUser.setStatus(user.getStatus()); 22 | thriftUser.setCreateTime(user.getCreateTime().toEpochSecond(ZoneOffset.UTC)); 23 | thriftUser.setUpdateTime(user.getUpdateTime().toEpochSecond(ZoneOffset.UTC)); 24 | 25 | return thriftUser; 26 | } 27 | 28 | public static benchmark.bean.User toRaw(benchmark.rpc.thrift.User user) { 29 | benchmark.bean.User rawUser = new benchmark.bean.User(); 30 | 31 | rawUser.setId(user.getId()); 32 | rawUser.setName(user.getName()); 33 | rawUser.setSex(user.getSex()); 34 | rawUser.setBirthday(LocalDate.ofEpochDay(user.getBirthday())); 35 | rawUser.setEmail(user.getEmail()); 36 | rawUser.setMobile(user.getMobile()); 37 | rawUser.setAddress(user.getAddress()); 38 | rawUser.setIcon(user.getIcon()); 39 | rawUser.setPermissions(user.getPermissions()); 40 | rawUser.setStatus(user.getStatus()); 41 | rawUser.setCreateTime(LocalDateTime.ofEpochSecond(user.getCreateTime(), 0, ZoneOffset.UTC)); 42 | rawUser.setUpdateTime(LocalDateTime.ofEpochSecond(user.getUpdateTime(), 0, ZoneOffset.UTC)); 43 | 44 | return rawUser; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /thrift-server/src/main/java/benchmark/rpc/thrift/UserServiceThriftServerImpl.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.thrift; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.apache.thrift.TException; 7 | 8 | import benchmark.bean.Page; 9 | import benchmark.service.UserServiceServerImpl; 10 | 11 | public class UserServiceThriftServerImpl implements UserService.Iface { 12 | 13 | private final benchmark.service.UserService userService = new UserServiceServerImpl(); 14 | 15 | @Override 16 | public boolean userExist(String email) throws TException { 17 | return userService.existUser(email); 18 | } 19 | 20 | @Override 21 | public boolean createUser(User user) throws TException { 22 | benchmark.bean.User rawUser = Converter.toRaw(user); 23 | return userService.createUser(rawUser); 24 | } 25 | 26 | @Override 27 | public User getUser(long id) throws TException { 28 | benchmark.bean.User rawUser = userService.getUser(id); 29 | User user = Converter.toThrift(rawUser); 30 | 31 | return user; 32 | } 33 | 34 | @Override 35 | public UserPage listUser(int pageNo) throws TException { 36 | Page page = userService.listUser(pageNo); 37 | 38 | List userList = new ArrayList<>(); 39 | for (benchmark.bean.User rawUser : page.getResult()) { 40 | User user = Converter.toThrift(rawUser); 41 | userList.add(user); 42 | } 43 | 44 | UserPage userPage = new UserPage(); 45 | userPage.setPageNo(page.getPageNo()); 46 | userPage.setTotal(page.getTotal()); 47 | userPage.setResult(userList); 48 | 49 | return userPage; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /thrift-server/src/main/thrift/UserService.thrift: -------------------------------------------------------------------------------- 1 | namespace java benchmark.rpc.thrift 2 | 3 | struct User { 4 | 1: i64 id, 5 | 2: string name, 6 | 3: i32 sex, 7 | 4: i32 birthday, 8 | 5: string email, 9 | 6: string mobile, 10 | 7: string address, 11 | 8: string icon, 12 | 9: list permissions, 13 | 10: i32 status, 14 | 11: i64 createTime, 15 | 12: i64 updateTime, 16 | } 17 | 18 | struct UserPage { 19 | 1: i32 pageNo, 20 | 2: i32 total, 21 | 3: list result, 22 | } 23 | 24 | service UserService { 25 | 26 | bool userExist(1: string email), 27 | 28 | bool createUser(1: User user), 29 | 30 | User getUser(1: i64 id), 31 | 32 | UserPage listUser(1: i32 pageNo) 33 | } -------------------------------------------------------------------------------- /turbo-rest-client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | benchmark.rpc 6 | turbo-rest-client 7 | round-5 8 | jar 9 | 10 | turbo-rest-client 11 | http://maven.apache.org 12 | 13 | 14 | 11 15 | 11 16 | UTF-8 17 | round-5 18 | 19 | 20 | 21 | 22 | benchmark.rpc 23 | benchmark-base 24 | ${version.benchmark-base} 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.12 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 3.8.0 43 | 44 | 45 | ${maven.compiler.source} 46 | ${maven.compiler.target} 47 | UTF-8 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-resources-plugin 53 | 3.1.0 54 | 55 | UTF-8 56 | 57 | 58 | 59 | 60 | maven-assembly-plugin 61 | 3.1.0 62 | 63 | 64 | jar-with-dependencies 65 | 66 | 67 | 68 | benchmark.rpc.Client 69 | 70 | 71 | 72 | 73 | 74 | make-assembly 75 | package 76 | 77 | single 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | src/main/resources 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /turbo-rest-client/src/main/java/benchmark/rpc/Client.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import org.openjdk.jmh.annotations.Benchmark; 6 | import org.openjdk.jmh.annotations.BenchmarkMode; 7 | import org.openjdk.jmh.annotations.Mode; 8 | import org.openjdk.jmh.annotations.OutputTimeUnit; 9 | import org.openjdk.jmh.annotations.Scope; 10 | import org.openjdk.jmh.annotations.State; 11 | import org.openjdk.jmh.runner.Runner; 12 | import org.openjdk.jmh.runner.RunnerException; 13 | import org.openjdk.jmh.runner.options.Options; 14 | import org.openjdk.jmh.runner.options.OptionsBuilder; 15 | import org.openjdk.jmh.runner.options.TimeValue; 16 | 17 | import benchmark.bean.Page; 18 | import benchmark.bean.User; 19 | import benchmark.service.TurboUserServiceJsonHttpClientImpl; 20 | import benchmark.service.UserService; 21 | 22 | @State(Scope.Benchmark) 23 | public class Client extends AbstractClient { 24 | public static final int CONCURRENCY = 32; 25 | 26 | private final UserService userService = new TurboUserServiceJsonHttpClientImpl(CONCURRENCY); 27 | 28 | @Override 29 | protected UserService getUserService() { 30 | return userService; 31 | } 32 | 33 | @Benchmark 34 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 35 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 36 | @Override 37 | public boolean existUser() throws Exception { 38 | return super.existUser(); 39 | } 40 | 41 | @Benchmark 42 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 43 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 44 | @Override 45 | public boolean createUser() throws Exception { 46 | return super.createUser(); 47 | } 48 | 49 | @Benchmark 50 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 51 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 52 | @Override 53 | public User getUser() throws Exception { 54 | return super.getUser(); 55 | } 56 | 57 | @Benchmark 58 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 59 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 60 | @Override 61 | public Page listUser() throws Exception { 62 | return super.listUser(); 63 | } 64 | 65 | public static void main(String[] args) throws RunnerException { 66 | Options opt = new OptionsBuilder()// 67 | .include(Client.class.getSimpleName())// 68 | .warmupIterations(3)// 69 | .warmupTime(TimeValue.seconds(10))// 70 | .measurementIterations(3)// 71 | .measurementTime(TimeValue.seconds(10))// 72 | .threads(CONCURRENCY)// 73 | .forks(1)// 74 | .build(); 75 | 76 | new Runner(opt).run(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /turbo-rest-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /turbo-rest-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import java.util.Map; 4 | 5 | import benchmark.rpc.service.TurboUserService; 6 | import benchmark.rpc.service.TurboUserServiceServerImpl; 7 | import io.netty.util.ResourceLeakDetector; 8 | import io.netty.util.ResourceLeakDetector.Level; 9 | import rpc.turbo.config.HostPort; 10 | import rpc.turbo.server.TurboServer; 11 | 12 | public class Server { 13 | 14 | public static void main(String[] args) throws Exception { 15 | ResourceLeakDetector.setLevel(Level.DISABLED); 16 | 17 | try (TurboServer server = new TurboServer("shop", "auth");) { 18 | Map, Object> services = Map.of(TurboUserService.class, new TurboUserServiceServerImpl()); 19 | server.registerService(services); 20 | 21 | server.startRestServer(new HostPort("benchmark-server", 8080)); 22 | server.waitUntilShutdown(); 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /turbo-rest-server/src/main/java/benchmark/rpc/service/TurboUserService.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.service; 2 | 3 | import java.util.concurrent.CompletableFuture; 4 | 5 | import benchmark.bean.Page; 6 | import benchmark.bean.User; 7 | import rpc.turbo.annotation.TurboService; 8 | 9 | @TurboService(version = "1.0.0", rest = "user") 10 | public interface TurboUserService { 11 | 12 | @TurboService(version = "2.1.2", rest = "exist") 13 | public CompletableFuture existUser(String email); 14 | 15 | @TurboService(version = "2.1.2", rest = "create") 16 | public CompletableFuture createUser(User user); 17 | 18 | @TurboService(version = "2.1.2", rest = "get") 19 | public CompletableFuture getUser(long id); 20 | 21 | @TurboService(version = "1.2.1", rest = "list") 22 | public CompletableFuture> listUser(int pageNo); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /turbo-rest-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /turbo-rpc-client/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /turbo-rpc-client/src/main/java/benchmark/rpc/service/TurboUserService.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.service; 2 | 3 | import java.util.concurrent.CompletableFuture; 4 | 5 | import benchmark.bean.Page; 6 | import benchmark.bean.User; 7 | import rpc.turbo.annotation.TurboService; 8 | 9 | @TurboService(version = "1.0.0", rest = "user") 10 | public interface TurboUserService { 11 | 12 | @TurboService(version = "2.1.2", rest = "exist") 13 | public CompletableFuture existUser(String email); 14 | 15 | @TurboService(version = "2.1.2", rest = "create") 16 | public CompletableFuture createUser(User user); 17 | 18 | @TurboService(version = "2.1.2", rest = "get") 19 | public CompletableFuture getUser(long id); 20 | 21 | @TurboService(version = "1.2.1", rest = "list") 22 | public CompletableFuture> listUser(int pageNo); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /turbo-rpc-client/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /turbo-rpc-client/src/main/resources/turbo-client.conf: -------------------------------------------------------------------------------- 1 | #https://github.com/lightbend/config 2 | 3 | apps = [ 4 | { 5 | group = "shop" 6 | app = "auth" 7 | 8 | serializer.class = "rpc.turbo.serialization.kryo.KryoSerializer" 9 | 10 | #app全局超时时间,大于0时会把所有方法的timeout都给覆盖掉,可空 11 | globalTimeout = 0 12 | 13 | maxRequestWait = 0 14 | 15 | #每个服务器几个连接,默认是1,性能不会太好 16 | connectPerServer = 4 17 | 18 | #每个服务器出错上限,达到后会把该服务器相关连接放到zombieMap里面,会有进程定期检查zombieMap,如果能连上就重新放到activeMap里面 19 | serverErrorThreshold = 16 20 | 21 | #如果设置的话必须满足connectErrorThreshold * connectonPerServer >= serverErrorThreshold,否则会导致无法进入zombieMap中 22 | #connectErrorThreshold = 16 23 | 24 | #负载均衡实现 25 | loadBalanceFactory.class = "rpc.turbo.loadbalance.RoundRobinLoadBalanceFactory" 26 | 27 | #服务发现 28 | discover { 29 | class = "rpc.turbo.discover.DirectConnectDiscover" 30 | address = ["benchmark-server:8080"] 31 | } 32 | } 33 | ] -------------------------------------------------------------------------------- /turbo-rpc-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /turbo-rpc-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | import io.netty.util.ResourceLeakDetector; 7 | import io.netty.util.ResourceLeakDetector.Level; 8 | import rpc.turbo.boot.EnableTurboServer; 9 | 10 | @SpringBootApplication(scanBasePackages = { "benchmark" }) 11 | @EnableTurboServer 12 | public class Server { 13 | 14 | public static void main(String[] args) throws Exception { 15 | ResourceLeakDetector.setLevel(Level.DISABLED); 16 | SpringApplication.run(Server.class, args); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /turbo-rpc-server/src/main/java/benchmark/rpc/service/TurboUserService.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.service; 2 | 3 | import java.util.concurrent.CompletableFuture; 4 | 5 | import benchmark.bean.Page; 6 | import benchmark.bean.User; 7 | import rpc.turbo.annotation.TurboService; 8 | 9 | @TurboService(version = "1.0.0", rest = "user") 10 | public interface TurboUserService { 11 | 12 | @TurboService(version = "2.1.2", rest = "exist") 13 | public CompletableFuture existUser(String email); 14 | 15 | @TurboService(version = "2.1.2", rest = "create") 16 | public CompletableFuture createUser(User user); 17 | 18 | @TurboService(version = "2.1.2", rest = "get") 19 | public CompletableFuture getUser(long id); 20 | 21 | @TurboService(version = "1.2.1", rest = "list") 22 | public CompletableFuture> listUser(int pageNo); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /turbo-rpc-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-client.log 15 | 16 | logs/benchmark-rpc-client.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /turbo-rpc-server/src/main/resources/turbo-server.conf: -------------------------------------------------------------------------------- 1 | group = shop 2 | app = auth 3 | 4 | #rpc序列化,默认为ProtostuffSerializer,可以修改为自己的实现 5 | #"rpc.turbo.serialization.protostuff.ProtostuffSerializer" 6 | #"rpc.turbo.serialization.kryo.KryoSerializer" 7 | serializer.class = "rpc.turbo.serialization.kryo.KryoSerializer" 8 | 9 | #http json转换,默认为JacksonMapper,可以修改为自己的实现 10 | jsonMapper.class = "rpc.turbo.serialization.jackson.JacksonMapper" 11 | 12 | registers = [ 13 | { 14 | #Register实现 15 | register.class = "rpc.turbo.registry.DirectConnectRegister" 16 | #Register地址 17 | register.address = ["benchmark-server:8086"] 18 | #发布协议,RPC或者REST 19 | server.protocol = "RPC" 20 | #对外服务地址,不同协议不能同一个端口,相同协议不允许出现port相同host不同的情况 21 | server.address = "benchmark-server:8080" 22 | #对外服务权重 23 | server.weight = 100 24 | } 25 | ] -------------------------------------------------------------------------------- /undertow-async-client/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /undertow-async-client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | benchmark.rpc 6 | undertow-async-client 7 | round-5 8 | jar 9 | 10 | undertow-async-client 11 | http://maven.apache.org 12 | 13 | 14 | 11 15 | 11 16 | UTF-8 17 | round-5 18 | 19 | 20 | 21 | 22 | benchmark.rpc 23 | benchmark-base 24 | ${version.benchmark-base} 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.12 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 3.8.0 43 | 44 | 45 | ${maven.compiler.source} 46 | ${maven.compiler.target} 47 | UTF-8 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-resources-plugin 53 | 3.1.0 54 | 55 | UTF-8 56 | 57 | 58 | 59 | 60 | maven-assembly-plugin 61 | 3.1.0 62 | 63 | 64 | jar-with-dependencies 65 | 66 | 67 | 68 | benchmark.rpc.Client 69 | 70 | 71 | 72 | 73 | 74 | make-assembly 75 | package 76 | 77 | single 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | src/main/resources 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /undertow-async-client/src/main/java/benchmark/rpc/Client.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import org.openjdk.jmh.annotations.Benchmark; 6 | import org.openjdk.jmh.annotations.BenchmarkMode; 7 | import org.openjdk.jmh.annotations.Mode; 8 | import org.openjdk.jmh.annotations.OutputTimeUnit; 9 | import org.openjdk.jmh.annotations.Scope; 10 | import org.openjdk.jmh.annotations.State; 11 | import org.openjdk.jmh.runner.Runner; 12 | import org.openjdk.jmh.runner.RunnerException; 13 | import org.openjdk.jmh.runner.options.Options; 14 | import org.openjdk.jmh.runner.options.OptionsBuilder; 15 | import org.openjdk.jmh.runner.options.TimeValue; 16 | 17 | import benchmark.bean.Page; 18 | import benchmark.bean.User; 19 | import benchmark.service.UserService; 20 | import benchmark.service.UserServiceJsonHttpClientImpl; 21 | 22 | @State(Scope.Benchmark) 23 | public class Client extends AbstractClient { 24 | public static final int CONCURRENCY = 32; 25 | 26 | private final UserService userService = new UserServiceJsonHttpClientImpl(CONCURRENCY); 27 | 28 | @Override 29 | protected UserService getUserService() { 30 | return userService; 31 | } 32 | 33 | @Benchmark 34 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 35 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 36 | @Override 37 | public boolean existUser() throws Exception { 38 | return super.existUser(); 39 | } 40 | 41 | @Benchmark 42 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 43 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 44 | @Override 45 | public boolean createUser() throws Exception { 46 | return super.createUser(); 47 | } 48 | 49 | @Benchmark 50 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 51 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 52 | @Override 53 | public User getUser() throws Exception { 54 | return super.getUser(); 55 | } 56 | 57 | @Benchmark 58 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 59 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 60 | @Override 61 | public Page listUser() throws Exception { 62 | return super.listUser(); 63 | } 64 | 65 | public static void main(String[] args) throws RunnerException { 66 | Options opt = new OptionsBuilder()// 67 | .include(Client.class.getSimpleName())// 68 | .warmupIterations(3)// 69 | .warmupTime(TimeValue.seconds(10))// 70 | .measurementIterations(3)// 71 | .measurementTime(TimeValue.seconds(10))// 72 | .threads(CONCURRENCY)// 73 | .forks(1)// 74 | .build(); 75 | 76 | new Runner(opt).run(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /undertow-async-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /undertow-async-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import benchmark.rpc.undertow.server.CreateUserHandler; 4 | import benchmark.rpc.undertow.server.UserExistHandler; 5 | import benchmark.rpc.undertow.server.GetUserHandler; 6 | import benchmark.rpc.undertow.server.ListUserHandler; 7 | import io.undertow.Undertow; 8 | import io.undertow.UndertowOptions; 9 | import io.undertow.server.HttpHandler; 10 | import io.undertow.server.handlers.PathHandler; 11 | 12 | public class Server { 13 | 14 | public static final String host = "benchmark-server"; 15 | public static final int port = 8080; 16 | 17 | public static void main(String[] args) { 18 | Undertow.builder()// 19 | .addHttpListener(port, host)// 20 | .setServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, false)// 21 | .setServerOption(UndertowOptions.ALWAYS_SET_DATE, false)// 22 | .setHandler(paths())// 23 | .build()// 24 | .start(); 25 | } 26 | 27 | private static HttpHandler paths() { 28 | return new PathHandler()// 29 | .addExactPath("/user-exist", new UserExistHandler())// 30 | .addExactPath("/create-user", new CreateUserHandler())// 31 | .addExactPath("/get-user", new GetUserHandler())// 32 | .addExactPath("/list-user", new ListUserHandler())// 33 | ; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /undertow-async-server/src/main/java/benchmark/rpc/undertow/server/CreateUserHandler.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.undertow.server; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | 5 | import benchmark.bean.User; 6 | import benchmark.rpc.util.JsonUtils; 7 | import benchmark.service.UserService; 8 | import benchmark.service.UserServiceServerImpl; 9 | import io.undertow.async.handler.AsyncHttpHandler; 10 | import io.undertow.async.io.PooledByteBufferInputStream; 11 | import io.undertow.server.HttpServerExchange; 12 | import io.undertow.util.StatusCodes; 13 | 14 | public class CreateUserHandler extends AsyncHttpHandler { 15 | 16 | private final ObjectMapper objectMapper = JsonUtils.objectMapper; 17 | private final UserService userService = new UserServiceServerImpl(); 18 | 19 | @Override 20 | protected void handleAsyncRequest(HttpServerExchange exchange, PooledByteBufferInputStream content) 21 | throws Exception { 22 | 23 | byte[] bytes = readBytesAndClose(content); 24 | User user = objectMapper.readValue(bytes, User.class); 25 | userService.createUser(user); 26 | 27 | send(exchange, StatusCodes.OK, "true"); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /undertow-async-server/src/main/java/benchmark/rpc/undertow/server/GetUserHandler.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.undertow.server; 2 | 3 | import java.util.Deque; 4 | import java.util.Map; 5 | 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | 8 | import benchmark.bean.User; 9 | import benchmark.rpc.util.JsonUtils; 10 | import benchmark.service.UserService; 11 | import benchmark.service.UserServiceServerImpl; 12 | import io.undertow.async.handler.AsyncHttpHandler; 13 | import io.undertow.async.io.PooledByteBufferInputStream; 14 | import io.undertow.async.io.PooledByteBufferOutputStream; 15 | import io.undertow.connector.ByteBufferPool; 16 | import io.undertow.server.HttpServerExchange; 17 | import io.undertow.util.StatusCodes; 18 | 19 | public class GetUserHandler extends AsyncHttpHandler { 20 | 21 | private final ObjectMapper objectMapper = JsonUtils.objectMapper; 22 | private final UserService userService = new UserServiceServerImpl(); 23 | 24 | @Override 25 | protected void handleAsyncRequest(HttpServerExchange exchange, PooledByteBufferInputStream content) 26 | throws Exception { 27 | 28 | Map> params = exchange.getQueryParameters(); 29 | String idStr = params.get("id").getFirst(); 30 | long id = Integer.parseInt(idStr); 31 | 32 | User user = userService.getUser(id); 33 | 34 | ByteBufferPool pool = exchange.getConnection().getByteBufferPool(); 35 | PooledByteBufferOutputStream output = new PooledByteBufferOutputStream(pool); 36 | objectMapper.writeValue(output, user); 37 | 38 | send(exchange, StatusCodes.OK, output); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /undertow-async-server/src/main/java/benchmark/rpc/undertow/server/ListUserHandler.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.undertow.server; 2 | 3 | import java.util.Deque; 4 | import java.util.Map; 5 | 6 | import com.fasterxml.jackson.databind.ObjectMapper; 7 | 8 | import benchmark.bean.Page; 9 | import benchmark.bean.User; 10 | import benchmark.rpc.util.JsonUtils; 11 | import benchmark.service.UserService; 12 | import benchmark.service.UserServiceServerImpl; 13 | import io.undertow.async.handler.AsyncHttpHandler; 14 | import io.undertow.async.io.PooledByteBufferInputStream; 15 | import io.undertow.async.io.PooledByteBufferOutputStream; 16 | import io.undertow.connector.ByteBufferPool; 17 | import io.undertow.server.HttpServerExchange; 18 | import io.undertow.util.StatusCodes; 19 | 20 | public class ListUserHandler extends AsyncHttpHandler { 21 | 22 | private final ObjectMapper objectMapper = JsonUtils.objectMapper; 23 | private final UserService userService = new UserServiceServerImpl(); 24 | 25 | @Override 26 | protected void handleAsyncRequest(HttpServerExchange exchange, PooledByteBufferInputStream content) 27 | throws Exception { 28 | 29 | Map> params = exchange.getQueryParameters(); 30 | String pageNoStr = params.get("pageNo").getFirst(); 31 | int pageNo = Integer.parseInt(pageNoStr); 32 | 33 | Page userList = userService.listUser(pageNo); 34 | 35 | ByteBufferPool pool = exchange.getConnection().getByteBufferPool(); 36 | PooledByteBufferOutputStream output = new PooledByteBufferOutputStream(pool); 37 | objectMapper.writeValue(output, userList); 38 | 39 | send(exchange, StatusCodes.OK, output); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /undertow-async-server/src/main/java/benchmark/rpc/undertow/server/UserExistHandler.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.undertow.server; 2 | 3 | import java.util.Deque; 4 | import java.util.Map; 5 | 6 | import benchmark.service.UserService; 7 | import benchmark.service.UserServiceServerImpl; 8 | import io.undertow.async.handler.AsyncHttpHandler; 9 | import io.undertow.async.io.PooledByteBufferInputStream; 10 | import io.undertow.server.HttpServerExchange; 11 | import io.undertow.util.StatusCodes; 12 | 13 | public class UserExistHandler extends AsyncHttpHandler { 14 | 15 | private final UserService userService = new UserServiceServerImpl(); 16 | 17 | @Override 18 | protected void handleAsyncRequest(HttpServerExchange exchange, PooledByteBufferInputStream content) 19 | throws Exception { 20 | 21 | Map> params = exchange.getQueryParameters(); 22 | String email = params.get("email").getFirst(); 23 | 24 | if (userService.existUser(email)) { 25 | send(exchange, StatusCodes.OK, "true"); 26 | } else { 27 | send(exchange, StatusCodes.OK, "false"); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /undertow-async-server/src/main/java/io/undertow/async/util/UnsafeUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package io.undertow.async.util; 18 | 19 | import sun.misc.Unsafe; 20 | 21 | /** 22 | * 23 | * @author hank.whu@gmail.com 24 | * 25 | */ 26 | public final class UnsafeUtils { 27 | final static private Unsafe _unsafe; 28 | 29 | static { 30 | Unsafe tmpUnsafe = null; 31 | 32 | try { 33 | java.lang.reflect.Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe"); 34 | field.setAccessible(true); 35 | tmpUnsafe = (sun.misc.Unsafe) field.get(null); 36 | } catch (java.lang.Exception e) { 37 | throw new RuntimeException(e); 38 | } 39 | 40 | _unsafe = tmpUnsafe; 41 | } 42 | 43 | public static final Unsafe unsafe() { 44 | return _unsafe; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /undertow-async-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-server.log 15 | 16 | logs/benchmark-rpc-server.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /undertow-client/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /undertow-client/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | benchmark.rpc 6 | undertow-client 7 | round-5 8 | jar 9 | 10 | undertow-client 11 | http://maven.apache.org 12 | 13 | 14 | 11 15 | 11 16 | UTF-8 17 | round-5 18 | 19 | 20 | 21 | 22 | benchmark.rpc 23 | benchmark-base 24 | ${version.benchmark-base} 25 | 26 | 27 | 28 | junit 29 | junit 30 | 4.12 31 | test 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 3.8.0 43 | 44 | 45 | ${maven.compiler.source} 46 | ${maven.compiler.target} 47 | UTF-8 48 | 49 | 50 | 51 | org.apache.maven.plugins 52 | maven-resources-plugin 53 | 3.1.0 54 | 55 | UTF-8 56 | 57 | 58 | 59 | 60 | maven-assembly-plugin 61 | 3.1.0 62 | 63 | 64 | jar-with-dependencies 65 | 66 | 67 | 68 | benchmark.rpc.Client 69 | 70 | 71 | 72 | 73 | 74 | make-assembly 75 | package 76 | 77 | single 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | src/main/resources 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /undertow-client/src/main/java/benchmark/rpc/Client.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import org.openjdk.jmh.annotations.Benchmark; 6 | import org.openjdk.jmh.annotations.BenchmarkMode; 7 | import org.openjdk.jmh.annotations.Mode; 8 | import org.openjdk.jmh.annotations.OutputTimeUnit; 9 | import org.openjdk.jmh.annotations.Scope; 10 | import org.openjdk.jmh.annotations.State; 11 | import org.openjdk.jmh.runner.Runner; 12 | import org.openjdk.jmh.runner.RunnerException; 13 | import org.openjdk.jmh.runner.options.Options; 14 | import org.openjdk.jmh.runner.options.OptionsBuilder; 15 | import org.openjdk.jmh.runner.options.TimeValue; 16 | 17 | import benchmark.bean.Page; 18 | import benchmark.bean.User; 19 | import benchmark.service.UserService; 20 | import benchmark.service.UserServiceJsonHttpClientImpl; 21 | 22 | @State(Scope.Benchmark) 23 | public class Client extends AbstractClient { 24 | public static final int CONCURRENCY = 32; 25 | 26 | private final UserService userService = new UserServiceJsonHttpClientImpl(CONCURRENCY); 27 | 28 | @Override 29 | protected UserService getUserService() { 30 | return userService; 31 | } 32 | 33 | @Benchmark 34 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 35 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 36 | @Override 37 | public boolean existUser() throws Exception { 38 | return super.existUser(); 39 | } 40 | 41 | @Benchmark 42 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 43 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 44 | @Override 45 | public boolean createUser() throws Exception { 46 | return super.createUser(); 47 | } 48 | 49 | @Benchmark 50 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 51 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 52 | @Override 53 | public User getUser() throws Exception { 54 | return super.getUser(); 55 | } 56 | 57 | @Benchmark 58 | @BenchmarkMode({ Mode.Throughput, Mode.AverageTime, Mode.SampleTime }) 59 | @OutputTimeUnit(TimeUnit.MILLISECONDS) 60 | @Override 61 | public Page listUser() throws Exception { 62 | return super.listUser(); 63 | } 64 | 65 | public static void main(String[] args) throws RunnerException { 66 | Options opt = new OptionsBuilder()// 67 | .include(Client.class.getSimpleName())// 68 | .warmupIterations(3)// 69 | .warmupTime(TimeValue.seconds(10))// 70 | .measurementIterations(3)// 71 | .measurementTime(TimeValue.seconds(10))// 72 | .threads(CONCURRENCY)// 73 | .forks(1)// 74 | .build(); 75 | 76 | new Runner(opt).run(); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /undertow-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.apt_generated_tests/ 2 | -------------------------------------------------------------------------------- /undertow-server/src/main/java/benchmark/rpc/Server.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc; 2 | 3 | import benchmark.rpc.undertow.server.CreateUserHandler; 4 | import benchmark.rpc.undertow.server.UserExistHandler; 5 | import benchmark.rpc.undertow.server.GetUserHandler; 6 | import benchmark.rpc.undertow.server.ListUserHandler; 7 | import io.undertow.Undertow; 8 | import io.undertow.UndertowOptions; 9 | import io.undertow.server.HttpHandler; 10 | import io.undertow.server.handlers.PathHandler; 11 | 12 | public class Server { 13 | 14 | public static final String host = "benchmark-server"; 15 | public static final int port = 8080; 16 | 17 | public static void main(String[] args) { 18 | Undertow.builder()// 19 | .addHttpListener(port, host)// 20 | .setServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, false)// 21 | .setServerOption(UndertowOptions.ALWAYS_SET_DATE, false)// 22 | .setHandler(paths())// 23 | .build()// 24 | .start(); 25 | } 26 | 27 | private static HttpHandler paths() { 28 | return new PathHandler()// 29 | .addExactPath("/user-exist", new UserExistHandler())// 30 | .addExactPath("/create-user", new CreateUserHandler())// 31 | .addExactPath("/get-user", new GetUserHandler())// 32 | .addExactPath("/list-user", new ListUserHandler())// 33 | ; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /undertow-server/src/main/java/benchmark/rpc/undertow/server/CreateUserHandler.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.undertow.server; 2 | 3 | import java.nio.ByteBuffer; 4 | 5 | import com.fasterxml.jackson.databind.ObjectMapper; 6 | 7 | import benchmark.bean.User; 8 | import benchmark.rpc.util.ByteBufferUtils; 9 | import benchmark.rpc.util.JsonUtils; 10 | import benchmark.service.UserService; 11 | import benchmark.service.UserServiceServerImpl; 12 | import io.undertow.server.HttpHandler; 13 | import io.undertow.server.HttpServerExchange; 14 | 15 | public class CreateUserHandler implements HttpHandler { 16 | 17 | private final ByteBuffer trueResult = ByteBufferUtils.allocateDirect("true"); 18 | private final ByteBuffer falseResult = ByteBufferUtils.allocateDirect("false"); 19 | 20 | private final ObjectMapper objectMapper = JsonUtils.objectMapper; 21 | private final UserService userService = new UserServiceServerImpl(); 22 | 23 | @Override 24 | public void handleRequest(HttpServerExchange _exchange) throws Exception { 25 | _exchange.getRequestReceiver().receiveFullBytes(// 26 | (exchange, data) -> {// do stuff with the data 27 | try { 28 | User user = objectMapper.readValue(data, User.class); 29 | userService.createUser(user); 30 | 31 | exchange.getResponseSender().send(trueResult.duplicate()); 32 | } catch (Exception e) { 33 | e.printStackTrace(); 34 | exchange.setStatusCode(500); 35 | exchange.getResponseSender().send(falseResult.duplicate()); 36 | } 37 | }, // 38 | (exchange, exception) -> {// optional error handler 39 | exchange.setStatusCode(500); 40 | exchange.getResponseSender().send(falseResult.duplicate()); 41 | }); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /undertow-server/src/main/java/benchmark/rpc/undertow/server/GetUserHandler.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.undertow.server; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Deque; 5 | import java.util.Map; 6 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; 8 | 9 | import benchmark.bean.User; 10 | import benchmark.rpc.util.ByteBufferUtils; 11 | import benchmark.rpc.util.JsonUtils; 12 | import benchmark.service.UserService; 13 | import benchmark.service.UserServiceServerImpl; 14 | import io.undertow.server.HttpHandler; 15 | import io.undertow.server.HttpServerExchange; 16 | 17 | public class GetUserHandler implements HttpHandler { 18 | 19 | private final ObjectMapper objectMapper = JsonUtils.objectMapper; 20 | private final UserService userService = new UserServiceServerImpl(); 21 | 22 | @Override 23 | public void handleRequest(HttpServerExchange exchange) throws Exception { 24 | Map> params = exchange.getQueryParameters(); 25 | String idStr = params.get("id").getFirst(); 26 | long id = Integer.parseInt(idStr); 27 | 28 | User user = userService.getUser(id); 29 | 30 | byte[] bytes = objectMapper.writeValueAsBytes(user); 31 | ByteBuffer buffer = ByteBufferUtils.allocate(bytes); 32 | 33 | exchange.getResponseSender().send(buffer); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /undertow-server/src/main/java/benchmark/rpc/undertow/server/ListUserHandler.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.undertow.server; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Deque; 5 | import java.util.Map; 6 | 7 | import com.fasterxml.jackson.databind.ObjectMapper; 8 | 9 | import benchmark.bean.Page; 10 | import benchmark.bean.User; 11 | import benchmark.rpc.util.ByteBufferUtils; 12 | import benchmark.rpc.util.JsonUtils; 13 | import benchmark.service.UserService; 14 | import benchmark.service.UserServiceServerImpl; 15 | import io.undertow.server.HttpHandler; 16 | import io.undertow.server.HttpServerExchange; 17 | 18 | public class ListUserHandler implements HttpHandler { 19 | 20 | private final ObjectMapper objectMapper = JsonUtils.objectMapper; 21 | private final UserService userService = new UserServiceServerImpl(); 22 | 23 | @Override 24 | public void handleRequest(HttpServerExchange exchange) throws Exception { 25 | Map> params = exchange.getQueryParameters(); 26 | String pageNoStr = params.get("pageNo").getFirst(); 27 | int pageNo = Integer.parseInt(pageNoStr); 28 | 29 | Page userList = userService.listUser(pageNo); 30 | 31 | byte[] bytes = objectMapper.writeValueAsBytes(userList); 32 | ByteBuffer buffer = ByteBufferUtils.allocate(bytes); 33 | 34 | exchange.getResponseSender().send(buffer); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /undertow-server/src/main/java/benchmark/rpc/undertow/server/UserExistHandler.java: -------------------------------------------------------------------------------- 1 | package benchmark.rpc.undertow.server; 2 | 3 | import java.nio.ByteBuffer; 4 | import java.util.Deque; 5 | import java.util.Map; 6 | 7 | import benchmark.rpc.util.ByteBufferUtils; 8 | import benchmark.service.UserService; 9 | import benchmark.service.UserServiceServerImpl; 10 | import io.undertow.server.HttpHandler; 11 | import io.undertow.server.HttpServerExchange; 12 | 13 | public class UserExistHandler implements HttpHandler { 14 | 15 | private final ByteBuffer trueResult = ByteBufferUtils.allocateDirect("true"); 16 | private final ByteBuffer falseResult = ByteBufferUtils.allocateDirect("false"); 17 | private final UserService userService = new UserServiceServerImpl(); 18 | 19 | @Override 20 | public void handleRequest(HttpServerExchange exchange) throws Exception { 21 | Map> params = exchange.getQueryParameters(); 22 | String email = params.get("email").getFirst(); 23 | 24 | if (userService.existUser(email)) { 25 | exchange.getResponseSender().send(trueResult.duplicate()); 26 | } else { 27 | exchange.getResponseSender().send(falseResult.duplicate()); 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /undertow-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 8 | 9 | 10 | 11 | 12 | 14 | logs/benchmark-rpc-server.log 15 | 16 | logs/benchmark-rpc-server.%d{yyyy-MM-dd}.log.gz 17 | 18 | 19 | 20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | --------------------------------------------------------------------------------