对比一下java内置序列化与hessian提供的序列化
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import org.junit.Test;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
public class Java {
@Test
public void testJava() throws IOException, ClassNotFoundException {
long start = new Date().getTime();
for (int i = 0;i < 1000000;i ++) {
Person person = new Person("Tom",12);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(person);
byte[] obj = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(obj);
ObjectInputStream ois = new ObjectInputStream(bis);
Person person2 = (Person) ois.readObject();
// System.out.println(person2);
}
long end = new Date().getTime();
System.out.println(end - start);
}
@Test
public void testHessian() throws IOException {
long start = new Date().getTime();
for (int i = 0;i < 1000000;i ++) {
Person person = new Person("Tom",12);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HessianOutput hos = new HessianOutput(bos);
hos.writeObject(person);
byte[] obj = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(obj);
HessianInput hit = new HessianInput(bis);
Person person2 = (Person) hit.readObject();
// System.out.println(person2);
}
long end = new Date().getTime();
System.out.println(end - start);
}
}
class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}