Comparing Java 7 Async NIO with NIO.
SDP (Sockets Direct Protocol)
in Java 7 promises better integration with InfiniBand. This comes with
a new Socket classes AsynchronousSocketChannel and
AsynchronousServerSocketChannel. Is there any advantage in using these
libraries if you don't have InfiniBand?
Creating a server socketThe API for AsynchronousServerSocketChannel is slightly different to ServerSocketChannel. The most notable difference is the use of Futures instead of always blocking.
final AsynchronousServerSocketChannel ssc = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("localhost", 9999)); Future<AsynchronousSocketChannel> accepted = ssc.accept(); AsynchronousSocketChannel sc2 = accepted.get(); ByteBuffer bb = ByteBuffer.allocateDirect(4096); Future<Integer> readFuture = sc2.read(bb);
Creating a client socketSimilarly the client can start a connection and later wait when the connection is really needed.
AsynchronousSocketChannel sc = AsynchronousSocketChannel.open(); Future connected = sc.connect(new InetSocketAddress("localhost", 9999)); // later ensure we are connected. connected.get(); ByteBuffer bb = ByteBuffer.allocateDirect(4096); // populate bb Future<Integer> writeFuture = sc2.write(bb);
Is there any performance differences?Without threads, the Asynchronous IO was 5-10% faster. However once threads were used, the performance dropped. This could be because this is Java 7 update 0. I look forward to testing this again in the future.
Async Socket latency was 1/50/99%tile 5.2/5.4/7.3 us Async Socket Throughput was 186 K/s Threaded Async Socket Latency for 1/50/99%tile 13.8/16.9/24.7 us Threaded Async Socket Throughput was 183 K/sThe best of three runs was used.
For comparison, the same tests using regular NIO on Java 7 were
Socket latency was 1/50/99%tile 5.6/5.8/7.0 us Socket Throughput was 170 K/s Threaded Socket Latency for 1/50/99%tile 6.0/8.5/10.7 us Threaded Socket Throughput was 234 K/s
The codeIts worth nothing that the code is slightly more complex to use than regular NIO. However, the features added are powerful and for the brave, it will be valuable.
Asynchronous NIO Socket - AsyncPingTest.java
Plain NIO Socket - PingTest.java
ConclusionThe API for Asynchronous sockets looks promising despite the added complexity. However the performance many not be there unless you have the right hardware, yet.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)