[关闭]
@fenjuly 2016-08-21T06:00:26.000000Z 字数 5385 阅读 3289

FlatBuffers调研结果


FlatBuffers简介

FlatBuffers是一个高效的跨平台序列化类库,可以在C++、C#、C、Go、Java、JavaScript、PHP和Python中使用。是Google开发的,是为了应用在游戏开发,以及其他注重性能的应用上。

特点

它主要有以下几个特点:

FlatBuffers和Protocol Buffers以及Json的比较:

FlatBuffers的使用步骤

详细用法可以参考谷歌提供的文档

  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. namespace sample;
  15. table Animal {
  16. name:string;
  17. sound:string;
  18. }
  19. root_type Animal;
  20. root_type UserList;

-c -o ./ ./Animal.fbs

  1. String name = builder.createString("brid");
  2. String sound = builder.createString("sweat");
  3. Animal.startAnimal(builder);
  4. Animal.addName(builder, name);
  5. Animal.addSound(builder, sound);
  6. int brid = Animal.endAnimal(builder);
  7. builder.finish(brid);
  8. // We now have a FlatBuffer that can be stored on disk or sent over a network.
  9. <div class="md-section-divider"></div>
  1. // This must be called after `finish()`.
  2. java.nio.ByteBuffer buf = builder.dataBuffer();
  3. // The data in this ByteBuffer does NOT start at 0, but at buf.position().
  4. // The number of bytes is buf.remaining().
  5. // Alternatively this copies the above data out of the ByteBuffer for you:
  6. bytes[] buf = builder.sizedByteArray();
  7. <div class="md-section-divider"></div>
  1. // We can access the buffer we just made directly. Pretend this came over a
  2. // network, was read off of disk, etc.
  3. java.nio.ByteBuffer buf = builder.dataBuffer();
  4. // Deserialize the data from the buffer.
  5. Animal animal = Animal.getRootAsAnimal(buf);
  6. <div class="md-section-divider"></div>

小结

利用FlatBuffers来进行数据保存及传输的优点显而易见,它利用自身特殊的编码格式,能一定程度上减少内存的占用,优化读取的性能。它对于数据结构的向前向后兼容提供了很好的扩展性,方便又高效:

FlatBuffers与Json的对比

因为我们个性化业务需要读取数据基本上都是Json,所以这里拿FlatBuffers与Json做个对比。这里以一个457KB大小的Json数据作为数据源,它经过转换成为FlatBuffers二进制文件后大小为354KB。主要做三个测试:

从两个方面来进行测试:1.读取速度。2.读取过程中的内存占用。

测试方式:用三种方式来读取Json数据文件,每种方式循环1000次,取平均值。

测试机型:三星Note3。

结果

内容 原生Json Json To FB 原生FlatBuffers
耗时 181ms 148ms 8.849ms

原生Json内存占用,依次是原生Json,Json To FB,原生FlatBuffers。

可以看出,无论是读取速度还是读取过程中的内存占用,原生Json读取性能最差,将Json转成FlatBuffers二进制文件后读取表现要稍微好一点,原生FlatBuffers读取表现很优越。

FlatBuffers与Protobuf的对比

这里参照了网上别人的对比文章。

http://blog.csdn.net/menggucaoyuan/article/details/34409433

测试用例为:序列化一个通讯录personal_info_list(table),通讯录可以认为是有每个人的信息(personal_info)的集合。每个人信息personal_info(table)有:个人id(uint)、名字(string)、年龄(byte)、性别(enum, byte)和电话号码(ulong)。

测试时,在内存中构造37个personal_info对象,并序列化之,重复这个过程100万次,然后再进行反序列化,再重复100万次。

测试环境:12Core Intel(R) Xeon(R) CPU E5-2620 0 @ 2.00GHz

  1. bin/tellist_pb
  2. encode: loop = 1000000, time diff = 14210ms
  3. decode: loop = 1000000, time diff = 11185ms
  4. buf size:841
  5. bin/tellist_pb
  6. encode: loop = 1000000, time diff = 14100ms
  7. decode: loop = 1000000, time diff = 11234ms
  8. buf size:841
  9. bin/tellist_pb
  10. encode: loop = 1000000, time diff = 14145ms
  11. decode: loop = 1000000, time diff = 11237ms
  12. buf size:841
  13. 序列化后占用内存空间841Byteencode平均运算时间42455ms / 3 = 14151.7msdecode平均计算时间33656ms / 3 = 11218.7ms
  14. <div class="md-section-divider"></div>
  1. bin/tellist_fb
  2. encode: loop = 1000000, time diff = 11666ms
  3. decode: loop = 1000000, time diff = 1141ms
  4. buf size:1712
  5. bin/tellist_fb
  6. encode: loop = 1000000, time diff = 11539ms
  7. decode: loop = 1000000, time diff = 1200ms
  8. buf size:1712
  9. bin/tellist_fb
  10. encode: loop = 1000000, time diff = 11737ms
  11. decode: loop = 1000000, time diff = 1141ms
  12. buf size:1712
  13. 序列化后占用内存空间1712Byteencode平均运算时间34942ms / 3 = 11647.3msdecode平均计算时间3482ms / 3 = 1160.7ms

可以看出:


http://www.carrotsight.com/2015/10/10/FlatBuffers%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93.html

官方也给出了测试结果http://google.github.io/flatbuffers/flatbuffers_benchmarks.html

FlatBuffers应用在我们业务的问题

参考链接

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注