protobuf 还没看,所以暂时没有加入对比,但是已经默认是最快的序列化协议了。
测试点:
测试代码:
项目随便找一个pojo.我自己找的常见属性,没有严格测试到每一种数据类型。所以测试数据仅供自己学习。
package com.daojia.hessian;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
public class Person implements Serializable {
private static final long serialVersionUID = -1923645274767028479L;
private List address;
private Date brithday;
private boolean gender;
private double height;
private int id;
private String name;
private int phone;
private float weight;
public List getAddress() {
return address;
}
public void setAddress(List address) {
this.address = address;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
测试接口:
package com.daojia.hessian;
public interface TestCallback {
String getName();
byte[] writeObject(Object source);
Object readObject(byte[] bytes);
}
package com.daojia.hessian;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thoughtworks.xstream.XStream;
public class Test {
final static int testCount = 1000000;
public static void main(String[] args) {
// TODO Auto-generated method stub
// 序列化
testTemplate(new TestCallback() {
public String getName() {
return "Serializable Test";
}
@Override
public byte[] writeObject(Object source) {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream output = new ObjectOutputStream(bout);
output.writeObject(source);
return bout.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public Object readObject(byte[] bytes) {
try {
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
ObjectInputStream input = new ObjectInputStream(bin);
return input.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}, getPerson(), testCount);
// Xstream测试
final XStream xstream = new XStream();
testTemplate(new TestCallback() {
public String getName() {
return "Xstream test";
}
@Override
public byte[] writeObject(Object source) {
return xstream.toXML(source).getBytes();
}
@Override
public Object readObject(byte[] bytes) {
return xstream.fromXML(new ByteArrayInputStream(bytes));
}
}, getPerson(), testCount);
// json测试
testTemplate(new TestCallback() {
public String getName() {
return "fastJson Test";
}
@Override
public byte[] writeObject(Object source) {
try {
//String tmp =JSON.toJSONString(source);
//System.out.println(":"+tmp);
return JSON.toJSONString(source).getBytes();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public Object readObject(byte[] bytes) {
try {
//String tmp =new String(bytes);
//System.out.println("parse before:"+tmp);
JSONObject object = JSON.parseObject(new String(bytes));
return object;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}, getPerson(), testCount);
// json测试
final ObjectMapper objectMapper = new ObjectMapper();
// JSON configuration for compatibility
objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
testTemplate(new TestCallback() {
public String getName() {
return "Jackson Test";
}
@Override
public byte[] writeObject(Object source) {
try {
return objectMapper.writeValueAsBytes(source);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public Object readObject(byte[] bytes) {
try {
return objectMapper.readValue(bytes, 0, bytes.length, Person.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}, getPerson(), testCount);
// //hession
testTemplate(new TestCallback() {
public String getName() {
return "hessian 2 with no deflat";
}
@Override
public byte[] writeObject(Object source) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Hessian2Output out = new Hessian2Output(bos);
// out.startMessage();
out.writeObject(source);
// out.completeMessage();
out.flush();
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public Object readObject(byte[] bytes) {
try {
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
Hessian2Input in = new Hessian2Input(bin);
// in.startMessage();
Object obj = in.readObject();
// in.completeMessage();
return obj;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}, getPerson(), testCount);
// hessian 1 with no deflat
testTemplate(new TestCallback() {
public String getName() {
return "hessian 1 with no deflat";
}
@Override
public byte[] writeObject(Object source) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
HessianOutput out = new HessianOutput(bos);
out.writeObject(source);
out.flush();
return bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public Object readObject(byte[] bytes) {
try {
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
HessianInput in = new HessianInput(bin);
Object obj = in.readObject();
in.close();
return obj;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}, getPerson(), testCount);
}
private static void testTemplate(TestCallback callback, Object source, int count) {
int warmup = 10;
// 先进行预热,加载一些类,避免影响测试
for (int i = 0; i < warmup; i++) {
byte[] bytes = callback.writeObject(source);
callback.readObject(bytes);
}
restoreJvm(); // 进行GC回收
// 进行测试
long start = System.nanoTime();
long size = 0l;
for (int i = 0; i < count; i++) {
byte[] bytes = callback.writeObject(source);
// System.out.println("byte:"+bytes.toString());
size = size + bytes.length;
callback.readObject(bytes);
// System.out.println(callback.readObject(bytes));
bytes = null;
}
long nscost = (System.nanoTime() - start);
System.out.println(callback.getName() + " total cost=" + nscost + "ns , each cost="
+ (nscost / count) + "ns , and byte sizes = " + size / count);
restoreJvm();// 进行GC回收
}
private static void restoreJvm() {
// TODO Auto-generated method stub
int maxRestoreJvmLoops = 10;
long memUsedPrev = memoryUsed();
for (int i = 0; i < maxRestoreJvmLoops; i++) {
System.runFinalization();
System.gc();
long memUsedNow = memoryUsed();
// break early if have no more finalization and get constant mem used
if ((ManagementFactory.getMemoryMXBean().getObjectPendingFinalizationCount() == 0)
&& (memUsedNow >= memUsedPrev)) {
break;
} else {
memUsedPrev = memUsedNow;
}
}
}
private static long memoryUsed() {
Runtime rt = Runtime.getRuntime();
return rt.totalMemory() - rt.freeMemory();
}
public byte[] writeObject(Object source) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Hessian2Output out = new Hessian2Output(bos);
// out.startMessage();
out.writeObject(source);
// out.completeMessage();
out.flush();
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public Object readObject(byte[] bytes) {
try {
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
Hessian2Input in = new Hessian2Input(bin);
// in.startMessage();
Object obj = in.readObject();
// in.completeMessage();
return obj;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static Person getPerson() {
Person person = new Person();
List l = new ArrayList();
l.add("Beijing");
l.add("TaiWan");
person.setAddress(l);
person.setBrithday(new Date());
person.setGender(false);
person.setHeight(168.5D);
person.setId(300);
person.setName("Jack");
person.setPhone(188888888);
person.setWeight(55.2F);
return person;
}
}
测试demo网上找的demo,觉得挺好的,自己改造了下。
测试结果:
Serializable Test total cost=13418353185ns , each cost=13418ns , and byte sizes = 316
Xstream test total cost=30640534128ns , each cost=30640ns , and byte sizes = 325
fastJson Test total cost=2053259922ns , each cost=2053ns , and byte sizes = 142
Jackson Test total cost=2144118971ns , each cost=2144ns , and byte sizes = 142
hessian 2 with no deflat total cost=3356286519ns , each cost=3356ns , and byte sizes = 133
hessian 1 with no deflat total cost=11453543539ns , each cost=11453ns , and byte sizes = 169
可以看出:耗时上
fastJson < Jackson < hessian2 <hessian1 < jdk<xstream
fastjson比jackson快一点点,没那么显著,我觉得都可以。hessian2是慢了点。dubbox已经支持protobuf了。
测试版本: jdk 1.8
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.51</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.9.5</version>
</dependency>
参考:http://agapple.iteye.com/blog/859052
文章浏览阅读1.6k次。安装配置gi、安装数据库软件、dbca建库见下:http://blog.csdn.net/kadwf123/article/details/784299611、检查集群节点及状态:[root@rac2 ~]# olsnodes -srac1 Activerac2 Activerac3 Activerac4 Active[root@rac2 ~]_12c查看crs状态
文章浏览阅读1.3w次,点赞45次,收藏99次。我个人用的是anaconda3的一个python集成环境,自带jupyter notebook,但在我打开jupyter notebook界面后,却找不到对应的虚拟环境,原来是jupyter notebook只是通用于下载anaconda时自带的环境,其他环境要想使用必须手动下载一些库:1.首先进入到自己创建的虚拟环境(pytorch是虚拟环境的名字)activate pytorch2.在该环境下下载这个库conda install ipykernelconda install nb__jupyter没有pytorch环境
文章浏览阅读5.2k次,点赞19次,收藏28次。选择scoop纯属意外,也是无奈,因为电脑用户被锁了管理员权限,所有exe安装程序都无法安装,只可以用绿色软件,最后被我发现scoop,省去了到处下载XXX绿色版的烦恼,当然scoop里需要管理员权限的软件也跟我无缘了(譬如everything)。推荐添加dorado这个bucket镜像,里面很多中文软件,但是部分国外的软件下载地址在github,可能无法下载。以上两个是官方bucket的国内镜像,所有软件建议优先从这里下载。上面可以看到很多bucket以及软件数。如果官网登陆不了可以试一下以下方式。_scoop-cn
文章浏览阅读4.5k次,点赞2次,收藏3次。首先要有一个color-picker组件 <el-color-picker v-model="headcolor"></el-color-picker>在data里面data() { return {headcolor: ’ #278add ’ //这里可以选择一个默认的颜色} }然后在你想要改变颜色的地方用v-bind绑定就好了,例如:这里的:sty..._vue el-color-picker
文章浏览阅读640次。基于芯片日益增长的问题,所以内核开发者们引入了新的方法,就是在内核中只保留函数,而数据则不包含,由用户(应用程序员)自己把数据按照规定的格式编写,并放在约定的地方,为了不占用过多的内存,还要求数据以根精简的方式编写。boot启动时,传参给内核,告诉内核设备树文件和kernel的位置,内核启动时根据地址去找到设备树文件,再利用专用的编译器去反编译dtb文件,将dtb还原成数据结构,以供驱动的函数去调用。firmware是三星的一个固件的设备信息,因为找不到固件,所以内核启动不成功。_exynos 4412 刷机
文章浏览阅读2w次,点赞24次,收藏42次。Linux系统配置jdkLinux学习教程,Linux入门教程(超详细)_linux配置jdk
文章浏览阅读3.3k次,点赞5次,收藏19次。xlabel('\delta');ylabel('AUC');具体符号的对照表参照下图:_matlab微米怎么输入
文章浏览阅读119次。顺序读写指的是按照文件中数据的顺序进行读取或写入。对于文本文件,可以使用fgets、fputs、fscanf、fprintf等函数进行顺序读写。在C语言中,对文件的操作通常涉及文件的打开、读写以及关闭。文件的打开使用fopen函数,而关闭则使用fclose函数。在C语言中,可以使用fread和fwrite函数进行二进制读写。 Biaoge 于2024-03-09 23:51发布 阅读量:7 ️文章类型:【 C语言程序设计 】在C语言中,用于打开文件的函数是____,用于关闭文件的函数是____。
文章浏览阅读3.4k次,点赞2次,收藏13次。跟随鼠标移动的粒子以grid(SOP)为partical(SOP)的资源模板,调整后连接【Geo组合+point spirit(MAT)】,在连接【feedback组合】适当调整。影响粒子动态的节点【metaball(SOP)+force(SOP)】添加mouse in(CHOP)鼠标位置到metaball的坐标,实现鼠标影响。..._touchdesigner怎么让一个模型跟着鼠标移动
文章浏览阅读178次。项目运行环境配置:Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。项目技术:Springboot + mybatis + Maven +mysql5.7或8.0+html+css+js等等组成,B/S模式 + Maven管理等等。环境需要1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。_基于java技术的停车场管理系统实现与设计
文章浏览阅读3.5k次。前言对于MediaPlayer播放器的源码分析内容相对来说比较多,会从Java-&amp;gt;Jni-&amp;gt;C/C++慢慢分析,后面会慢慢更新。另外,博客只作为自己学习记录的一种方式,对于其他的不过多的评论。MediaPlayerDemopublic class MainActivity extends AppCompatActivity implements SurfaceHolder.Cal..._android多媒体播放源码分析 时序图
文章浏览阅读2.4k次,点赞41次,收藏13次。java 数据结构与算法 ——快速排序法_快速排序法