Vous êtes sur la page 1sur 12


What are we looking for?

Lightweight /Scalable cross-language services Why not JSON or XML?


is verbose by design


XML is a text format and it uses tags to delimit the data, XML files are nearly always larger than comparable binary formats


and JSON use UTF-8 encoding which are larger than binary when sent across the wire.
<?xml version="1.0" encoding="UTF-8" ?> JSON "version": "1.0", "encoding": "UTF-8",

Basic Information
Protocol Buffers


Open sourced by Google Maintained by Google Serialization API Write a proto file to define message and then use compiler to generate classes for target language Supports Binary serialization

Open sourced by Facebook Maintained by Apache Incubator Serialization and RPC API Write a thrift file to define message and then use compiler to generate classes for target language Supports Binary and JSON serialization

Language Support
Protocol Buffers


C++ Java Python Community Supported

C# Ruby Perl Objective C Etc.

C++ Java Python PHP Ruby Erlang Perl Haskell C# Cocoa Smalltalk OCaml

Primitive Types Supported

Protocol Buffers


Double Float Unsigned int32/64

Signed int32/64
Bool String Bytes Repeated properties (lists) Enumerations

Bool Byte Signed int32/64 Double (64-bit floating point) String List<type> Set<type> Map<type1, type2> Enumerations Exceptions Constants

Message Definition
package tutorial; option java_package = com.example.tutorial";

Protocol Buffers

option java_outer_classname = AddressBookProtos"; message Person { required string name = 1; required int32 id = 2;

optional string email = 3;

enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 4;

Message Definition


namespace java tutorial namespace csharp Tutorial enum PhoneType { MOBILE = 1, HOME = 2, WORK = 3 } struct PhoneNumber { 1: string number, 2: PhoneType type = 2 } struct Person { 1: string name, 2: i32 id, 3: string email, 4: set<PhoneNumber> phone } struct AddressBook { 1: list<Person> person }

RPC Implementation
Protocol Buffers


Not Applicable

ZeroMQ is a possible RPC implementation that has cross language support

Cross language RPC built into the APIs RPC interfaces reference Thrift types Json and Binary Protocols

service Calculator extends shared.SharedService { void ping(), i32 add(1:i32 num1, 2:i32 num2) throws (1:InvalidOperation ouch) }

Create .thrift file and gen Java code Write java code struct dns_record {
1: string key, 2: string value, 3: string type = 'A', 4: i32 ttl = 86400, 5: string first, 6: string last } Service TestDns { dns_record test(1:string q); }

<thrift_root>/bin/thrift gen java tim.thrift code will be generated in gen-java/*.java // new object dns_record dr = new dns_record(key, value, type, ttl, first, last) // serialize TSerializer serializer = new TSerializer(new TBinaryProtocol.Factory()); TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory()); byte[] bytes = serializer.serialize(dr);

Create .proto file and gen Java code Write Java code
package dns; message DnsRecord { required string key = 1; required string value = 2; required string first = 3; required string last = 4; optional string type = 5 [default = "A"]; optional int32 ttl = 6 [default = 86400]; } message DnsResponse { repeated DnsRecord records = 1; }

bin/protoc java_out . tim.proto

// protocol buffer need a builder to create object Dns.DnsRecord.Builder b = Dns.DnsRecord.newBuilder(); b.setKey("key"); b.setValue("value..."); b.builder(); byte[] bytes = dr.toByteArray(); Dns.DnsRecord dr2 = Dns.DnsRecord.parseFrom(bytes);

Performance in Java
Protocol Buffers


Get object

: 8,170 msec

Get object

: 14,394msec

Serdes protobuf: 33,054 msec

Serdes thrift : 37,671msec Objs per second: 265,456

Objs per second: 302,535

Total bytes : 829,997,866

Total bytes

: 1,130,000,000

From the result, Protocol Buffers is 1.1 times faster than Thrift!

Thank You
Group Members 2010/mcs/051 2010/mcs/037 2010/mcs/075 2010/mcs/007 2010/mcs/046 2010/mcs/056 2010/mcs/068 2010/mcs/072 2010/mcs/076 Saman Perera Kamal Mettananda Hansaka Wickramasinghe Yasindu Dambadeniya B S Dilshan Perera L Prashanthan Gayanath Senanayake W Sriwathsan Darshana Wijegunarathna