Created
June 7, 2012 09:51
-
-
Save coderplay/2887921 to your computer and use it in GitHub Desktop.
Kryo BigObject Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.util.ArrayList; | |
import org.objenesis.strategy.StdInstantiatorStrategy; | |
import com.esotericsoftware.kryo.Kryo; | |
import com.esotericsoftware.kryo.io.Output; | |
public class TestKryo { | |
public static class Simple implements java.io.Serializable{ | |
private String name; | |
private int age; | |
private Simple son; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public Simple getSon() { | |
return son; | |
} | |
public void setSon(Simple son) { | |
this.son = son; | |
} | |
public boolean equals(Object o) { | |
if (!(o instanceof Simple)) | |
return false; | |
Simple p = (Simple) o; | |
if (age == p.getAge() && name.equalsIgnoreCase(p.getName())) { | |
if (son == null && p.getSon() == null) | |
return true; | |
else if (son.getAge() == p.getSon().getAge() | |
&& son.getName().equalsIgnoreCase(p.getSon().getName())) | |
return true; | |
} | |
return false; | |
} | |
} | |
public static class BigObject implements java.io.Serializable{ | |
private int[] blob; | |
private ArrayList<Object> list; | |
public ArrayList<Object> getList() { | |
return list; | |
} | |
public void setList(ArrayList<Object> list) { | |
this.list = list; | |
} | |
public BigObject(int[] blob){ | |
this.blob=blob; | |
} | |
public int[] getBlob() { | |
return blob; | |
} | |
public BigObject(){ | |
} | |
public void setBlob(int[] blob) { | |
this.blob = blob; | |
} | |
public boolean equals(Object o){ | |
if(!(o instanceof BigObject)) | |
return false; | |
BigObject b=(BigObject)o; | |
if(blob.length==b.getBlob().length) | |
return true; | |
return false; | |
} | |
} | |
public static int BUFFER_SIZE = Math.max( | |
Integer.getInteger("buffer_size", 1024), 256); | |
private static byte[] buffer = new byte[BUFFER_SIZE]; | |
private static Output output = new Output(buffer, -1); | |
private static Simple getSimple() { | |
Simple dad = new Simple(); | |
dad.setAge(10); | |
dad.setName("XiaoMing"); | |
Simple son = new Simple(); | |
son.setAge(10); | |
son.setName("XiaoFang"); | |
dad.setSon(son); | |
return dad; | |
} | |
private static BigObject getBigObject(){ | |
int[] a=new int[10]; | |
ArrayList<Object> list =new ArrayList<Object>(1000); | |
for(int i=0;i<1000;i++){ | |
list.add(getSimple()); | |
} | |
BigObject big=new BigObject(a); | |
big.setList(list); | |
return big; | |
} | |
public static void main(String[] args) { | |
BigObject bo = getBigObject(); | |
Kryo kryo = new Kryo(); | |
kryo.setReferences(true); | |
kryo.setRegistrationRequired(false); | |
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy()); | |
for(int i = 1; i < 30000; i ++) { | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
output.setOutputStream(bos); | |
kryo.writeClassAndObject(output, bo); | |
output.flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment