public
Last active

  • Download Gist
loadtest.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#coding=utf8
 
import time
import simplejson as json
 
from cadaapi.stock.ttypes import *
 
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
 
 
def load(data):
head = data[0]
data = data[1:]
body = []
head = [str(v) for v in head]
for row in data:
row = row[0]
try:
body.append(RatingByReport(**dict(zip(head,row))))
except:import pdb;pdb.set_trace()
result = TableRatingByReport(head=head, body=body)
return result
 
def ser(data):
transportOut = TTransport.TMemoryBuffer()
protocolOut = TBinaryProtocol.TBinaryProtocol(transportOut)
data.write(protocolOut)
vals = transportOut.getvalue()
return vals
 
def test_thrift(data):
print >>open('/tmp/test.thrift', 'w'), ser(data)
now = time.time()
print 'start:', now
for i in range(10):
print '\tFile length:', len(ser(data))
now2 = time.time()
print 'end:', now2
print 'elapse:', now2-now
 
def test_json(data):
now = time.time()
print 'start:', now
for i in range(10):
print '\tFile length:', len(json.dumps(data))
now2 = time.time()
print 'end:', now2
print 'elapse:', now2-now
 
 
if __name__ == '__main__':
rawdata = json.loads(open('/tmp/test.json', 'r').read())
data = load(rawdata)
print 'Test thrift'
test_thrift(data)
 
print '\n\nTest json'
test_json(rawdata)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.