You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Michael Bauroth <mi...@falcom.de> on 2007/07/04 14:46:57 UTC

Performance comparision of Mina vs. java.nio ByteBuffer use

Hi,

I've made some performance tests while the last hours. My special 
interest was the performance of non-direct ByteBuffer operations like an 
simple get(byte[]). Here is a very short code sample:

import java.nio.ByteBuffer;
import com.sun.grizzly.util.ByteBufferFactory;
//import org.apache.mina.common.ByteBuffer;

public class Test
{
	public static void main(String[] pArgs)
	{
		byte[] tBytes = new byte[ 100 ];
		
		//ByteBuffer buf = ByteBuffer.allocate( 100, false );
		ByteBuffer buf = ByteBufferFactory.allocateView( 7, false );

		long tStart = System.currentTimeMillis();
		for ( int i = 0; i < 10000000; i++)
		{
			buf.get( tBytes );
			buf.rewind();
			//buf.position( 0 );
		}
		
		long tStop = System.currentTimeMillis();
		System.out.println( tStop - tStart );
	}
}

It seems that the Mina buffers need twice the time against the use of 
java.nio ByteBuffer directly (ByteBuffers which are constructed from 
Grizzly ByteBufferFactory are also a little bit slower because of the 
use of slice(), but only about 10%). You can check it simply if you use 
mybuffer.buf().get(...) instead of mybuffer.get(...)

What happens here?

Best Regards
Michael

Btw.: rewind() is ~15% quicker then position( 0 ) ;)


Re: Performance comparision of Mina vs. java.nio ByteBuffer use

Posted by Michael Bauroth <mi...@falcom.de>.
Extending the test with Grizzly and after running 100 test loops of each 
method I have now correct results (not normalized):

Loop: 100   Mina(avg.): 691   Nio(avg.): 669   Grizzly(avg.): 691

Loop: 100    M (avg.): 699    N (avg.): 662    G (avg.): 686
Loop: 100    M (avg.): 708    N (avg.): 670    G (avg.): 698

Mina and Grizzly have the exact same values (used for Grizzly not-direct 
viewbuffers), pure Nio is naturally 3-4% quicker.

Used Java 6 and Win XP as testbed.

Best Regards
Michael


Re: Performance comparision of Mina vs. java.nio ByteBuffer use

Posted by Michael Bauroth <mi...@falcom.de>.
Damn ... I forgot to switch on the -server option for the test:

without -server option:

M: 1823
N: 931
M: 1873
N: 911
M: 1823
N: 881
M: 1823
N: 891
M: 1822
N: 862
M: 1812
N: 882
M: 1862
N: 962

with -server option:

M: 711
N: 681
M: 711
N: 671
M: 671
N: 671
M: 671
N: 671
M: 701
N: 681
M: 681
N: 661

Strange :)

Thanx Trustin ...

Best Regards
Michael


Trustin Lee schrieb:
> I tweaked your test code a little bit, and ran the VM with '-server'
> option.  With MINA 1.x, you will have to use
> SimpleByteBufferAllocator.  I used MINA 2.0 in the test, and found NIO
> buffers and MINA buffers don't differ in performance.
> 


Re: Performance comparision of Mina vs. java.nio ByteBuffer use

Posted by Trustin Lee <tr...@gmail.com>.
I tweaked your test code a little bit, and ran the VM with '-server'
option.  With MINA 1.x, you will have to use
SimpleByteBufferAllocator.  I used MINA 2.0 in the test, and found NIO
buffers and MINA buffers don't differ in performance.

public class Main {



    public static void main(String args[]) throws Exception {

        org.apache.mina.common.ByteBuffer mb =
org.apache.mina.common.ByteBuffer.allocate(100, false);

        java.nio.ByteBuffer nb = java.nio.ByteBuffer.allocate(100);

        byte[] tBytes = new byte[100];



        for (;;) {

            testMina(mb, tBytes);

            testNio(nb, tBytes);

        }

    }



    private static void testMina(org.apache.mina.common.ByteBuffer mb,
byte[] tBytes) {

        long tStart = System.currentTimeMillis();

        for ( int i = 10000000; i > 0 ; i--)

        {

                mb.get( tBytes );

                mb.rewind();

        }



        long tStop = System.currentTimeMillis();

        System.out.println( "M: " + (tStop - tStart) );

    }



    private static void testNio(java.nio.ByteBuffer nb, byte[] tBytes) {

        long tStart = System.currentTimeMillis();

        for ( int i = 10000000; i > 0 ; i--)

        {

                nb.get( tBytes );

                nb.rewind();

        }



        long tStop = System.currentTimeMillis();

        System.out.println( "N: " + (tStop - tStart) );

    }

}





On 7/4/07, Michael Bauroth <mi...@falcom.de> wrote:
> Hi,
>
> I've made some performance tests while the last hours. My special
> interest was the performance of non-direct ByteBuffer operations like an
> simple get(byte[]). Here is a very short code sample:
>
> import java.nio.ByteBuffer;
> import com.sun.grizzly.util.ByteBufferFactory;
> //import org.apache.mina.common.ByteBuffer;
>
> public class Test
> {
>         public static void main(String[] pArgs)
>         {
>                 byte[] tBytes = new byte[ 100 ];
>
>                 //ByteBuffer buf = ByteBuffer.allocate( 100, false );
>                 ByteBuffer buf = ByteBufferFactory.allocateView( 7, false );
>
>                 long tStart = System.currentTimeMillis();
>                 for ( int i = 0; i < 10000000; i++)
>                 {
>                         buf.get( tBytes );
>                         buf.rewind();
>                         //buf.position( 0 );
>                 }
>
>                 long tStop = System.currentTimeMillis();
>                 System.out.println( tStop - tStart );
>         }
> }
>
> It seems that the Mina buffers need twice the time against the use of
> java.nio ByteBuffer directly (ByteBuffers which are constructed from
> Grizzly ByteBufferFactory are also a little bit slower because of the
> use of slice(), but only about 10%). You can check it simply if you use
> mybuffer.buf().get(...) instead of mybuffer.get(...)
>
> What happens here?
>
> Best Regards
> Michael
>
> Btw.: rewind() is ~15% quicker then position( 0 ) ;)
>
>


-- 
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6