package example.entity.test;

import com.tangosol.io.pof.PofAnnotationSerializer;
import com.tangosol.io.pof.SimplePofContext;
import com.tangosol.util.Binary;
import com.tangosol.util.ExternalizableHelper;
import com.tangosol.util.Base;

import example.entity.*;

public class TestPersonPOF {
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) {

	        
        SimplePofContext ctx = new SimplePofContext();
        ctx.registerUserType(1000, Person.class ,new PofAnnotationSerializer(1000,Person.class));
        ctx.registerUserType(1001, Address.class ,new PofAnnotationSerializer(1001,Address.class));

		
		
        Address address = new Address("8 Van de Graaf Drive", "Burlington", "MA");
        Person original =  new Person("Bill Iards",35, "male");
        original.setAddress(address);
        
        System.out.println("Original: " + original);      
        
        Binary binary   = ExternalizableHelper.toBinary(original,ctx);

        System.out.println("\n\nBinary value ("+binary.length()+"):" + Base.toHexDump(binary.toByteArray(), 16) +"\n\n");

        Object copy     = ExternalizableHelper.fromBinary(binary,ctx);  
        System.out.println("Returned: " + copy);
        System.out.println("Copy: " + original);
        
        System.out.println("Starting timing test (serialize)\n");
        long start = System.currentTimeMillis();
        
        for ( int i = 0; i < 100000; i++) {
            binary   = ExternalizableHelper.toBinary(original,ctx);
        }
        
        long end = System.currentTimeMillis();
        System.out.printf("Total time '%d'ms for 100k serializations\n",end-start);
        
        System.out.println("Starting timing test (de-serialize)\n");
        start = System.currentTimeMillis();
        
        for ( int i = 0; i < 100000; i++) {
            copy = ExternalizableHelper.fromBinary(binary,ctx);  
        }
        
        end = System.currentTimeMillis();
        System.out.printf("Total time '%d'ms for 100k de-serializations\n",end-start);
        
	}	
}
