On Mon, Jun 6, 2011 at 2:02 PM, Emmanuel Lecharny <[email protected]> wrote: > On 6/6/11 1:42 PM, Fritz Richter wrote: >> >> Hi together, >> in my current project I have the following szenario: >> >> I have to continuously stream data via UDP in chunks which are about 1200 >> bytes. I only want to send this data to one specific host, >> so this is the only connection which I need. >> >> My data is prepared natively and it is stored in a ByteBuffer, which I >> prepare in the background and reuse via an object pool. >> >> My first approach was to send it via the old plain java way like this >> example here: >> >> >> byte[] data = new byte[byteBuffer.capacity()]; >> byteBuffer.rewind(); >> byteBuffer.get(data); >> DatagramPacket udp = new DatagramPacket(data, 0, data.length, >> holder.getRemoteAddress()); >> serverSocket.send(udp); >> >> I switched to Apache Mina as I thought it might be faster, the code now >> looks like this: >> >> byteBuffer.rewind(); >> IoBuffer ioBuffer = IoBuffer.wrap(byteBuffer); >> IoSession session = getSession(); >> session.write(ioBuffer); >> >> I figured out, that this solution is not really faster. Actually, I have >> the >> strong feeling, that it is actually a bit slower, maybe the GC is running >> more often. >> >> My question to you guys is, does it makes sense to use the NIO approach >> here >> or is just fine to use the old java approach? >> Is it worth to use MINA and the IoBuffer directly or is the overhead of >> the >> framework to much? > > Hi, > > short answer : NIO will be 30% slower than plain BIO. > > Now, having said that does not help to understand when you should use NIO or > BIO. basically the BIO approach means 1 connection => one thread when NIO > can use a thread pool to deal with the incoming connections. Bottom line, if > you have to deal with many many connections, then NIO might be a good > solution. (many = > tens of thousand). > > One more thing : you should first focus on making your code working, instead > of focusing on performance, unless absolutely necessary.
if you are 1 connection and doesn't do fancy stuff, well NIO and an NIO framework like MINA is an overkill. Your network code is pretty simple. Socket syscalls are really quick nowadays, your business logic is or will probably be the performance hog. Julien
