dpzc-4/module_4_pzc.ipynb
2022-01-05 14:31:13 +01:00

74 KiB

import requests
import random
import math
import time
import threading
import logging
logging.getLogger().setLevel(logging.INFO)

# 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)
        req_addr = API_URL+'/factors/%d'%(n)
        r = requests.get(req_addr )
        if r.status_code > 299:
            logging.error("wrong status code from webservice for addr: " + req_addr )
        else: 
            result = r.json()
            if result != factors:
                logging.error("Wrong factors")

        processing_time+=time.time() - iter_start
        reqs+=1
        time.sleep(0.5)

4.2 - AWS

API_URL="http://3.219.135.136:8080"
UNIT = 60.0 # secs
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: 36	mean time: 0.340s	fast	
INFO:root:Thread: 0	reqs: 35	mean time: 0.350s	fast	
INFO:root:Thread: 1	reqs: 14	mean time: 1.786s	
INFO:root:Thread: 0	reqs: 30	mean time: 0.511s	fast	
INFO:root:Thread: 1	reqs: 14	mean time: 1.712s	
INFO:root:Thread: 0	reqs: 29	mean time: 0.548s	fast	
INFO:root:Thread: 2	reqs: 13	mean time: 1.889s	
INFO:root:Thread: 1	reqs: 13	mean time: 1.817s	
INFO:root:Thread: 0	reqs: 26	mean time: 0.657s	fast	
INFO:root:Thread: 2	reqs: 13	mean time: 1.956s	
INFO:root:Thread: 1	reqs: 13	mean time: 1.938s	
INFO:root:Thread: 0	reqs: 25	mean time: 0.770s	fast	
INFO:root:Thread: 3	reqs: 12	mean time: 2.001s	
INFO:root:Thread: 2	reqs: 12	mean time: 2.030s	
INFO:root:Thread: 0	reqs: 23	mean time: 0.831s	fast	
INFO:root:Thread: 1	reqs: 13	mean time: 1.909s	
INFO:root:Thread: 3	reqs: 13	mean time: 1.813s	
INFO:root:Thread: 2	reqs: 13	mean time: 1.932s	
INFO:root:Thread: 0	reqs: 25	mean time: 0.760s	fast	
INFO:root:Thread: 1	reqs: 13	mean time: 1.937s	
INFO:root:Thread: 4	reqs: 13	mean time: 1.994s	
INFO:root:Thread: 3	reqs: 11	mean time: 2.218s	
INFO:root:Thread: 2	reqs: 12	mean time: 2.252s	
INFO:root:Thread: 0	reqs: 17	mean time: 1.299s	fast	
INFO:root:Thread: 1	reqs: 10	mean time: 2.651s	
INFO:root:Thread: 4	reqs: 11	mean time: 2.355s	
INFO:root:Thread: 3	reqs: 12	mean time: 2.417s	
INFO:root:Thread: 2	reqs: 11	mean time: 2.279s	
INFO:root:Thread: 0	reqs: 18	mean time: 1.201s	fast	
INFO:root:Thread: 1	reqs: 11	mean time: 2.353s	
INFO:root:Thread: 4	reqs: 13	mean time: 1.812s	
INFO:root:Thread: 3	reqs: 14	mean time: 1.847s	
INFO:root:Thread: 2	reqs: 12	mean time: 2.024s	
INFO:root:Thread: 0	reqs: 18	mean time: 1.166s	fast	
INFO:root:Thread: 1	reqs: 12	mean time: 2.231s	
INFO:root:Thread: 4	reqs: 9	mean time: 2.275s	
INFO:root:Thread: 3	reqs: 12	mean time: 2.085s	
INFO:root:Thread: 2	reqs: 13	mean time: 1.947s	
INFO:root:Thread: 0	reqs: 22	mean time: 0.887s	fast	
INFO:root:Thread: 1	reqs: 13	mean time: 1.960s	
INFO:root:Thread: 3	reqs: 6	mean time: 2.069s	
INFO:root:Thread: 2	reqs: 14	mean time: 1.772s	
INFO:root:Thread: 0	reqs: 27	mean time: 0.657s	fast	
INFO:root:Thread: 2	reqs: 2	mean time: 1.711s	
INFO:root:Thread: 1	reqs: 14	mean time: 1.708s	
INFO:root:Thread: 0	reqs: 32	mean time: 0.451s	fast	
INFO:root:Thread: 1	reqs: 13	mean time: 1.728s	
INFO:root:Thread: 0	reqs: 36	mean time: 0.338s	fast	
INFO:root:Thread: 0	reqs: 34	mean time: 0.378s	fast	
INFO:root:Thread: 0	reqs: 36	mean time: 0.334s	fast	
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
%matplotlib inline
def plot():
    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()
