- 浏览: 2560971 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
2009-10-19 16:47接口调研(一)序列化和反序列化性能对比
参考文档
http://www.iteye.com/topic/333720
jsonplugin首页(structs的json插件)
http://code.google.com/p/jsonplugin/
phprpc首页
http://www.phprpc.org/zh_CN/
hessian首页
http://hessian.caucho.com/
性能对比所用到的MAVEN2管理的JAR包
<dependency>
<groupId>com.exadel.flamingo.flex</groupId>
<artifactId>amf-serializer</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.googlecode</groupId>
<artifactId>jsonplugin</artifactId>
<version>0.34</version>
</dependency>
<dependency>
<groupId>org.phprpc</groupId>
<artifactId>phprpc-client</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.1</version>
</dependency>
测试代码:
package com.sillycat.easyserialize;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.phprpc.util.PHPSerializer;
import com.caucho.burlap.io.BurlapInput;
import com.caucho.burlap.io.BurlapOutput;
import com.caucho.hessian.io.Hessian2StreamingInput;
import com.caucho.hessian.io.Hessian2StreamingOutput;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.exadel.flamingo.flex.messaging.amf.io.AMF3Deserializer;
import com.exadel.flamingo.flex.messaging.amf.io.AMF3Serializer;
import com.googlecode.jsonplugin.JSONUtil;
import com.sillycat.easyserialize.model.User;
public class MainTest {
public static void runTest(int times, Object data, String info,
String filename) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException, IOException,
ClassNotFoundException {
Date start, end;
long size = 0;
long stime = 0;
long dtime = 0;
//综述
StringBuilder sb = new StringBuilder();
StringBuilder categories = new StringBuilder();
//序列化时间
StringBuilder dataset1 = new StringBuilder();
//反序列化时间
StringBuilder dataset2 = new StringBuilder();
//空间
StringBuilder dataset3 = new StringBuilder();
sb
.append("<graph xaxisname='Continent' yaxisname='Export' hovercapbg='DEDEBE' hovercapborder='889E6D' rotateNames='0' yAxisMaxValue='100' numdivlines='9' divLineColor='CCCCCC' divLineAlpha='80' decimalPrecision='0' showAlternateHGridColor='1' AlternateHGridAlpha='30' AlternateHGridColor='CCCCCC' caption='"
+ times
+ "次"
+ info
+ "序列化与反序列化的比较' shownames='1' showvalues='1' decimals='0' formatNumberScale='0' baseFont='Tahama' baseFontSize='12'>");
categories.append("<categories>");
dataset1.append("<dataset seriesname='序列化时间' color='FDC12E'>");
dataset2.append("<dataset seriesname='反序列化时间' color='56B9F9'>");
dataset3.append("<dataset seriesname='空间' color='C9198D'>");
System.out
.println("--------------------------------------------------------------------------------");
System.out.println(times + "次" + info);
//Java
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(data);
oos.close();
byte[] b = bos.toByteArray();
size = b.length;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bis);
ois.readObject();
ois.close();
start = new Date();
for (int i = 0; i < times; i++) {
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(data);
oos.close();
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
bis = new ByteArrayInputStream(b);
ois = new ObjectInputStream(bis);
ois.readObject();
ois.close();
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='Java' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("Java - " + "时间:" + stime + "|" + dtime + " 长度:"
+ size);
} catch (Exception e) {
System.out.println("Java 不支持该类型");
}
//PHPRPC
try {
PHPSerializer formator1 = new PHPSerializer();
byte[] b = formator1.serialize(data);
size = b.length;
formator1.unserialize(b);
start = new Date();
for (int i = 0; i < times; i++) {
formator1.serialize(data);
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
formator1.unserialize(b);
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='PHPRPC' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("PHPRPC - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("PHPRPC 不支持该类型");
}
//Hessian
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HessianOutput oos = new HessianOutput(bos);
oos.writeObject(data);
oos.close();
byte[] b = bos.toByteArray();
size = b.length;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
HessianInput ois = new HessianInput(bis);
ois.readObject();
ois.close();
start = new Date();
for (int i = 0; i < times; i++) {
bos = new ByteArrayOutputStream();
oos = new HessianOutput(bos);
oos.writeObject(data);
oos.close();
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
bis = new ByteArrayInputStream(b);
ois = new HessianInput(bis);
ois.readObject();
ois.close();
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='Hessian' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("Hessian - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("Hessian 不支持该类型");
}
//Hessian2
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Hessian2StreamingOutput oos = new Hessian2StreamingOutput(bos);
oos.writeObject(data);
oos.close();
byte[] b = bos.toByteArray();
size = b.length;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
Hessian2StreamingInput ois = new Hessian2StreamingInput(bis);
ois.readObject();
ois.close();
start = new Date();
for (int i = 0; i < times; i++) {
bos = new ByteArrayOutputStream();
oos = new Hessian2StreamingOutput(bos);
oos.writeObject(data);
oos.close();
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
bis = new ByteArrayInputStream(b);
ois = new Hessian2StreamingInput(bis);
ois.readObject();
ois.close();
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='Hessian2' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("Hessian2 - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("Hessian2 不支持该类型");
}
//Burlap
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BurlapOutput oos = new BurlapOutput(bos);
oos.writeObject(data);
oos.close();
byte[] b = bos.toByteArray();
size = b.length;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
BurlapInput ois = new BurlapInput(bis);
ois.readObject();
ois.close();
start = new Date();
for (int i = 0; i < times; i++) {
bos = new ByteArrayOutputStream();
oos = new BurlapOutput(bos);
oos.writeObject(data);
oos.close();
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
bis = new ByteArrayInputStream(b);
ois = new BurlapInput(bis);
ois.readObject();
ois.close();
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='Burlap' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("Burlap - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("Burlap 不支持该类型");
}
//AMF3
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AMF3Serializer oos = new AMF3Serializer(bos);
oos.writeObject(data);
oos.close();
byte[] b = bos.toByteArray();
size = b.length;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
AMF3Deserializer ois = new AMF3Deserializer(bis);
ois.readObject();
ois.close();
start = new Date();
for (int i = 0; i < times; i++) {
bos = new ByteArrayOutputStream();
oos = new AMF3Serializer(bos);
oos.writeObject(data);
oos.close();
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
bis = new ByteArrayInputStream(b);
ois = new AMF3Deserializer(bis);
ois.readObject();
ois.close();
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='AMF3' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("AMF3 - " + "时间:" + stime + "|" + dtime + " 长度:"
+ size);
} catch (Exception e) {
System.out.println("AMF3 不支持该类型");
}
//json-lib
try {
JSON json = JSONSerializer.toJSON(data);
size = json.toString().getBytes("UTF-8").length;
JSONSerializer.toJava(json);
start = new Date();
for (int i = 0; i < times; i++) {
JSONSerializer.toJSON(data);
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
JSONSerializer.toJava(json);
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='json-lib' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("json-lib - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("json-lib 不支持该类型");
}
//jsonplugin
try {
String json = JSONUtil.serialize(data);
size = json.getBytes("UTF-8").length;
JSONUtil.deserialize(json);
start = new Date();
for (int i = 0; i < times; i++) {
JSONUtil.serialize(data);
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
JSONUtil.deserialize(json);
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='jsonplugin' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("jsonplugin - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("jsonplugin 不支持该类型");
}
categories.append("</categories>");
dataset1.append("</dataset>");
dataset2.append("</dataset>");
dataset3.append("</dataset>");
sb.append(categories);
sb.append(dataset1);
sb.append(dataset2);
sb.append(dataset3);
sb.append("</graph>");
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(
filename), "GBK");
ow.write(sb.toString());
ow.close();
System.out
.println("--------------------------------------------------------------------------------");
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException, IOException,
ClassNotFoundException {
//整数
runTest(20000, 12, "对整数12", "1.xml");
//布尔值
runTest(20000, true, "对布尔值true", "2.xml");
//null
runTest(20000, null, "对 null", "3.xml");
//浮点数
runTest(20000, 1.2, "对浮点数1.2", "4.xml");
//LONG型
runTest(20000, 1234567890987654321L, "对UInt64型1234567890987654321",
"5.xml");
//无穷大
runTest(20000, Double.POSITIVE_INFINITY, "对无穷大", "6.xml");
//字符串
String s = "PHPRPC - perfect high performance remote procedure call";
runTest(20000, s, "对字符串“" + s + "”", "7.xml");
//10000个字节数组
byte[] ba = new byte[10000];
for (int i = 0; i < 10000; i++) {
ba[i] = (byte) (i % 255);
}
runTest(2000, ba, "对10000个元素的字节数组", "8.xml");
//100个相同字符串的字符串数组
String[] sa = new String[100];
for (int i = 0; i < 100; i++) {
sa[i] = s;
}
runTest(2000, sa, "对100个相同元素的字符串数组", "9.xml");
//100个不同字符串的字符串数组
sa = new String[100];
for (int i = 0; i < 100; i++) {
sa[i] = s + i;
}
runTest(2000, sa, "对100个不同元素的字符串数组", "10.xml");
//HASHTABLE的字符串
HashMap h = new HashMap();
for (int i = 0; i < 100; i++) {
h.put(s + i, s + (i + 100));
}
runTest(2000, h, "对索引不同内容不同具有100个字符串元素和字符串索引的 Hashtable", "11.xml");
//自定义POJO
User tc = new User();
tc.setId(1);
tc.setUserName("Ma Bingyao");
tc.setPassword("PHPRPC");
tc.setAge(28);
runTest(200000, tc, "对自定义类型对象", "12.xml");
}
}
参考文档
http://www.iteye.com/topic/333720
jsonplugin首页(structs的json插件)
http://code.google.com/p/jsonplugin/
phprpc首页
http://www.phprpc.org/zh_CN/
hessian首页
http://hessian.caucho.com/
性能对比所用到的MAVEN2管理的JAR包
<dependency>
<groupId>com.exadel.flamingo.flex</groupId>
<artifactId>amf-serializer</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.googlecode</groupId>
<artifactId>jsonplugin</artifactId>
<version>0.34</version>
</dependency>
<dependency>
<groupId>org.phprpc</groupId>
<artifactId>phprpc-client</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.1</version>
</dependency>
测试代码:
package com.sillycat.easyserialize;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.phprpc.util.PHPSerializer;
import com.caucho.burlap.io.BurlapInput;
import com.caucho.burlap.io.BurlapOutput;
import com.caucho.hessian.io.Hessian2StreamingInput;
import com.caucho.hessian.io.Hessian2StreamingOutput;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.exadel.flamingo.flex.messaging.amf.io.AMF3Deserializer;
import com.exadel.flamingo.flex.messaging.amf.io.AMF3Serializer;
import com.googlecode.jsonplugin.JSONUtil;
import com.sillycat.easyserialize.model.User;
public class MainTest {
public static void runTest(int times, Object data, String info,
String filename) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException, IOException,
ClassNotFoundException {
Date start, end;
long size = 0;
long stime = 0;
long dtime = 0;
//综述
StringBuilder sb = new StringBuilder();
StringBuilder categories = new StringBuilder();
//序列化时间
StringBuilder dataset1 = new StringBuilder();
//反序列化时间
StringBuilder dataset2 = new StringBuilder();
//空间
StringBuilder dataset3 = new StringBuilder();
sb
.append("<graph xaxisname='Continent' yaxisname='Export' hovercapbg='DEDEBE' hovercapborder='889E6D' rotateNames='0' yAxisMaxValue='100' numdivlines='9' divLineColor='CCCCCC' divLineAlpha='80' decimalPrecision='0' showAlternateHGridColor='1' AlternateHGridAlpha='30' AlternateHGridColor='CCCCCC' caption='"
+ times
+ "次"
+ info
+ "序列化与反序列化的比较' shownames='1' showvalues='1' decimals='0' formatNumberScale='0' baseFont='Tahama' baseFontSize='12'>");
categories.append("<categories>");
dataset1.append("<dataset seriesname='序列化时间' color='FDC12E'>");
dataset2.append("<dataset seriesname='反序列化时间' color='56B9F9'>");
dataset3.append("<dataset seriesname='空间' color='C9198D'>");
System.out
.println("--------------------------------------------------------------------------------");
System.out.println(times + "次" + info);
//Java
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(data);
oos.close();
byte[] b = bos.toByteArray();
size = b.length;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bis);
ois.readObject();
ois.close();
start = new Date();
for (int i = 0; i < times; i++) {
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(data);
oos.close();
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
bis = new ByteArrayInputStream(b);
ois = new ObjectInputStream(bis);
ois.readObject();
ois.close();
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='Java' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("Java - " + "时间:" + stime + "|" + dtime + " 长度:"
+ size);
} catch (Exception e) {
System.out.println("Java 不支持该类型");
}
//PHPRPC
try {
PHPSerializer formator1 = new PHPSerializer();
byte[] b = formator1.serialize(data);
size = b.length;
formator1.unserialize(b);
start = new Date();
for (int i = 0; i < times; i++) {
formator1.serialize(data);
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
formator1.unserialize(b);
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='PHPRPC' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("PHPRPC - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("PHPRPC 不支持该类型");
}
//Hessian
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HessianOutput oos = new HessianOutput(bos);
oos.writeObject(data);
oos.close();
byte[] b = bos.toByteArray();
size = b.length;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
HessianInput ois = new HessianInput(bis);
ois.readObject();
ois.close();
start = new Date();
for (int i = 0; i < times; i++) {
bos = new ByteArrayOutputStream();
oos = new HessianOutput(bos);
oos.writeObject(data);
oos.close();
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
bis = new ByteArrayInputStream(b);
ois = new HessianInput(bis);
ois.readObject();
ois.close();
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='Hessian' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("Hessian - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("Hessian 不支持该类型");
}
//Hessian2
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Hessian2StreamingOutput oos = new Hessian2StreamingOutput(bos);
oos.writeObject(data);
oos.close();
byte[] b = bos.toByteArray();
size = b.length;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
Hessian2StreamingInput ois = new Hessian2StreamingInput(bis);
ois.readObject();
ois.close();
start = new Date();
for (int i = 0; i < times; i++) {
bos = new ByteArrayOutputStream();
oos = new Hessian2StreamingOutput(bos);
oos.writeObject(data);
oos.close();
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
bis = new ByteArrayInputStream(b);
ois = new Hessian2StreamingInput(bis);
ois.readObject();
ois.close();
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='Hessian2' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("Hessian2 - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("Hessian2 不支持该类型");
}
//Burlap
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BurlapOutput oos = new BurlapOutput(bos);
oos.writeObject(data);
oos.close();
byte[] b = bos.toByteArray();
size = b.length;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
BurlapInput ois = new BurlapInput(bis);
ois.readObject();
ois.close();
start = new Date();
for (int i = 0; i < times; i++) {
bos = new ByteArrayOutputStream();
oos = new BurlapOutput(bos);
oos.writeObject(data);
oos.close();
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
bis = new ByteArrayInputStream(b);
ois = new BurlapInput(bis);
ois.readObject();
ois.close();
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='Burlap' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("Burlap - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("Burlap 不支持该类型");
}
//AMF3
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AMF3Serializer oos = new AMF3Serializer(bos);
oos.writeObject(data);
oos.close();
byte[] b = bos.toByteArray();
size = b.length;
ByteArrayInputStream bis = new ByteArrayInputStream(b);
AMF3Deserializer ois = new AMF3Deserializer(bis);
ois.readObject();
ois.close();
start = new Date();
for (int i = 0; i < times; i++) {
bos = new ByteArrayOutputStream();
oos = new AMF3Serializer(bos);
oos.writeObject(data);
oos.close();
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
bis = new ByteArrayInputStream(b);
ois = new AMF3Deserializer(bis);
ois.readObject();
ois.close();
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='AMF3' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("AMF3 - " + "时间:" + stime + "|" + dtime + " 长度:"
+ size);
} catch (Exception e) {
System.out.println("AMF3 不支持该类型");
}
//json-lib
try {
JSON json = JSONSerializer.toJSON(data);
size = json.toString().getBytes("UTF-8").length;
JSONSerializer.toJava(json);
start = new Date();
for (int i = 0; i < times; i++) {
JSONSerializer.toJSON(data);
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
JSONSerializer.toJava(json);
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='json-lib' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("json-lib - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("json-lib 不支持该类型");
}
//jsonplugin
try {
String json = JSONUtil.serialize(data);
size = json.getBytes("UTF-8").length;
JSONUtil.deserialize(json);
start = new Date();
for (int i = 0; i < times; i++) {
JSONUtil.serialize(data);
}
end = new Date();
stime = end.getTime() - start.getTime();
start = new Date();
for (int i = 0; i < times; i++) {
JSONUtil.deserialize(json);
}
end = new Date();
dtime = end.getTime() - start.getTime();
categories.append("<category name='jsonplugin' />");
dataset1.append("<set value='" + stime + "'/>");
dataset2.append("<set value='" + dtime + "'/>");
dataset3.append("<set value='" + size + "'/>");
System.out.println("jsonplugin - " + "时间:" + stime + "|" + dtime
+ " 长度:" + size);
} catch (Exception e) {
System.out.println("jsonplugin 不支持该类型");
}
categories.append("</categories>");
dataset1.append("</dataset>");
dataset2.append("</dataset>");
dataset3.append("</dataset>");
sb.append(categories);
sb.append(dataset1);
sb.append(dataset2);
sb.append(dataset3);
sb.append("</graph>");
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(
filename), "GBK");
ow.write(sb.toString());
ow.close();
System.out
.println("--------------------------------------------------------------------------------");
}
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException, IOException,
ClassNotFoundException {
//整数
runTest(20000, 12, "对整数12", "1.xml");
//布尔值
runTest(20000, true, "对布尔值true", "2.xml");
//null
runTest(20000, null, "对 null", "3.xml");
//浮点数
runTest(20000, 1.2, "对浮点数1.2", "4.xml");
//LONG型
runTest(20000, 1234567890987654321L, "对UInt64型1234567890987654321",
"5.xml");
//无穷大
runTest(20000, Double.POSITIVE_INFINITY, "对无穷大", "6.xml");
//字符串
String s = "PHPRPC - perfect high performance remote procedure call";
runTest(20000, s, "对字符串“" + s + "”", "7.xml");
//10000个字节数组
byte[] ba = new byte[10000];
for (int i = 0; i < 10000; i++) {
ba[i] = (byte) (i % 255);
}
runTest(2000, ba, "对10000个元素的字节数组", "8.xml");
//100个相同字符串的字符串数组
String[] sa = new String[100];
for (int i = 0; i < 100; i++) {
sa[i] = s;
}
runTest(2000, sa, "对100个相同元素的字符串数组", "9.xml");
//100个不同字符串的字符串数组
sa = new String[100];
for (int i = 0; i < 100; i++) {
sa[i] = s + i;
}
runTest(2000, sa, "对100个不同元素的字符串数组", "10.xml");
//HASHTABLE的字符串
HashMap h = new HashMap();
for (int i = 0; i < 100; i++) {
h.put(s + i, s + (i + 100));
}
runTest(2000, h, "对索引不同内容不同具有100个字符串元素和字符串索引的 Hashtable", "11.xml");
//自定义POJO
User tc = new User();
tc.setId(1);
tc.setUserName("Ma Bingyao");
tc.setPassword("PHPRPC");
tc.setAge(28);
runTest(200000, tc, "对自定义类型对象", "12.xml");
}
}
发表评论
-
RESTful JSON Mock Server
2015-03-19 11:58 803RESTful JSON Mock Server C ... -
Performance Tool(7)Improve Lua and Wrk
2015-01-17 06:37 1043Performance Tool(7)Improve Lua ... -
Performance Tool(6)Gatling Upgrade to 2.1.2 Version Or wrk
2015-01-10 01:15 983Performance Tool(6)Gatling Upg ... -
Performance Tool(5)Upgrade to 2.0.x
2014-08-27 03:34 1134Performance Tool(5)Upgrade to 2 ... -
Performance Tool(4)CSV File Data Feeder
2014-08-25 10:50 1037Performance Tool(4)CSV File Dat ... -
wrk with LuaJIT
2014-08-19 06:30 1343wrk with LuaJITHere is an exa ... -
Performance Tool(3)Gatling Upgrade and Cluster
2014-07-25 02:32 1344Performance Tool(3)Gatling Upgr ... -
WRK a HTTP Benchmarking Tool
2014-03-07 04:42 1153WRK a HTTP Benchmarking Tool1 ... -
Performance Tool(1)Gatling
2013-03-15 05:28 1303Performance Tool(1)Gatling 1. ... -
Jenkins Configuration(4)Improve Shell Script Debug/Info Message
2013-01-07 06:32 1346Jenkins Configuration(4)Improve ... -
Jenkins Configuration(3)Shell Script
2012-12-28 01:17 2691Jenkins Configuration(3)Shell S ... -
Eclipse Plugin(2)SOAP UI
2012-06-08 10:48 1358Eclipse Plugin(2)SOAP UI Plugi ... -
Spring3 and REST Integeration(VII)Controller JUnit Test and Mock/Spring Test MVC
2012-04-06 15:57 1916Spring3 and REST Integeration(V ... -
Spring3 and REST Integration(VI)Controller JUnit Test and Mock/Spring HandlerAda
2012-04-06 15:51 1828Spring3 and REST Integration(VI ... -
Spring3 and REST Integration(V)Controller JUnit Test and Mock/HandlerAdapter
2012-04-06 15:41 2845Spring3 and REST Integration(V) ... -
Spring3 and REST Integration(IV)Controller JUnit Test and Mock/Servlet
2012-04-06 15:13 2003Spring3 and REST Integration(IV ... -
Jbehave(2)Some Improvement and POM changes
2012-03-28 23:11 1438Jbehave(2)Some Improvement and ... -
buildr(1)Introduce and Install
2011-12-23 16:37 2195buildr(1)Introduce and Install ... -
Jbehave(1) First Web Page Sample
2011-10-26 15:00 2212Jbehave(1) First Web Page Sampl ... -
WarcraftIII Problem on English Win7
2011-07-25 10:18 1954WarcraftIII Problem on English ...
相关推荐
* 序列化和反序列化过程中可能会出现性能问题 * 序列化和反序列化过程中可能会出现安全问题 Java 序列化和反序列化是 Java 语言中的一种重要机制,用于将对象转换为字节流,以便在网络上传输或存储。开发者需要根据...
在IT领域,序列化和反序列化是两个关键的概念,特别是在网络通信、数据持久化以及对象存储中。本文将深入探讨Hessian框架的基础知识,它是一个高效的二进制序列化协议,广泛应用于Java和.NET之间跨语言通信。通过...
在编程领域,序列化和反序列化是两个关键的概念,特别是在数据存储、网络传输和对象持久化等场景中。它们允许我们将对象的状态转换为字节流(序列化),然后在需要的时候将字节流还原为原来的对象(反序列化)。在C#...
1. 复杂类型的处理:除了基本类型外,还可以序列化和反序列化自定义类、结构体以及容器(如vector、map等)。对于自定义类型,通常需要重载`operator和`operator>>`,或者使用nlohmann/json库中的`to_json`和`from_...
在本项目中,“学生管理系统(序列化和反序列化)”是一个基于Java或类似编程语言实现的系统,其核心功能是有效地存储和恢复学生信息。序列化和反序列化是这个系统的关键技术,它们允许程序将对象的状态转化为可存储...
Java对象的序列化和反序列化是Java编程中一项重要的技术,主要用于将对象的状态转换为字节流,以便存储或在网络上传输。这一过程对于理解Java的IO操作、持久化数据以及实现分布式通信等场景非常关键。 首先,我们来...
- **性能考虑:** 序列化和反序列化操作可能会对应用程序性能造成一定影响,特别是在大量数据处理时。 综上所述,Java序列化提供了一种简单有效的方式来处理对象的持久化和传输需求,但开发者也需要考虑其可能带来...
protobuf序列化和反序列化技术是大数据处理领域中不可或缺的一部分,尤其在实时大数据场景下,高效的数据传输和存储对性能有着直接影响。谷歌推出的Protocol Buffers(简称protobuf)是一种语言无关、平台无关的数据...
这使得XML序列化和反序列化变得简单,因为编译后的类可以直接与XML文件进行转换,无需手动编写解析和构造对象的代码。 在实际应用中,XML序列化和反序列化有多种用途,例如: 1. 存储和加载应用程序设置:用户配置...
3. **jackson-annotations**:这个模块包含了一组注解,用于定制序列化和反序列化的规则。`jackson-annotations-2.2.3.jar` 提供了诸如`@JsonProperty`、`@JsonIgnore`、`@JsonInclude`等注解,可以方便地控制哪些...
2. commons-lang3-3.3.2.jar:Apache Commons Lang库提供了许多实用的Java语言功能,包括序列化和反序列化的辅助工具,如`SerializationUtils`类,可用于简化对象的序列化和反序列化操作。 3. commons-beanutils-...
本文将深入探讨如何使用JavaScript对表单数据进行序列化和反序列化。 序列化是将数据结构或对象转换为字符串的过程,便于存储或传输。在HTML表单中,序列化通常是指将表单元素的值转换为URL编码的字符串,以便通过...
C#提供了Newtonsoft.Json库(也称为Json.NET),这是一个功能强大的JSON库,可以方便地进行JSON序列化和反序列化。例如,通过JsonConvert类的SerializeObject和DeserializeObject方法,可以轻松地在C#对象和JSON字符...
总结一下,`HashTable`的序列化和反序列化是通过实现`Serializable`接口并利用`ObjectOutputStream`和`ObjectInputStream`来完成的。在实际应用中,这有助于数据的持久化存储和跨进程通信。然而,进行序列化操作时,...
在编程领域,序列化和反序列化是两个关键的概念,它们用于将对象的状态转换为可存储或可传输的格式,然后在需要时恢复为原始对象。Boost库提供了一个强大的工具——Boost.Serialization,来帮助程序员实现这个功能。...
序列化和反序列化是计算机科学中的重要概念,特别是在数据存储、网络通信和对象持久化等领域。接口在编程中则扮演着定义规范和提供抽象的角色。让我们深入了解一下这些概念。 **序列化** 是将对象的状态转换为可...
而在Java中,我们可以通过实现`Serializable`接口来使类支持序列化,或者使用`java.io.ObjectOutputStream`和`java.io.ObjectInputStream`进行对象的序列化和反序列化。 接下来,我们讨论反序列化。反序列化是序列...
在Java中,可以使用`java.io.Serializable`接口标记一个类为可序列化的,然后使用`ObjectOutputStream`来序列化对象,而`ObjectInputStream`则用于反序列化。 在描述的"序列化与反序列化Demo"中,我们可以推测这...
在编程领域,序列化和反序列化是两个关键的概念,主要用在数据持久化、网络传输和对象状态的保存与恢复等场景。本示例旨在教你如何在Java中实现自定义的序列化和反序列化过程。让我们深入探讨这两个概念。 **序列化...
Xson是一个Java对象序列化和反序列化程序。支持Java对象到字节数组的序列化,和从字节数组到Java对象的反序列化。 Maven: <groupId>com.github.xsonorg</groupId> <artifactId>xson-core <version>1.0.1 ...