1216 lines
312 KiB
Plaintext
1216 lines
312 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"metadata": {},
|
||
|
"cell_type": "markdown",
|
||
|
"source": "### Dwie instancje - 5s",
|
||
|
"id": "26ad0da599026613"
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"id": "initial_id",
|
||
|
"metadata": {
|
||
|
"collapsed": true,
|
||
|
"ExecuteTime": {
|
||
|
"end_time": "2024-12-12T19:50:47.681398Z",
|
||
|
"start_time": "2024-12-12T19:49:22.595934Z"
|
||
|
}
|
||
|
},
|
||
|
"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",
|
||
|
"\n",
|
||
|
"API_URL=\"http://464979-loadbalancer-af7b87d5a8a2b254.elb.us-east-1.amazonaws.com:8080\"\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()"
|
||
|
],
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.310s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.291s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.264s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 0.807s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.192s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 0.889s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.207s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 1.014s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.276s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.217s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 1\tmean time: 2.238s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 1.830s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.373s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.577s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 2\tmean time: 1.599s\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 1\tmean time: 2.513s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 2.082s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 1.520s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 1\tmean time: 3.425s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 2\tmean time: 1.823s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.838s\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 2\tmean time: 2.551s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 2.324s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 1\tmean time: 2.642s\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 1\tmean time: 2.553s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 1\tmean time: 3.185s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 1\tmean time: 2.372s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.471s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.447s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 1\tmean time: 2.549s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.468s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.369s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.088s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.315s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 1.319s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.293s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.292s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 1\tmean time: 0.304s\tfast\t\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"execution_count": 5
|
||
|
},
|
||
|
{
|
||
|
"metadata": {
|
||
|
"ExecuteTime": {
|
||
|
"end_time": "2024-12-12T19:50:47.854365Z",
|
||
|
"start_time": "2024-12-12T19:50:47.688366Z"
|
||
|
}
|
||
|
},
|
||
|
"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": "769f1da39d2be428",
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
],
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/GU6VOAAAACXBIWXMAAA9hAAAPYQGoP6dpAACtFUlEQVR4nOydd3hb5fXHP1fTkve245G9d8JKWGGvMlva0tJAKRtKKYX2l1J2aehgpINdRgu0tOwyAmEkjAQyHTLIHnbivZeseX9/XF1525IjWbJzPs/jJ7Lue+99bbB0dM73fI+iqqqKIAiCIAhClDBEewOCIAiCIBzeSDAiCIIgCEJUkWBEEARBEISoIsGIIAiCIAhRRYIRQRAEQRCiigQjgiAIgiBEFQlGBEEQBEGIKhKMCIIgCIIQVUzR3kAw+Hw+SktLSUxMRFGUaG9HEARBEIQgUFWVpqYmRowYgcHQe/5jSAQjpaWlFBQURHsbgiAIgiAMgJKSEvLz83s9PiSCkcTERED7YZKSkqK8G0EQBEEQgqGxsZGCgoLA+3hvDIlgRC/NJCUlSTAiC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2Ll0CSZYkUq2aaHO4lWqWlywH4IT8Ezo9nx6XjlEx4lW91H3zdadjrWvWDNLuBCH8SDAiCELM0nVAXleGY6mm2dXM2oq1ACwoWNDpmNFgDGhnWrZ/oz2XofmttIhuRBjCSDAiCELMEvAY6aIX0RmOtvCrylbh8XkYlTSKkUkjux3XdSOePXsBSLn4OwA41q1H9XgGb6OCEEYkGBEEIWbRMyMd3Vc
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"execution_count": 6
|
||
|
},
|
||
|
{
|
||
|
"metadata": {},
|
||
|
"cell_type": "markdown",
|
||
|
"source": "### Dwie instancje - 60s",
|
||
|
"id": "48befdffa20b83d7"
|
||
|
},
|
||
|
{
|
||
|
"metadata": {
|
||
|
"ExecuteTime": {
|
||
|
"end_time": "2024-12-12T22:01:25.529006Z",
|
||
|
"start_time": "2024-12-12T21:44:25.216553Z"
|
||
|
}
|
||
|
},
|
||
|
"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",
|
||
|
"\n",
|
||
|
"API_URL=\"http://464979-1-LoadBalancer-69cc70eab93f0b76.elb.us-east-1.amazonaws.com:8080\"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"UNIT = 60.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": "fe96b6b86f45c601",
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"INFO:root:Thread: 0\treqs: 38\tmean time: 0.298s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 38\tmean time: 0.296s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 18\tmean time: 1.230s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 24\tmean time: 0.787s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 18\tmean time: 1.196s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 28\tmean time: 0.624s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 17\tmean time: 1.314s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 17\tmean time: 1.358s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 25\tmean time: 0.732s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 17\tmean time: 1.290s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 17\tmean time: 1.292s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 26\tmean time: 0.714s\tfast\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 14\tmean time: 1.792s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 15\tmean time: 1.542s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 15\tmean time: 1.573s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 17\tmean time: 1.279s\tfast\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 15\tmean time: 1.578s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 14\tmean time: 1.649s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 15\tmean time: 1.591s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 20\tmean time: 1.126s\tfast\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 11\tmean time: 2.223s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 13\tmean time: 1.831s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 13\tmean time: 1.851s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 14\tmean time: 1.657s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 15\tmean time: 1.573s\tfast\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 12\tmean time: 2.031s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 12\tmean time: 2.097s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 12\tmean time: 2.049s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 11\tmean time: 2.243s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 17\tmean time: 1.269s\tfast\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 13\tmean time: 1.867s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 12\tmean time: 1.995s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 12\tmean time: 2.199s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 14\tmean time: 1.719s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 14\tmean time: 1.665s\tfast\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 11\tmean time: 1.911s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 14\tmean time: 1.737s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 14\tmean time: 1.723s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 14\tmean time: 1.649s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 18\tmean time: 1.172s\tfast\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 10\tmean time: 1.455s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 17\tmean time: 1.333s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 17\tmean time: 1.372s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 23\tmean time: 0.870s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 7\tmean time: 1.359s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 30\tmean time: 0.498s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 18\tmean time: 1.208s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 5\tmean time: 1.212s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 38\tmean time: 0.296s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 38\tmean time: 0.295s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 38\tmean time: 0.296s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 5\tmean time: 0.295s\tfast\t\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"execution_count": 1
|
||
|
},
|
||
|
{
|
||
|
"metadata": {
|
||
|
"ExecuteTime": {
|
||
|
"end_time": "2024-12-12T22:01:26.803013Z",
|
||
|
"start_time": "2024-12-12T22:01:26.661011Z"
|
||
|
}
|
||
|
},
|
||
|
"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": "137da80ee1917887",
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
],
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjIAAAGdCAYAAAAIbpn/AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/GU6VOAAAACXBIWXMAAA9hAAAPYQGoP6dpAACl60lEQVR4nOzdd3zU9f3A8dft7L0HCSvsDSIoU2SoOHHgQNxaHEjVll9bV1u1Vq3YOloXWot7LxCRIbLBMMIIIyF7733j+/vjmzsSSELGXS7j/Xw87pHk7js+R2vunc/n/Xm/NYqiKAghhBBCdENadw9ACCGEEKK9JJARQgghRLclgYwQQgghui0JZIQQQgjRbUkgI4QQQohuSwIZIYQQQnRbEsgIIYQQotuSQEYIIYQQ3Zbe3QNwBpvNRlZWFr6+vmg0GncPRwghhBCtoCgK5eXlREVFodW2b26lRwQyWVlZxMbGunsYQgghhGiH9PR0YmJi2nVujwhkfH19AfUfws/Pz82jEUIIIURrlJWVERsb6/gcb48eEcjYl5P8/PwkkBFCCCG6mY6khUiyrxBCCCG6LQlkhBBCCNFtSSAjhBBCiG6rR+TICCGEEF2doihYLBasVqu7h9KpdDoder3eZeVRJJARQgghXKyuro7s7GyqqqrcPRS38PLyIjIyEqPR6PRrSyAjhBBCuJDNZiMlJQWdTkdUVBRGo7HXFG9VFIW6ujry8/NJSUlh4MCB7S581xwJZIQQQggXqqurw2azERsbi5eXl7uH0+k8PT0xGAycPHmSuro6PDw8nHp9SfYVQgghOoGzZyK6E1e+9zZd+emnn2bChAn4+voSFhbG5ZdfzpEjR1o85/XXX2fKlCkEBgYSGBjIrFmz2LFjR6NjFi9ejEajafSYO3du29+NEEIIIXqVNgUyGzduZMmSJWzbto21a9diNpuZPXs2lZWVzZ6zYcMGFi5cyPr169m6dSuxsbHMnj2bzMzMRsfNnTuX7Oxsx+P9999v3zsSQgghRK/RphyZ1atXN/p55cqVhIWFsXv3bqZOndrkOf/73/8a/fzGG2/w6aefsm7dOhYtWuR43mQyERER0ZbhCCGEEKKX69CiVWlpKQBBQUGtPqeqqgqz2XzGORs2bCAsLIxBgwZxzz33UFhY2Ow1amtrKSsra/QQQgghhHNNnz6dpUuXNnouLS2Niy++GC8vL8LCwnj44YexWCzuGSAd2LVks9lYunQp5513HsOHD2/1eb/73e+Iiopi1qxZjufmzp3LlVdeSd++fTl+/Dj/93//x7x589i6dSs6ne6Mazz99NM88cQT7R26EEI078Cn4BkE/We4eyRCdDlWq5WLL76YiIgItmzZQnZ2NosWLcJgMPDUU0+5ZUwaRVGU9px4zz338P3337N582ZiYmJadc4zzzzDs88+y4YNGxg5cmSzx504cYL+/fvz448/csEFF5zxem1tLbW1tY6f7W3AS0tLpfu1EKL90nfAmxeCwRt+fxJ0BnePSPQANTU1pKSk0LdvX8fWY0VRqDZ3foVfT4Ou1TVsFi9ezDvvvNPouVdeeYV7772XrKwswsPDAXjttdf43e9+R35+frMF75r6NwD189vf379Dn9/tmpG59957+eabb9i0aVOrg5jnnnuOZ555hh9//LHFIAagX79+hISEcOzYsSYDGZPJhMlkas/QhRCiedteUb+aKyH/MESMcO94RI9VbbYy9NE1nX7fg0/OwcvYuo/+FStWkJyczPDhw3nyyScBePnllxkxYoQjiAGYM2cO99xzD0lJSYwZM8Yl425JmwIZRVG47777+Pzzz9mwYQN9+/Zt1XnPPvssf/3rX1mzZg3jx48/6/EZGRkUFhYSGRnZluEJIbqp6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7N06dJGz91///2MGzcOk8nE6NGj3TK
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"execution_count": 3
|
||
|
},
|
||
|
{
|
||
|
"metadata": {},
|
||
|
"cell_type": "markdown",
|
||
|
"source": "### Skalowanie bez odczekania - 45s",
|
||
|
"id": "cfc4f63a6d75d706"
|
||
|
},
|
||
|
{
|
||
|
"metadata": {
|
||
|
"ExecuteTime": {
|
||
|
"end_time": "2024-12-15T12:18:13.889672Z",
|
||
|
"start_time": "2024-12-15T12:05:28.613196Z"
|
||
|
}
|
||
|
},
|
||
|
"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",
|
||
|
"\n",
|
||
|
"API_URL=\"http://464979-LoadBalancer-272a50d718ade55d.elb.us-east-1.amazonaws.com:8080\"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"UNIT = 45.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": "ccc64a8d2cd6f5aa",
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"INFO:root:Thread: 0\treqs: 28\tmean time: 0.297s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 28\tmean time: 0.299s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 23\tmean time: 0.477s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 13\tmean time: 1.274s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 23\tmean time: 0.503s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 14\tmean time: 1.211s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 13\tmean time: 1.349s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 20\tmean time: 0.642s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 13\tmean time: 1.374s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 12\tmean time: 1.442s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 16\tmean time: 0.900s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 12\tmean time: 1.411s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 9\tmean time: 1.992s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 17\tmean time: 0.848s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 10\tmean time: 1.906s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 12\tmean time: 1.553s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 10\tmean time: 1.784s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 18\tmean time: 0.804s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 12\tmean time: 1.586s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 11\tmean time: 1.549s\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 11\tmean time: 1.740s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 9\tmean time: 2.065s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 12\tmean time: 1.411s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 11\tmean time: 1.740s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 10\tmean time: 1.989s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 9\tmean time: 1.999s\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 11\tmean time: 1.749s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 12\tmean time: 1.435s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 9\tmean time: 2.036s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 10\tmean time: 1.991s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 11\tmean time: 1.751s\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 10\tmean time: 1.978s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 16\tmean time: 0.951s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 11\tmean time: 1.589s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 9\tmean time: 2.112s\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 7\tmean time: 1.789s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 12\tmean time: 1.455s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 10\tmean time: 1.824s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 18\tmean time: 0.825s\tfast\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 7\tmean time: 1.603s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 12\tmean time: 1.409s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 24\tmean time: 0.437s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 13\tmean time: 1.322s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.131s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 13\tmean time: 1.296s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 25\tmean time: 0.406s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 10\tmean time: 1.272s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 28\tmean time: 0.300s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 28\tmean time: 0.298s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 28\tmean time: 0.296s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 1\tmean time: 0.287s\tfast\t\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"execution_count": 1
|
||
|
},
|
||
|
{
|
||
|
"metadata": {
|
||
|
"ExecuteTime": {
|
||
|
"end_time": "2024-12-15T12:18:15.017610Z",
|
||
|
"start_time": "2024-12-15T12:18:13.905578Z"
|
||
|
}
|
||
|
},
|
||
|
"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": "8efd3f7f8448156",
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
],
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAioAAAGdCAYAAAA8F1jjAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/GU6VOAAAACXBIWXMAAA9hAAAPYQGoP6dpAACStElEQVR4nOzdd5xU5dXA8d+dmZ3Z3nuBpcPSQaSJYAM72MUoYEGjGDXGmPgmsaWYxBhD1FhjQBHFCooNRClK771uAbb3Xqbc94+7M7sLW2Z3Z3dmlvP9fCY7O3Pn3mc2snP2ec45j6KqqooQQgghhAfSuXsAQgghhBAtkUBFCCGEEB5LAhUhhBBCeCwJVIQQQgjhsSRQEUIIIYTHkkBFCCGEEB5LAhUhhBBCeCwJVIQQQgjhsQzuHoAzbDYbWVlZBAUFoSiKu4cjhBBCCCeoqkp5eTnx8fHodB2bG/GKQCUrK4ukpCR3D0MIIYQQHXDq1CkSExM79FqvCFSCgoIA7Y0GBwe7eTRCCCGEcEZZWRlJSUmOz/GO8IpAxb7cExwcLIGKEEII4WU6k7YhybRCCCGE8FgSqAghhBDCY7UrUHn11VcZMWKEYwlm4sSJfP31162+5qOPPmLw4MH4+voyfPhwvvrqq04NWAghhBDnjnblqCQmJvLXv/6VAQMGoKoqixcvZubMmezatYuhQ4eedfzGjRuZPXs2zz33HFdffTVLly5l1qxZ7Ny5k2HDhrnsTQghhBDupKoqFosFq9Xq7qF0K71ej8Fg6NLWIYqqqmpnThAeHs7zzz/P3XfffdZzt9xyC5WVlaxcudLx2IQJExg1ahSvvfaa09coKysjJCSE0tJSSaYVQgjhUerq6sjOzqaqqsrdQ3ELf39/4uLiMBqNZz3nis/vDlf9WK1WPvroIyorK5k4cWKzx2zatIlHH320yWMzZsxg+fLlrZ67traW2tpax/dlZWUdHaYQQgjRZWw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LikpKVRXV5OWlsY999zDunXrHMeMHj0a0EqXk5OT3TJOCVSEEN6nudJkO6n8EcIpAwcOZNOmTU0eW7t2rXsG0wqp+hFCeB9HIm2vs59zNH2TGRUhegIJVIQQ3qe1GZXGSz+q2n1jEkJ0CQlUhBDep7lmb3b2QKW2DKqLu29MQoguIYGKEML72GdUmlv68fGDgOj642T5RwhvJ4GKEMK7qGrTnZObI5U/QvQYEqgIIbxLZT5YagAFghOaP0Yqf4ToMSRQEUJ4F/uyT1AcGFpoQCUzKkL0GBKoCCG8S3N7/JxJAhUhegwJVIQQ3qW10mQ7ey+VYln6EcLbSaAihPAurZUm2zVu+ia9VITwahKoCCG8izMzKiGJ2ldzJVQVdf2YhPBC06ZN45FHHnF8v2fPHmbPnk1SUhJ+fn4MGTKEhQsXum+A9WSvHyGEd7HnndhnTZrj46sl25ZnQ0k6BER0y9CE8GY7duwgOjqaJUuWkJSUxMaNG7n33nvR6/U8+OCDbhuXBCpCCO/izNIPaAm15dlaYJMwtuvHJYQXmTdvHuvWrWPdunWOWZO0tDTuuusuxzF9+/Zl06ZNfPrppxKoCCGEU6pLtNb40LC805LQXnBqi1T+iG6nqirVlmq3XNvP4IeiKG0et3DhQo4ePcqwYcN49tlnAYiKijrruNLSUsLDw10+zvaQQEUI4T3ssyn+EWAMaP1YqfwRblJtqWb80vFuufaW27bg7+Pf5nEhISEYjUb8/f2JjY1t9piNGzeybNkyvvzyS1cPs10kmVYI4T2cSaS1k14qQnTY/v37mTlzJk899RTTp09361hkRkUI4T2czU8BCVSE2/gZ/Nhy2xa3XbuzDh48yCWXXMK9997L73//exeMqnMkUBFCeA970BH
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"execution_count": 2
|
||
|
},
|
||
|
{
|
||
|
"metadata": {},
|
||
|
"cell_type": "markdown",
|
||
|
"source": "### Skalowanie z odczekaniem - 60s",
|
||
|
"id": "3e9cf6b37d5768d2"
|
||
|
},
|
||
|
{
|
||
|
"metadata": {
|
||
|
"ExecuteTime": {
|
||
|
"end_time": "2024-12-16T18:43:04.722472Z",
|
||
|
"start_time": "2024-12-16T18:26:04.487945Z"
|
||
|
}
|
||
|
},
|
||
|
"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",
|
||
|
"\n",
|
||
|
"API_URL=\"http://464979-LoadBalancer-0165784e7aab6ce5.elb.us-east-1.amazonaws.com:8080\"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"UNIT = 60.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": "1f32ef0d5d8c5253",
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"INFO:root:Thread: 0\treqs: 38\tmean time: 0.295s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 38\tmean time: 0.297s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 18\tmean time: 1.198s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 28\tmean time: 0.591s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 17\tmean time: 1.272s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 27\tmean time: 0.619s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 18\tmean time: 1.263s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 17\tmean time: 1.281s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 26\tmean time: 0.689s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 16\tmean time: 1.433s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 16\tmean time: 1.415s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 27\tmean time: 0.628s\tfast\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 14\tmean time: 1.734s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 16\tmean time: 1.501s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 16\tmean time: 1.449s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 17\tmean time: 1.343s\tfast\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 15\tmean time: 1.594s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 15\tmean time: 1.500s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 21\tmean time: 0.930s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 16\tmean time: 1.477s\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 13\tmean time: 1.995s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 12\tmean time: 2.077s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 12\tmean time: 2.025s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 11\tmean time: 2.229s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 16\tmean time: 1.446s\tfast\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 13\tmean time: 1.815s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 13\tmean time: 2.048s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 11\tmean time: 2.350s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 11\tmean time: 2.351s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 15\tmean time: 1.567s\tfast\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 15\tmean time: 1.561s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 12\tmean time: 2.006s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 13\tmean time: 1.966s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 14\tmean time: 1.681s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 20\tmean time: 1.038s\tfast\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 13\tmean time: 1.568s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 17\tmean time: 1.359s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 15\tmean time: 1.510s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 17\tmean time: 1.340s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 23\tmean time: 0.820s\tfast\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 9\tmean time: 1.457s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 17\tmean time: 1.298s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 32\tmean time: 0.459s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 18\tmean time: 1.312s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 7\tmean time: 1.313s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 34\tmean time: 0.389s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 18\tmean time: 1.204s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.278s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 38\tmean time: 0.302s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 38\tmean time: 0.307s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 38\tmean time: 0.304s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.296s\tfast\t\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"execution_count": 1
|
||
|
},
|
||
|
{
|
||
|
"metadata": {
|
||
|
"ExecuteTime": {
|
||
|
"end_time": "2024-12-16T18:43:08.232446Z",
|
||
|
"start_time": "2024-12-16T18:43:04.748446Z"
|
||
|
}
|
||
|
},
|
||
|
"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": "113a8e2ddaa31689",
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
],
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAioAAAGdCAYAAAA8F1jjAAAAOXRFWHRTb2Z0d2FyZQBNY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vmmWCaIYmKEMIrlBfWYLc60Bt1hEQ3PlrS3qZvZXVlnLvgLGIqi/jEHonyx5Wt6jUhhPA8qUYTQngF14qfuAB0usaTiOAoLYFp6xLlDbkbKLaUsctkIKdwB5RmdSxYIUSXkURFCOEVmlvx49TeJcobMhe77meYTJC5qh0RCiE8QRIVIYRXaG7Fj1N7u9Nuylrqur/dbIKs1e2IUAjhCZKoCCG8wrERlaAmj3GOqFSWaL1UWqPy8K/schzbd2W72QxZv3QgUiFEV5JERQjhcQ67g9IWVvwABISY0BkUVIdKZUldq869dfk/cCgKJrS6l+1mE46STCg70uG4hRCdTxIVIYTHlRXUYLc5MJh0rlGTxjh7qUAr61SyN7Kx6DcATks4GbPeTKVOR5bRAFlSpyKEL5BERQjhcc5pn/C4QJQmVvw4tWmJ8tLH2ehnBmB8ygwGRwwG6gtqJVERwidIoiKE8DhXIW0zK36cnAW1LS5RzlpD3YGfyDBricro2NEMixoG1NepyMofIXyCJCpCCI9zFdI2U5/iFBLVyqmfpY+TYTJjURQi/SLpHdyboZFDgfqVP0V7oSKvY4ELITqdJCpCCI9r24hKK6Z+DiyHzJVsCtC2vB8TOwZFUVwjKrvMZmwg0z+iR5s2bRpz5851/bx161Yuu+wykpKS8Pf3Z8iQIcybN89zAdaTFvpCCI+y2x2U5tWv+GlFohL
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"execution_count": 2
|
||
|
},
|
||
|
{
|
||
|
"metadata": {},
|
||
|
"cell_type": "markdown",
|
||
|
"source": "### Skalowanie z odczekaniem - 5s",
|
||
|
"id": "5a9fdd8f3a7e16e1"
|
||
|
},
|
||
|
{
|
||
|
"metadata": {
|
||
|
"ExecuteTime": {
|
||
|
"start_time": "2024-12-16T18:52:23.253459Z"
|
||
|
}
|
||
|
},
|
||
|
"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",
|
||
|
"\n",
|
||
|
"API_URL=\"http://464979-LoadBalancer-0165784e7aab6ce5.elb.us-east-1.amazonaws.com:8080\"\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()"
|
||
|
],
|
||
|
"id": "1dd74c36ea8583ba",
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stderr",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.308s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.319s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.326s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 0.753s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.457s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.471s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.283s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.054s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.159s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 3\tmean time: 0.636s\tfast\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 2\tmean time: 1.484s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 1\tmean time: 2.007s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 0.784s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.343s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 2\tmean time: 1.404s\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 1\tmean time: 2.357s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 1.212s\tfast\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.852s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.634s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 1\tmean time: 2.103s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.416s\tfast\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 2\tmean time: 1.963s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.638s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 2.269s\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 1\tmean time: 3.795s\t\n",
|
||
|
"INFO:root:Thread: 4\treqs: 1\tmean time: 2.685s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 0.942s\tfast\t\n",
|
||
|
"INFO:root:Thread: 3\treqs: 1\tmean time: 1.513s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.452s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.444s\t\n",
|
||
|
"INFO:root:Thread: 2\treqs: 2\tmean time: 1.434s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.463s\tfast\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 2\tmean time: 1.531s\t\n",
|
||
|
"INFO:root:Thread: 1\treqs: 1\tmean time: 1.768s\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 2\tmean time: 1.028s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.299s\tfast\t\n",
|
||
|
"INFO:root:Thread: 0\treqs: 4\tmean time: 0.309s\tfast\t\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"execution_count": 1
|
||
|
},
|
||
|
{
|
||
|
"metadata": {
|
||
|
"ExecuteTime": {
|
||
|
"end_time": "2024-12-16T19:05:15.538980Z",
|
||
|
"start_time": "2024-12-16T19:05:15.412945Z"
|
||
|
}
|
||
|
},
|
||
|
"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": "fdf923e84b0ff64d",
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
],
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiYAAAGdCAYAAAAmK7htAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/GU6VOAAAACXBIWXMAAA9hAAAPYQGoP6dpAACh6ElEQVR4nOydd3hb9dmG76PpvffI3oMsQkgCJKyEFQgtlFn2aAstlAItLYU20KZ8zLRlU5q2QIGyEjZhJIyE7L2Hkzjx3tvWON8fR0eyEw9JlizZfu/r0mVZOuMnR9F59I7nVVRVVREEQRAEQQgDDKFegCAIgiAIgo4IE0EQBEEQwgYRJoIgCIIghA0iTARBEARBCBtEmAiCIAiCEDaIMBEEQRAEIWwQYSIIgiAIQtggwkQQBEEQhLDBFOoFeIPT6aSgoIDY2FgURQn1cgRBEARB8AJVVamtrSUrKwuDwbtYSK8QJgUFBeTm5oZ6GYIgCIIg+EF+fj45OTlebdsrhElsbCygvbC4uLgQr0YQBEEQBG+oqakhNzfXfR33hl4hTPT0TVxcnAgTQRAEQehl+FKGIcWvgiAIgiCEDSJMBEEQBEEIG0SYCIIgCIIQNvSKGhNBEARBCGdUVcVut+NwOEK9lB7FaDRiMpkCauUhwkQQBEEQukFLSwuFhYU0NDSEeikhISoqiszMTCwWS0COJ8JEEARBEPzE6XSSl5eH0WgkKysLi8XSb4xAVVWlpaWF0tJS8vLyGD58uNcmap0hwkQQBEEQ/KSlpQWn00lubi5RUVGhXk6PExkZidls5tChQ7S0tBAREdHtY0rxqyAIgiB0k0BECnorgX7t/fcvKQiCIAhC2OGTMHn22Wc54YQT3A6s06dP5+OPP+50n//973+MGjWKiIgIxo8fz0cffdStBQuCIAiC0HfxSZjk5OTwl7/8hfXr17Nu3TrOOOMMLrroIrZv397u9itXruSKK67gxhtvZOPGj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h/+1YEI7l7rvv5tprr2XMmDE0NjaSl5fHTTfdxIoVK9zbTJo0CdBaiQcNGtQj65LiV6F3cmQtlO+DzW+EeiVCH8dRWQlOJygKxsREAAzRUSiRkYBnuJ8g9DZGjBjBqlWraGhoQFVVBg0axPLly1FV9bhbT4kSEGEi9FbqirWftYXgsIV2LUKfxt2Rk5CAYtKCzIqiiJeJIAQJESZC76SuxHVH1cSJIASJYz1MdNx1JiVSZyIIgUSEidA7qW91Mag+Grp1CH2eYz1MdCRiIgjBQYSJ0DvRUzkA1UdCtw6hz3PsAD8djzCRiIkgBBIRJkLvpK7VxaBGhIkQPI4d4KdjShNbekEIBiJMhN5JfYnnvqRyhCBy7AA/HbGlF4TgIMJE6J1IKkfoIY61o9cxivurIAQFESZC78PeDE3Vnt8llSMEEUeHxa+SyhGEYCDCROh91B8TOpeIiRBEdAO1DtuFy8tRnc4eX5cg9FVEmAi9D93DxBqn/WyshJaG0K1H6LOoqoqjrIOunKREbVhbK8t6QRC6jwgTofehC5OkIWCJ1e7XSAGsEHicdXWoNs1Z2HiMMFHMZrdFvaRzhN7I7NmzufPOO92/b968mSuuuILc3FwiIyMZPXo0ixYt6vF1yRA/ofehd+TEpGn1JqU7oTofUoaHdl1Cn0MXHIboaAwREcc9b0pJwVFRgb20DEaO7OnlCUJAWb9+PWlpabzyyivk5uaycuVKbrnlFoxGI7fffnuPrUOEidD70CMm0WmgOl3CRCImQuDpyMNEx5SaSvOePdIyLPQ6rrvuOlasWMGKFSvcUZG8vDxuuOEG9zZDhgxh1apVvPPOOyJMBKFT6lpFTAxG7b6kcoQ
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"execution_count": 5
|
||
|
}
|
||
|
],
|
||
|
"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
|
||
|
}
|