Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binc and MsgPack RPC Benchmark #9

Closed
jochumdev opened this issue Aug 6, 2013 · 6 comments
Closed

Binc and MsgPack RPC Benchmark #9

jochumdev opened this issue Aug 6, 2013 · 6 comments
Labels

Comments

@jochumdev
Copy link

I did some benchmarks of RPC encoding speeds.

https://gist.github.com/pcdummy/6168792

Do you know why Binc and MsgPack perform that bad?

@ugorji
Copy link
Owner

ugorji commented Aug 7, 2013

The reason for the discrepancy is that gob and json (and probably bson) decoders internally do read buffering. The concern with that is that it can read beyond the actual gob and json data in the input stream. Both gob and json point this out explicitly in their docs.

See
http://golang.org/pkg/encoding/gob/#NewDecoder
http://golang.org/pkg/encoding/json/#NewDecoder (json mitigates this somewhat by providing a Buffered method that returns the io.Reader which may have already read past the json data requested).

For my codec library, I wanted to support the ability to have multiple data in an inputstream. E.g. use binc decoder to decode a struct, then read a boundary, then read a gif image right after that.

Folks can pass a buffered reader directly to the NewDecoder call, and get the same benefits of buffering. At this point, they are managing the buffering themselves, and all is well. Binc/Msgpack will not internally read past its data.

For RPC, since the assumption is that the RPC owns the connection, then RPC can create a buffered reader/writer around the connection and pass that to the codec. I've implemented that now, and the numbers are now as you would expect.

BenchmarkEndToEndGob 50000 32450 ns/op
BenchmarkEndToEndMsgPack 50000 34325 ns/op
BenchmarkEndToEndBinc 50000 33911 ns/op
BenchmarkEndToEndJSON 50000 52476 ns/op

It took me a while to figure out because I had to hunt down why gob/json was faster, which is unexpected because the encoding of binc/msgpack is consistently faster.

I will push the change out within the next few hours and close this issue when I am done.

Please let me know if this makes sense.

@ugorji ugorji closed this as completed in 8f3b3ef Aug 7, 2013
@jochumdev
Copy link
Author

Thanks for the detailed response! Yes, the added buffer speeds up MsgPack-RPC and Binc-RPC a lot. Thx!

Here are my results:
pcdummy@ThinkPad-T410 ~/Projekte/golang/src/rpc_test $ GOMAXPROCS=2 go test -test.bench="." bson_bench_test.go
PASS
BenchmarkEndToEndBSON-2 50000 46872 ns/op
BenchmarkEndToEndMsgPack-2 50000 35437 ns/op
BenchmarkEndToEndGob-2 50000 32116 ns/op
BenchmarkEndToEndBinc-2 50000 37007 ns/op
BenchmarkEndToEndJSON-2 50000 49059 ns/op
ok command-line-arguments 12.071s
pcdummy@ThinkPad-T410 ~/Projekte/golang/src/rpc_test $ go test -test.bench=".
" bson_bench_test.go
PASS
BenchmarkEndToEndBSON 50000 66116 ns/op
BenchmarkEndToEndMsgPack 50000 45132 ns/op
BenchmarkEndToEndGob 50000 39081 ns/op
BenchmarkEndToEndBinc 50000 45951 ns/op
BenchmarkEndToEndJSON 50000 59995 ns/op
ok command-line-arguments 15.419s

ugorji added a commit that referenced this issue Aug 8, 2013
….ByteReader, and other misc optimizations.

Also include other misc optimizations and clean up of comments.

Improves RPC performance as seen in #9 .
@ugorji
Copy link
Owner

ugorji commented Aug 8, 2013

Thanks.

Added one last optimization in the latest update that should narrow the difference further.

Since io.Reader may now be a buffered reader, it is also most likely a ByteReader with a specialized ReadByte() method. I now call ReadByte if available, instead of calling Read([]byte) when needing to read just one byte.

In my testing, it brought the times vs gob to be about the same.

BenchmarkEndToEndGob 50000 32226 ns/op
BenchmarkEndToEndMsgPack 50000 32261 ns/op
BenchmarkEndToEndBinc 50000 31841 ns/op
BenchmarkEndToEndJSON 50000 51524 ns/op

Please test again on your end and let me know if you are seeing the same results.

@jochumdev
Copy link
Author

Can't go get, getting errors.

pcdummy@ThinkPad-T410 ~/Projekte/golang/src $ go get -u github.com/ugorji/go/codec

github.com/ugorji/go/codec

github.com/ugorji/go/codec/binc.go:28: undefined: encdecHandle
github.com/ugorji/go/codec/binc.go:225: undefined: charEncoding
github.com/ugorji/go/codec/binc.go:295: undefined: charEncoding
github.com/ugorji/go/codec/binc.go:303: undefined: charEncoding
github.com/ugorji/go/codec/decode.go:179: undefined: Handle
github.com/ugorji/go/codec/decode.go:189: undefined: Handle
github.com/ugorji/go/codec/encode.go:44: undefined: charEncoding
github.com/ugorji/go/codec/encode.go:46: undefined: charEncoding
github.com/ugorji/go/codec/encode.go:178: undefined: Handle
github.com/ugorji/go/codec/encode.go:198: undefined: Handle
github.com/ugorji/go/codec/encode.go:198: too many errors

@ugorji
Copy link
Owner

ugorji commented Aug 8, 2013

That is probably a problem with your environment. You will have to do some more digging there. The problem is not in the commit.

Look at the commit description:
5a8bee9

It's all removal of comments, and a few short edits to use ReadByte and io.ReadAtLeast.

@jochumdev
Copy link
Author

Yes it was a problem at my side.

New Results:
BenchmarkEndToEndBSON 50000 45805 ns/op
BenchmarkEndToEndMsgPack 50000 41245 ns/op
BenchmarkEndToEndGob 50000 39104 ns/op
BenchmarkEndToEndBinc 50000 42863 ns/op
BenchmarkEndToEndJSON 50000 61911 ns/op

Thanks a lot, will use MsgPack at my project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants