Spaces:
Sleeping
Sleeping
Create speedtest.py
Browse files- speedtest.py +81 -0
speedtest.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
from functools import wraps
|
3 |
+
from nylon import *
|
4 |
+
|
5 |
+
def speed_test(func):
|
6 |
+
@wraps(func)
|
7 |
+
def wrapper(*args, **kwargs):
|
8 |
+
start_time = time.time()
|
9 |
+
result = func(*args, **kwargs)
|
10 |
+
end_time = time.time()
|
11 |
+
execution_time = end_time - start_time
|
12 |
+
wrapper.last_execution_time = execution_time
|
13 |
+
return result
|
14 |
+
wrapper.last_execution_time = 0
|
15 |
+
return wrapper
|
16 |
+
|
17 |
+
class SpeedScorer:
|
18 |
+
def __init__(self):
|
19 |
+
self.total_time = 0
|
20 |
+
self.function_times = {}
|
21 |
+
|
22 |
+
@speed_test
|
23 |
+
def test_get_keywords(self, chat_db, text, cache):
|
24 |
+
return get_keywords(text, cache)
|
25 |
+
|
26 |
+
@speed_test
|
27 |
+
def test_calculate_weight(self, chat_db, message, sender_messages, cache):
|
28 |
+
return calculate_weight(message, sender_messages, cache)
|
29 |
+
|
30 |
+
@speed_test
|
31 |
+
def test_predict_response_separate(self, chat_db, query, sender, cache):
|
32 |
+
return chat_db.predict_response_separate(query, sender, cache)
|
33 |
+
|
34 |
+
@speed_test
|
35 |
+
def test_get_relevant_messages(self, chat_db, sender, query, N, cache, query_tag=None):
|
36 |
+
return chat_db.get_relevant_messages(sender, query, N, cache, query_tag)
|
37 |
+
|
38 |
+
@speed_test
|
39 |
+
def test_generate_response(self, chat_db, query, sender, cache, query_tag=None):
|
40 |
+
return chat_db.generate_response(query, sender, cache, query_tag)
|
41 |
+
|
42 |
+
def run_tests(self, chat_db, num_iterations=100):
|
43 |
+
cache = {}
|
44 |
+
sample_text = "This is a sample text for testing purposes."
|
45 |
+
sample_message = ("sender", "2023-07-19 12:00:00", sample_text, None)
|
46 |
+
sample_sender_messages = [sample_message] * 10
|
47 |
+
|
48 |
+
for _ in range(num_iterations):
|
49 |
+
self.test_get_keywords(chat_db, sample_text, cache)
|
50 |
+
self.test_calculate_weight(chat_db, sample_message, sample_sender_messages, cache)
|
51 |
+
self.test_predict_response_separate(chat_db, sample_text, "sender", cache)
|
52 |
+
self.test_get_relevant_messages(chat_db, "sender", sample_text, 5, cache)
|
53 |
+
self.test_generate_response(chat_db, sample_text, "sender", cache)
|
54 |
+
|
55 |
+
self.function_times = {
|
56 |
+
"get_keywords": self.test_get_keywords.last_execution_time,
|
57 |
+
"calculate_weight": self.test_calculate_weight.last_execution_time,
|
58 |
+
"predict_response_separate": self.test_predict_response_separate.last_execution_time,
|
59 |
+
"get_relevant_messages": self.test_get_relevant_messages.last_execution_time,
|
60 |
+
"generate_response": self.test_generate_response.last_execution_time
|
61 |
+
}
|
62 |
+
self.total_time = sum(self.function_times.values())
|
63 |
+
|
64 |
+
def calculate_score(self):
|
65 |
+
base_score = 1000000000
|
66 |
+
time_penalty = int(self.total_time * 1000000) # Convert seconds to microseconds
|
67 |
+
score = max(0, base_score - time_penalty)
|
68 |
+
return score
|
69 |
+
|
70 |
+
def print_results(self):
|
71 |
+
print(f"Total execution time: {self.total_time:.6f} seconds")
|
72 |
+
print("\nExecution times for each function:")
|
73 |
+
for func, time in self.function_times.items():
|
74 |
+
print(f"{func}: {time:.6f} seconds")
|
75 |
+
print(f"\nSpeed Score (out of 1,000,000,000): {self.calculate_score():,}")
|
76 |
+
|
77 |
+
def run_speed_scoring(chat_db):
|
78 |
+
scorer = SpeedScorer()
|
79 |
+
scorer.run_tests(chat_db)
|
80 |
+
scorer.print_results()
|
81 |
+
return scorer.calculate_score()
|