Skip to content

Instantly share code, notes, and snippets.

@soumyadipdm
Created November 20, 2015 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soumyadipdm/30ac4cba625395231cf3 to your computer and use it in GitHub Desktop.
Save soumyadipdm/30ac4cba625395231cf3 to your computer and use it in GitHub Desktop.
import sys
import time
import numpy as np
import json
CF_HASHTABLESIZE = 8192
def stringhash(s, seed, max):
# Implementation of StringHash() from:
# https://github.com/lpefferkorn/cfe-rsplaytime/blob/master/cfe-rsplaytime.py
h = np.uint32(seed)
for i in bytearray(s):
h += np.uint32(i)
h += np.uint32(h << 10)
h ^= np.uint32(h >> 6)
h += np.uint32(h << 3)
h ^= np.uint32(h >> 11)
h += np.uint32(h << 15)
return np.uint32(h & (max - 1))
def get_index_changed(hostname, ip, list_length, ti):
# changed calculation of hash time as seed
ti -= ti%CF_HASHTABLESIZE
hashstr = '{0}+{1}+{2}'.format(hostname, ip, ti)
hash_value = stringhash(hashstr, 0, CF_HASHTABLESIZE)
return int(list_length * hash_value / float(CF_HASHTABLESIZE))
def get_index_original(hostname, ip, list_length):
# original calculation of hash
hashstr = '{0}+{1}+{2}'.format(hostname, ip, 0)
hash_value = stringhash(hashstr, 0, CF_HASHTABLESIZE)
start = 0
return int(list_length * hash_value / float(CF_HASHTABLESIZE))
def get_splaytime(hostname, ip):
# implementation of GetSplay() in cf-execd/execd-config.c
hashstr = '{0}+{1}+{2}'.format(hostname, ip, 0)
hash_value = stringhash(hashstr, 0, CF_HASHTABLESIZE)
splay = np.float64(hash_value / float(CF_HASHTABLESIZE))
return int(4*60*splay)
def main():
mpses = ['mps1', 'mps2', 'mps3', 'mps4']
data = {}
ti = time.time()
for i in range(4):
for j in range(1, 201):
host_num = j+i*100
hostname = "dc1-app{0}.internal.local.net".format(host_num)
ip = "10.1.{0}.{1}".format(i, j)
splaytime = get_splaytime(hostname, ip)
mps_orig = mpses[get_index_original(hostname, ip, len(mpses))]
select_class_changed = mpses[get_index_changed(hostname, ip, len(mpses), ti+splaytime)]
if splaytime not in data:
data[splaytime] = []
data[splaytime].append({'hostname': hostname, 'select_class_original': mps_orig, 'select_class_changed': select_class_changed })
#print json.dumps(data, indent=2, sort_keys=True)
mps_count = []
for seconds in sorted(data.keys()):
if seconds%60 == 0:
temp_data = { 'select_class_original': {}, 'select_class_changed': {} }
mps_count.append(temp_data)
for i in ['select_class_original', 'select_class_changed']:
for j in data[seconds]:
if j[i] not in temp_data[i]:
temp_data[i][j[i]] = 0
temp_data[i][j[i]] += 1
for minute, methods in enumerate(mps_count):
print "During Minute: {0}".format(minute+1)
for method in methods:
print "{0}:".format(method)
for mps in methods[method]:
print "{0} was selected by {1} hosts".format(mps, methods[method][mps])
print ""
print ""
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment