我这的测试用例是,定义了一个简单的正方体(Cube)对象,它还有个颜色(Color)属性对象。
TLV的编解码是我们自己写的;JSON编解码,使用的是阿里巴巴的fastjson-1.1.39.jar。
public static class Color {
@TLV(tag = 1)
long value; // 值
@TLV(tag = 2)
String name; // 名称
// setter and getter
@Override
public String toString() {
return "Color [value=" + value + ", name=" + name + "]";
}
}
public static class Cube {
@TLV(tag = 1)
int id; // 编号
@TLV(tag = 2)
Long size; // 大小
@TLV(tag = 3)
String name; // 名称
@TLV(tag = 4)
Color color;
// setter and getter
@Override
public String toString() {
return "Cube [id=" + id + ", size=" + size + ", name=" + name
+ ", color=" + color + "]";
}
}
public static void main(String[] args) throws Exception {
Color color = new Color();
color.setValue(0xFFFF);
color.setName("白色");
Cube cube = new Cube();
cube.setId(1);
cube.setSize(6L);
cube.setName("正方体");
cube.setColor(color);
//下面用两种方法测试
boolean test = true;
final long CYCEL_NUM = 1;
System.out.println(cube);
System.out.println("CYCEL_NUM:" + CYCEL_NUM);
if (test) { // TLV编解码
System.out.println("-----TLV-----");
byte[] bytes = null;
long time1 = (new Date()).getTime();
for (int i = 0; i < CYCEL_NUM; i++) {
bytes = CodecUtil.encode(cube);
}
long time2 = (new Date()).getTime();
System.out.println("encode time:" + (time2 - time1));
System.out.println("length:" + bytes.length);
Cube cb = null;
long time3 = (new Date()).getTime();
for (int i = 0; i < CYCEL_NUM; i++) {
cb = CodecUtil.decode(bytes, Cube.class);
}
long time4 = (new Date()).getTime();
System.out.println("decode time:" + (time4 - time3));
System.out.println(cb);
}
if (test) { // JSON编解码
System.out.println("-----JSON-----");
String jsonString = null;
long time1 = (new Date()).getTime();
for (int i = 0; i < CYCEL_NUM; i++) {
jsonString = JSON.toJSONString(cube);
}
long time2 = (new Date()).getTime();
System.out.println("encode time:" + (time2 - time1));
System.out.println("length:" + jsonString.length() + ",data:\n"
+ jsonString);
Cube cb = null;
long time3 = (new Date()).getTime();
for (int i = 0; i < CYCEL_NUM; i++) {
cb = JSON.parseObject(jsonString, Cube.class);
}
long time4 = (new Date()).getTime();
System.out.println("decode time:" + (time4 - time3));
System.out.println(cb);
}
}
执行结果为:TLV和JSON的编码
长度几乎相当(
71和
66);TLV是JSON的
编码速度的
5倍,
解码速度的
30倍。
Cube [id=1, size=6, name=正方体, color=Color [value=65535, name=白色]]
CYCEL_NUM:1
-----TLV-----
encode time:27
length:71
decode time:1
Cube [id=1, size=6, name=正方体, color=Color [value=65535, name=白色]]
-----JSON-----
encode time:146
length:66,data:
{"color":{"name":"白色","value":65535},"id":1,"name":"正方体","size":6}
decode time:34
Cube [id=1, size=6, name=正方体, color=Color [value=65535, name=白色]]
如果将循环次数(CYCEL_NUM)加大到1000,JSON的编解码速度几乎能赶上TLV(同一个数量级)了。
Cube [id=1, size=6, name=正方体, color=Color [value=65535, name=白色]]
CYCEL_NUM:1000
-----TLV-----
encode time:122
length:71
decode time:89
Cube [id=1, size=6, name=正方体, color=Color [value=65535, name=白色]]
-----JSON-----
encode time:169
length:66,data:
{"color":{"name":"白色","value":65535},"id":1,"name":"正方体","size":6}
decode time:58
Cube [id=1, size=6, name=正方体, color=Color [value=65535, name=白色]]
如果将循环次数(CYCEL_NUM加大到1000000,JSON的编解码速度远远超过TLV(8倍)了。
Cube [id=1, size=6, name=正方体, color=Color [value=65535, name=白色]]
CYCEL_NUM:1000000
-----TLV-----
encode time:5538
length:71
decode time:4877
Cube [id=1, size=6, name=正方体, color=Color [value=65535, name=白色]]
-----JSON-----
encode time:756
length:66,data:
{"color":{"name":"白色","value":65535},"id":1,"name":"正方体","size":6}
decode time:677
Cube [id=1, size=6, name=正方体, color=Color [value=65535, name=白色]]
由此可见,FASTJSON在缓存方面,做的挺不错啊!