评论
# re: hessian序列化协议+memcached的缓存存取
2008-06-18 10:17
pony
另外附带说一句,我的经验,在使用memcached的时候,客户端在创建socket连接时最好把nagle设置为false.熟悉tcp协议的都知道setTcpNoDelay有什么作用.如果Tcp_NoDelay被设置为true,那么发送数据包时将使用nagle算法对要发送的数据进行缓存处理,只有当达到一定数量之后才把包发送出去,设置为false则立即发送包.对于memcached缓存,一般我们放进去的数据,以及发送的get命令都是很小的包,为了将数据及时传输出去,所以要禁用nagle. 回复 更多评论
# re: hessian序列化协议+memcached的缓存存取
2008-06-18 10:35
CHINA BAIDU
# re: hessian序列化协议+memcached的缓存存取
2008-06-18 12:19
dennis
@pony
很好的帖子,不知道换成了hession有没有测试数据?
对于setTcpNoDelay说反了吧?setTcpNoDelay为true,就是设置TCP_NODELAY,也就是禁掉Nagle算法;默认就是false,表示启用Nagle算法。禁掉Nagle算法可以提高响应性,相应地会降低吞吐量,在这个场景中没啥必要。
回复 更多评论
# re: hessian序列化协议+memcached的缓存存取
2008-06-18 13:24
lizongbo
hession 的序列化效率确实不错,
不过需要序列化的对象,试过实现java.io.Externalizable接口的方式没?
回复 更多评论
# re: hessian序列化协议+memcached的缓存存取
2008-06-18 13:41
pony
@dennis
测试hessian和java序列化的代码,写的不好请见谅^_^
由于把数据存储到memcached服务器中跟网络环境,内存有很大关系,跟memcached client设置的参数也有很大关系,所以就不做memcached的存取测试了.
下面的代码在我的机器上的结果是:
serialize 1000000 users with hessian spend 8 seconds, total size:93000000
serialize 1000000 users with java spend 17 seconds, total size:190000000
/**
* Created at 2008-06-13.
* 测试比较java和hessian的序列化效率.
*/
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import com.caucho.hessian.io.AbstractHessianOutput;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;
/**
* @author pony
*
* 如果有任何对代码的修改,请按下面的格式注明修改的内容.
* 序号 时间 作者 修改内容
* 1. 2008-6-13 pony created this class.
*/
public class TestSerializerPerformanceFucntion {
//序列化次数.
private static final int COUNT = 1000000;
private SerializerFactory factory;
public static void main(String[] args) throws Exception {
new TestSerializerPerformanceFucntion().testHessianSerializer();
new TestSerializerPerformanceFucntion().testJavaSerializer();
}
public void testHessianSerializer() throws Exception {
long curr = new Date().getTime();
int size = COUNT;
int len = 0;
for (int i=0; i
User user = new User();
ByteArrayOutputStream os = new ByteArrayOutputStream();
AbstractHessianOutput out = new Hessian2Output(os);;
SerializerFactory serializerFactory = getSerializerFactory();
out.setSerializerFactory(serializerFactory);
out.startReply();
out.writeObject(user);
out.completeReply();
out.flush();
os.flush();
len += os.size();
out.close();
}
long now = new Date().getTime();
System.out.println("serialize " + size + " users with hessian spend " + (now-curr)/1000 + " seconds, total size:" + len);
}
public void testJavaSerializer() throws Exception {
long curr = new Date().getTime();
int size = COUNT;
int len = 0;
for (int i=0; i
User user = new User();
ByteArrayOutputStream os = new ByteArrayOutputStream();
new ObjectOutputStream(os).writeObject(user);
os.flush();
len += os.size();
os.close();
}
long now = new Date().getTime();
System.out.println("serialize " + size + " users with java spend " + (now-curr)/1000 + " seconds, total size:" + len);
}
public SerializerFactory getSerializerFactory() {
if (null == factory) {
factory = new SerializerFactory();
}
return factory;
}
}
class User implements Serializable {
/**
* serial Version UID.
*/
private static final long serialVersionUID = -4845300297590675952L;
private String name = "test name";
private Date birthday = new Date();
private int age = 10;
private double money = 1000.56;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the birthday
*/
public Date getBirthday() {
return birthday;
}
/**
* @param birthday the birthday to set
*/
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the money
*/
public double getMoney() {
return money;
}
/**
* @param money the money to set
*/
public void setMoney(double money) {
this.money = money;
}
/**
* @return the serialVersionUID
*/
public static long getSerialVersionUID() {
return serialVersionUID;
}
} 回复 更多评论
# re: hessian序列化协议+memcached的缓存存取
2008-06-18 13:54
pony
@lizongbo
谢谢!这是个好建议!试过之后,只有惊讶!结果是比hessian的还快了!
运行结果是:
serialize 1000000 users with hessian spend 10 seconds, total size:93000000
serialize 1000000 users with java spend 9 seconds, total size:86000000
把上面代码中的Serializable 改为
Externalizable,实现下面两个方法:
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
String name = in.readUTF();
long time = in.readLong();
int age = in.readInt();
double money = in.readDouble();
this.setName(name);
this.setBirthday(new Date(time));
this.setAge(age);
this.setMoney(money);
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
out.writeLong(birthday.getTime());
out.writeInt(age);
out.writeDouble(money);
}
不过因为从架构上考虑,我要用php来读取cache中的内容,所以我希望序列化后的对象能遵循一个统一的协议,所以,我暂时还是要用hessian的方式. 回复 更多评论
# re: hessian序列化协议+memcached的缓存存取
2008-12-04 10:31
LiMengyan
博主能否来个hessian序集?讲讲hessian部署的结构?我想看看hessian这个服务模块是怎么给其他应用提供服务的? 回复 更多评论
# re: hessian序列化协议+memcached的缓存存取
2010-09-20 11:00
thebye85
@pony
怎么我这边测试还是hessian快 回复 更多评论
# re: hessian序列化协议+memcached的缓存存取[未登录]
2010-10-05 16:38
Jeff
# re: hessian序列化协议+memcached的缓存存取[未登录]
2011-06-24 12:03
HK
# re: hessian序列化协议+memcached的缓存存取[未登录]
2013-12-19 15:40
sa
hessian4.0.7有一个bigdecimal序列化的bug 回复 更多评论