Created
May 5, 2014 19:16
-
-
Save exrhizo/f5822dce62fb4b538de0 to your computer and use it in GitHub Desktop.
This generates test cases for program 3 in CSc 422 Spring 2014 - Could automate test with this, could use improvements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
CSc 422 | |
Author: Alex Warren | |
Program 3 Distributed Hash Table | |
Test correct key placement. | |
""" | |
import subprocess | |
from random import random, randint, seed | |
NUM_ITERATIONS = 45 | |
CHECK_INTERVAL = 5 | |
NUM_NODES = 10 | |
MIN_INT = -2147483648 | |
MAX_INT = 2147483647 | |
MAX_ID = MAX_INT | |
MIN_ID = MIN_INT | |
MIN_INT = -100 | |
MAX_INT = 100 | |
MAX_ID = 100 | |
MIN_ID = -10 | |
PROB_LOW = 10 | |
PROB_HIGH = 300 | |
def piecemeal_probablities(iteration): | |
""" | |
Function for controlling the probability of Add vs Remove. | |
Put probablility is whatever remains. | |
Returns tuple: (P(Add), P(Remove)) | |
""" | |
if iteration < PROB_LOW: | |
return (.4, .58) | |
elif iteration < PROB_HIGH: | |
return (.6, .38) | |
else: | |
return (.43, .43) | |
def dht_test(): | |
""" | |
Test the dht with mpirun. Check that values are stored in | |
the correct node. | |
""" | |
seed(5) | |
put_str = "0 {key} {val}" | |
get_str = "1 {key}" | |
add_str = "2 {rank} {id}" | |
remove_str = "3 {id}" | |
output_str = "{val} {id}" | |
iteration = 0 | |
active_nodes = [] #(rank, id) | |
inactive_nodes = range(1, NUM_NODES) # rank | |
entries = [] #(key, value) | |
commands = [] | |
outputs = [] | |
#Add the first node | |
rank = inactive_nodes.pop(randint(1, NUM_NODES-1)) | |
node_id = randint(MIN_ID, MAX_ID) | |
active_nodes.append((rank, node_id)) | |
commands.append(add_str.format(rank=rank, id=node_id)) | |
while iteration < NUM_ITERATIONS: | |
prob_add, prob_remove = (.4, .5) #piecemeal_probablities(iteration) | |
rand = random() | |
if rand < prob_add: | |
#Add | |
if len(inactive_nodes) == 0: | |
continue | |
elif len(inactive_nodes) == 1: | |
rank = inactive_nodes.pop(0) | |
else: | |
rank = inactive_nodes.pop(randint(1, len(inactive_nodes)-1)) | |
node_id = randint(MIN_ID, MAX_ID) | |
if sum((x[1]==node_id for x in active_nodes)): | |
print "WHAT ARE THE CHANCES (add)?" | |
#TODO figure out the chances | |
print "..." | |
continue | |
active_nodes.append((rank, node_id)) | |
commands.append(add_str.format(rank=rank, id=node_id)) | |
elif rand < prob_add+prob_remove: | |
#Remove | |
if len(active_nodes) == 1: | |
continue | |
node = active_nodes.pop(randint(0, len(active_nodes)-1)) | |
inactive_nodes.append(node[0]) | |
commands.append(remove_str.format(id=node[1])) | |
else: | |
#Put | |
key = randint(MIN_INT, MAX_INT) | |
val = randint(MIN_INT, MAX_INT) | |
if (key, val) in entries: | |
print "WHAT ARE THE CHANCES (put)?" | |
#TODO figure out the chances | |
print "..." | |
continue | |
entries.append((key, val)) | |
commands.append(put_str.format(key=key, val=val)) | |
iteration += 1 | |
if (iteration % CHECK_INTERVAL) == 0: | |
active_nodes.sort(key=lambda tup: tup[1]) #sort by id | |
entries.sort(key=lambda tup: tup[0]) #sort by key | |
node_id = 0 | |
for entry in entries: | |
while (entry[0] > active_nodes[node_id][1] | |
and not node_id == len(active_nodes) - 1): | |
node_id += 1 | |
commands.append(get_str.format(key=entry[0])) | |
outputs.append(output_str.format(val=entry[1], | |
id=active_nodes[node_id][1])) | |
commands.append("4") #end command | |
print('\n'.join(commands)) | |
print "-----------------------------------" | |
print('\n'.join(outputs)) | |
def main(): | |
""" | |
Run dht_test.py as a script. | |
""" | |
dht_test() | |
if( __name__ == "__main__"): | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment