Hetzner/load-balancer/test.ipynb

277 lines
67 KiB
Plaintext
Raw Normal View History

2023-01-30 22:53:49 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: requests in /home/kuba/uam/sem2/chmura/Hetzner/code-server/hetz_gitea_venv/lib/python3.9/site-packages (2.28.2)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /home/kuba/uam/sem2/chmura/Hetzner/code-server/hetz_gitea_venv/lib/python3.9/site-packages (from requests) (3.0.1)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /home/kuba/uam/sem2/chmura/Hetzner/code-server/hetz_gitea_venv/lib/python3.9/site-packages (from requests) (2022.12.7)\n",
"Requirement already satisfied: idna<4,>=2.5 in /home/kuba/uam/sem2/chmura/Hetzner/code-server/hetz_gitea_venv/lib/python3.9/site-packages (from requests) (3.4)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/kuba/uam/sem2/chmura/Hetzner/code-server/hetz_gitea_venv/lib/python3.9/site-packages (from requests) (1.26.14)\n",
"\u001b[33mWARNING: You are using pip version 22.0.4; however, version 22.3.1 is available.\n",
"You should consider upgrading via the '/home/kuba/uam/sem2/chmura/Hetzner/code-server/hetz_gitea_venv/bin/python -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n",
"\u001b[0mNote: you may need to restart the kernel to use updated packages.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.192s\tfast\t\n",
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.178s\tfast\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.489s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.523s\tfast\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.755s\tfast\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.824s\t\n",
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.823s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.610s\tfast\t\n",
"INFO:root:Thread: 2\treqs: 1\tmean time: 2.086s\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.465s\t\n",
"INFO:root:Thread: 3\treqs: 1\tmean time: 2.584s\t\n",
"INFO:root:Thread: 0\treqs: 2\tmean time: 1.565s\tfast\t\n",
"INFO:root:Thread: 1\treqs: 1\tmean time: 2.151s\t\n",
"INFO:root:Thread: 3\treqs: 2\tmean time: 1.598s\t\n",
"INFO:root:Thread: 2\treqs: 2\tmean time: 2.198s\t\n",
"INFO:root:Thread: 4\treqs: 1\tmean time: 2.863s\t\n",
"INFO:root:Thread: 0\treqs: 2\tmean time: 2.258s\tfast\t\n",
"INFO:root:Thread: 1\treqs: 1\tmean time: 2.775s\t\n",
"INFO:root:Thread: 3\treqs: 1\tmean time: 2.154s\t\n",
"INFO:root:Thread: 2\treqs: 1\tmean time: 2.530s\t\n",
"INFO:root:Thread: 4\treqs: 1\tmean time: 2.278s\t\n",
"INFO:root:Thread: 0\treqs: 2\tmean time: 1.543s\tfast\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.730s\t\n",
"INFO:root:Thread: 3\treqs: 1\tmean time: 3.747s\t\n",
"INFO:root:Thread: 4\treqs: 1\tmean time: 2.123s\t\n",
"INFO:root:Thread: 2\treqs: 1\tmean time: 4.172s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.420s\tfast\t\n",
"INFO:root:Thread: 3\treqs: 1\tmean time: 2.872s\t\n",
"INFO:root:Thread: 1\treqs: 1\tmean time: 4.077s\t\n",
"INFO:root:Thread: 2\treqs: 1\tmean time: 2.762s\t\n",
"INFO:root:Thread: 2\treqs: 1\tmean time: 2.906s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.707s\tfast\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.576s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.447s\tfast\t\n",
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.205s\tfast\t\n",
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.190s\tfast\t\n",
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.200s\tfast\t\n"
]
}
],
"source": [
"%pip install requests\n",
"import requests\n",
"import random\n",
"import math\n",
"import time\n",
"import threading\n",
"import logging\n",
"logging.getLogger().setLevel(logging.INFO)\n",
"\n",
"\n",
"API_URL=\"http://65.109.40.56:80\"\n",
"\n",
"\n",
"UNIT = 5.0 # secs\n",
"\n",
"# Pre generated primes\n",
"first_primes_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29,\n",
" 31, 37, 41, 43, 47, 53, 59, 61, 67,\n",
" 71, 73, 79, 83, 89, 97, 101, 103,\n",
" 107, 109, 113, 127, 131, 137, 139,\n",
" 149, 151, 157, 163, 167, 173, 179,\n",
" 181, 191, 193, 197, 199, 211, 223,\n",
" 227, 229, 233, 239, 241, 251, 257,\n",
" 263, 269, 271, 277, 281, 283, 293,\n",
" 307, 311, 313, 317, 331, 337, 347, 349]\n",
"\n",
"\n",
"def nBitRandom(n):\n",
" return random.randrange(2**(n-1)+1, 2**n - 1)\n",
" \n",
"def getLowLevelPrime(n):\n",
" '''Generate a prime candidate divisible\n",
" by first primes'''\n",
" while True:\n",
" # Obtain a random number\n",
" pc = nBitRandom(n)\n",
" \n",
" # Test divisibility by pre-generated\n",
" # primes\n",
" for divisor in first_primes_list:\n",
" if pc % divisor == 0 and divisor**2 <= pc:\n",
" break\n",
" else: return pc\n",
" \n",
"def isMillerRabinPassed(mrc):\n",
" '''Run 20 iterations of Rabin Miller Primality test'''\n",
" maxDivisionsByTwo = 0\n",
" ec = mrc-1\n",
" while ec % 2 == 0:\n",
" ec >>= 1\n",
" maxDivisionsByTwo += 1\n",
" assert(2**maxDivisionsByTwo * ec == mrc-1)\n",
" \n",
" def trialComposite(round_tester):\n",
" if pow(round_tester, ec, mrc) == 1:\n",
" return False\n",
" for i in range(maxDivisionsByTwo):\n",
" if pow(round_tester, 2**i * ec, mrc) == mrc-1:\n",
" return False\n",
" return True\n",
" \n",
" # Set number of trials here\n",
" numberOfRabinTrials = 20\n",
" for i in range(numberOfRabinTrials):\n",
" round_tester = random.randrange(2, mrc)\n",
" if trialComposite(round_tester):\n",
" return False\n",
" return True\n",
" \n",
"def random_large_prime(bits):\n",
" while True:\n",
" prime_candidate = getLowLevelPrime(bits)\n",
" if not isMillerRabinPassed(prime_candidate):\n",
" continue\n",
" else:\n",
" return prime_candidate\n",
"\n",
"def thread_function(i, fast, timeout):\n",
" start = time.time()\n",
"\n",
" c = 5 # bits: 20: 200ms; 21: 350ms; 22: 700ms 23: 1.5s; 25: 6s; 26: 10s; 27: 24s\n",
" bits = 19 if fast else 23\n",
" last_report = time.time()\n",
" processing_time = 0.0\n",
" reqs = 0\n",
" while True:\n",
" iter_start = time.time()\n",
" if iter_start - start > timeout:\n",
" logging.info(\"Thread: %d\\treqs: %d\\tmean time: %.3fs\\t%s\"%(i, reqs, processing_time/reqs if reqs>0 else 0.0, \"fast\\t\" if fast else \"\"))\n",
" results[i][iter_start] = processing_time/reqs if reqs>0 else 0.0\n",
" return\n",
" if iter_start - last_report > UNIT/2:\n",
" if len(results[i])%2 == 0:\n",
" logging.info(\"Thread: %d\\treqs: %d\\tmean time: %.3fs\\t%s\"%(i, reqs, processing_time/reqs if reqs>0 else 0.0, \"fast\\t\" if fast else \"\"))\n",
" results[i][iter_start] = processing_time/reqs if reqs>0 else 0.0\n",
" processing_time = 0.0\n",
" reqs = 0\n",
" last_report=iter_start\n",
"\n",
" factors = [random_large_prime(bits) for i in range(c)]\n",
" factors.sort()\n",
" n=math.prod(factors)\n",
"\n",
" r = requests.get(API_URL+'/factors/%d'%(n))\n",
" if r.status_code != 200:\n",
" logging.error(\"wrong status code from webservice\")\n",
" else:\n",
" result = r.json()\n",
" if result != factors:\n",
" logging.error(\"Wrong factors\")\n",
"\n",
" processing_time+=time.time() - iter_start\n",
" reqs+=1\n",
" time.sleep(0.5)\n",
"\n",
"START = time.time()\n",
"slow_threads = 4\n",
"\n",
"results = [ {} for i in range(slow_threads+1)]\n",
"\n",
"t0 = threading.Thread(target=thread_function, args=(0, True, (5 + slow_threads*3) * UNIT))\n",
"t0.start()\n",
"time.sleep(2 * UNIT)\n",
"for i in range(slow_threads):\n",
" t = threading.Thread(target=thread_function, args=(i+1, False, (slow_threads-i) * 3 * UNIT))\n",
" t.start()\n",
" time.sleep(2 * UNIT)\n",
"\n",
"t0.join()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mWARNING: You are using pip version 22.0.4; however, version 22.3.1 is available.\n",
"You should consider upgrading via the '/home/kuba/uam/sem2/chmura/Hetzner/code-server/hetz_gitea_venv/bin/python -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n",
"\u001b[0mNote: you may need to restart the kernel to use updated packages.\n",
"\u001b[33mWARNING: You are using pip version 22.0.4; however, version 22.3.1 is available.\n",
"You should consider upgrading via the '/home/kuba/uam/sem2/chmura/Hetzner/code-server/hetz_gitea_venv/bin/python -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n",
"\u001b[0mNote: you may need to restart the kernel to use updated packages.\n",
"\u001b[33mWARNING: You are using pip version 22.0.4; however, version 22.3.1 is available.\n",
"You should consider upgrading via the '/home/kuba/uam/sem2/chmura/Hetzner/code-server/hetz_gitea_venv/bin/python -m pip install --upgrade pip' command.\u001b[0m\u001b[33m\n",
"\u001b[0mNote: you may need to restart the kernel to use updated packages.\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAigAAAGdCAYAAAA44ojeAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/P9b71AAAACXBIWXMAAA9hAAAPYQGoP6dpAACnd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ber8OpW39SVtEoPQcRYkozRJvLBZLSP+y/oIIFEEQhD5AHxCYZE5iTOqYsPsFO3l6O8VzZI32s4NA0bt4RKCc7AwfPpwNGzZQUFBAcnIy6enpHD58mIaGBsrKymhubmbbtm0ATJw4EYvF0qfrE4EiCILQB2yp0Oo9pmZMxWjoaGqpoxfKFjoLe28xTTVQtkN73kGgBMzaXHXgqgervffWIcSVe+65hxtuuIGJEyfS3NzMkSNH+Na3vsWaNWuC+8yYoUX7jhw5wvDhw/t0fSJQBEEQ+oDO/E/aorca92oERe/eyZwAyVnt37PawZqiCRRnKWSKQDlZGTt2LOvXr2+3LZRVSLyQLh5BEIQ+IFggm92FQAmkeGpaaqh31/fOYsLVn+ik6Gmeo71zfUGIABEogiAIvUxZYxmljaUYFSNTM6Z2um+SOYlBtkFAL0ZRuhIoUigr9ANEoAiCIPQyevRkbNpYEiNoAw1ONe6NoYHOUqjaDygwfF7ofUSgCP0AESiCIAi9TKT1Jzq9OjRQrz/JnaYNBgyFzOMR+gEiUARBEHqZaAVKcCZPb0RQwrUXt0UiKEI/QASKIAhCL9LoaWTf8X1
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%pip install numpy --quiet\n",
"%pip install matplotlib --quiet\n",
"%pip install scipy --quiet\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import scipy.stats as stats\n",
"mu = 0\n",
"std = 1\n",
"for i, result in enumerate(results):\n",
" x = [(x - START)/UNIT for x in result.keys()]\n",
" y = result.values()\n",
" plt.plot(x, y, label=\"t%d\"%(i,))\n",
"\n",
"plt.legend()\n",
"plt.savefig('load-balancer.png')\n",
"plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "hetz_gitea_venv",
"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.9.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "d3d864e1be7564ec15be68b6f80dea10522185afad36544549c9f900b3e93631"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}