Hi,
I created a very simple test that just compares java and kryo
deserialization, with both just a string and a list of Person classes
(FieldSerializer), this is the result:
kryo time for String: 140 ms (serialized size: 10243)
kryo time for ArrayList: 5836 ms (serialized size: 37503)
java time for String: 184 ms (serialized size: 10247)
java time for ArrayList: 589 ms (serialized size: 8182)
(the code is pasted at the end of the email)
It shows, that for String deserialization kryo is faster than java,
but the FieldSerializer is 10x slower (serialized data also more than
java).
Any clue what's causing this?
Cheers,
Martin
The code:
public static void main( final String[] args ) throws
InterruptedException, IOException, ClassNotFoundException {
benchDeserializationKryo( newString( 10 ) );
recover();
benchDeserializationKryo( createPersons( 500 ) );
recover();
benchDeserializationJava( newString( 10 ) );
recover();
benchDeserializationJava( createPersons( 500 ) );
}
private static void recover() throws InterruptedException {
System.gc();
Thread.sleep( 500 );
}
private static void benchDeserializationKryo( final Object obj ) {
final Kryo kryo = new Kryo();
kryo.setRegistrationOptional( true );
final byte[] data = new ObjectBuffer(kryo, 10 * 1024, 100 *
1024 ).writeObject( obj );
final long start = System.currentTimeMillis();
for( int i = 0; i < 1000; i++ ) {
new ObjectBuffer(kryo).readObject( data, obj.getClass() );
}
System.out.println( "kryo time for "+
obj.getClass().getSimpleName() +": " + ( System.currentTimeMillis() -
start) + " ms (serialized size: " + data.length + ")");
}
private static void benchDeserializationJava( final Object obj )
throws IOException, ClassNotFoundException {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream( bos );
oos.writeObject( obj );
final byte[] data = bos.toByteArray();
oos.close();
final long start = System.currentTimeMillis();
for( int i = 0; i < 1000; i++ ) {
final ObjectInputStream ois = new ObjectInputStream( new
ByteArrayInputStream( data ) );
ois.readObject();
ois.close();
}
System.out.println( "java time for "+
obj.getClass().getSimpleName() +": " + ( System.currentTimeMillis() -
start) + " ms (serialized size: " + data.length + ")");
}
private static List<Person> createPersons( final int numPersons )
{
final List<Person> items = new ArrayList<Person>(numPersons);
for ( int i = 0; i < numPersons; i++ ) {
items.add( new Person( "foo", "bar" ) );
}
return items;
}
private static String newString( final int lengthInKb ) {
final StringBuilder sb = new StringBuilder( lengthInKb );
final Random random = new Random();
for( int i = 0; i < lengthInKb * 1024; i++ ) {
sb.append( random.nextInt( 9 ) );
}
return sb.toString();
}
private static class Person implements Serializable {
private String firstName;
private String lastName;
public Person() {
}
public Person( final String firstName, final String lastName )
{
this.firstName = firstName;
this.lastName = lastName;
}
}
On May 5, 1:54 am, Martin Grotzke <
martin.grot...@googlemail.com>
wrote:
> Hi,
>
> these days I finally integrated the kryo based serialization strategy
> for memcached-session-manager in our project, and deserialization is
> incredibly slow (10 - 30 times slower than java deserialization!).
> Still, serialization time is good compared to java serialization.
>
> I just assembled a benchmark that compares java serialization,
> javolution (xml) based serialization and kryo serialization.
>
> The benchmark (the code at github:
http://is.gd/bUib7) creates a
> simple object graph with some sample data like e.g. a list of persons
> that have name, birthday, connected persons (friends) etc,.
> This data is serialized 1000 times, this is done in 10 rounds, to get
> min/avg/max time needed for 1000 serializations. The same is done for
> deserialization.
> To see how numbers depend on object (graph) size, this was done for 3
> different samples (20 person objects, 100 persons, 500 persons).
>
> You can see the results here (size in bytes, time in millis):
http://spreadsheets.google.com/pub?key=0AspDnQzTrkhddDRWeEl3LWs5R0Uxb...