aws/lb2.ipynb

264 lines
66 KiB
Plaintext
Raw Permalink Normal View History

2024-12-21 08:46:43 +01:00
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "### 2 maszyny - bez skalowania",
"id": "434259c70359c18f"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-12-21T07:45:52.828283Z",
"start_time": "2024-12-21T07:44:27.547304Z"
}
},
"cell_type": "code",
"source": [
"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",
"API_URL=\"http://s464863-lb-13561059.us-east-1.elb.amazonaws.com:8080/\"\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()"
],
"id": "666144f984e22119",
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.384s\tfast\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.379s\tfast\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.382s\tfast\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.348s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.371s\tfast\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.383s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.380s\tfast\t\n",
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.450s\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.403s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.397s\tfast\t\n",
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.522s\t\n",
"INFO:root:Thread: 3\treqs: 2\tmean time: 1.761s\t\n",
"INFO:root:Thread: 1\treqs: 1\tmean time: 2.168s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.681s\tfast\t\n",
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.294s\t\n",
"INFO:root:Thread: 3\treqs: 1\tmean time: 2.006s\t\n",
"INFO:root:Thread: 0\treqs: 2\tmean time: 0.751s\tfast\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 2.080s\t\n",
"INFO:root:Thread: 4\treqs: 2\tmean time: 2.193s\t\n",
"INFO:root:Thread: 2\treqs: 1\tmean time: 2.307s\t\n",
"INFO:root:Thread: 3\treqs: 2\tmean time: 1.567s\t\n",
"INFO:root:Thread: 0\treqs: 2\tmean time: 1.124s\tfast\t\n",
"INFO:root:Thread: 4\treqs: 1\tmean time: 2.090s\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 2.061s\t\n",
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.488s\t\n",
"INFO:root:Thread: 4\treqs: 2\tmean time: 1.430s\t\n",
"INFO:root:Thread: 0\treqs: 2\tmean time: 1.017s\tfast\t\n",
"INFO:root:Thread: 3\treqs: 2\tmean time: 1.454s\t\n",
"INFO:root:Thread: 3\treqs: 1\tmean time: 1.419s\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.411s\t\n",
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.315s\t\n",
"INFO:root:Thread: 0\treqs: 2\tmean time: 0.887s\tfast\t\n",
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.469s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.351s\tfast\t\n",
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.454s\t\n",
"INFO:root:Thread: 1\treqs: 1\tmean time: 1.375s\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.377s\tfast\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.378s\tfast\t\n",
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.392s\tfast\t\n",
"INFO:root:Thread: 0\treqs: 1\tmean time: 0.363s\tfast\t\n"
]
}
],
"execution_count": 5
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-12-21T07:46:10.757734Z",
"start_time": "2024-12-21T07:46:10.661303Z"
}
},
"cell_type": "code",
"source": [
"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.show()"
],
"id": "e3e72d6479cc9ba7",
"outputs": [
{
"data": {
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
],
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjEAAAGdCAYAAADjWSL8AAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjAsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvlHJYcgAAAAlwSFlzAAAPYQAAD2EBqD+naQAAqolJREFUeJzs3Xd4HNXV+PHvbFfv1ZIsF9x7xaYZMNjGEExChwCmhp9JAg4kr98kkE564ryhhBZDCODQCQZTbGyKGy5y77aq1XvfNr8/ZmelVV1Jq+rzeZ59JM3OzN6VQTq699xzFFVVVYQQQgghBhlDfw9ACCGEEKI7JIgRQgghxKAkQYwQQgghBiUJYoQQQggxKEkQI4QQQohBSYIYIYQQQgxKEsQIIYQQYlCSIEYIIYQQg5KpvwcQCG63mzNnzhAWFoaiKP09HCGEEEL4QVVVqqurSU5OxmDo+rzKkAhizpw5Q2pqan8PQwghhBDdkJOTQ0pKSpevGxJBTFhYGKB9E8LDw/t5NEIIIYTwR1VVFampqd7f4101JIIYfQkpPDxcghghhBBikOluKogk9gohhBBiUJIgRgghhBCDkgQxQgghhBiUhkROjBBCCDHQqaqK0+nE5XL191D6lNFoxGQy9UoJFAlihBBCiF5mt9vJz8+nrq6uv4fSL4KDg0lKSsJisQT0vhLECCGEEL3I7XZz+vRpjEYjycnJWCyWs6Ywq6qq2O12iouLOX36NOecc063itq1R4IYIYQQohfZ7XbcbjepqakEBwf393D6XFBQEGazmaysLOx2OzabLWD3lsReIYQQog8EcgZisOmt9372fkeFEEII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ATAYTd066E4A1B9fgcDn6bGxCiMCSIEYIMei5XW5cTnerlgPtuXr01cQGxVJQW8C60+v6aphCDDoLFizgwQcf9Dn2ve99j5kzZ2K1Wpk2bVq/jEsnQYwQYlD7/LVjPLvyC07uKaK8odyn5UB7rEYr357wbQBeOPACbtXdV8MVYki48847ueGGG/p7GBLECCEGN4NBwdnoouBEpXdnUpQ1yttyoD3Xj7meMEsYpytPszF7Y18MVYhB5Y477mDz5s2sXr0aRVFQFIXMzEz+9re/sWLFCkaOHNnfQ5QgRggxuCWOigDgzMnKDncmtRRqCeWmcTcB8Nz+5/zauCBEwKgq2Gv7/tGF/85Xr17NvHnzuOeee8jPzyc/P3/Atfnp0u4kIYQYaJJGa0FMWV4N1opqoP2dSS3dMv4WXjr4EgdLD7I1fyvzk+f32jiF8OGog98k9/3r/u8ZsIT4dWpERAQWi4Xg4GASExN7eWDdIzMxQpwtdjwLe4degbeQCCvhsTZUFUqyaoG2C921JdoWzbfGfAuA5/c/32tjFEL0DpmJEeJsUF0AHzwMBhOM/wZYgvt7RAGVNCqSqpICanLcYPNvOUl3x8Q7WHtkLTsKdrCveB9T4jru7SZEQJiDtVmR/njdIURmYoQ4G5R6mqm6nVB2sn/H0gv0vBj3Ga3Kt7/LSQCJIYlcOepKQMuNEaJPKIq2rNPXD0Xp0jAtFgsul6uXvgk9J0GMEGeDstNNn5cc679x9JIkTxBjLglHUQ1dmokBuHPSnSgofJbzGSfKT/TGEIUYlNLT09m+fTuZmZmUlJTgdrs5ceIEGRkZ3t6JGRkZZGRkYLfb+3x8EsQIcTYoO9X0ecnx/htHL4lOCsEabMLoMhNTm9zlIGZExAgWDl8IwPMHJDdGCN3DDz+M0WhkwoQJxMXFkZ2dzd1338306dP5xz/+wbFjx5g+fTrTp0/nzJm+Xx6TIEaIs0H50J6JUQwKiSO12ZjE6pFdWk7S6Y0hPzz9Ibn
},
"metadata": {},
"output_type": "display_data"
}
],
"execution_count": 6
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "51fd366e7ea4a24d"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}