func BenchmarkFlatBuffersUnmarshal(b *testing.B) { b.StopTimer() builder := flatbuffers.NewBuilder(0) data := generate() ser := make([][]byte, len(data)) for i, d := range data { ser[i] = serializeUsingFlatBuffers(builder, d) } b.ReportAllocs() b.StartTimer() for i := 0; i < b.N; i++ { n := rand.Intn(len(ser)) o := FlatBufferA{} sData := ser[n] o.Init(sData, flatbuffers.GetUOffsetT(sData)) if validate != "" { i := data[n] spouseVal := o.Spouse() == byte(1) correct := o.Name() == i.Name && o.Phone() == i.Phone && int(o.Siblings()) == i.Siblings && spouseVal == i.Spouse && o.Money() == i.Money && o.BirthDay() == i.BirthDay.Unix() if !correct { b.Fatalf("unmarshaled object differed:\n%v\n%v", i, o) } } } }
func (s *FlatBufferSerializer) Unmarshal(d []byte, i interface{}) error { a := i.(*A) o := FlatBufferA{} o.Init(d, flatbuffers.GetUOffsetT(d)) a.Name = string(o.Name()) a.BirthDay = time.Unix(o.BirthDay(), 0) a.Phone = string(o.Phone()) a.Siblings = int(o.Siblings()) a.Spouse = o.Spouse() == byte(1) a.Money = o.Money() return nil }
func BenchmarkUnmarshal(b *testing.B) { b.Run("colfer", func(b *testing.B) { b.ReportAllocs() for i := b.N; i > 0; i-- { o := new(Colfer) holdData = o _, err := o.Unmarshal(colferSerials[i%len(colferSerials)]) if err != nil { b.Fatal(err) } } }) b.Run("protobuf", func(b *testing.B) { b.ReportAllocs() for i := b.N; i > 0; i-- { o := new(ProtoBuf) holdProtoBufData = o err := o.Unmarshal(protoSerials[i%len(protoSerials)]) if err != nil { b.Fatal(err) } } }) b.Run("flatbuf", func(b *testing.B) { b.ReportAllocs() for i := b.N; i > 0; i-- { o := new(Colfer) holdData = o bytes := flatSerials[i%len(flatSerials)] buf := new(FlatBuffers) buf.Init(bytes, flatbuffers.GetUOffsetT(bytes)) o.Key = buf.Key() o.Host = string(buf.Host()) o.Port = buf.Port() o.Size = buf.Size() o.Hash = buf.Hash() o.Ratio = buf.Ratio() o.Route = buf.Route() == 1 } }) }
func GetRootAsMonster(buf []byte, offset flatbuffers.UOffsetT) *Monster { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &Monster{} x.Init(buf, n+offset) return x }
func GetRootAsFlatBufferColorGroup(buf []byte, offset flatbuffers.UOffsetT) *FlatBufferColorGroup { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &FlatBufferColorGroup{} x.Init(buf, n+offset) return x }
func GetRootAsTestSimpleTableWithEnum(buf []byte, offset flatbuffers.UOffsetT) *TestSimpleTableWithEnum { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &TestSimpleTableWithEnum{} x.Init(buf, n+offset) return x }
func GetRootAsRoute(buf []byte, offset flatbuffers.UOffsetT) *Route { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &Route{} x.Init(buf, n+offset) return x }
func GetRootAsPostingList(buf []byte, offset flatbuffers.UOffsetT) *PostingList { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &PostingList{} x.Init(buf, n+offset) return x }
func GetRootAsSecondTableInA(buf []byte, offset flatbuffers.UOffsetT) *SecondTableInA { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &SecondTableInA{} x.Init(buf, n+offset) return x }
func GetRootAsNotice(buf []byte, offset flatbuffers.UOffsetT) *Notice { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &Notice{} x.Init(buf, n+offset) return x }
func GetRootAsTableInNestedNS(buf []byte, offset flatbuffers.UOffsetT) *TableInNestedNS { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &TableInNestedNS{} x.Init(buf, n+offset) return x }
func GetRootAsStatus(buf []byte, offset flatbuffers.UOffsetT) *Status { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &Status{} x.Init(buf, n+offset) return x }
func GetRootAsWeapon(buf []byte, offset flatbuffers.UOffsetT) *Weapon { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &Weapon{} x.Init(buf, n+offset) return x }
func GetRootAsIndex(buf []byte, offset flatbuffers.UOffsetT) *Index { n := flatbuffers.GetUOffsetT(buf[offset:]) x := &Index{} x.Init(buf, n+offset) return x }