Skip to content

Commit

Permalink
Split bson package and update mongo package
Browse files Browse the repository at this point in the history
GODRIVER-537
GODRIVER-551
GODRIVER-527
GODRIVER-566
GODRIVER-494

Change-Id: I55bdfe7f48ded7fd9923d906d3cc6ecaec8156da
  • Loading branch information
skriptble committed Sep 27, 2018
1 parent 6ea632f commit 71de844
Show file tree
Hide file tree
Showing 121 changed files with 10,585 additions and 14,878 deletions.
19 changes: 9 additions & 10 deletions benchmark/bson_map.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package benchmark

import (
"bytes"
"context"
"errors"
"fmt"

"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
)

func bsonMapDecoding(ctx context.Context, tm TimerManager, iters int, dataSet string) error {
Expand All @@ -19,10 +18,9 @@ func bsonMapDecoding(ctx context.Context, tm TimerManager, iters int, dataSet st

for i := 0; i < iters; i++ {
out := make(map[string]interface{})
dec := bson.NewDecoder(bytes.NewReader(r))
err := dec.Decode(out)
err := bsoncodec.Unmarshal(r, &out)
if err != nil {
return err
return nil
}
if len(out) == 0 {
return fmt.Errorf("decoding failed")
Expand All @@ -38,19 +36,20 @@ func bsonMapEncoding(ctx context.Context, tm TimerManager, iters int, dataSet st
}

doc := make(map[string]interface{})
dec := bson.NewDecoder(bytes.NewReader(r))
if err = dec.Decode(doc); err != nil {
err = bsoncodec.Unmarshal(r, &doc)
if err != nil {
return err
}

buf := bytes.NewBuffer([]byte{})
var buf []byte
tm.ResetTimer()
for i := 0; i < iters; i++ {
if err = bson.NewEncoder(buf).Encode(&doc); err != nil {
buf, err = bsoncodec.MarshalAppend(buf[:0], doc)
if err != nil {
return nil
}

if buf.Len() == 0 {
if len(buf) == 0 {
return errors.New("encoding failed")
}
}
Expand Down
29 changes: 15 additions & 14 deletions benchmark/bson_struct.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package benchmark

import (
"bytes"
"context"
"errors"

"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
)

func BSONFlatStructDecoding(ctx context.Context, tm TimerManager, iters int) error {
Expand All @@ -18,8 +17,7 @@ func BSONFlatStructDecoding(ctx context.Context, tm TimerManager, iters int) err

for i := 0; i < iters; i++ {
out := flatBSON{}
dec := bson.NewDecoder(bytes.NewReader(r))
err := dec.Decode(&out)
err := bsoncodec.Unmarshal(r, &out)
if err != nil {
return err
}
Expand All @@ -34,18 +32,20 @@ func BSONFlatStructEncoding(ctx context.Context, tm TimerManager, iters int) err
}

doc := flatBSON{}
if err = bson.NewDecoder(bytes.NewReader(r)).Decode(&doc); err != nil {
err = bsoncodec.Unmarshal(r, &doc)
if err != nil {
return err
}

buf := bytes.NewBuffer([]byte{})
var buf []byte

tm.ResetTimer()
for i := 0; i < iters; i++ {
if err = bson.NewEncoder(buf).Encode(&doc); err != nil {
buf, err = bsoncodec.Marshal(doc)
if err != nil {
return err
}
if buf.Len() == 0 {
if len(buf) == 0 {
return errors.New("encoding failed")
}
}
Expand All @@ -59,18 +59,20 @@ func BSONFlatStructTagsEncoding(ctx context.Context, tm TimerManager, iters int)
}

doc := flatBSONTags{}
if err = bson.NewDecoder(bytes.NewReader(r)).Decode(&doc); err != nil {
err = bsoncodec.Unmarshal(r, &doc)
if err != nil {
return err
}

buf := bytes.NewBuffer([]byte{})
var buf []byte

tm.ResetTimer()
for i := 0; i < iters; i++ {
if err = bson.NewEncoder(buf).Encode(&doc); err != nil {
buf, err = bsoncodec.MarshalAppend(buf[:0], doc)
if err != nil {
return err
}
if buf.Len() == 0 {
if len(buf) == 0 {
return errors.New("encoding failed")
}
}
Expand All @@ -86,8 +88,7 @@ func BSONFlatStructTagsDecoding(ctx context.Context, tm TimerManager, iters int)
tm.ResetTimer()
for i := 0; i < iters; i++ {
out := flatBSONTags{}
dec := bson.NewDecoder(bytes.NewReader(r))
err := dec.Decode(&out)
err := bsoncodec.Unmarshal(r, &out)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions benchmark/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func MultiFindMany(ctx context.Context, tm TimerManager, iters int) error {

payload := make([]interface{}, iters)
for idx := range payload {
payload[idx] = *doc
payload[idx] = doc
}

if _, err = coll.InsertMany(ctx, payload); err != nil {
Expand Down Expand Up @@ -109,7 +109,7 @@ func multiInsertCase(ctx context.Context, tm TimerManager, iters int, data strin

payload := make([]interface{}, iters)
for idx := range payload {
payload[idx] = *doc
payload[idx] = doc
}

coll := db.Collection("corpus")
Expand Down
66 changes: 0 additions & 66 deletions bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
package bson

import (
"bytes"
"encoding/binary"
"math"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestValue(t *testing.T) {
Expand Down Expand Up @@ -107,64 +102,3 @@ func TestValue(t *testing.T) {
})
t.Run("document", func(t *testing.T) {})
}

func TestTimeRoundTrip(t *testing.T) {
val := struct {
Value time.Time
ID string
}{
ID: "time-rt-test",
}

assert.True(t, val.Value.IsZero())

bsonOut, err := Marshal(val)
assert.NoError(t, err)
rtval := struct {
Value time.Time
ID string
}{}

err = Unmarshal(bsonOut, &rtval)
assert.NoError(t, err)
assert.Equal(t, val, rtval)
assert.True(t, rtval.Value.IsZero())

}

func TestBasicEncode(t *testing.T) {
for _, tc := range marshalingTestCases {
t.Run(tc.name, func(t *testing.T) {
got := make(writer, 0, 1024)
vw := newValueWriter(&got)
reg := NewRegistryBuilder().Build()
codec, err := reg.Lookup(reflect.TypeOf(tc.val))
noerr(t, err)
err = codec.EncodeValue(EncodeContext{Registry: reg}, vw, tc.val)
noerr(t, err)

if !bytes.Equal(got, tc.want) {
t.Errorf("Bytes are not equal. got %v; want %v", Reader(got), Reader(tc.want))
t.Errorf("Bytes:\n%v\n%v", got, tc.want)
}
})
}
}

func TestBasicDecode(t *testing.T) {
for _, tc := range unmarshalingTestCases {
t.Run(tc.name, func(t *testing.T) {
got := reflect.New(tc.sType).Interface()
vr := newValueReader(tc.data)
reg := NewRegistryBuilder().Build()
codec, err := reg.Lookup(reflect.TypeOf(got))
noerr(t, err)
err = codec.DecodeValue(DecodeContext{Registry: reg}, vr, got)
noerr(t, err)

if !reflect.DeepEqual(got, tc.want) {
t.Errorf("Results do not match. got %+v; want %+v", got, tc.want)
}
})
}
}
52 changes: 22 additions & 30 deletions bson/benchmark_test.go → bson/bsoncodec/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package bson
package bsoncodec

import "testing"
import (
"testing"

"github.com/mongodb/mongo-go-driver/bson"
)

type encodetest struct {
Field1String string
Expand Down Expand Up @@ -113,54 +117,42 @@ var nestedInstance = nestedtest1{
},
}

func BenchmarkEncodingv1(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Marshal(encodetestInstance)
}
}

func BenchmarkEncodingv2(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Marshalv2(encodetestInstance)
_, _ = Marshal(encodetestInstance)
}
}

func BenchmarkEncodingv2ToDocument(b *testing.B) {
var buf []byte
for i := 0; i < b.N; i++ {
buf, _ = Marshalv2(encodetestInstance)
_, _ = ReadDocument(buf)
buf, _ = Marshal(encodetestInstance)
_, _ = bson.ReadDocument(buf)
}
}

func BenchmarkEncodingDocument(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = MarshalDocument(encodetestInstance)
}
}

func BenchmarkEncodingv1Nested(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Marshal(nestedInstance)
}
}
// func BenchmarkEncodingDocument(b *testing.B) {
// for i := 0; i < b.N; i++ {
// _, _ = MarshalDocument(encodetestInstance)
// }
// }

func BenchmarkEncodingv2Nested(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Marshalv2(nestedInstance)
_, _ = Marshal(nestedInstance)
}
}

func BenchmarkEncodingv2ToDocumentNested(b *testing.B) {
var buf []byte
for i := 0; i < b.N; i++ {
buf, _ = Marshalv2(nestedInstance)
_, _ = ReadDocument(buf)
buf, _ = Marshal(nestedInstance)
_, _ = bson.ReadDocument(buf)
}
}

func BenchmarkEncodingDocumentNested(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = MarshalDocument(nestedInstance)
}
}
// func BenchmarkEncodingDocumentNested(b *testing.B) {
// for i := 0; i < b.N; i++ {
// _, _ = MarshalDocument(nestedInstance)
// }
// }
Loading

0 comments on commit 71de844

Please sign in to comment.