Skip to content

Instantly share code, notes, and snippets.

@amirziai
Last active September 25, 2022 23:21
Show Gist options
  • Select an option

  • Save amirziai/b9b0d47e725e0c21533fbe9f294833b6 to your computer and use it in GitHub Desktop.

Select an option

Save amirziai/b9b0d47e725e0c21533fbe9f294833b6 to your computer and use it in GitHub Desktop.
def consistent_hashing_same_allocations(
n: int,
urls: List[str],
ch_cls: ConsistentHashing,
) -> float:
ch = ch_cls(server_ids={random_str() for _ in range(n)})
allocs = {url: ch.key_lookup(key=url) for url in urls}
ch.add_server(server_id=random_str())
return sum(
allocs[url] == ch.key_lookup(key=url)
for url in urls
) / len(urls)
def run_exp(
ns: Sequence[int],
experiment_cnt: int,
ch_cls: Type[ConsistentHashing],
) -> pd.DataFrame:
return pd.DataFrame(
dict(
n=n,
idx_experiment=idx_experiment,
same_alloc=consistent_hashing_same_allocations(
n=n,
urls=urls,
ch_cls=ch_cls,
),
)
for n in ns
for idx_experiment in range(experiment_cnt)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment