replaced json with rapidjson

This commit is contained in:
Rodolphe Marques 2016-05-18 15:43:47 +02:00
parent 8138a4b1c2
commit 50da984626
2 changed files with 76 additions and 6 deletions

View File

@ -1,5 +1,4 @@
import copy import copy
import json
import time import time
import contextlib import contextlib
import threading import threading
@ -7,6 +6,8 @@ import queue
import multiprocessing as mp import multiprocessing as mp
from datetime import datetime from datetime import datetime
import rapidjson
import cryptoconditions as cc import cryptoconditions as cc
from cryptoconditions.exceptions import ParsingError from cryptoconditions.exceptions import ParsingError
@ -109,8 +110,7 @@ def serialize(data):
str: JSON formatted string str: JSON formatted string
""" """
return json.dumps(data, skipkeys=False, ensure_ascii=False, return rapidjson.dumps(data, skipkeys=False, ensure_ascii=False, sort_keys=True)
separators=(',', ':'), sort_keys=True)
def deserialize(data): def deserialize(data):
@ -123,7 +123,7 @@ def deserialize(data):
dict: dict resulting from the serialization of a JSON formatted string. dict: dict resulting from the serialization of a JSON formatted string.
""" """
return json.loads(data, encoding="utf-8") return rapidjson.loads(data)
def timestamp(): def timestamp():
@ -275,7 +275,7 @@ def create_tx(current_owners, new_owners, inputs, operation, payload=None):
conditions.append({ conditions.append({
'new_owners': new_owners, 'new_owners': new_owners,
'condition': { 'condition': {
'details': json.loads(condition.serialize_json()), 'details': rapidjson.loads(condition.serialize_json()),
'uri': condition.condition.serialize_uri() 'uri': condition.condition.serialize_uri()
}, },
'cid': fulfillment['fid'] 'cid': fulfillment['fid']
@ -493,7 +493,7 @@ def get_fulfillment_message(transaction, fulfillment, serialized=False):
# there is no previous transaction so we need to create one on the fly # there is no previous transaction so we need to create one on the fly
else: else:
current_owner = transaction['transaction']['fulfillments'][0]['current_owners'][0] current_owner = transaction['transaction']['fulfillments'][0]['current_owners'][0]
condition = json.loads(cc.Ed25519Fulfillment(public_key=current_owner).serialize_json()) condition = rapidjson.loads(cc.Ed25519Fulfillment(public_key=current_owner).serialize_json())
fulfillment_message['condition'] = {'condition': {'details': condition}} fulfillment_message['condition'] = {'condition': {'details': condition}}
if serialized: if serialized:
return serialize(fulfillment_message) return serialize(fulfillment_message)

View File

@ -1,3 +1,7 @@
import json
import time
import rapidjson
from line_profiler import LineProfiler from line_profiler import LineProfiler
import bigchaindb import bigchaindb
@ -21,5 +25,71 @@ def speedtest_validate_transaction():
profiler.print_stats() profiler.print_stats()
def speedtest_serialize_block_json():
# create a block
b = bigchaindb.Bigchain()
tx = b.create_transaction(b.me, b.me, None, 'CREATE')
tx_signed = b.sign_transaction(tx, b.me_private)
block = b.create_block([tx_signed] * 1000)
time_start = time.time()
for _ in range(1000):
_ = json.dumps(block, skipkeys=False, ensure_ascii=False, sort_keys=True)
time_elapsed = time.time() - time_start
print('speedtest_serialize_block_json: {} s'.format(time_elapsed))
def speedtest_serialize_block_rapidjson():
# create a block
b = bigchaindb.Bigchain()
tx = b.create_transaction(b.me, b.me, None, 'CREATE')
tx_signed = b.sign_transaction(tx, b.me_private)
block = b.create_block([tx_signed] * 1000)
time_start = time.time()
for _ in range(1000):
_ = rapidjson.dumps(block, skipkeys=False, ensure_ascii=False, sort_keys=True)
time_elapsed = time.time() - time_start
print('speedtest_serialize_block_rapidjson: {} s'.format(time_elapsed))
def speedtest_deserialize_block_json():
# create a block
b = bigchaindb.Bigchain()
tx = b.create_transaction(b.me, b.me, None, 'CREATE')
tx_signed = b.sign_transaction(tx, b.me_private)
block = b.create_block([tx_signed] * 1000)
block_serialized = json.dumps(block, skipkeys=False, ensure_ascii=False, sort_keys=True)
time_start = time.time()
for _ in range(1000):
_ = json.loads(block_serialized)
time_elapsed = time.time() - time_start
print('speedtest_deserialize_block_json: {} s'.format(time_elapsed))
def speedtest_deserialize_block_rapidjson():
# create a block
b = bigchaindb.Bigchain()
tx = b.create_transaction(b.me, b.me, None, 'CREATE')
tx_signed = b.sign_transaction(tx, b.me_private)
block = b.create_block([tx_signed] * 1000)
block_serialized = rapidjson.dumps(block, skipkeys=False, ensure_ascii=False, sort_keys=True)
time_start = time.time()
for _ in range(1000):
_ = rapidjson.loads(block_serialized)
time_elapsed = time.time() - time_start
print('speedtest_deserialize_block_rapidjson: {} s'.format(time_elapsed))
if __name__ == '__main__': if __name__ == '__main__':
speedtest_validate_transaction() speedtest_validate_transaction()
speedtest_serialize_block_json()
speedtest_serialize_block_rapidjson()
speedtest_deserialize_block_json()
speedtest_deserialize_block_rapidjson()