<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tim[后端技术] &#187; protocol buffers</title>
	<atom:link href="http://timyang.net/tag/protocol-buffers/feed/" rel="self" type="application/rss+xml" />
	<link>http://timyang.net</link>
	<description>Tim&#039;s blog, 关于后端架构、互联网技术、分布式、大型网络应用、NoSQL、Key Value等</description>
	<lastBuildDate>Mon, 26 Jul 2010 15:32:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Thrift and Protocol Buffers performance in Java Round 2</title>
		<link>http://timyang.net/programming/thrift-protocol-buffers-performance-2/</link>
		<comments>http://timyang.net/programming/thrift-protocol-buffers-performance-2/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 02:29:40 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[protocol buffers]]></category>
		<category><![CDATA[thrift]]></category>

		<guid isPermaLink="false">http://timyang.net/?p=91</guid>
		<description><![CDATA[In my last test Thrift and Protocol Buffers performance in Java, Some comments told me that there are some tuning parameter for Protocol Buffer which can improve performance magically. The parameter was not turn on by default. I added
option optimize_for = SPEED
to the proto file, and re-generated the Java class, and the result:
Thrift Loop    : [...]]]></description>
			<content:encoded><![CDATA[<p>In my last test <a href="http://timyang.net/programming/thrift-protocol-buffers-performance-java/" target="_blank">Thrift and Protocol Buffers performance in Java</a>, Some comments told me that there are some tuning parameter for Protocol Buffer which can improve performance magically. The parameter was not turn on by default. I added<br />
option optimize_for = SPEED<br />
to the proto file, and re-generated the Java class, and the result:</p>
<pre>Thrift Loop    : 10,000,000
Get object     : 14,394msec
Serdes thrift  : 37,671msec
Objs per second: 265,456
Total bytes    : 1,130,000,000

ProtoBuf Loop  : 10,000,000
Get object     : 8,170msec
Serdes protobuf: 33,054msec
Objs per second: 302,535
Total bytes    : 829,997,866
</pre>
<p>From the result, Protocol Buffers is 1.1 times faster than Thrift!</p>
<p>And from the Google Protocol Buffers group, why the optimize for speed was not turn on by default.</p>
<blockquote><p>When using C++ or Java protocol buffers, for best performance you need to add a line to your .proto files:</p>
<p>option optimize_for = SPEED;</p>
<p>Otherwise, by default, the compiler optimizes for code size.  Optimizing for code size results in generated code that around a half to a third of the size, but runs an order of magnitude slower&#8230;</p></blockquote>
<p>Here is the <a href="http://groups.google.com/group/protobuf/browse_thread/thread/3ad2fb05be0d8912" target="_blank">original post<br />
</a></p>
Similar Posts:<ul><li><a href="http://timyang.net/programming/thrift-protocol-buffers-performance-java/" rel="bookmark" title="April 2, 2009">Thrift and Protocol Buffers performance in Java</a></li>

<li><a href="http://timyang.net/programming/thrift-protocol-buffers-java-howto/" rel="bookmark" title="April 2, 2009">Thrift, Protocol Buffers installation and Java code howto</a></li>

<li><a href="http://timyang.net/data/mcdb-tt-redis/" rel="bookmark" title="August 11, 2009">MemcacheDB, Tokyo Tyrant, Redis performance test</a></li>

<li><a href="http://timyang.net/programming/c-erlang-java-performance/" rel="bookmark" title="November 11, 2009">C, Erlang, Java and Go Web Server performance test</a></li>

<li><a href="http://timyang.net/linux/gearman-monitor/" rel="bookmark" title="August 26, 2009">利用Gearman来实现远程监控与管理</a></li>
</ul><!-- Similar Posts took 10.179 ms -->]]></content:encoded>
			<wfw:commentRss>http://timyang.net/programming/thrift-protocol-buffers-performance-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Thrift and Protocol Buffers performance in Java</title>
		<link>http://timyang.net/programming/thrift-protocol-buffers-performance-java/</link>
		<comments>http://timyang.net/programming/thrift-protocol-buffers-performance-java/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 12:18:54 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[protocol buffers]]></category>
		<category><![CDATA[thrift]]></category>

		<guid isPermaLink="false">http://timyang.net/?p=77</guid>
		<description><![CDATA[I&#8217;ve used Thrift for some log client in our system. I&#8217;m going to use Protocol Buffers as the internal communication protocol between our XMPP servers. But I am hard to believe from the thrift and protocol buffers Python performance comparison, that that Protocol Buffers is 4-10 slower than Thrift. I&#8217;m going to do some similar [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve used <a href="http://incubator.apache.org/thrift/" target="_blank">Thrift</a> for some log client in our system. I&#8217;m going to use <a href="http://code.google.com/apis/protocolbuffers/">Protocol Buffers</a> as the internal communication protocol between our XMPP servers. But I am hard to believe from the <a href="http://bouncybouncy.net/ramblings/posts/thrift_and_protocol_buffers/" target="_blank">thrift and protocol buffers</a> Python performance comparison, that that Protocol Buffers is 4-10 slower than Thrift. I&#8217;m going to do some similar tests on Java.</p>
<p>The test is very similiar as the Python test. the .proto and .thrift file are copied from the above python test.</p>
<p>The .thrift content:</p>
<pre>struct dns_record {
1: string key,
2: string value,
3: string type = 'A',
4: i32 ttl = 86400,
5: string first,
6: string last
}

typedef list&lt;dns_record&gt; biglist

struct dns_response {
1: biglist records
}

service PassiveDns {
biglist search_question(1:string q);
biglist search_answer(1:string q);
}</pre>
<p>The .proto content</p>
<pre>package passive_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;
}</pre>
<p>From the document, I learn that the optional and default values are one of the benefits of both serialization libraries. A record that matches the default value does not need to be included in the serialized output.</p>
<p>I wrote up a simple test program to compare thrift, protocol buffers. I tested the serialize and deserialize together, because this is the most called part in most scenarioes.</p>
<p>Test 1: 10,000,000 times</p>
<pre>ProtoBuf Loop  : 10,000,000
Get object     : 15,130msec
Serdes protobuf: 68,600msec
Objs per second: 145,772
Total bytes    : 829,996,683

Thrift Loop    : 10,000,000
Get object     : 12,651msec
Serdes thrift  : 36,904msec
Objs per second: 270,973
Total bytes    : 1,130,000,000</pre>
<p>Test 2: 1,000,000 times</p>
<pre>ProtoBuf Loop  : 1,000,000
Get object     : 1,094msec
Serdes protobuf: 7,467msec
Objs per second: 133,922
Total bytes    : 83,000,419

Thrift Loop    : 1,000,000
Get object     : 524msec
Serdes thrift  : 5,969msec
Objs per second: 167,532
Total bytes    : 113,000,000</pre>
<p>The serde_* functions are the times needed to serialize, and de-serialize the java object to and from a byte[].</p>
<p>The result in Java was that Protocol Buffers 1.2-2 times slower than Thrift. (in the python test was 4~10 times). And PB binary size is smaller than Thrift. I think this is acceptable, and Google may improve the Protocol Buffers performance in the future version.</p>
<p>Download my test code in Java: <a href="http://timyang.net/code/thrift-protocol-buffers-java.tgz">thrift-protocol-buffers-java.tgz</a>,</p>
<p>More information about thrift and protocol buffers: <a href="http://timyang.net/programming/thrift-protocol-buffers-java-howto/">Thrift, Protocol Buffers installation and Java code howto</a></p>
<p>Update: There is another <a href="http://stuartsierra.com/2008/07/10/thrift-vs-protocol-buffers" target="_blank">Thrift vs. Protocol Buffers</a> compare non-performance factors.</p>
<p>UPDATE 2 (Apr 17): There is a performance tuning parameter optimize_for = SPEED (thanks <a href="http://stvchu.org/" target="_blank">Steve Chu</a>) for Protocol Buffers,  please see my next performance tests <a href="http://timyang.net/programming/thrift-protocol-buffers-performance-2/">Thrift and Protocol Buffers performance in Java Round 2</a></p>
Similar Posts:<ul><li><a href="http://timyang.net/programming/thrift-protocol-buffers-performance-2/" rel="bookmark" title="April 17, 2009">Thrift and Protocol Buffers performance in Java Round 2</a></li>

<li><a href="http://timyang.net/programming/thrift-protocol-buffers-java-howto/" rel="bookmark" title="April 2, 2009">Thrift, Protocol Buffers installation and Java code howto</a></li>

<li><a href="http://timyang.net/data/mcdb-tt-redis/" rel="bookmark" title="August 11, 2009">MemcacheDB, Tokyo Tyrant, Redis performance test</a></li>

<li><a href="http://timyang.net/programming/c-erlang-java-performance/" rel="bookmark" title="November 11, 2009">C, Erlang, Java and Go Web Server performance test</a></li>

<li><a href="http://timyang.net/linux/gearman-monitor/" rel="bookmark" title="August 26, 2009">利用Gearman来实现远程监控与管理</a></li>
</ul><!-- Similar Posts took 10.526 ms -->]]></content:encoded>
			<wfw:commentRss>http://timyang.net/programming/thrift-protocol-buffers-performance-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Thrift, Protocol Buffers installation and Java code howto</title>
		<link>http://timyang.net/programming/thrift-protocol-buffers-java-howto/</link>
		<comments>http://timyang.net/programming/thrift-protocol-buffers-java-howto/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 11:44:27 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[protocol buffers]]></category>
		<category><![CDATA[thrift]]></category>

		<guid isPermaLink="false">http://timyang.net/?p=71</guid>
		<description><![CDATA[I. Thrift installation and Java code
1. build and install thrift
install boost
cd &#60;boost_root&#62;/tools/jam
./build_dist.sh
# linux* will depends the platform
cp stage/bin.linux*/bjam &#60;boost_root&#62;
# build boost, use bjam will faster
cd &#60;boost_root&#62;
./configure &#8211;without-icu &#8211;prefix=/usr/local/boost
./bjam -toolset=gcc &#8211;build-type=release install &#8211;prefix=/usr/local/boost
# build thrift
./bootstrap.sh
./configure &#8211;with-boost=/usr/local
make
make install
2. Build Thrift java library
install apache ant if necessary
cd lib/java/
ant
get libthrift.jar
3. Create .thrift file and gen Java code
(See http://wiki.apache.org/thrift/Tutorial for [...]]]></description>
			<content:encoded><![CDATA[<h1>I. Thrift installation and Java code</h1>
<h2>1. build and install thrift</h2>
<p>install boost<br />
cd &lt;boost_root&gt;/tools/jam<br />
./build_dist.sh<br />
# linux* will depends the platform<br />
cp stage/bin.linux*/bjam &lt;boost_root&gt;<br />
# build boost, use bjam will faster<br />
cd &lt;boost_root&gt;<br />
./configure &#8211;without-icu &#8211;prefix=/usr/local/boost<br />
./bjam -toolset=gcc &#8211;build-type=release install &#8211;prefix=/usr/local/boost</p>
<p># build thrift<br />
./bootstrap.sh<br />
./configure &#8211;with-boost=/usr/local<br />
make<br />
make install</p>
<h2>2. Build Thrift java library</h2>
<p>install apache ant if necessary</p>
<p>cd lib/java/<br />
ant</p>
<p>get libthrift.jar</p>
<h2>3. Create .thrift file and gen Java code</h2>
<p>(See <a href="http://wiki.apache.org/thrift/Tutorial" target="_blank">http://wiki.apache.org/thrift/Tutorial</a> for more .thrift tutorial info)<br />
tim.thrift</p>
<pre>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);
}</pre>
<p>&lt;thrift_root&gt;/bin/thrift &#8211;gen java tim.thrift<br />
code will be generated in gen-java/*.java</p>
<h2>4. Write java code</h2>
<pre>// 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);</pre>
<p>see also: <a href="thrift, protocol buffer install and java howto" target="_blank">http://wiki.apache.org/thrift/ThriftUsageJava</a></p>
<h1>II. Protocol Buffers install and Java code</h1>
<h2>1. Build and install Protocol buffers</h2>
<p>./configure<br />
make<br />
make install</p>
<h2>2. Build protobuf Java library</h2>
<p>install maven if not necessary</p>
<p>cd protobuf/java<br />
mvn test<br />
mvn package</p>
<p>get jar from target/protobuf-java-x.x.x.jar</p>
<h2>3. Create .proto file and gen Java code</h2>
<p>tim.proto</p>
<pre>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;
}</pre>
<p>bin/protoc &#8211;java_out . tim.proto</p>
<h2>4. Write Java code</h2>
<pre>// 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);</pre>
<h1>III. Resources</h1>
<p>Thrift: <a href="http://incubator.apache.org/thrift/" target="_blank">http://incubator.apache.org/thrift/</a></p>
<p>Protocol Buffers: <a href="http://code.google.com/apis/protocolbuffers/" target="_blank">http://code.google.com/apis/protocolbuffers/</a></p>
<p>Tim&#8217;s Blog: <a href="http://timyang.net/" target="_self">http://timyang.net/</a></p>
Similar Posts:<ul><li><a href="http://timyang.net/programming/thrift-protocol-buffers-performance-java/" rel="bookmark" title="April 2, 2009">Thrift and Protocol Buffers performance in Java</a></li>

<li><a href="http://timyang.net/programming/thrift-protocol-buffers-performance-2/" rel="bookmark" title="April 17, 2009">Thrift and Protocol Buffers performance in Java Round 2</a></li>

<li><a href="http://timyang.net/data/mcdb-tt-redis/" rel="bookmark" title="August 11, 2009">MemcacheDB, Tokyo Tyrant, Redis performance test</a></li>

<li><a href="http://timyang.net/programming/c-erlang-java-performance/" rel="bookmark" title="November 11, 2009">C, Erlang, Java and Go Web Server performance test</a></li>

<li><a href="http://timyang.net/linux/gearman-monitor/" rel="bookmark" title="August 26, 2009">利用Gearman来实现远程监控与管理</a></li>
</ul><!-- Similar Posts took 11.246 ms -->]]></content:encoded>
			<wfw:commentRss>http://timyang.net/programming/thrift-protocol-buffers-java-howto/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
