def setup(self, urls, numkeys): self.localState = {} self.transactions = [] self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() print "Checking for pre-existing state" self.state.fetch() keys = self.state.State.keys() for k, v in self.state.State.iteritems(): self.localState[k] = v with Progress("Populating initial key values") as p: txncount = 0 starttime = time.clock() for n in range(1, numkeys + 1): n = str(n) if n not in keys: c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) txncount += 1 self.txnrate(starttime, txncount) self._wait_for_transaction_commits()
def __init__(self, baseurl, name='IntegerKeyClient', keystring=None, keyfile=None, state=None): super(IntegerKeyClient, self).__init__(baseurl) self.LastTransaction = None self.CurrentState = state or IntegerKeyState(self.BaseURL) self.CurrentState.fetch() # set up the signing key if keystring: logger.debug("set signing key from string\n%s", keystring) signingkey = signed_object.generate_signing_key(wifstr=keystring) elif keyfile: logger.debug("set signing key from file %s", keyfile) signingkey = signed_object.generate_signing_key( wifstr=open(keyfile, "r").read()) else: raise TypeError('expecting valid signing key, none provided') identifier = signed_object.generate_identifier(signingkey) self.LocalNode = node.Node(identifier=identifier, signingkey=signingkey, name=name)
def setup(self, urls, numkeys): self.localState = {} self.transactions = [] self.last_key_txn = {} self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: try: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() except MessageException: logger.warn("Unable to connect to Url: %s ", u) if len(self.clients) == 0: return # add check for if a state already exists with Progress("Checking for pre-existing state") as p: self.state.fetch() for k, v in self.state.State.iteritems(): self.localState[k] = v p.step() keys = self.state.State.keys() with Progress("Populating initial key values") as p: txncount = 0 starttime = time.time() for n in range(1, numkeys + 1): n = str(n) if n not in keys: c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) txncount += 1 self.last_key_txn[n] = txnid p.step() print self.txnrate(starttime, txncount, "submitted") self._wait_for_transaction_commits() self.txnrate(starttime, txncount, "committed")
def setup(self, urls): self.global_store = {} self.running_url_list = urls self.global_keys = [] self.transactions = [] self.lastKeyTxn = {} self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: print "Creating clients" for u in self.running_url_list: try: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() except MessageException: print "Unable to connect to Url: {}".format(u)
def setup(self, urls, numKeys): self.localState = {} self.transactions = [] self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() with Progress("Creating initial key values") as p: for n in range(1, numKeys + 1): n = str(n) c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) self._wait_for_transaction_commits()
def setup(self, urls): self.GlobalStore = {} self.RunningUrlList = urls self.GlobalKeys = [] self.transactions = [] self.lastKeyTxn = {} self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: print "Creating clients" for u in self.RunningUrlList: try: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() except MessageException: print "Unable to connect to Url: {}".format(u)
class IntKeyLoadTest(object): def __init__(self, timeout=None): print("start inkeyloadtest") self.localState = {} self.transactions = [] self.clients = [] self.state = None self.Timeout = 240 if timeout is None else timeout self.fake_txn_id = '123456789ABCDEFGHJKLMNPQRSTUV' \ 'WXYZabcdefghijkmnopqrstuvwxyz' self.committedBlckIds = [] self.pendingTxnCount = 0 self.pendingTxns = [] def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _update_uncommitted_transactions(self): remaining = [] # For each client, we want to verify that its corresponding validator # has the transaction. For a transaction to be considered committed, # all validators must have it in its blockchain as a committed # transaction. for c in self.clients: for t in self.transactions: status = c.get_transaction_status(t) # If the transaction has not been committed and we don't # already have it in our list of uncommitted transactions # then add it. if (status != http.OK) and (t not in remaining): remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(self.Timeout) txnCnt = len(self.transactions) with Progress("Waiting for %s transactions to commit" % (txnCnt)) \ as p: while not to() and txnCnt > 0: p.step() time.sleep(1) txnCnt = self._update_uncommitted_transactions() if txnCnt != 0: if len(self.transactions) != 0: print("Uncommitted transactions: ", self.transactions) raise Exception("{} transactions failed to commit in {}s".format( txnCnt, to.WaitTime)) def _wait_for_no_transaction_commits(self): # for the case where no transactions are expected to commit to = TimeOut(240) starting_txn_count = len(self.transactions) remaining_txn_cnt = len(self.transactions) with Progress("Waiting for transactions to NOT commit") as p: while not to() and remaining_txn_cnt > 0: p.step() time.sleep(1) remaining_txn_cnt = self._update_uncommitted_transactions() if remaining_txn_cnt != starting_txn_count: committedtxncount = starting_txn_count - remaining_txn_cnt raise Exception("{} transactions with missing dependencies " "were committed in {}s" .format(committedtxncount, to.WaitTime)) else: print("No transactions with missing dependencies " "were committed in {0}s".format(to.WaitTime)) def _wait_for_limit_pending_transactions(self): result = self.is_registered('http://localhost:8800/statistics/journal') json_data = json.loads(result) self.committedBlckCount = json_data['journal']['CommittedBlockCount'] print(("committedBlckCount: ", self.committedBlckCount)) self.pendingTxnCount = json_data['journal']['PendingTxnCount'] print(("PendingTxnCount: ", self.pendingTxnCount)) if (self.committedBlckCount > 3 & self.pendingTxnCount != 0): raise Exception("{} blocks were committed " "with {} invalid transactions." .format(self.committedBlckCount, self.pendingTxnCount)) else: print("All pending transactions after " "3 blocks have been dropped") def setup(self, urls, numkeys): self.localState = {} self.transactions = [] self.last_key_txn = {} self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: try: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() except MessageException: logger.warn("Unable to connect to Url: %s ", u) if len(self.clients) == 0: return # add check for if a state already exists with Progress("Checking for pre-existing state") as p: self.state.fetch() for k, v in self.state.State.iteritems(): self.localState[k] = v p.step() keys = self.state.State.keys() with Progress("Populating initial key values") as p: txncount = 0 starttime = time.time() for n in range(1, numkeys + 1): n = str(n) if n not in keys: c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) txncount += 1 self.last_key_txn[n] = txnid p.step() print() self.txnrate(starttime, txncount, "submitted") self._wait_for_transaction_commits() self.txnrate(starttime, txncount, "committed") def run(self, numkeys, rounds=1, txintv=0): if len(self.clients) == 0: return self.state.fetch() keys = self.state.State.keys() print("Running {0} rounds for {1} keys " "with {2} second inter-transaction time" .format(rounds, numkeys, txintv)) for r in range(0, rounds): cnt = 0 starttime = time.time() with Progress("Round {}".format(r)) as p: for k in keys: k = str(k) c = self._get_client() self.localState[k] += 2 txn_dep = self.last_key_txn.get(k, None) txn_id = c.inc(k, 2, txn_dep) if txn_id is None: raise Exception( "Failed to inc key:{} value:{} by 2".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() time.sleep(txintv) for k in keys: k = str(k) c = self._get_client() self.localState[k] -= 1 txn_dep = self.last_key_txn[k] txn_id = c.dec(k, 1, txn_dep) if txn_id is None: raise Exception( "failed to dec key:{} value:{} by 1".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() time.sleep(txintv) txn_count = len(self.transactions) self.txnrate(starttime, txn_count, "submitted") self._wait_for_transaction_commits() self.txnrate(starttime, txn_count, "committed") def validate(self): if len(self.clients) == 0: logger.warn("Unable to connect to Validators, No Clients created") return self.state.fetch() print("Validating IntegerKey State") for k, v in self.state.State.iteritems(): if self.localState[k] != v: print("key {} is {} expected to be {}".format( k, v, self.localState[k])) assert self.localState[k] == v def journalstate(self): self.state.fetch() print("state: ") for k, v in self.state.State.iteritems(): print(k, v) print() def txnrate(self, starttime, numtxns, purpose): if numtxns > 0: endtime = time.time() totaltime = endtime - starttime avgrate = (numtxns / totaltime) print("{0} transaction in {1} seconds averaging {2} t/s " "{3}" .format(numtxns, totaltime, avgrate, purpose)) def generate_txn_id(self): string_id = '' for i in range(0, 16): string_id = string_id + random.choice(self.fake_txn_id) return string_id def is_registered(self, url=None): response = urllib2.urlopen(url).read() return response def run_with_limit_txn_dependencies(self, numkeys, rounds=1, txintv=0): if len(self.clients) == 0: return self.state.fetch() keys = self.state.State.keys() print("Running {0} rounds for {1} keys " "with {2} second inter-transaction time" "with limit on missing dep transactions" .format(rounds, numkeys, txintv)) for r in range(1, rounds + 1): with Progress("Updating clients state") as p: print("Round {}".format(r)) for k in range(1, numkeys + 1): k = str(k) c = self._get_client() print ("Sending invalid txnx: ") cnt = 0 starttime = time.time() for inf in range(0, 3): missingid = self.generate_txn_id() dependingtid = c.inc(k, 1, txndep=missingid) self.pendingTxns.append(dependingtid) cnt += 1 print(("pendingTxns: ", self.pendingTxns)) self.txnrate(starttime, cnt, " invalid transactions NOT submitted") result = self.is_registered( 'http://localhost:8800/statistics/journal') json_data = json.loads(result) self.pendingTxnCount = \ json_data['journal']['PendingTxnCount'] print(("PendingTxnCount: ", self.pendingTxnCount)) for loop_ind in range(0, 4): print ("Sending valid txn:") cnt = 0 starttime = time.time() for ind in range(0, 5): self.localState[k] += 2 txn_dep = self.last_key_txn.get(k, None) txn_id = c.inc(k, 2, txn_dep) if txn_id is None: raise Exception( "Failed to inc key:{} value:{}" " by 2".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() time.sleep(txintv) txn_count = len(self.transactions) self.txnrate(starttime, txn_count, " valid transactions submitted") self._wait_for_transaction_commits() self._wait_for_limit_pending_transactions() print("checking pending txn limit") self._wait_for_limit_pending_transactions() def run_with_missing_dep(self, numkeys, rounds=1): self.state.fetch() print("Running {0} rounds for {1} keys " "with missing transactions" .format(rounds, numkeys)) starttime = time.time() for r in range(1, rounds + 1): print("Round {}".format(r)) for k in range(1, numkeys + 1): k = str(k) c = c = self._get_client() for ind in range(0, 5): missingid = self.generate_txn_id() dependingtid = c.inc(k, 1, txndep=missingid) self.transactions.append(dependingtid) txn_count = len(self.transactions) self.txnrate(starttime, txn_count, " dep txn submitted") self._wait_for_no_transaction_commits()
class IntKeyLoadTest(object): def __init__(self): pass def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _has_uncommitted_transactions(self): remaining = [] for t in self.transactions: status = self.clients[0].get_transaction_status(t) if status != http.OK: remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(900) txn_cnt = len(self.transactions) with Progress("Waiting for %s transactions to commit" % (txn_cnt)) \ as p: while not to() and txn_cnt > 0: p.step() time.sleep(1) self._has_uncommitted_transactions() txn_cnt = len(self.transactions) if txn_cnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txn_cnt, to.WaitTime)) def setup(self, urls): self.global_store = {} self.running_url_list = urls self.global_keys = [] self.transactions = [] self.lastKeyTxn = {} self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: print "Creating clients" for u in self.running_url_list: try: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() except MessageException: print "Unable to connect to Url: {}".format(u) def run(self, count=1, interval=0.0): if self.clients == []: return prev = "" self.state.fetch() self.global_keys = self.state.State.keys() for k, v in self.state.State.iteritems(): self.global_store[k] = v while count > 0: count -= 1 c = self._get_client() c.fetch_state() k = str(random.randint(0, len(self.global_keys) + 1)) # Stops the inc of a key that was just set while k == prev: k = str(random.randint(0, len(self.global_keys) + 1)) if k in self.global_keys: self.global_store[k] += 1 if k in self.lastKeyTxn: txn_dep = self.lastKeyTxn[k] else: txn_dep = None txnid = c.inc(k, 1, txn_dep) if txnid is None: raise Exception( "Failed to inc key:{} value:{} by 1".format( k, self.global_store[k])) self.transactions.append(txnid) self.lastKeyTxn[k] = txnid time.sleep(interval) else: self.global_keys += [k] v = random.randint(5, 1000) self.global_store[k] = v txnid = c.set(k, v, txndep=None) prev = k if txnid is None: raise Exception("Failed to set {} to {}".format(k, v)) self.transactions.append(txnid) self.lastKeyTxn[k] = txnid time.sleep(interval) self._wait_for_transaction_commits() self._wait_for_transaction_commits() def validate(self): if self.clients == []: print "Unable to connect to Validators, No Clients created" return self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.global_store[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.global_store[k] == v
class IntKeyLoadTest(object): def __init__(self, timeout=None): print "start inkeyloadtest" self.localState = {} self.transactions = [] self.clients = [] self.state = None self.Timeout = 240 if timeout is None else timeout def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _update_uncommitted_transactions(self): remaining = [] # For each client, we want to verify that its corresponding validator # has the transaction. For a transaction to be considered committed, # all validators must have it in its blockchain as a committed # transaction. for c in self.clients: for t in self.transactions: status = c.get_transaction_status(t) # If the transaction has not been committed and we don't # already have it in our list of uncommitted transactions # then add it. if (status != http.OK) and (t not in remaining): remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(self.Timeout) txnCnt = len(self.transactions) with Progress("Waiting for %s transactions to commit" % (txnCnt)) \ as p: while not to() and txnCnt > 0: p.step() time.sleep(1) txnCnt = self._update_uncommitted_transactions() if txnCnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txnCnt, to.WaitTime)) def _wait_for_no_transaction_commits(self): # for the case where no transactions are expected to commit to = TimeOut(120) starting_txn_count = len(self.transactions) remaining_txn_cnt = len(self.transactions) with Progress("Waiting for transactions to NOT commit") as p: while not to() and remaining_txn_cnt > 0: p.step() time.sleep(1) remaining_txn_cnt = self._update_uncommitted_transactions() if remaining_txn_cnt != starting_txn_count: committedtxncount = starting_txn_count - remaining_txn_cnt raise Exception("{} transactions with missing dependencies " "were committed in {}s" .format(committedtxncount, to.WaitTime)) else: print "No transactions with missing dependencies " \ "were committed in {0}s".format(to.WaitTime) def setup(self, urls, numkeys): self.localState = {} self.transactions = [] self.last_key_txn = {} self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: try: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() except MessageException: logger.warn("Unable to connect to Url: %s ", u) if len(self.clients) == 0: return # add check for if a state already exists with Progress("Checking for pre-existing state") as p: self.state.fetch() for k, v in self.state.State.iteritems(): self.localState[k] = v p.step() keys = self.state.State.keys() with Progress("Populating initial key values") as p: txncount = 0 starttime = time.time() for n in range(1, numkeys + 1): n = str(n) if n not in keys: c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) txncount += 1 self.last_key_txn[n] = txnid p.step() print self.txnrate(starttime, txncount, "submitted") self._wait_for_transaction_commits() self.txnrate(starttime, txncount, "committed") def run(self, numkeys, rounds=1, txintv=0): if len(self.clients) == 0: return self.state.fetch() keys = self.state.State.keys() print "Running {0} rounds for {1} keys " \ "with {2} second inter-transaction time" \ .format(rounds, numkeys, txintv) for r in range(0, rounds): with Progress("Updating clients state") as p: for c in self.clients: c.fetch_state() p.step() cnt = 0 starttime = time.time() with Progress("Round {}".format(r)) as p: for k in keys: k = str(k) c = self._get_client() self.localState[k] += 2 txn_dep = self.last_key_txn.get(k, None) txn_id = c.inc(k, 2, txn_dep) if txn_id is None: raise Exception( "Failed to inc key:{} value:{} by 2".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() time.sleep(txintv) for k in keys: k = str(k) c = self._get_client() self.localState[k] -= 1 txn_dep = self.last_key_txn[k] txn_id = c.dec(k, 1, txn_dep) if txn_id is None: raise Exception( "failed to dec key:{} value:{} by 1".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() time.sleep(txintv) txn_count = len(self.transactions) self.txnrate(starttime, txn_count, "submitted") self._wait_for_transaction_commits() self.txnrate(starttime, txn_count, "committed") def validate(self): if len(self.clients) == 0: logger.warn("Unable to connect to Validators, No Clients created") return self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.localState[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.localState[k] == v def ledgerstate(self): self.state.fetch() print "state: " for k, v in self.state.State.iteritems(): print k, v print def txnrate(self, starttime, numtxns, purpose): if numtxns > 0: endtime = time.time() totaltime = endtime - starttime avgrate = (numtxns / totaltime) print "{0} transaction in {1} seconds averaging {2} t/s " \ "{3}" .format(numtxns, totaltime, avgrate, purpose) def run_with_missing_dep(self, numkeys, rounds=1): self.state.fetch() print "Running {0} rounds for {1} keys " \ "with missing transactions" \ .format(rounds, numkeys) for r in range(1, rounds + 1): for c in self.clients: c.CurrentState.fetch() print "Round {}".format(r) for k in range(1, numkeys + 1): k = str(k) c = c = self._get_client() missingid = c.inc(k, 1, txndep=None, postmsg=False) dependingtid = c.inc(k, 1, txndep=missingid) self.transactions.append(dependingtid) self._wait_for_no_transaction_commits()
class IntKeyLoadTest(object): def __init__(self): pass def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _has_uncommitted_transactions(self): remaining = [] for t in self.transactions: status = self.clients[0].headrequest('/transaction/{0}'.format(t)) if status != http.OK: remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(120) txnCnt = len(self.transactions) with Progress("Waiting for transactions to commit") as p: while not to() and txnCnt > 0: p.step() time.sleep(1) self._has_uncommitted_transactions() txnCnt = len(self.transactions) if txnCnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txnCnt, to.WaitTime)) def setup(self, urls, numKeys): self.localState = {} self.transactions = [] self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() with Progress("Creating initial key values") as p: for n in range(1, numKeys + 1): n = str(n) c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) self._wait_for_transaction_commits() def run(self, rounds=1): self.state.fetch() keys = self.state.State.keys() for r in range(0, rounds): for c in self.clients: c.CurrentState.fetch() print "Round {}".format(r) for k in keys: c = self._get_client() self.localState[k] += 2 txnid = c.inc(k, 2) if txnid is None: raise Exception( "Failed to inc key:{} value:{} by 2".format( k, self.localState[k])) self.transactions.append(txnid) for k in keys: c = self._get_client() self.localState[k] -= 1 txnid = c.dec(k, 1) if txnid is None: raise Exception( "Failed to dec key:{} value:{} by 1".format( k, self.localState[k])) self.transactions.append(txnid) self._wait_for_transaction_commits() def validate(self): self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.localState[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.localState[k] == v
class IntKeyLoadTest(object): def __init__(self, timeout=None): print "start inkeyloadtest" self.localState = {} self.transactions = [] self.clients = [] self.state = None self.Timeout = 240 if timeout is None else timeout self.fake_txn_id = '123456789ABCDEFGHJKLMNPQRSTUV' \ 'WXYZabcdefghijkmnopqrstuvwxyz' self.committedBlckIds = [] self.pendingTxnCount = 0 self.pendingTxns = [] def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _update_uncommitted_transactions(self): remaining = [] # For each client, we want to verify that its corresponding validator # has the transaction. For a transaction to be considered committed, # all validators must have it in its blockchain as a committed # transaction. for c in self.clients: for t in self.transactions: status = c.get_transaction_status(t) # If the transaction has not been committed and we don't # already have it in our list of uncommitted transactions # then add it. if (status != http.OK) and (t not in remaining): remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(self.Timeout) txnCnt = len(self.transactions) with Progress("Waiting for %s transactions to commit" % (txnCnt)) \ as p: while not to() and txnCnt > 0: p.step() time.sleep(1) txnCnt = self._update_uncommitted_transactions() if txnCnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txnCnt, to.WaitTime)) def _wait_for_no_transaction_commits(self): # for the case where no transactions are expected to commit to = TimeOut(120) starting_txn_count = len(self.transactions) remaining_txn_cnt = len(self.transactions) with Progress("Waiting for transactions to NOT commit") as p: while not to() and remaining_txn_cnt > 0: p.step() time.sleep(1) remaining_txn_cnt = self._update_uncommitted_transactions() if remaining_txn_cnt != starting_txn_count: committedtxncount = starting_txn_count - remaining_txn_cnt raise Exception("{} transactions with missing dependencies " "were committed in {}s".format( committedtxncount, to.WaitTime)) else: print "No transactions with missing dependencies " \ "were committed in {0}s".format(to.WaitTime) def _wait_for_limit_pending_transactions(self): result = self.is_registered('http://localhost:9000/statistics/ledger') json_data = json.loads(result) self.committedBlckCount = json_data['ledger']['CommittedBlockCount'] print("committedBlckCount: ", self.committedBlckCount) self.pendingTxnCount = json_data['ledger']['PendingTxnCount'] print("PendingTxnCount: ", self.pendingTxnCount) if (self.committedBlckCount > 3 & self.pendingTxnCount != 0): raise Exception("{} blocks were committed " "with {} invalid transactions.".format( self.committedBlckCount, self.pendingTxnCount)) else: print "All pending transactions after " \ "3 blocks have been dropped" def setup(self, urls, numkeys): self.localState = {} self.transactions = [] self.last_key_txn = {} self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: try: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() except MessageException: logger.warn("Unable to connect to Url: %s ", u) if len(self.clients) == 0: return # add check for if a state already exists with Progress("Checking for pre-existing state") as p: self.state.fetch() for k, v in self.state.State.iteritems(): self.localState[k] = v p.step() keys = self.state.State.keys() with Progress("Populating initial key values") as p: txncount = 0 starttime = time.time() for n in range(1, numkeys + 1): n = str(n) if n not in keys: c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) txncount += 1 self.last_key_txn[n] = txnid p.step() print self.txnrate(starttime, txncount, "submitted") self._wait_for_transaction_commits() self.txnrate(starttime, txncount, "committed") def run(self, numkeys, rounds=1, txintv=0): if len(self.clients) == 0: return self.state.fetch() keys = self.state.State.keys() print "Running {0} rounds for {1} keys " \ "with {2} second inter-transaction time" \ .format(rounds, numkeys, txintv) for r in range(0, rounds): with Progress("Updating clients state") as p: for c in self.clients: c.fetch_state() p.step() cnt = 0 starttime = time.time() with Progress("Round {}".format(r)) as p: for k in keys: k = str(k) c = self._get_client() self.localState[k] += 2 txn_dep = self.last_key_txn.get(k, None) txn_id = c.inc(k, 2, txn_dep) if txn_id is None: raise Exception( "Failed to inc key:{} value:{} by 2".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() time.sleep(txintv) for k in keys: k = str(k) c = self._get_client() self.localState[k] -= 1 txn_dep = self.last_key_txn[k] txn_id = c.dec(k, 1, txn_dep) if txn_id is None: raise Exception( "failed to dec key:{} value:{} by 1".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() time.sleep(txintv) txn_count = len(self.transactions) self.txnrate(starttime, txn_count, "submitted") self._wait_for_transaction_commits() self.txnrate(starttime, txn_count, "committed") def validate(self): if len(self.clients) == 0: logger.warn("Unable to connect to Validators, No Clients created") return self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.localState[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.localState[k] == v def ledgerstate(self): self.state.fetch() print "state: " for k, v in self.state.State.iteritems(): print k, v print def txnrate(self, starttime, numtxns, purpose): if numtxns > 0: endtime = time.time() totaltime = endtime - starttime avgrate = (numtxns / totaltime) print "{0} transaction in {1} seconds averaging {2} t/s " \ "{3}" .format(numtxns, totaltime, avgrate, purpose) def generate_txn_id(self): string_id = '' for i in range(0, 16): string_id = string_id + random.choice(self.fake_txn_id) return string_id def is_registered(self, url=None): response = urllib2.urlopen(url).read() return response def run_with_limit_txn_dependencies(self, numkeys, rounds=1, txintv=0): if len(self.clients) == 0: return self.state.fetch() keys = self.state.State.keys() print "Running {0} rounds for {1} keys " \ "with {2} second inter-transaction time" \ "with limit on missing dep transactions" \ .format(rounds, numkeys, txintv) for r in range(1, rounds + 1): with Progress("Updating clients state") as p: for c in self.clients: c.fetch_state() p.step() print "Round {}".format(r) for k in range(1, numkeys + 1): k = str(k) c = self._get_client() print("Sending invalid txnx: ") cnt = 0 starttime = time.time() for inf in range(0, 3): missingid = self.generate_txn_id() dependingtid = c.inc(k, 1, txndep=missingid) self.pendingTxns.append(dependingtid) cnt += 1 print("pendingTxns: ", self.pendingTxns) self.txnrate(starttime, cnt, " invalid transactions NOT submitted") result = self.is_registered( 'http://localhost:9000/statistics/ledger') json_data = json.loads(result) self.pendingTxnCount = \ json_data['ledger']['PendingTxnCount'] print("PendingTxnCount: ", self.pendingTxnCount) for loop_ind in range(0, 4): print("Sending valid txn:") cnt = 0 starttime = time.time() for ind in range(0, 5): self.localState[k] += 2 txn_dep = self.last_key_txn.get(k, None) txn_id = c.inc(k, 2, txn_dep) if txn_id is None: raise Exception("Failed to inc key:{} value:{}" " by 2".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() time.sleep(txintv) txn_count = len(self.transactions) self.txnrate(starttime, txn_count, " valid transactions submitted") self._wait_for_transaction_commits() self._wait_for_limit_pending_transactions() print "checking pending txn limit" self._wait_for_limit_pending_transactions() def run_with_missing_dep(self, numkeys, rounds=1): self.state.fetch() print "Running {0} rounds for {1} keys " \ "with missing transactions" \ .format(rounds, numkeys) starttime = time.time() for r in range(1, rounds + 1): for c in self.clients: c.fetch_state() print "Round {}".format(r) for k in range(1, numkeys + 1): k = str(k) c = c = self._get_client() for ind in range(0, 5): missingid = self.generate_txn_id() dependingtid = c.inc(k, 1, txndep=missingid) self.transactions.append(dependingtid) txn_count = len(self.transactions) self.txnrate(starttime, txn_count, " dep txn submitted") self._wait_for_no_transaction_commits()
class IntKeyLoadTest: def __init__(self): pass def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _has_uncommitted_transactions(self): remaining = [] for t in self.transactions: status = self.clients[0].headrequest('/transaction/{0}'.format(t)) if status != http.OK: remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(120) txnCnt = len(self.transactions) with Progress("Waiting for transactions to commit") as p: while not to() and txnCnt > 0: p.step() time.sleep(1) self._has_uncommitted_transactions() txnCnt = len(self.transactions) if txnCnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txnCnt, to.WaitTime)) def setup(self, urls, numKeys): self.localState = {} self.transactions = [] self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() with Progress("Creating initial key values") as p: for n in range(1, numKeys + 1): n = str(n) c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) self._wait_for_transaction_commits() def run(self, rounds=1): self.state.fetch() inc = True keys = self.state.State.keys() for r in range(0, rounds): for c in self.clients: c.CurrentState.fetch() print "Round {}".format(r) for k in keys: c = self._get_client() self.localState[k] += 2 txnid = c.inc(k, 2) if txnid is None: raise Exception( "Failed to inc key:{} value:{} by 2".format( k, self.localState[k])) self.transactions.append(txnid) for k in keys: c = self._get_client() self.localState[k] -= 1 txnid = c.dec(k, 1) if txnid is None: raise Exception( "Failed to dec key:{} value:{} by 1".format( k, self.localState[k])) self.transactions.append(txnid) self._wait_for_transaction_commits() def validate(self): self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.localState[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.localState[k] == v
class IntKeyLoadTest(object): def __init__(self, timeout=None): self.Timeout = 240 if timeout is None else timeout def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _update_uncommitted_transactions(self): remaining = [] # For each client, we want to verify that its corresponding validator # has the transaction. For a transaction to be considered committed, # all validators must have it in its blockchain as a committed # transaction. for c in self.clients: for t in self.transactions: status = c.headrequest('/transaction/{0}'.format(t)) # If the transaction has not been committed and we don't # already have it in our list of uncommitted transactions # then add it. if (status != http.OK) and (t not in remaining): remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(self.Timeout) txn_cnt = len(self.transactions) with Progress("Waiting for %s transactions to commit" % (txn_cnt)) \ as p: while not to() and txn_cnt > 0: p.step() time.sleep(1) txn_cnt = self._update_uncommitted_transactions() if txn_cnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txn_cnt, to.WaitTime)) def setup(self, urls, num_keys): self.localState = {} self.transactions = [] self.last_key_txn = {} self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: try: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() except MessageException: logger.warn("Unable to connect to Url: %s ", u) if len(self.clients) == 0: return # add check for if a state already exists with Progress("Checking for pre-existing state") as p: self.state.fetch() keys = self.state.State.keys() for k, v in self.state.State.iteritems(): self.localState[k] = v with Progress("Creating initial key values") as p: for n in range(1, num_keys + 1): n = str(n) if n not in keys: c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txn_id = c.set(n, v, txndep=None) if txn_id is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txn_id) self.last_key_txn[n] = txn_id self._wait_for_transaction_commits() def run(self, rounds=1): if len(self.clients) == 0: return self.state.fetch() keys = self.state.State.keys() for r in range(1, rounds + 1): with Progress("Updating clients state") as p: for c in self.clients: c.CurrentState.fetch() p.step() cnt = 0 with Progress("Round {}".format(r)) as p: for k in keys: c = self._get_client() self.localState[k] += 2 if k in self.last_key_txn: txn_dep = self.last_key_txn[k] else: txn_dep = None txn_id = c.inc(k, 2, txn_dep) if txn_id is None: raise Exception( "Failed to inc key:{} value:{} by 2".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() for k in keys: c = self._get_client() self.localState[k] -= 1 txn_dep = self.last_key_txn[k] txn_id = c.dec(k, 1, txn_dep) if txn_id is None: raise Exception( "Failed to dec key:{} value:{} by 1".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() self._wait_for_transaction_commits() def validate(self): if len(self.clients) == 0: logger.warn("Unable to connect to Validators, No Clients created") return self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.localState[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.localState[k] == v
class IntKeyLoadTest(object): def __init__(self): pass def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _has_uncommitted_transactions(self): remaining = [] for t in self.transactions: status = self.clients[0].headrequest('/transaction/{0}'.format(t)) if status != http.OK: remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(240) txnCnt = len(self.transactions) with Progress("Waiting for %s transactions to commit" % (txnCnt)) as p: while not to() and txnCnt > 0: p.step() time.sleep(1) self._has_uncommitted_transactions() txnCnt = len(self.transactions) if txnCnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txnCnt, to.WaitTime)) def _wait_for_no_transaction_commits(self): # for the case where no transactions are expected to commit to = TimeOut(120) startingTxnCnt = len(self.transactions) remainingTxnCnt = len(self.transactions) with Progress("Waiting for no transactions to commit") as p: while not to() and remainingTxnCnt > 0: p.step() time.sleep(1) self._has_uncommitted_transactions() remainingTxnCnt = len(self.transactions) if startingTxnCnt != remainingTxnCnt: raise Exception("{} unexpected transaction commits after {}s" .format(startingTxnCnt - remainingTxnCnt, to.WaitTime)) # def _wait_for_no_transaction_commits(self): # to = TimeOut(120) # txnCnt = len(self.transactions) # with Progress("Waiting for transactions to commit") as p: # while not to() and txnCnt > 0: # p.step() # time.sleep(1) # self._has_uncommitted_transactions() # txnCnt = len(self.transactions) # # if txnCnt != 0: # if len(self.transactions) != 0: # print "Uncommitted transactions: ", self.transactions # "{} transactions with unmet dependencies did not " \ # "to commit in {}s".format( # txnCnt, to.WaitTime) # else: # raise Exception("{} transactions with unmet dependencies " # "committed in {}s".format( # txnCnt, to.WaitTime)) def setup(self, urls, numKeys): self.localState = {} self.transactions = [] self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() with Progress("Creating initial key values") as p: for n in range(1, numKeys + 1): n = str(n) c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) self._wait_for_transaction_commits() def run(self, rounds=1): self.state.fetch() keys = self.state.State.keys() for r in range(0, rounds): for c in self.clients: c.CurrentState.fetch() print "Round {}".format(r) for k in keys: c = self._get_client() self.localState[k] += 2 txnid = c.inc(k, 2) if txnid is None: raise Exception( "Failed to inc key:{} value:{} by 2".format( k, self.localState[k])) self.transactions.append(txnid) # for k in keys: # c = self._get_client() # self.localState[k] -= 1 # txnid = c.dec(k, 1) # if txnid is None: # raise Exception( # "Failed to dec key:{} value:{} by 1".format( # k, self.localState[k])) # self.transactions.append(txnid) self._wait_for_transaction_commits() def run_missing_dep_test(self, rounds=0): self.state.fetch() keys = self.state.State.keys() for r in range(0, rounds): for c in self.clients: c.CurrentState.fetch() print "Round {}".format(r) for k in keys: c = self._get_client() missingid = c.inc(k, 1, txndep=None, postmsg=False) dependingtid = c.inc(k, 1, txndep=missingid) self.transactions.append(dependingtid) self._wait_for_no_transaction_commits() def validate(self): self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.localState[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.localState[k] == v
class IntKeyLoadTest(object): def __init__(self): pass def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _has_uncommitted_transactions(self): remaining = [] for t in self.transactions: status = self.clients[0].headrequest('/transaction/{0}'.format(t)) if status != http.OK: remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(900) txnCnt = len(self.transactions) with Progress("Waiting for %s transactions to commit" % (txnCnt)) as p: while not to() and txnCnt > 0: p.step() time.sleep(1) self._has_uncommitted_transactions() txnCnt = len(self.transactions) if txnCnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txnCnt, to.WaitTime)) def setup(self, urls): self.GlobalStore = {} self.RunningUrlList = urls self.GlobalKeys = [] self.transactions = [] self.lastKeyTxn = {} self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: print "Creating clients" for u in self.RunningUrlList: try: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() except MessageException: print "Unable to connect to Url: {}".format(u) def run(self, count=1, interval=0.0): if self.clients == []: return prev = "" self.state.fetch() self.GlobalKeys = self.state.State.keys() for k, v in self.state.State.iteritems(): self.GlobalStore[k] = v while count > 0: count -= 1 c = self._get_client() c.CurrentState.fetch() k = str(random.randint(0, len(self.GlobalKeys) + 1)) # Stops the inc of a key that was just set while k == prev: k = str(random.randint(0, len(self.GlobalKeys) + 1)) if k in self.GlobalKeys: self.GlobalStore[k] += 1 if k in self.lastKeyTxn: txndep = self.lastKeyTxn[k] else: txndep = None txnid = c.inc(k, 1, txndep) if txnid is None: raise Exception( "Failed to inc key:{} value:{} by 1".format( k, self.GlobalStore[k])) self.transactions.append(txnid) self.lastKeyTxn[k] = txnid time.sleep(interval) else: self.GlobalKeys += [k] v = random.randint(5, 1000) self.GlobalStore[k] = v txnid = c.set(k, v, txndep=None) prev = k if txnid is None: raise Exception("Failed to set {} to {}".format(k, v)) self.transactions.append(txnid) self.lastKeyTxn[k] = txnid time.sleep(interval) self._wait_for_transaction_commits() self._wait_for_transaction_commits() def validate(self): if self.clients == []: print "Unable to connect to Validators, No Clients created" return self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.GlobalStore[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.GlobalStore[k] == v
class IntKeyLoadTest(object): def __init__(self, timeout=None): self.Timeout = 240 if timeout is None else timeout def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _update_uncommitted_transactions(self): remaining = [] # For each client, we want to verify that its corresponding validator # has the transaction. For a transaction to be considered committed, # all validators must have it in its blockchain as a committed # transaction. for c in self.clients: for t in self.transactions: status = c.get_transaction_status(t) # If the transaction has not been committed and we don't # already have it in our list of uncommitted transactions # then add it. if (status != http.OK) and (t not in remaining): remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(self.Timeout) txn_cnt = len(self.transactions) with Progress("Waiting for %s transactions to commit" % (txn_cnt)) \ as p: while not to() and txn_cnt > 0: p.step() time.sleep(1) txn_cnt = self._update_uncommitted_transactions() if txn_cnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txn_cnt, to.WaitTime)) def setup(self, urls, num_keys): self.localState = {} self.transactions = [] self.last_key_txn = {} self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: try: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() except MessageException: logger.warn("Unable to connect to Url: %s ", u) if len(self.clients) == 0: return # add check for if a state already exists with Progress("Checking for pre-existing state") as p: self.state.fetch() for k, v in self.state.State.iteritems(): self.localState[k] = v p.step() with Progress("Creating initial key values") as p: for n in range(1, num_keys + 1): n = str(n) c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txn_id = c.set(n, v, txndep=None) if txn_id is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txn_id) self.last_key_txn[n] = txn_id p.step() self._wait_for_transaction_commits() def run(self, rounds=1): if len(self.clients) == 0: return self.state.fetch() keys = self.state.State.keys() for r in range(1, rounds + 1): with Progress("Updating clients state") as p: for c in self.clients: c.fetch_state() p.step() cnt = 0 with Progress("Round {}".format(r)) as p: for k in keys: c = self._get_client() self.localState[k] += 2 if k in self.last_key_txn: txn_dep = self.last_key_txn[k] else: txn_dep = None txn_id = c.inc(k, 2, txn_dep) if txn_id is None: raise Exception( "Failed to inc key:{} value:{} by 2".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() for k in keys: c = self._get_client() self.localState[k] -= 1 txn_dep = self.last_key_txn[k] txn_id = c.dec(k, 1, txn_dep) if txn_id is None: raise Exception( "Failed to dec key:{} value:{} by 1".format( k, self.localState[k])) self.transactions.append(txn_id) self.last_key_txn[k] = txn_id cnt += 1 if cnt % 10 == 0: p.step() self._wait_for_transaction_commits() def validate(self): if len(self.clients) == 0: logger.warn("Unable to connect to Validators, No Clients created") return self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.localState[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.localState[k] == v
class IntKeyLoadTest(object): def __init__(self): pass def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _has_uncommitted_transactions(self): remaining = [] for t in self.transactions: status = self.clients[0].headrequest('/transaction/{0}'.format(t)) if status != http.OK: remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(240) txnCnt = len(self.transactions) with Progress("Waiting for %s transactions to commit" % (txnCnt)) as p: while not to() and txnCnt > 0: p.step() time.sleep(1) self._has_uncommitted_transactions() txnCnt = len(self.transactions) if txnCnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txnCnt, to.WaitTime)) def _wait_for_no_transaction_commits(self): # for the case where no transactions are expected to commit to = TimeOut(120) startingTxnCnt = len(self.transactions) remainingTxnCnt = len(self.transactions) with Progress("Waiting for no transactions to commit") as p: while not to() and remainingTxnCnt > 0: p.step() time.sleep(1) self._has_uncommitted_transactions() remainingTxnCnt = len(self.transactions) if startingTxnCnt != remainingTxnCnt: raise Exception( "{} unexpected transaction commits after {}s".format( startingTxnCnt - remainingTxnCnt, to.WaitTime)) # def _wait_for_no_transaction_commits(self): # to = TimeOut(120) # txnCnt = len(self.transactions) # with Progress("Waiting for transactions to commit") as p: # while not to() and txnCnt > 0: # p.step() # time.sleep(1) # self._has_uncommitted_transactions() # txnCnt = len(self.transactions) # # if txnCnt != 0: # if len(self.transactions) != 0: # print "Uncommitted transactions: ", self.transactions # "{} transactions with unmet dependencies did not " \ # "to commit in {}s".format( # txnCnt, to.WaitTime) # else: # raise Exception("{} transactions with unmet dependencies " # "committed in {}s".format( # txnCnt, to.WaitTime)) def setup(self, urls, numKeys): self.localState = {} self.transactions = [] self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() with Progress("Creating initial key values") as p: for n in range(1, numKeys + 1): n = str(n) c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) self._wait_for_transaction_commits() def run(self, rounds=1): self.state.fetch() keys = self.state.State.keys() for r in range(0, rounds): for c in self.clients: c.CurrentState.fetch() print "Round {}".format(r) for k in keys: c = self._get_client() self.localState[k] += 2 txnid = c.inc(k, 2) if txnid is None: raise Exception( "Failed to inc key:{} value:{} by 2".format( k, self.localState[k])) self.transactions.append(txnid) # for k in keys: # c = self._get_client() # self.localState[k] -= 1 # txnid = c.dec(k, 1) # if txnid is None: # raise Exception( # "Failed to dec key:{} value:{} by 1".format( # k, self.localState[k])) # self.transactions.append(txnid) self._wait_for_transaction_commits() def run_missing_dep_test(self, rounds=0): self.state.fetch() keys = self.state.State.keys() for r in range(0, rounds): for c in self.clients: c.CurrentState.fetch() print "Round {}".format(r) for k in keys: c = self._get_client() missingid = c.inc(k, 1, txndep=None, postmsg=False) dependingtid = c.inc(k, 1, txndep=missingid) self.transactions.append(dependingtid) self._wait_for_no_transaction_commits() def validate(self): self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.localState[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.localState[k] == v
class IntKeyLoadTest(object): def __init__(self): print "start inkeyloadtest" self.localState = {} self.transactions = [] self.clients = [] self.state = None def _get_client(self): return self.clients[random.randint(0, len(self.clients) - 1)] def _update_uncommitted_transactions(self): remaining = [] # For each client, we want to verify that its corresponding validator # has the transaction. For a transaction to be considered committed, # all validators must have it in its blockchain as a committed # transaction. for c in self.clients: for t in self.transactions: status = c.get_transaction_status(t) # If the transaction has not been committed and we don't # already have it in our list of uncommitted transactions # then add it. if (status != http.OK) and (t not in remaining): remaining.append(t) self.transactions = remaining return len(self.transactions) def _wait_for_transaction_commits(self): to = TimeOut(240) txnCnt = len(self.transactions) with Progress("Waiting for transactions to commit") as p: while not to() and txnCnt > 0: p.step() time.sleep(1) txnCnt = self._update_uncommitted_transactions() if txnCnt != 0: if len(self.transactions) != 0: print "Uncommitted transactions: ", self.transactions raise Exception("{} transactions failed to commit in {}s".format( txnCnt, to.WaitTime)) def _wait_for_no_transaction_commits(self): # for the case where no transactions are expected to commit to = TimeOut(120) starting_txn_count = len(self.transactions) remaining_txn_cnt = len(self.transactions) with Progress("Waiting for transactions to NOT commit") as p: while not to() and remaining_txn_cnt > 0: p.step() time.sleep(1) remaining_txn_cnt = self._update_uncommitted_transactions() if remaining_txn_cnt != starting_txn_count: committedtxncount = starting_txn_count - remaining_txn_cnt raise Exception("{} transactions with missing dependencies " "were committed in {}s".format( committedtxncount, to.WaitTime)) else: print "No transactions with missing dependencies " \ "were committed in {0}s".format(to.WaitTime) def setup(self, urls, numkeys): self.localState = {} self.transactions = [] self.clients = [] self.state = IntegerKeyState(urls[0]) with Progress("Creating clients") as p: for u in urls: key = generate_private_key() self.clients.append(IntegerKeyClient(u, keystring=key)) p.step() print "Checking for pre-existing state" self.state.fetch() keys = self.state.State.keys() for k, v in self.state.State.iteritems(): self.localState[k] = v with Progress("Populating initial key values") as p: txncount = 0 starttime = time.clock() for n in range(1, numkeys + 1): n = str(n) if n not in keys: c = self._get_client() v = random.randint(5, 1000) self.localState[n] = v txnid = c.set(n, v) if txnid is None: raise Exception("Failed to set {} to {}".format(n, v)) self.transactions.append(txnid) txncount += 1 self.txnrate(starttime, txncount) self._wait_for_transaction_commits() def run(self, numkeys, rounds=1, txintv=0): self.state.fetch() print "Running {0} rounds for {1} keys " \ "with {2} second inter-transaction time" \ .format(rounds, numkeys, txintv) for r in range(0, rounds): for c in self.clients: c.fetch_state() print "Round {}".format(r) # for k in keys: starttime = time.clock() for k in range(1, numkeys + 1): k = str(k) c = self._get_client() self.localState[k] += 2 txnid = c.inc(k, 2) if txnid is None: raise Exception( "Failed to inc key:{} value:{} by 2".format( k, self.localState[k])) self.transactions.append(txnid) time.sleep(txintv) # for k in keys: for k in range(1, numkeys + 1): k = str(k) c = self._get_client() self.localState[k] -= 1 txnid = c.dec(k, 1) if txnid is None: raise Exception( "Failed to dec key:{} value:{} by 1".format( k, self.localState[k])) self.transactions.append(txnid) time.sleep(txintv) self.txnrate(starttime, 2 * numkeys) self._wait_for_transaction_commits() def validate(self): self.state.fetch() print "Validating IntegerKey State" for k, v in self.state.State.iteritems(): if self.localState[k] != v: print "key {} is {} expected to be {}".format( k, v, self.localState[k]) assert self.localState[k] == v def ledgerstate(self): self.state.fetch() print "state: " for k, v in self.state.State.iteritems(): print k, v print def txnrate(self, starttime, numtxns): if numtxns > 0: endtime = time.clock() totaltime = endtime - starttime avgrate = (numtxns / totaltime) print "Sent {0} transaction in {1} seconds averaging {2} t/s" \ .format(numtxns, totaltime, avgrate) def run_with_missing_dep(self, numkeys, rounds=1): self.state.fetch() print "Running {0} rounds for {1} keys " \ "with missing transactions" \ .format(rounds, numkeys) for r in range(1, rounds + 1): for c in self.clients: c.CurrentState.fetch() print "Round {}".format(r) for k in range(1, numkeys + 1): k = str(k) c = c = self._get_client() missingid = c.inc(k, 1, txndep=None, postmsg=False) dependingtid = c.inc(k, 1, txndep=missingid) self.transactions.append(dependingtid) self._wait_for_no_transaction_commits()