Latency issue while accessing server in multiple threads
koguty
kohinoor at julysystems.com
Mon Aug 16 10:36:12 EDT 2010
Hi folks,
I am new to netty and need some help regarding latency issue :
implemented a simple server and client app using netty -
the client sends a request and the server sends back a fixed resonse and
closes the channel;
while using multiple threads (say 10 threads) it takes almost 30 ms or more
to get the resonse from server.
This is in local machine - no network delays.
using the included test class for testing.
I have set child.tcpNoDelay in server and tcpNoDelay in client as true.
Query :
1. is it possible to take the timing down to <10 milli secs?
2. is the implementation wrong or anything wrong with the test case?
Please let me know.
Thanks in advance.
Test class :
public class Test {
private static int numOfSearchThreads = 10;
private static ExecutorService search =
Executors.newFixedThreadPool(numOfSearchThreads);
public static void main(String[] args) throws Exception
{
TimerTask task = new TimerTask() {
public void run() {
System.out.println();
System.out.println("Starting multi search ... ");
for (int i = 0; i < numOfSearchThreads; i++){
Runnable task = new Runnable() {
@Override
public void run() {
try {
long t0 = System.currentTimeMillis();
Client.getInstance().getSubscriberFromServer();
System.out.println("Time taken : " +
(System.currentTimeMillis() - t0));
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
};
search.submit(task);
}
}
};
new Timer().schedule(task, 0, 10 * 1000);
}
}
Server :
public class Server {
public static final int PORT = 7007;
public static void start(){
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.getPipeline().addLast("handler", new
SearchServerHandler());
bootstrap.bind(new InetSocketAddress(PORT));
}
public static void main(String[] args) throws Exception {
start();
}
}
class SearchServerHandler extends SimpleChannelHandler {
private static final Logger logger =
Logger.getLogger(SearchServerHandler.class.getName());
private static final ChannelBuffer
fixedResponse=ChannelBuffers.copiedBuffer("1".getBytes());
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
e.getChannel().write(fixedResponse);
e.getChannel().close();
}
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
{
logger.log(Level.WARNING, "Unexpected exception from downstream.",
e.getCause());
e.getChannel().close();
}
}
Client :
public class Client {
private static final String host = "localhost";
private static final ExecutorService executor =
Executors.newCachedThreadPool();
private static final ChannelFactory channelFactory = new
NioClientSocketChannelFactory(executor, executor);
private static final ClientBootstrap bootstrap = new
ClientBootstrap(channelFactory);
private static final Client instance = new Client();
private static SocketAddress address;
private Client(){
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new ClientHandler());
}
});
address = new InetSocketAddress(host, Server.PORT);
}
public static Client getInstance(){
return instance;
}
public String getSubscriberFromServer() throws UnknownHostException{
ChannelFuture future = bootstrap.connect(address);
future.getChannel().getCloseFuture().awaitUninterruptibly();
return "";
}
private ChannelFuture connect() throws UnknownHostException{
ChannelFuture future = bootstrap.connect(address);
future.getChannel().getCloseFuture().awaitUninterruptibly();
return future;
}
public void shutdown(){
bootstrap.releaseExternalResources();
}
}
class ClientHandler extends SimpleChannelUpstreamHandler{
private static final Logger logger =
Logger.getLogger(SearchClientHandler.class.getName());
public void channelConnected(ChannelHandlerContext ctx,
ChannelStateEvent e) {
e.getChannel().write(ChannelBuffers.copiedBuffer("1".getBytes()));
}
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
byte[] bytes = ((ChannelBuffer)e.getMessage()).array();
String s = new String(bytes);
logger.info("obtained : " + s);
e.getChannel().close();
}
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
{
logger.log(Level.WARNING, "Unexpected exception from downstream.",
e.getCause());
e.getChannel().close();
}
}
http://netty-forums-and-mailing-lists.685743.n2.nabble.com/file/n5428086/Client.java
Client.java
http://netty-forums-and-mailing-lists.685743.n2.nabble.com/file/n5428086/Server.java
Server.java
http://netty-forums-and-mailing-lists.685743.n2.nabble.com/file/n5428086/Test.java
Test.java
--
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Latency-issue-while-accessing-server-in-multiple-threads-tp5428086p5428086.html
Sent from the Netty User Group mailing list archive at Nabble.com.
More information about the netty-users
mailing list