import time from functools import wraps from nylon import * def speed_test(func): @wraps(func) def wrapper(*args, **kwargs): start_time = time.time() result = func(*args, **kwargs) end_time = time.time() execution_time = end_time - start_time wrapper.last_execution_time = execution_time return result wrapper.last_execution_time = 0 return wrapper class SpeedScorer: def __init__(self): self.total_time = 0 self.function_times = {} @speed_test def test_get_keywords(self, chat_db, text, cache): return get_keywords(text, cache) @speed_test def test_calculate_weight(self, chat_db, message, sender_messages, cache): return calculate_weight(message, sender_messages, cache) @speed_test def test_predict_response_separate(self, chat_db, query, sender, cache): return chat_db.predict_response_separate(query, sender, cache) @speed_test def test_get_relevant_messages(self, chat_db, sender, query, N, cache, query_tag=None): return chat_db.get_relevant_messages(sender, query, N, cache, query_tag) @speed_test def test_generate_response(self, chat_db, query, sender, cache, query_tag=None): return chat_db.generate_response(query, sender, cache, query_tag) def run_tests(self, chat_db, num_iterations=100): cache = {} sample_text = "This is a sample text for testing purposes." sample_message = ("sender", "2023-07-19 12:00:00", sample_text, None) sample_sender_messages = [sample_message] * 10 for _ in range(num_iterations): self.test_get_keywords(chat_db, sample_text, cache) self.test_calculate_weight(chat_db, sample_message, sample_sender_messages, cache) self.test_predict_response_separate(chat_db, sample_text, "sender", cache) self.test_get_relevant_messages(chat_db, "sender", sample_text, 5, cache) self.test_generate_response(chat_db, sample_text, "sender", cache) self.function_times = { "get_keywords": self.test_get_keywords.last_execution_time, "calculate_weight": self.test_calculate_weight.last_execution_time, "predict_response_separate": self.test_predict_response_separate.last_execution_time, "get_relevant_messages": self.test_get_relevant_messages.last_execution_time, "generate_response": self.test_generate_response.last_execution_time } self.total_time = sum(self.function_times.values()) def calculate_score(self): base_score = 1000000000 time_penalty = int(self.total_time * 1000000) # Convert seconds to microseconds score = max(0, base_score - time_penalty) return score def print_results(self): print(f"Total execution time: {self.total_time:.6f} seconds") print("\nExecution times for each function:") for func, time in self.function_times.items(): print(f"{func}: {time:.6f} seconds") print(f"\nSpeed Score (out of 1,000,000,000): {self.calculate_score():,}") def run_speed_scoring(chat_db): scorer = SpeedScorer() scorer.run_tests(chat_db) scorer.print_results() return scorer.calculate_score()