def test_no_params(): with pytest.raises(ValueError): awscosts.EC2("t2.micro") with pytest.raises(ValueError): awscosts.EC2("t2.micro", ms_per_req=200) with pytest.raises(ValueError): awscosts.EC2("t2.micro", MB_per_req=200)
def get_ec2_cost(df: pd.DataFrame, flavor, **kwargs): """ Given a Dataframe with requests per time unit, calculates the EC2 costs for each row. Args: df (pandas.Dataframe): Dataframe with requests for each time unit per row. flavor (str): A valid EC2 instance flavor name (e.g. 'm4.large'). **kwargs: Keyword arguments to the awscosts.EC2 object (see awscosts package) Returns: The same DataFrame with two new columns: 'FLAVOR': Cost of this period (row) to process the given requests. 'FLAVOR_instances': Number of instances needed to process the requests in that row. FLAVOR is the actual name of the flavor. 'FLAVOR_sum': Accumulative cost of this and the previous rows. """ myec2 = awscosts.EC2(instance_type=flavor, **kwargs) df[flavor] = df.apply(lambda x: myec2.get_cost_and_num_instances( 3600, reqs=x['requests'])[0], axis=1) df[flavor + '_instances'] = df.apply( lambda x: myec2.get_num_instances(reqs=x['requests'] / 3600), axis=1) df[flavor + '_sum'] = df[flavor].cumsum() df = df.round({flavor: 2, flavor + '_sum': 2}) return df
def generate_costs_in_month(requests_range, flavors, memory, time, throughput_ratio=1): """Generates EC2 and Lambda costs in a month by a list of requests per second. Args: requests_range (:obj:`list` of :obj:`int`): list of reqs/s. flavors (:obj:`list` of :obj:`str`): list of valid EC2 flavors. memory time (int): duration (in milliseconds) of the Lambda request. throughput_ratio (int, optional): 1 Returns: Cost dict: - 'lambda': - REQS/S_1: COST_IN_DOLLARS - REQS/S_2: COST_IN_DOLLARS ... - REQS/S_n: COST_IN_DOLLARS - FLAVOR_1: - REQS/S_1: COST_IN_DOLLARS ... ... """ cost = dict() SECONDS_IN_A_MONTH = 3600 * 24 * 30 # generate costs for EC2 instances: for flavor in flavors: myec2 = awscosts.EC2( flavor, MB_per_req=memory, ms_per_req=time, throughput_ratio=throughput_ratio, ) cost[flavor] = dict() for reqs_per_second in requests_range: cost[flavor][reqs_per_second] = \ myec2.get_cost_per_second(reqs_per_second) * SECONDS_IN_A_MONTH # generate costs for Lambda: mylambda = awscosts.Lambda( MB_per_req=memory, ms_per_req=time, ) cost['lambda'] = dict() for reqs_per_second in requests_range: requests_per_month = reqs_per_second * SECONDS_IN_A_MONTH cost['lambda'][reqs_per_second] = \ mylambda.get_cost(requests_per_month, reset_free_tier=True) return cost
def test_scalability(): max_reqs = 200 t2micro = awscosts.EC2('t2.micro', max_reqs_per_second=max_reqs) assert t2micro.get_num_instances(max_reqs + 1) == 2
def test_max_reqs(): t2micro = awscosts.EC2('t2.micro', ms_per_req=200, MB_per_req=1024) assert t2micro.max_reqs_per_second == 5
def test_init_1(): t2micro = awscosts.EC2('t2.micro', max_reqs_per_second=200) assert t2micro.max_reqs_per_second == 200