EC2 performance improvements (#4855)

This commit is contained in:
Bert Blommers 2022-02-14 21:40:48 -01:00 committed by GitHub
parent ec928d5c56
commit a7e169a545
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 17 deletions

View File

@ -13,6 +13,7 @@ from urllib.parse import urlparse
REQUEST_ID_LONG = string.digits + string.ascii_uppercase REQUEST_ID_LONG = string.digits + string.ascii_uppercase
HEX_CHARS = list(range(10)) + ["a", "b", "c", "d", "e", "f"]
def camelcase_to_underscores(argument): def camelcase_to_underscores(argument):
@ -73,8 +74,7 @@ def method_names_from_class(clazz):
def get_random_hex(length=8): def get_random_hex(length=8):
chars = list(range(10)) + ["a", "b", "c", "d", "e", "f"] return "".join(str(random.choice(HEX_CHARS)) for _ in range(length))
return "".join(str(random.choice(chars)) for x in range(length))
def get_random_message_id(): def get_random_message_id():

View File

@ -217,7 +217,11 @@ DEFAULT_VPC_ENDPOINT_SERVICES = []
def utc_date_and_time(): def utc_date_and_time():
return datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.000Z") x = datetime.utcnow()
# Better performing alternative to x.strftime("%Y-%m-%dT%H:%M:%S.000Z")
return "{}-{:02d}-{:02d}T{:02d}:{:02d}:{:02d}.000Z".format(
x.year, x.month, x.day, x.hour, x.minute, x.second
)
def validate_resource_ids(resource_ids): def validate_resource_ids(resource_ids):
@ -5477,14 +5481,12 @@ class VPCEndPoint(TaggedEC2Resource):
self.add_tags(tags or {}) self.add_tags(tags or {})
self.destination_prefix_list_id = destination_prefix_list_id self.destination_prefix_list_id = destination_prefix_list_id
self.created_at = utc_date_and_time()
@property @property
def owner_id(self): def owner_id(self):
return ACCOUNT_ID return ACCOUNT_ID
@property
def created_at(self):
return utc_date_and_time()
class ManagedPrefixList(TaggedEC2Resource): class ManagedPrefixList(TaggedEC2Resource):
def __init__( def __init__(

View File

@ -1,4 +1,3 @@
from moto.autoscaling import autoscaling_backends
from moto.core.responses import BaseResponse from moto.core.responses import BaseResponse
from moto.core.utils import camelcase_to_underscores from moto.core.utils import camelcase_to_underscores
from moto.ec2.exceptions import ( from moto.ec2.exceptions import (
@ -10,7 +9,6 @@ from moto.ec2.utils import (
filters_from_querystring, filters_from_querystring,
dict_from_querystring, dict_from_querystring,
) )
from moto.elbv2 import elbv2_backends
from moto.core import ACCOUNT_ID from moto.core import ACCOUNT_ID
from copy import deepcopy from copy import deepcopy
@ -95,6 +93,9 @@ class InstanceResponse(BaseResponse):
instance_ids = self._get_multi_param("InstanceId") instance_ids = self._get_multi_param("InstanceId")
if self.is_not_dryrun("TerminateInstance"): if self.is_not_dryrun("TerminateInstance"):
instances = self.ec2_backend.terminate_instances(instance_ids) instances = self.ec2_backend.terminate_instances(instance_ids)
from moto.autoscaling import autoscaling_backends
from moto.elbv2 import elbv2_backends
autoscaling_backends[self.region].notify_terminate_instances(instance_ids) autoscaling_backends[self.region].notify_terminate_instances(instance_ids)
elbv2_backends[self.region].notify_terminate_instances(instance_ids) elbv2_backends[self.region].notify_terminate_instances(instance_ids)
template = self.response_template(EC2_TERMINATE_INSTANCES) template = self.response_template(EC2_TERMINATE_INSTANCES)

View File

@ -57,16 +57,15 @@ EC2_RESOURCE_TO_PREFIX = {
EC2_PREFIX_TO_RESOURCE = dict((v, k) for (k, v) in EC2_RESOURCE_TO_PREFIX.items()) EC2_PREFIX_TO_RESOURCE = dict((v, k) for (k, v) in EC2_RESOURCE_TO_PREFIX.items())
HEX_CHARS = list(str(x) for x in range(10)) + ["a", "b", "c", "d", "e", "f"]
def random_resource_id(size=8): def random_resource_id(size=8):
chars = list(range(10)) + ["a", "b", "c", "d", "e", "f"] return "".join(random.choice(HEX_CHARS) for _ in range(size))
resource_id = "".join(str(random.choice(chars)) for _ in range(size))
return resource_id
def random_id(prefix="", size=8): def random_id(prefix="", size=8):
return "{0}-{1}".format(prefix, random_resource_id(size)) return f"{prefix}-{random_resource_id(size)}"
def random_ami_id(): def random_ami_id():

View File

@ -2066,9 +2066,8 @@ def test_update_usage_plan():
{"op": "replace", "path": "/productCode", "value": "new-productionCode"}, {"op": "replace", "path": "/productCode", "value": "new-productionCode"},
], ],
) )
response["quota"]["limit"].should.equal("1000") response["quota"]["limit"].should.equal(1000)
response["quota"]["period"].should.equal("MONTH") response["quota"]["period"].should.equal("MONTH")
response["quota"]["limit"].should.equal("1000")
response["name"].should.equal("new-name") response["name"].should.equal("new-name")
response["description"].should.equal("new-description") response["description"].should.equal("new-description")
response["productCode"].should.equal("new-productionCode") response["productCode"].should.equal("new-productionCode")

View File

@ -955,7 +955,7 @@ def test_ami_filter_wildcard():
ec2_resource = boto3.resource("ec2", region_name="us-west-1") ec2_resource = boto3.resource("ec2", region_name="us-west-1")
ec2_client = boto3.client("ec2", region_name="us-west-1") ec2_client = boto3.client("ec2", region_name="us-west-1")
image_name = str(uuid4())[0:6] image_name = str(uuid4())[0:12]
instance = ec2_resource.create_instances( instance = ec2_resource.create_instances(
ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1 ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1
@ -967,7 +967,7 @@ def test_ami_filter_wildcard():
my_images = ec2_client.describe_images( my_images = ec2_client.describe_images(
Owners=[ACCOUNT_ID], Owners=[ACCOUNT_ID],
Filters=[{"Name": "name", "Values": [f"{image_name[0:4]}*"]}], Filters=[{"Name": "name", "Values": [f"{image_name[0:8]}*"]}],
)["Images"] )["Images"]
my_images.should.have.length_of(1) my_images.should.have.length_of(1)