-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiling.py
57 lines (47 loc) · 1.52 KB
/
profiling.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
# -*- coding: utf-8 -*-
"""Script for memory and execution time profiling."""
import tracemalloc
from cProfile import Profile
from pstats import Stats
from hku_diabetes.analytics import Analyser
from hku_diabetes.config import TestConfig
from hku_diabetes.importer import import_all
from hku_diabetes.plot import plot_all
TOP_STATS = 20
class ProfilingConfig(TestConfig):
test_samples = 100
plot_samples = 100
def test():
"""Testing sequence for profiling"""
analyser = Analyser(config=ProfilingConfig)
data = import_all(config=ProfilingConfig)
analyser.load()
plot_all(analyser)
def main():
"""Main sequence"""
analyser = Analyser(config=ProfilingConfig)
data = import_all(config=ProfilingConfig)
analyser.run(data)
del analyser
del data
profiler = Profile()
tracemalloc.start(10)
time1 = tracemalloc.take_snapshot()
profiler.runcall(test)
time2 = tracemalloc.take_snapshot()
time_stats = Stats(profiler)
time_stats.strip_dirs()
time_stats.sort_stats('cumulative')
print("\n===Time Profiler Stats===\n")
time_stats.print_stats(TOP_STATS)
print("\n===Time Profiler Callers===\n")
time_stats.print_callers(TOP_STATS)
memory_stats = time2.compare_to(time1, 'lineno')
print("\n===Memory Profiler Callers===\n")
for stat in memory_stats[:3]:
print(stat)
print("\n===Top Memory Consumer===\n")
top = memory_stats[0]
print('\n'.join(top.traceback.format()))
if __name__ == '__main__':
main()