一、测试条件(LoadRunner)
(1)测试并发人数:20人(有聚集点)
(2)事务完成的思考时间:1s
(3)一个事务包含4次请求(一个完整的同步过程)
(4)持续时间:30分钟
(5)联系人数量:350
(6)硬件环境(虚拟机):
CentOS5(2.6.18-164.el5内核)
4G内存
双核CPU
二、测试结果
top观察服务器的状态:
vcard:60%-80%
json:20%-30%
protobuf:7%-10%(因为protobuf转换成字符串之后又特殊字符,
在xml中无法解析,故加入base64的加解码,
同时数据传输也会减低很多)
三、优化
对json,只要按照JSONArray的字符串格式传输即可。
对于protobuf,需要定义自己的proto文件,如下:
// See README.txt for information and build instructions. package com.test.pim.common; option java_package = "com.test.pim.common"; option java_outer_classname = "ContactProtos"; message Contact { optional Name name = 1; optional Organization org = 2; repeated PhoneEntry phones = 3; repeated EmailEntry emails = 4; repeated WebPageEntry webpages = 5; repeated AddressEntry addresses = 6; repeated OnlineMessageEntry onlineMessages = 7; repeated string groups = 8; repeated string notes = 9; optional string birthday = 10; //Name Definition message Name { optional string firstName = 1; optional string middleName = 2; optional string lastName = 3; optional string suffix = 4; optional string prefix = 5; optional string nickName = 6; } //Organization Definition message Organization { optional string company = 1; optional string department = 2; optional string jobTitle = 3; } //Phone Definition message PhoneEntry { enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; WORKFAX = 3; HOMEFAX = 4; OTHERFAX = 5; OTHER = 6; CAR = 7; ISDN = 8; CUSTOM = 9; } optional string label = 1; required PhoneType type = 2 [default = MOBILE]; required string value = 3; } //Email Definition message EmailEntry { enum EmailType { HOME = 0; WORK = 1; OTHER = 2; CUSTOM = 3; } optional string label = 1; required EmailType type = 2 [default = HOME]; required string value = 3; } //WebPage Definition message WebPageEntry { enum WebPageType { HOME = 0; BLOG = 1; WORK = 2; OTHER = 3; CUSTOM = 4; } optional string label = 1; required WebPageType type = 2 [default = HOME]; required string value = 3; } //Address Definition message AddressEntry { enum AddressType { HOME = 0; WORK = 1; OTHER = 2; CUSTOM = 3; } optional string label = 1; required AddressType type = 2 [default = HOME]; required AddressExtend value = 3; } message AddressExtend { optional string street = 1; optional string city = 2; optional string state = 3; optional string postcode = 4; optional string country = 5; } //OnlineMessage Definition message OnlineMessageEntry { enum OnlineMessageType { QQ = 0; MSN = 1; SKYPE = 2; GOOGLETALK = 3; AIM = 4; YAHOO = 5; ICQ = 6; JABBER = 7; } optional string label = 1; required OnlineMessageType type = 2 [default = QQ]; required string value = 3; } }
数据构造发送代码:
ContactProtos.Contact.Builder contact = ContactProtos.Contact.newBuilder(); contact.setName(ContactProtos.Contact.Name.newBuilder().setLastName("testProtobuf").build()); contact.addPhones(ContactProtos.Contact.PhoneEntry.newBuilder().setType(ContactProtos.Contact.PhoneEntry.PhoneType.HOME) .setValue("425900").build()); contact.addPhones(ContactProtos.Contact.PhoneEntry.newBuilder().setType(ContactProtos.Contact.PhoneEntry.PhoneType.MOBILE) .setValue("13163209158").build()); byte[] encode = Base64.encode(contact.build().toByteArray()); xml.append(new String(encode, "UTF-8"));
服务端解析代码:
ContactProtos.Contact c = ContactProtos.Contact.parseFrom(Base64.decode(data.getData().getBytes("UTF-8")));