154 KiB
154 KiB
2 do 5 maszyn - ze skalowaniem (60s)
import requests
import random
import math
import time
import threading
import logging
logging.getLogger().setLevel(logging.INFO)
API_URL="http://s464863-lb-2086103880.us-east-1.elb.amazonaws.com:8080/"
UNIT = 60.0 # secs
# Pre generated primes
first_primes_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101, 103,
107, 109, 113, 127, 131, 137, 139,
149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, 211, 223,
227, 229, 233, 239, 241, 251, 257,
263, 269, 271, 277, 281, 283, 293,
307, 311, 313, 317, 331, 337, 347, 349]
def nBitRandom(n):
return random.randrange(2**(n-1)+1, 2**n - 1)
def getLowLevelPrime(n):
'''Generate a prime candidate divisible
by first primes'''
while True:
# Obtain a random number
pc = nBitRandom(n)
# Test divisibility by pre-generated
# primes
for divisor in first_primes_list:
if pc % divisor == 0 and divisor**2 <= pc:
break
else: return pc
def isMillerRabinPassed(mrc):
'''Run 20 iterations of Rabin Miller Primality test'''
maxDivisionsByTwo = 0
ec = mrc-1
while ec % 2 == 0:
ec >>= 1
maxDivisionsByTwo += 1
assert(2**maxDivisionsByTwo * ec == mrc-1)
def trialComposite(round_tester):
if pow(round_tester, ec, mrc) == 1:
return False
for i in range(maxDivisionsByTwo):
if pow(round_tester, 2**i * ec, mrc) == mrc-1:
return False
return True
# Set number of trials here
numberOfRabinTrials = 20
for i in range(numberOfRabinTrials):
round_tester = random.randrange(2, mrc)
if trialComposite(round_tester):
return False
return True
def random_large_prime(bits):
while True:
prime_candidate = getLowLevelPrime(bits)
if not isMillerRabinPassed(prime_candidate):
continue
else:
return prime_candidate
def thread_function(i, fast, timeout):
start = time.time()
c = 5 # bits: 20: 200ms; 21: 350ms; 22: 700ms 23: 1.5s; 25: 6s; 26: 10s; 27: 24s
bits = 19 if fast else 23
last_report = time.time()
processing_time = 0.0
reqs = 0
while True:
iter_start = time.time()
if iter_start - start > timeout:
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 ""))
results[i][iter_start] = processing_time/reqs if reqs>0 else 0.0
return
if iter_start - last_report > UNIT/2:
if len(results[i])%2 == 0:
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 ""))
results[i][iter_start] = processing_time/reqs if reqs>0 else 0.0
processing_time = 0.0
reqs = 0
last_report=iter_start
factors = [random_large_prime(bits) for i in range(c)]
factors.sort()
n=math.prod(factors)
r = requests.get(API_URL+'/factors/%d'%(n))
if r.status_code != 200:
logging.error("wrong status code from webservice")
else:
result = r.json()
if result != factors:
logging.error("Wrong factors")
processing_time+=time.time() - iter_start
reqs+=1
time.sleep(0.5)
START = time.time()
slow_threads = 4
results = [ {} for i in range(slow_threads+1)]
t0 = threading.Thread(target=thread_function, args=(0, True, (5 + slow_threads*3) * UNIT))
t0.start()
time.sleep(2 * UNIT)
for i in range(slow_threads):
t = threading.Thread(target=thread_function, args=(i+1, False, (slow_threads-i) * 3 * UNIT))
t.start()
time.sleep(2 * UNIT)
t0.join()
INFO:root:Thread: 0 reqs: 34 mean time: 0.394s fast INFO:root:Thread: 0 reqs: 25 mean time: 1.235s fast INFO:root:Thread: 1 reqs: 5 mean time: 5.553s INFO:root:Thread: 0 reqs: 17 mean time: 1.370s fast INFO:root:Thread: 1 reqs: 13 mean time: 2.978s INFO:root:Thread: 0 reqs: 2 mean time: 21.416s fast INFO:root:Thread: 2 reqs: 2 mean time: 22.743s INFO:root:Thread: 1 reqs: 16 mean time: 1.436s INFO:root:Thread: 0 reqs: 15 mean time: 1.586s fast INFO:root:Thread: 2 reqs: 15 mean time: 1.637s INFO:root:Thread: 1 reqs: 14 mean time: 1.746s INFO:root:Thread: 3 reqs: 10 mean time: 2.624s INFO:root:Thread: 2 reqs: 4 mean time: 7.588s INFO:root:Thread: 0 reqs: 10 mean time: 4.636s fast INFO:root:Thread: 1 reqs: 2 mean time: 23.023s INFO:root:Thread: 3 reqs: 4 mean time: 7.500s INFO:root:Thread: 2 reqs: 10 mean time: 2.699s INFO:root:Thread: 0 reqs: 9 mean time: 2.929s fast INFO:root:Thread: 4 reqs: 8 mean time: 3.502s INFO:root:Thread: 1 reqs: 8 mean time: 3.540s INFO:root:Thread: 3 reqs: 8 mean time: 3.638s INFO:root:Thread: 2 reqs: 8 mean time: 3.663s INFO:root:Thread: 0 reqs: 8 mean time: 3.670s fast INFO:root:Thread: 4 reqs: 8 mean time: 3.527s INFO:root:Thread: 1 reqs: 8 mean time: 3.522s INFO:root:Thread: 3 reqs: 8 mean time: 3.624s INFO:root:Thread: 2 reqs: 8 mean time: 3.738s INFO:root:Thread: 0 reqs: 8 mean time: 3.691s fast INFO:root:Thread: 4 reqs: 8 mean time: 3.527s INFO:root:Thread: 1 reqs: 8 mean time: 3.502s INFO:root:Thread: 4 reqs: 5 mean time: 3.653s INFO:root:Thread: 3 reqs: 8 mean time: 3.592s INFO:root:Thread: 2 reqs: 9 mean time: 3.011s INFO:root:Thread: 0 reqs: 9 mean time: 2.892s fast INFO:root:Thread: 1 reqs: 11 mean time: 2.508s INFO:root:Thread: 3 reqs: 8 mean time: 2.525s INFO:root:Thread: 2 reqs: 14 mean time: 1.679s INFO:root:Thread: 0 reqs: 14 mean time: 1.679s fast INFO:root:Thread: 2 reqs: 6 mean time: 5.037s INFO:root:Thread: 1 reqs: 6 mean time: 5.041s INFO:root:Thread: 0 reqs: 2 mean time: 21.444s fast INFO:root:Thread: 1 reqs: 6 mean time: 1.288s INFO:root:Thread: 0 reqs: 34 mean time: 1.010s fast INFO:root:Thread: 0 reqs: 2 mean time: 21.476s fast INFO:root:Thread: 0 reqs: 2 mean time: 21.461s fast
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
mu = 0
std = 1
for i, result in enumerate(results):
x = [(x - START)/UNIT for x in result.keys()]
y = result.values()
plt.plot(x, y, label="t%d"%(i,))
plt.legend()
plt.show()
import requests
import random
import math
import time
import threading
import logging
logging.getLogger().setLevel(logging.INFO)
API_URL="http://s464863-lb-2086103880.us-east-1.elb.amazonaws.com:8080/"
UNIT = 5 # secs
# Pre generated primes
first_primes_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101, 103,
107, 109, 113, 127, 131, 137, 139,
149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, 211, 223,
227, 229, 233, 239, 241, 251, 257,
263, 269, 271, 277, 281, 283, 293,
307, 311, 313, 317, 331, 337, 347, 349]
def nBitRandom(n):
return random.randrange(2**(n-1)+1, 2**n - 1)
def getLowLevelPrime(n):
'''Generate a prime candidate divisible
by first primes'''
while True:
# Obtain a random number
pc = nBitRandom(n)
# Test divisibility by pre-generated
# primes
for divisor in first_primes_list:
if pc % divisor == 0 and divisor**2 <= pc:
break
else: return pc
def isMillerRabinPassed(mrc):
'''Run 20 iterations of Rabin Miller Primality test'''
maxDivisionsByTwo = 0
ec = mrc-1
while ec % 2 == 0:
ec >>= 1
maxDivisionsByTwo += 1
assert(2**maxDivisionsByTwo * ec == mrc-1)
def trialComposite(round_tester):
if pow(round_tester, ec, mrc) == 1:
return False
for i in range(maxDivisionsByTwo):
if pow(round_tester, 2**i * ec, mrc) == mrc-1:
return False
return True
# Set number of trials here
numberOfRabinTrials = 20
for i in range(numberOfRabinTrials):
round_tester = random.randrange(2, mrc)
if trialComposite(round_tester):
return False
return True
def random_large_prime(bits):
while True:
prime_candidate = getLowLevelPrime(bits)
if not isMillerRabinPassed(prime_candidate):
continue
else:
return prime_candidate
def thread_function(i, fast, timeout):
start = time.time()
c = 5 # bits: 20: 200ms; 21: 350ms; 22: 700ms 23: 1.5s; 25: 6s; 26: 10s; 27: 24s
bits = 19 if fast else 23
last_report = time.time()
processing_time = 0.0
reqs = 0
while True:
iter_start = time.time()
if iter_start - start > timeout:
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 ""))
results[i][iter_start] = processing_time/reqs if reqs>0 else 0.0
return
if iter_start - last_report > UNIT/2:
if len(results[i])%2 == 0:
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 ""))
results[i][iter_start] = processing_time/reqs if reqs>0 else 0.0
processing_time = 0.0
reqs = 0
last_report=iter_start
factors = [random_large_prime(bits) for i in range(c)]
factors.sort()
n=math.prod(factors)
r = requests.get(API_URL+'/factors/%d'%(n))
if r.status_code != 200:
logging.error("wrong status code from webservice")
else:
result = r.json()
if result != factors:
logging.error("Wrong factors")
processing_time+=time.time() - iter_start
reqs+=1
time.sleep(0.5)
START = time.time()
slow_threads = 4
results = [ {} for i in range(slow_threads+1)]
t0 = threading.Thread(target=thread_function, args=(0, True, (5 + slow_threads*3) * UNIT))
t0.start()
time.sleep(2 * UNIT)
for i in range(slow_threads):
t = threading.Thread(target=thread_function, args=(i+1, False, (slow_threads-i) * 3 * UNIT))
t.start()
time.sleep(2 * UNIT)
t0.join()
INFO:root:Thread: 0 reqs: 1 mean time: 21.442s fast INFO:root:Thread: 1 reqs: 1 mean time: 22.393s INFO:root:Thread: 2 reqs: 1 mean time: 22.035s INFO:root:Thread: 4 reqs: 2 mean time: 1.301s INFO:root:Thread: 0 reqs: 2 mean time: 1.606s fast INFO:root:Thread: 2 reqs: 2 mean time: 1.551s INFO:root:Thread: 4 reqs: 2 mean time: 1.677s INFO:root:Thread: 3 reqs: 1 mean time: 23.503s INFO:root:Thread: 4 reqs: 1 mean time: 2.620s INFO:root:Thread: 0 reqs: 1 mean time: 2.646s fast INFO:root:Thread: 2 reqs: 1 mean time: 2.535s INFO:root:Thread: 1 reqs: 1 mean time: 2.514s INFO:root:Thread: 3 reqs: 1 mean time: 2.443s INFO:root:Thread: 0 reqs: 1 mean time: 2.544s fast INFO:root:Thread: 2 reqs: 2 mean time: 1.671s INFO:root:Thread: 1 reqs: 2 mean time: 1.527s INFO:root:Thread: 1 reqs: 2 mean time: 1.147s INFO:root:Thread: 0 reqs: 3 mean time: 0.891s fast INFO:root:Thread: 0 reqs: 3 mean time: 0.379s fast INFO:root:Thread: 0 reqs: 3 mean time: 0.394s fast INFO:root:Thread: 0 reqs: 2 mean time: 0.380s fast
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
mu = 0
std = 1
for i, result in enumerate(results):
x = [(x - START)/UNIT for x in result.keys()]
y = result.values()
plt.plot(x, y, label="t%d"%(i,))
plt.legend()
plt.show()
import requests
import random
import math
import time
import threading
import logging
logging.getLogger().setLevel(logging.INFO)
API_URL="http://s464863-lb-2086103880.us-east-1.elb.amazonaws.com:8080/"
UNIT = 30 # secs
# Pre generated primes
first_primes_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101, 103,
107, 109, 113, 127, 131, 137, 139,
149, 151, 157, 163, 167, 173, 179,
181, 191, 193, 197, 199, 211, 223,
227, 229, 233, 239, 241, 251, 257,
263, 269, 271, 277, 281, 283, 293,
307, 311, 313, 317, 331, 337, 347, 349]
def nBitRandom(n):
return random.randrange(2**(n-1)+1, 2**n - 1)
def getLowLevelPrime(n):
'''Generate a prime candidate divisible
by first primes'''
while True:
# Obtain a random number
pc = nBitRandom(n)
# Test divisibility by pre-generated
# primes
for divisor in first_primes_list:
if pc % divisor == 0 and divisor**2 <= pc:
break
else: return pc
def isMillerRabinPassed(mrc):
'''Run 20 iterations of Rabin Miller Primality test'''
maxDivisionsByTwo = 0
ec = mrc-1
while ec % 2 == 0:
ec >>= 1
maxDivisionsByTwo += 1
assert(2**maxDivisionsByTwo * ec == mrc-1)
def trialComposite(round_tester):
if pow(round_tester, ec, mrc) == 1:
return False
for i in range(maxDivisionsByTwo):
if pow(round_tester, 2**i * ec, mrc) == mrc-1:
return False
return True
# Set number of trials here
numberOfRabinTrials = 20
for i in range(numberOfRabinTrials):
round_tester = random.randrange(2, mrc)
if trialComposite(round_tester):
return False
return True
def random_large_prime(bits):
while True:
prime_candidate = getLowLevelPrime(bits)
if not isMillerRabinPassed(prime_candidate):
continue
else:
return prime_candidate
def thread_function(i, fast, timeout):
start = time.time()
c = 5 # bits: 20: 200ms; 21: 350ms; 22: 700ms 23: 1.5s; 25: 6s; 26: 10s; 27: 24s
bits = 19 if fast else 23
last_report = time.time()
processing_time = 0.0
reqs = 0
while True:
iter_start = time.time()
if iter_start - start > timeout:
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 ""))
results[i][iter_start] = processing_time/reqs if reqs>0 else 0.0
return
if iter_start - last_report > UNIT/2:
if len(results[i])%2 == 0:
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 ""))
results[i][iter_start] = processing_time/reqs if reqs>0 else 0.0
processing_time = 0.0
reqs = 0
last_report=iter_start
factors = [random_large_prime(bits) for i in range(c)]
factors.sort()
n=math.prod(factors)
r = requests.get(API_URL+'/factors/%d'%(n))
if r.status_code != 200:
logging.error("wrong status code from webservice")
else:
result = r.json()
if result != factors:
logging.error("Wrong factors")
processing_time+=time.time() - iter_start
reqs+=1
time.sleep(0.5)
START = time.time()
slow_threads = 4
results = [ {} for i in range(slow_threads+1)]
t0 = threading.Thread(target=thread_function, args=(0, True, (5 + slow_threads*3) * UNIT))
t0.start()
time.sleep(2 * UNIT)
for i in range(slow_threads):
t = threading.Thread(target=thread_function, args=(i+1, False, (slow_threads-i) * 3 * UNIT))
t.start()
time.sleep(2 * UNIT)
t0.join()
INFO:root:Thread: 0 reqs: 16 mean time: 1.717s fast INFO:root:Thread: 0 reqs: 1 mean time: 21.419s fast INFO:root:Thread: 1 reqs: 1 mean time: 22.195s INFO:root:Thread: 0 reqs: 9 mean time: 1.339s fast INFO:root:Thread: 1 reqs: 9 mean time: 1.361s INFO:root:Thread: 2 reqs: 8 mean time: 1.505s INFO:root:Thread: 0 reqs: 8 mean time: 1.586s fast INFO:root:Thread: 1 reqs: 8 mean time: 1.575s INFO:root:Thread: 2 reqs: 8 mean time: 1.622s INFO:root:Thread: 0 reqs: 8 mean time: 1.587s fast INFO:root:Thread: 1 reqs: 8 mean time: 1.554s INFO:root:Thread: 3 reqs: 6 mean time: 2.339s INFO:root:Thread: 2 reqs: 5 mean time: 2.570s INFO:root:Thread: 0 reqs: 5 mean time: 2.604s fast INFO:root:Thread: 1 reqs: 5 mean time: 2.694s INFO:root:Thread: 3 reqs: 5 mean time: 2.558s INFO:root:Thread: 2 reqs: 5 mean time: 2.583s INFO:root:Thread: 0 reqs: 5 mean time: 2.570s fast INFO:root:Thread: 1 reqs: 5 mean time: 2.792s INFO:root:Thread: 4 reqs: 4 mean time: 3.601s INFO:root:Thread: 3 reqs: 4 mean time: 3.686s INFO:root:Thread: 1 reqs: 1 mean time: 22.297s INFO:root:Thread: 0 reqs: 3 mean time: 9.913s fast INFO:root:Thread: 2 reqs: 4 mean time: 8.330s INFO:root:Thread: 4 reqs: 1 mean time: 22.307s INFO:root:Thread: 3 reqs: 1 mean time: 22.306s INFO:root:Thread: 4 reqs: 1 mean time: 1.490s INFO:root:Thread: 1 reqs: 1 mean time: 23.343s INFO:root:Thread: 0 reqs: 1 mean time: 23.323s fast INFO:root:Thread: 2 reqs: 1 mean time: 24.088s INFO:root:Thread: 3 reqs: 5 mean time: 2.595s INFO:root:Thread: 3 reqs: 5 mean time: 2.638s INFO:root:Thread: 1 reqs: 5 mean time: 2.624s INFO:root:Thread: 0 reqs: 5 mean time: 2.625s fast INFO:root:Thread: 2 reqs: 6 mean time: 2.302s INFO:root:Thread: 1 reqs: 6 mean time: 5.040s INFO:root:Thread: 0 reqs: 6 mean time: 5.038s fast INFO:root:Thread: 2 reqs: 4 mean time: 6.798s INFO:root:Thread: 1 reqs: 1 mean time: 22.445s INFO:root:Thread: 0 reqs: 1 mean time: 21.447s fast INFO:root:Thread: 0 reqs: 1 mean time: 21.433s fast INFO:root:Thread: 0 reqs: 1 mean time: 21.465s fast
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
mu = 0
std = 1
for i, result in enumerate(results):
x = [(x - START)/UNIT for x in result.keys()]
y = result.values()
plt.plot(x, y, label="t%d"%(i,))
plt.legend()
plt.show()