plot()

4.1 - Hetzner

API_URL="http://95.217.175.98:8080"
UNIT = 5.0 # secs
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: 4	mean time: 0.243s	fast	
INFO:root:Thread: 0	reqs: 4	mean time: 0.160s	fast	
INFO:root:Thread: 1	reqs: 2	mean time: 1.580s	
INFO:root:Thread: 0	reqs: 3	mean time: 0.585s	fast	
INFO:root:Thread: 0	reqs: 3	mean time: 0.512s	fast	
INFO:root:Thread: 1	reqs: 2	mean time: 1.289s	
INFO:root:Thread: 2	reqs: 2	mean time: 1.451s	
INFO:root:Thread: 0	reqs: 2	mean time: 1.323s	fast	
INFO:root:Thread: 1	reqs: 2	mean time: 1.550s	
INFO:root:Thread: 2	reqs: 1	mean time: 2.738s	
INFO:root:Thread: 3	reqs: 1	mean time: 2.642s	
INFO:root:Thread: 0	reqs: 2	mean time: 1.370s	fast	
INFO:root:Thread: 2	reqs: 2	mean time: 1.457s	
INFO:root:Thread: 1	reqs: 2	mean time: 1.997s	
INFO:root:Thread: 3	reqs: 1	mean time: 2.158s	
INFO:root:Thread: 0	reqs: 2	mean time: 1.973s	fast	
INFO:root:Thread: 4	reqs: 2	mean time: 2.063s	
INFO:root:Thread: 2	reqs: 1	mean time: 2.384s	
INFO:root:Thread: 1	reqs: 2	mean time: 1.721s	
INFO:root:Thread: 3	reqs: 1	mean time: 3.673s	
INFO:root:Thread: 0	reqs: 2	mean time: 1.371s	fast	
INFO:root:Thread: 4	reqs: 1	mean time: 3.717s	
INFO:root:Thread: 2	reqs: 2	mean time: 1.469s	
INFO:root:Thread: 1	reqs: 1	mean time: 3.726s	
INFO:root:Thread: 3	reqs: 1	mean time: 3.833s	
INFO:root:Thread: 4	reqs: 1	mean time: 2.686s	
INFO:root:Thread: 0	reqs: 1	mean time: 2.421s	fast	
INFO:root:Thread: 1	reqs: 1	mean time: 2.063s	
INFO:root:Thread: 2	reqs: 1	mean time: 2.196s	
INFO:root:Thread: 3	reqs: 2	mean time: 1.929s	
INFO:root:Thread: 0	reqs: 3	mean time: 0.539s	fast	
INFO:root:Thread: 1	reqs: 2	mean time: 1.196s	
INFO:root:Thread: 2	reqs: 1	mean time: 1.531s	
INFO:root:Thread: 1	reqs: 1	mean time: 1.502s	
INFO:root:Thread: 0	reqs: 3	mean time: 0.389s	fast	
INFO:root:Thread: 0	reqs: 4	mean time: 0.177s	fast	
INFO:root:Thread: 0	reqs: 4	mean time: 0.160s	fast	
INFO:root:Thread: 0	reqs: 1	mean time: 0.254s	fast	
plot()