diff --git a/Zadanie_4_3_b/Dockerfile.txt b/Zadanie_4_3_b/Dockerfile similarity index 100% rename from Zadanie_4_3_b/Dockerfile.txt rename to Zadanie_4_3_b/Dockerfile diff --git a/Zadanie_4_3_b/aws.py b/Zadanie_4_3_b/aws.py index 51d9c6d..a9c5eb9 100644 --- a/Zadanie_4_3_b/aws.py +++ b/Zadanie_4_3_b/aws.py @@ -3,7 +3,8 @@ aws_access_key_id='' aws_secret_access_key='' aws_session_token='' VPC='' -ID='444376' +ID='' + # biblioteka dostępowa do AWS import boto3 @@ -25,8 +26,8 @@ key_pair = ec2_resource.create_key_pair( ) security_group = ec2_resource.create_security_group( - Description=ID+'_SECURITY_GROUP', - GroupName=ID+'_SECURITY_GROUP', + Description=ID+'-SECURITY-GROUP', + GroupName=ID+'-SECURITY-GROUP', VpcId=VPC ) @@ -49,11 +50,12 @@ instance_1, instance_2 = ec2_resource.create_instances( sudo yum update -y sudo yum install git -y git clone https://git.wmi.amu.edu.pl/s444376/DPZC_Ola.git + cd DPZC_Ola/Zadanie_4_3_b sudo yum install docker -y sudo service docker start sudo usermod -a -G docker ec2-user - docker build -t webservice . - docker run -d -p 80:8080 -t webservice + sudo docker build -t webservice . + sudo docker run -d -p 80:8080 -t webservice ''', SecurityGroups=[security_group.group_name] ) @@ -65,75 +67,72 @@ while True: if instance_1.state['Code'] == 16 and instance_2.state['Code'] == 16: break - elbv2 = boto3.client( - 'elbv2', - region_name='us-east-1', - aws_access_key_id=aws_access_key_id, - aws_secret_access_key=aws_secret_access_key, - aws_session_token=aws_session_token, - ) +elbv2 = boto3.client( + 'elbv2', + region_name='us-east-1', + aws_access_key_id=aws_access_key_id, + aws_secret_access_key=aws_secret_access_key, + aws_session_token=aws_session_token +) - target_group = elbv2.create_target_group( - Name=ID+'_TARGET_GROUP', - Protocol='TCP', - Port=80, - VpcId=VPC, - TargetType='instance', - IpAddressType='ipv4', - ) +target_group = elbv2.create_target_group( + Name=ID+'-TARGET-GROUP', + Protocol='TCP', + Port=80, + VpcId=VPC, + TargetType='instance', + IpAddressType='ipv4' +) - register_targets = elbv2.register_targets( - TargetGroupArn=target_group['TargetGroups'][0]['TargetGroupArn'], - Targets=[ - { - 'Id': instance_1.id, - 'Port': 80, - }, - { - 'Id': instance_2.id, - 'Port': 80, - }, - ] - ) +register_targets = elbv2.register_targets( + TargetGroupArn=target_group['TargetGroups'][0]['TargetGroupArn'], + Targets=[ + { + 'Id': instance_1.id, + 'Port': 80, + }, + { + 'Id': instance_2.id, + 'Port': 80, + } +]) - ec2_client = boto3.client( - 'ec2', - region_name='us-east-1', - aws_access_key_id=aws_access_key_id, - aws_secret_access_key=aws_secret_access_key, - aws_session_token=aws_session_token, - ) +ec2_client = boto3.client( + 'ec2', + region_name='us-east-1', + aws_access_key_id=aws_access_key_id, + aws_secret_access_key=aws_secret_access_key, + aws_session_token=aws_session_token +) - allocation = ec2_client.allocate_address( - Domain='vpc' - ) +allocation = ec2_client.allocate_address( + Domain='vpc' +) - load_balancer = elbv2.create_load_balancer( - Name=ID+'_LOAD_BALANCER', - SubnetMappings=[ - { - 'SubnetId': instance_1.subnet_id, - 'AllocationId': allocation['AllocationId'], - }, - ], - Scheme='internet-facing', - Type='network', - IpAddressType='ipv4', - ) +load_balancer = elbv2.create_load_balancer( + Name=ID+'-LOAD-BALANCER', + SubnetMappings=[ + { + 'SubnetId': instance_1.subnet_id, + 'AllocationId': allocation['AllocationId'] + }], + Scheme='internet-facing', + Type='network', + IpAddressType='ipv4', +) - listener = elbv2.create_listener( - LoadBalancerArn=load_balancer['LoadBalancers'][0]['LoadBalancerArn'], - Protocol='TCP', - Port=80, - DefaultActions=[ - { - 'Type': 'forward', - 'TargetGroupArn': target_group['TargetGroups'][0]['TargetGroupArn'], - }, - ], - ) +listener = elbv2.create_listener( + LoadBalancerArn=load_balancer['LoadBalancers'][0]['LoadBalancerArn'], + Protocol='TCP', + Port=80, + DefaultActions=[ + { + 'Type': 'forward', + 'TargetGroupArn': target_group['TargetGroups'][0]['TargetGroupArn'], + } +]) - print(f'Done: {allocation["PublicIp"]}:80') +print(f'Done: {allocation["PublicIp"]}:80') ''' #Stopping and terminating multiple instances given a list of instance IDs uses Boto3 collection filtering: diff --git a/Zadanie_4_3_b/check.py b/Zadanie_4_3_b/check.py new file mode 100644 index 0000000..33519c2 --- /dev/null +++ b/Zadanie_4_3_b/check.py @@ -0,0 +1,141 @@ +import requests +import random +import math +import time +import threading +import logging +logging.getLogger().setLevel(logging.INFO) + + +API_URL="http://34.230.97.151:80" + + +UNIT = 5.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() + +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() \ No newline at end of file