169 lines
5.8 KiB
Plaintext
169 lines
5.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"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://95.217.174.58: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()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"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()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
},
|
|
"orig_nbformat": 4
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|