- 结论
- 数据包大小区别不大。
- 但是小对象 json 快,约快3倍。
- 大对象java的序列化速度慢慢追平json(单个对象json长度3000的左右追平),这个可能和java对象转json有关系,但是装换这步在序列化前必不可可少
- 另外序列化和不止和对象大小有关还和字段数量有关系,这个因素我没测试,我这里都是7字段。
- 数据包大小总是 java 比 json小5M,应该是这5MB的差距只和序列化的次数有关(我这里都是100W次)和字段名有关,json比java对了字段名java的字段名应该有特殊处理,或者直接省略了。
- 实验过程数据记录
- goods 是空的时候,100W次对象序列化
- goods json字符串长度 158字符的时候,10W次序列化
- goods json字符串长度 477 字符的时候,10W次序列化(goods只有7个属性,我是通过改变随机生成的字段长度老控制的)
- goods json字符串长度 3357 字符的时候,10W次序列化(goods只有7个属性,我是通过改变随机生成的字段长度老控制的)
- 测试代码如下
public class SerializeTest {
int size = 10000*10;
@Test
public void goodsSize() throws IOException {
System.out.println("goods示例:" + JSONUtil.toJsonStr( Goods.randomGoods() ));
System.out.println("goods示例:" + JSONUtil.toJsonStr( Goods.randomGoods() ).length());
}
@Test
public void java() throws IOException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
List<Goods> list = new ArrayList<>();
for( int i=0;i<size;i++ ){
//list.add( Goods() );
list.add( Goods.randomGoods() );
}
File file = new File("C:\\Users\\ZHANGYUKUN\\Desktop\\java.data");
FileOutputStream fileInputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileInputStream);
for (Goods goods : list) {
//这个序列化使用了java的序列化,必须实现Serializable接口,否则要报错
objectOutputStream.writeObject( goods );
}
objectOutputStream.flush();
objectOutputStream.close();
fileInputStream.close();
stopWatch.stop();
long time = stopWatch.getLastTaskTimeMillis();
System.out.println("java序列化耗时:" + time);
}
@Test
public void json() throws IOException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
List<Goods> list = new ArrayList<>();
for( int i=0;i<size;i++ ){
//list.add( new Goods() );
list.add( Goods.randomGoods() );
}
File file = new File("C:\\Users\\ZHANGYUKUN\\Desktop\\json.data");
FileOutputStream fileInputStream = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileInputStream);
for (Goods goods : list) {
outputStreamWriter.write( JSONUtil.toJsonStr( goods ) );
}
outputStreamWriter.flush();
outputStreamWriter.close();
fileInputStream.close();
stopWatch.stop();
long time = stopWatch.getLastTaskTimeMillis();
System.out.println("json耗时:" + time);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- goods结构如下,改变good的长度通过 randomGoods 方法里面随机生成字段长度
package com.lomi.entity;
import com.lomi.utils.CodeUtil;
import cn.hutool.core.util.IdUtil;
import com.lomi.utils.IdUtils;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
public class Goods implements Serializable {
private Long id;
private Date createDate;
private String name;
private Integer stock;
private String des;
private String des2;
private String data;
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Goods(String name ) {
this.name = name;
}
public Goods() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public String name(int i) {
return "name????" + i;
}
public Goods setName(String name) {
this.name = name == null ? null : name.trim();
return this;
}
public Integer getStock() {
return stock;
}
public void setStock(Integer stock) {
this.stock = stock;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des == null ? null : des.trim();
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data == null ? null : data.trim();
}
public String getDes2() {
return des2;
}
public void setDes2(String des2) {
this.des2 = des2;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", createDate=" + createDate +
", name='" + name + '\'' +
", stock=" + stock +
", des='" + des + '\'' +
", des2='" + des2 + '\'' +
", data='" + data + '\'' +
'}';
}
public static Goods randomGoods() {
Goods goods = new Goods();
goods.setId(IdUtils.nextId());
goods.setData( CodeUtil.getRandomNum18() );
goods.setDes( CodeUtil.randomCode(16) ); //我通过改变这几个属性的长度来改变goods的大小的
goods.setDes2( CodeUtil.randomCode(16) ); //我通过改变这几个属性的长度来改变goods的大小的
goods.setName( CodeUtil.randomCode(3) ); //我通过改变这几个属性的长度来改变goods的大小的
goods.setStock( Integer.valueOf( CodeUtil.randomNumCode(4) ) );
goods.setCreateDate( new Date());
return goods;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.