{ "cells": [ { "cell_type": "code", "execution_count": 10, "outputs": [], "source": [ "from typing import Dict\n", "import uuid\n", "from uhashring import HashRing" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 11, "outputs": [], "source": [ "def rnd_key() -> str:\n", " return uuid.uuid4().hex\n", "\n", "def print_hist(counts: Dict[str, int]):\n", " total = sum(list(counts.values()))\n", " hist = {\n", " k: \"%.2f %%\" % (v / total * 100,)\n", " for k, v in counts.items()\n", " }\n", " print(hist)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 12, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "at begin, only two nodes\n", "test with 1000 requests\n", "{'n1': '52.00 %', 'n2': '48.00 %'}\n" ] } ], "source": [ "print(\"at begin, only two nodes\")\n", "hr = HashRing(nodes=[\"n1\", \"n2\"])\n", "counts = {\"n1\": 0, \"n2\": 0}\n", "n_test = 1000\n", "for _ in range(n_test):\n", " counts[hr.get_node(rnd_key())] += 1\n", "print(f\"test with {n_test} requests\")\n", "print_hist(counts)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 13, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "add third node\n", "test with 1000 requests\n", "{'n1': '33.00 %', 'n2': '32.40 %', 'n3': '34.60 %'}\n" ] } ], "source": [ "print(\"add third node\")\n", "hr.add_node(\"n3\")\n", "counts = {\"n1\": 0, \"n2\": 0, \"n3\": 0}\n", "for _ in range(n_test):\n", " counts[hr.get_node(rnd_key())] += 1\n", "print(f\"test with {n_test} requests\")\n", "print_hist(counts)\n" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 14, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "add fourth node\n", "test with 1000 requests\n", "{'n1': '25.50 %', 'n2': '24.00 %', 'n3': '25.10 %', 'n4': '25.40 %'}\n" ] } ], "source": [ "print(\"add fourth node\")\n", "hr.add_node(\"n4\")\n", "counts = {\"n1\": 0, \"n2\": 0, \"n3\": 0, \"n4\": 0}\n", "for _ in range(n_test):\n", " counts[hr.get_node(rnd_key())] += 1\n", "print(f\"test with {n_test} requests\")\n", "print_hist(counts)\n" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.11" } }, "nbformat": 4, "nbformat_minor": 5 }