From d0dd93974a7bcc6c906f6f900d3743b77c7d7ad4 Mon Sep 17 00:00:00 2001 From: Sam Pullara Date: Wed, 28 Mar 2012 13:24:28 -0700 Subject: [PATCH] clean up the style --- .../netty/example/redis/RedisClient.java | 93 +++++++++++-------- .../handler/codec/redis/RedisEncoder.java | 7 +- 2 files changed, 58 insertions(+), 42 deletions(-) diff --git a/src/main/java/org/jboss/netty/example/redis/RedisClient.java b/src/main/java/org/jboss/netty/example/redis/RedisClient.java index 9518243475..53c16c147c 100644 --- a/src/main/java/org/jboss/netty/example/redis/RedisClient.java +++ b/src/main/java/org/jboss/netty/example/redis/RedisClient.java @@ -16,7 +16,11 @@ package org.jboss.netty.example.redis; import org.jboss.netty.bootstrap.ClientBootstrap; -import org.jboss.netty.channel.*; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelFuture; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.channel.ChannelPipelineFactory; +import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; import org.jboss.netty.handler.codec.redis.Command; import org.jboss.netty.handler.codec.redis.RedisDecoder; @@ -24,6 +28,7 @@ import org.jboss.netty.handler.codec.redis.RedisEncoder; import org.jboss.netty.handler.codec.redis.Reply; import org.jboss.netty.handler.queue.BlockingReadHandler; +import java.io.IOException; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; @@ -57,51 +62,57 @@ public final class RedisClient { int CALLS = 1000000; int PIPELINE = 50; - { - long start = System.currentTimeMillis(); - byte[] SET_BYTES = "SET".getBytes(); - for (int i = 0; i < CALLS; i++) { - channel.write(new Command(SET_BYTES, String.valueOf(i).getBytes(), VALUE)); - blockingReadHandler.read(); - } - long end = System.currentTimeMillis(); - System.out.println(CALLS * 1000 / (end - start) + " calls per second"); - } - { - long start = System.currentTimeMillis(); - byte[] SET_BYTES = "SET".getBytes(); - for (int i = 0; i < CALLS / PIPELINE; i++) { - for (int j = 0; j < PIPELINE; j++) { - channel.write(new Command(SET_BYTES, String.valueOf(i).getBytes(), VALUE)); - } - for (int j = 0; j < PIPELINE; j++) { - blockingReadHandler.read(); - } - } - long end = System.currentTimeMillis(); - System.out.println(CALLS * 1000 / (end - start) + " calls per second"); - } - { - long start = System.currentTimeMillis(); - byte[] SET_BYTES = "SET".getBytes(); - for (int i = 0; i < CALLS / PIPELINE; i++) { - List list = new ArrayList(); - for (int j = 0; j < PIPELINE; j++) { - list.add(new Command(SET_BYTES, String.valueOf(i).getBytes(), VALUE)); - } - channel.write(list); - for (int j = 0; j < PIPELINE; j++) { - blockingReadHandler.read(); - } - } - long end = System.currentTimeMillis(); - System.out.println(CALLS * 1000 / (end - start) + " calls per second"); - } + requestResponse(blockingReadHandler, channel, CALLS); + pipelinedIndividualRequests(blockingReadHandler, channel, CALLS, PIPELINE); + pipelinedListOfRequests(blockingReadHandler, channel, CALLS, PIPELINE); channel.close(); cb.releaseExternalResources(); } + private static void pipelinedListOfRequests(BlockingReadHandler blockingReadHandler, Channel channel, int CALLS, int PIPELINE) throws IOException, InterruptedException { + long start = System.currentTimeMillis(); + byte[] SET_BYTES = "SET".getBytes(); + for (int i = 0; i < CALLS / PIPELINE; i++) { + List list = new ArrayList(); + for (int j = 0; j < PIPELINE; j++) { + list.add(new Command(SET_BYTES, String.valueOf(i).getBytes(), VALUE)); + } + channel.write(list); + for (int j = 0; j < PIPELINE; j++) { + blockingReadHandler.read(); + } + } + long end = System.currentTimeMillis(); + System.out.println(CALLS * 1000 / (end - start) + " calls per second"); + } + + private static void pipelinedIndividualRequests(BlockingReadHandler blockingReadHandler, Channel channel, int CALLS, int PIPELINE) throws IOException, InterruptedException { + long start = System.currentTimeMillis(); + byte[] SET_BYTES = "SET".getBytes(); + for (int i = 0; i < CALLS / PIPELINE; i++) { + for (int j = 0; j < PIPELINE; j++) { + channel.write(new Command(SET_BYTES, String.valueOf(i).getBytes(), VALUE)); + } + for (int j = 0; j < PIPELINE; j++) { + blockingReadHandler.read(); + } + } + long end = System.currentTimeMillis(); + System.out.println(CALLS * 1000 / (end - start) + " calls per second"); + } + + private static void requestResponse(BlockingReadHandler blockingReadHandler, Channel channel, int CALLS) throws IOException, InterruptedException { + long start = System.currentTimeMillis(); + byte[] SET_BYTES = "SET".getBytes(); + for (int i = 0; i < CALLS; i++) { + channel.write(new Command(SET_BYTES, String.valueOf(i).getBytes(), VALUE)); + blockingReadHandler.read(); + } + long end = System.currentTimeMillis(); + System.out.println(CALLS * 1000 / (end - start) + " calls per second"); + } + private RedisClient() { } diff --git a/src/main/java/org/jboss/netty/handler/codec/redis/RedisEncoder.java b/src/main/java/org/jboss/netty/handler/codec/redis/RedisEncoder.java index 825d4cdbfc..20ed530e0b 100644 --- a/src/main/java/org/jboss/netty/handler/codec/redis/RedisEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/redis/RedisEncoder.java @@ -17,8 +17,13 @@ package org.jboss.netty.handler.codec.redis; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.*; +import org.jboss.netty.channel.ChannelFuture; +import org.jboss.netty.channel.ChannelFutureListener; import org.jboss.netty.channel.ChannelHandler.Sharable; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.Channels; +import org.jboss.netty.channel.MessageEvent; +import org.jboss.netty.channel.SimpleChannelDownstreamHandler; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue;