-
Notifications
You must be signed in to change notification settings - Fork 4
/
profile_etm.py
executable file
·79 lines (63 loc) · 1.86 KB
/
profile_etm.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
78
79
#! /usr/bin/env python3
import cProfile
import pstats
import sys
import os
import shutil
from datetime import datetime
from zoneinfo import ZoneInfo
from etm.__main__ import main
# from etm.view import main
etmhome = os.environ.get('ETMHOME')
bn = 'profile'
ext = 'txt'
cwd = os.getcwd()
maybe_etmdir = os.path.exists(
os.path.join(cwd, 'etm.json')
) and os.path.exists(os.path.join(cwd, 'cfg.yaml'))
fn = f'{bn}.{ext}'
if len(sys.argv) > 1 and os.path.isdir(sys.argv[-1]):
etmdir = sys.argv[-1]
elif maybe_etmdir:
etmdir = cwd
sys.argv.append(etmdir)
elif etmhome:
etmdir = etmhome
else:
print(
"""Canceled
An etmhome directory was not provided,
the current working directory does not appear to be suitable
and the environmental variable ETMHOME is not set.
"""
)
sys.exit()
# pstats directory
pd = os.path.join(etmdir, 'pstats')
if not os.path.isdir(pd):
os.makedirs(pd)
print(f'created profile directory: {pd}')
fp = os.path.join(pd, fn)
if os.path.exists(fp):
timestamp = (
datetime.now().astimezone(ZoneInfo('UTC')).strftime('%y%m%dT%H%M')
)
backup = os.path.join(pd, f'{bn}-{timestamp}.{ext}')
shutil.copy2(fp, backup)
os.remove(fp)
print(f'backed up {fp} to {backup} and removed it.')
# use contexts for profile and stdout
with cProfile.Profile() as profile:
main()
with open(fp, 'w') as file:
# Save the original stdout so we can restore it later
original_stdout = sys.stdout
# Set stdout to the file object
sys.stdout = file
results = pstats.Stats(profile)
results.sort_stats(pstats.SortKey.TIME)
results.print_stats('dag', 0.3)
# results.dump_stats('results.prof')
# Reset stdout to its original value
sys.stdout = original_stdout
print(f'\n### pstats saved to {fp} ###')