Use more realistic cipher test sizes and counts.

This commit is contained in:
md_5 2016-01-16 14:03:47 +11:00
parent 7fb1f4b81f
commit 79dbdea107

View File

@ -5,6 +5,7 @@ import net.md_5.bungee.jni.cipher.JavaCipher;
import net.md_5.bungee.jni.cipher.BungeeCipher; import net.md_5.bungee.jni.cipher.BungeeCipher;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import java.util.Random;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
@ -23,7 +24,7 @@ public class NativeCipherTest
50, -7, 89, 1, -11, -32, -118, -48, -2, -72, 105, 97, -70, -81 50, -7, 89, 1, -11, -32, -118, -48, -2, -72, 105, 97, -70, -81
}; };
private final SecretKey secret = new SecretKeySpec( new byte[ 16 ], "AES" ); private final SecretKey secret = new SecretKeySpec( new byte[ 16 ], "AES" );
private static final int BENCHMARK_COUNT = 50000; private static final int BENCHMARK_COUNT = 4096;
// //
private static final NativeCode<BungeeCipher> factory = new NativeCode( "native-cipher", JavaCipher.class, NativeCipher.class ); private static final NativeCode<BungeeCipher> factory = new NativeCode( "native-cipher", JavaCipher.class, NativeCipher.class );
@ -110,25 +111,28 @@ public class NativeCipherTest
public void testBenchmark(BungeeCipher cipher) throws Exception public void testBenchmark(BungeeCipher cipher) throws Exception
{ {
// Create input buf // Create input buf
ByteBuf nativePlain = Unpooled.directBuffer( plainBytes.length ); byte[] random = new byte[ 1 << 12 ];
nativePlain.writeBytes( plainBytes ); new Random().nextBytes( random );
// Create expected buf ByteBuf nativePlain = Unpooled.directBuffer();
ByteBuf nativeCiphered = Unpooled.directBuffer( cipheredBytes.length ); nativePlain.writeBytes( random );
nativeCiphered.writeBytes( cipheredBytes );
// Create output buf // Create output buf
ByteBuf out = Unpooled.directBuffer( plainBytes.length ); ByteBuf nativeCiphered = Unpooled.directBuffer( plainBytes.length );
// Encrypt // Encrypt
cipher.init( true, secret ); cipher.init( true, secret );
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
for ( int i = 0; i < BENCHMARK_COUNT; i++ ) for ( int i = 0; i < BENCHMARK_COUNT; i++ )
{ {
cipher.cipher( nativePlain, out ); nativeCiphered.clear();
cipher.cipher( nativePlain, nativeCiphered );
nativePlain.readerIndex( 0 ); nativePlain.readerIndex( 0 );
out.clear();
} }
System.out.println( String.format( "Encryption Iteration: %d, Elapsed: %d ms", BENCHMARK_COUNT, System.currentTimeMillis() - start ) ); System.out.println( String.format( "Encryption Iteration: %d, Elapsed: %d ms", BENCHMARK_COUNT, System.currentTimeMillis() - start ) );
// Create output buf
ByteBuf out = Unpooled.directBuffer( plainBytes.length );
// Decrypt // Decrypt
cipher.init( false, secret ); cipher.init( false, secret );
start = System.currentTimeMillis(); start = System.currentTimeMillis();