Skip to content

Instantly share code, notes, and snippets.

@dln
Created October 18, 2010 09:22
Show Gist options
  • Save dln/631958 to your computer and use it in GitHub Desktop.
Save dln/631958 to your computer and use it in GitHub Desktop.
"""
$ cat testar.thrift
struct Testar {
1: i32 heltal = 42,
2: optional string texten,
3: optional string mertext = 'foo',
4: optional map<string,string> properties
}
$ thrift --gen py testar.thrift
"""
from cStringIO import StringIO
import sys
sys.path.insert(0, 'gen-py')
import msgpack
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from testar import *
from testar.ttypes import *
obj_dict = dict(heltal=42, mertext='foo', texten=None, properties={'baz': '33', 'foo': '11', 'bar': '22'})
obj = Testar(**obj_dict)
def testit(n=1000):
transport = TTransport.TMemoryBuffer()
prot = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
for i in xrange(n):
obj.write(prot)
return transport.getvalue()
def testit_msgpack(n=1000):
packer = msgpack.Packer()
buf = StringIO()
for i in xrange(n):
buf.write(packer.pack(obj_dict))
return buf.getvalue()
"""
$ ipython testar.py
Python 2.4.3 (#1, Sep 3 2009, 15:37:37)
Type "copyright", "credits" or "license" for more information.
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
1 >>> %timeit s=testit()
100 loops, best of 3: 3.52 ms per loop
2 >>> %timeit s=testit_msgpack()
100 loops, best of 3: 4.82 ms per loop
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment