From eb002bba65464856794ab9c227a939ed00b308af Mon Sep 17 00:00:00 2001 From: Joe Hattori Date: Tue, 29 Nov 2022 11:32:01 -0800 Subject: [PATCH 1/2] improve context --- spot/Spot.py | 7 ++++--- spot/constants.py | 1 + spot/context.py | 13 ++++++------- spot/invocation/aws_lambda_invoker.py | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/spot/Spot.py b/spot/Spot.py index 0a1208f..32bf91d 100644 --- a/spot/Spot.py +++ b/spot/Spot.py @@ -29,7 +29,6 @@ def __init__(self, config_dir: str, aws_session): self.config: BenchmarkConfig = BenchmarkConfig(f) self.last_log_timestamp = None - self.ctx.create_function_df(self.config.function_name) # Instantiate SPOT system components self.price_retriever = AWSPriceRetriever(self.ctx, self.config.region) @@ -44,7 +43,9 @@ def __init__(self, config_dir: str, aws_session): ) def optimize(self): - return self.recommendation_engine.run() + final_df = self.recommendation_engine.run() + self.ctx.save_final_result(final_df) + return final_df def collect_data(self): # retrieve latest config, logs, pricing scheme @@ -57,6 +58,6 @@ def invoke(self, memory_mb): def teardown(self): # Just saving the Context for now. os.makedirs(CTX_DIR, exist_ok=True) - ctx_file = os.path.join(CTX_DIR, f"{int(time.time() * 1000)}.pkl") + ctx_file = os.path.join(CTX_DIR, f"{self.config.function_name}_{int(time.time() * 1000)}.pkl") with open(ctx_file, "wb") as f: pickle.dump(self.ctx, f) diff --git a/spot/constants.py b/spot/constants.py index 7e12a0e..5c5c423 100644 --- a/spot/constants.py +++ b/spot/constants.py @@ -49,3 +49,4 @@ TERMINATION_CV = 0.3 KNOWLEDGE_RATIO = 0.2 DYNAMIC_SAMPLING_MAX = 5 + DYNAMIC_SAMPLING_INITIAL_STEP = 2 diff --git a/spot/context.py b/spot/context.py index 74f1b72..44066f1 100644 --- a/spot/context.py +++ b/spot/context.py @@ -9,16 +9,15 @@ class Context: def __init__(self): - self.function_dfs = {} + self.function_df = pd.DataFrame() self.pricing_df = pd.DataFrame() + self.final_df = None - # Creates database for the function name if the doesnt exist already - def create_function_df(self, function_name): - self.function_dfs[function_name] = pd.DataFrame() + def save_invocation_result(self, result_df): + self.function_df = pd.concat([self.function_df, result_df]) - def save_invokation_result(self, function_name, result_df): - old = self.function_dfs.get(function_name, pd.DataFrame()) - self.function_dfs[function_name] = pd.concat([old, result_df]) + def save_final_result(self, final_df): + self.final_df = final_df def record_pricing(self, row): df = pd.DataFrame(row) diff --git a/spot/invocation/aws_lambda_invoker.py b/spot/invocation/aws_lambda_invoker.py index 3053317..7e4f559 100644 --- a/spot/invocation/aws_lambda_invoker.py +++ b/spot/invocation/aws_lambda_invoker.py @@ -89,7 +89,7 @@ def invoke_sequential(count): result_df = pd.DataFrame.from_dict(results) if save_to_ctx: - self.ctx.save_invokation_result(self.lambda_name, result_df) + self.ctx.save_invocation_result(result_df) return result_df def _check_and_set_memory_value(self, memory_mb): From e07cc837893b3c24bbf44207e7f89264ce04fa1f Mon Sep 17 00:00:00 2001 From: Joe Hattori Date: Tue, 29 Nov 2022 12:13:38 -0800 Subject: [PATCH 2/2] invoke n times & enalbe being called with opt --- spot/Spot.py | 5 +++-- spot/main.py | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/spot/Spot.py b/spot/Spot.py index 32bf91d..0755991 100644 --- a/spot/Spot.py +++ b/spot/Spot.py @@ -52,8 +52,9 @@ def collect_data(self): self.price_retriever.fetch_current_pricing() self.last_log_timestamp = self.log_retriever.get_logs(self.last_log_timestamp) - def invoke(self, memory_mb): - return self.recommendation_engine.invoke_once(memory_mb) + def invoke(self, memory_mb, count): + for _ in range(count): + self.recommendation_engine.invoke_once(memory_mb) def teardown(self): # Just saving the Context for now. diff --git a/spot/main.py b/spot/main.py index 1816eeb..ea2e796 100644 --- a/spot/main.py +++ b/spot/main.py @@ -24,7 +24,7 @@ def main(): "--fetch", "-f", action="store_true", help="Fetch log and config data from AWS" ) parser.add_argument( - "--invoke", "-i", action="store_true", help="Invoke the function" + "--invoke", "-i", type=int, help="The number of times you invoke the function" ) parser.add_argument( "--memory_mb", "-m", type=int, help="Memory (MB) of the function" @@ -51,14 +51,15 @@ def main(): spot = Spot(path, session) if args.optimize: - spot.optimize() + opt = spot.optimize() + args.memory_mb = int(opt["Minimum Cost Memory"][0]) if args.fetch: spot.collect_data() if args.invoke: if not args.memory_mb: print("Please specify a memory value when invoking a function") exit(1) - spot.invoke(args.memory_mb) + spot.invoke(args.memory_mb, args.invoke) spot.teardown()