-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstress_test.py
77 lines (62 loc) · 2.59 KB
/
stress_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import time
import json
import datetime
import tempfile
import pstats
import cProfile
import io
from il_supermarket_scarper.scrappers_factory import ScraperFactory
from il_supermarket_scarper.utils import _now
def format_stats_as_json(profile, project_name):
"""get the stats from the profiler and format them as json"""
stream = io.StringIO()
ps = pstats.Stats(profile, stream=stream)
ps.sort_stats(pstats.SortKey.CUMULATIVE) # Sort by cumulative time
ps.print_stats()
# Convert the printed stats to a list of lines
stats_output = stream.getvalue().splitlines()
# Filter the lines to include only functions within the project
project_stats = []
for line in stats_output:
if project_name in line: # Filter for project-specific lines
parts = line.split()
if len(parts) >= 5: # Basic sanity check for the parts
function_data = {
"function": parts[-1], # Function path
"ncalls": parts[0], # Number of calls
"tottime": parts[1],
"tottime_per_call": parts[2], # Time spent in function
"cumtime": parts[3], # Cumulative time including subcalls
"cumtime_per_call": parts[4], #
}
project_stats.append(function_data)
return project_stats
if __name__ == "__main__":
result = {}
for scraper_name in ScraperFactory.all_scrapers_name():
def full_execution(scraper):
"""full execution of the scraper"""
with tempfile.TemporaryDirectory() as tmpdirname:
try:
initer = ScraperFactory.get(scraper)(folder_name=tmpdirname)
return initer.scrape(when_date=_now()), ""
except Exception as e: # pylint: disable=broad-exception-caught
return [], str(e)
execution_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
start_time = time.time()
pr = cProfile.Profile()
pr.enable()
files, error = full_execution(scraper_name)
pr.disable()
end_time = time.time()
result[scraper_name] = {
"status": format_stats_as_json(pr, "israeli-supermarket-scarpers"),
"execution_time": execution_time,
"start_time": start_time,
"end_time": end_time,
"time": end_time - start_time,
"files": len(files),
"error": error,
}
with open("stress_test_results.json", "w", encoding="utf-8") as f:
json.dump(result, f)