Skip to content

Instantly share code, notes, and snippets.

@vrde
Last active August 16, 2018 13:24
Show Gist options
  • Save vrde/ce1718c4d646c10e16c4f12776b4fada to your computer and use it in GitHub Desktop.
Save vrde/ce1718c4d646c10e16c4f12776b4fada to your computer and use it in GitHub Desktop.
import sys
import base64
import json
import time
from bigchaindb.lib import BigchainDB
from bigchaindb.models import Transaction
from bigchaindb.common.crypto import generate_key_pair
try:
TOTAL_TXS = int(sys.argv[1])
except IndexError:
TOTAL_TXS = 1000
encode = lambda x: base64.b64encode(json.dumps(x).encode('utf8'))
decode = lambda x: json.loads(base64.b64decode(x).decode('utf8'))
now = lambda: time.perf_counter()
BIGCHAINDB = None
def validate(tx):
global BIGCHAINDB
if not BIGCHAINDB:
BIGCHAINDB = BigchainDB()
BIGCHAINDB.is_valid_transaction(decode(tx))
def generate_transaction():
keypair = generate_key_pair()
tx = Transaction.create(
[keypair.public_key],
[([keypair.public_key], 1)],
).sign([keypair.private_key]).to_dict()
return encode(tx)
def serial_validation(txs):
t = now()
for tx in txs:
validate(tx)
return now() - t
def parallel_validation(txs):
from multiprocessing import Pool
with Pool() as p:
t = now()
p.map(validate, txs)
return now() - t
def boot():
txs = []
print('Create {} transactions'.format(TOTAL_TXS))
for _ in range(TOTAL_TXS):
txs.append(generate_transaction())
print('Start parallel validation')
delta = parallel_validation(txs)
print(' Total time: {:.6f}\n Time per transaction: {:.6f}'.format(delta, delta / TOTAL_TXS))
print('Start serial validation')
delta = serial_validation(txs)
print(' Total time: {:.6f}\n Time per transaction: {:.6f}'.format(delta, delta / TOTAL_TXS))
boot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment