1 |
When things get slow, this option helps narrow down where things |
2 |
are getting hung up. |
3 |
--- |
4 |
catalyst/main.py | 24 ++++++++++++++++++++++++ |
5 |
1 file changed, 24 insertions(+) |
6 |
|
7 |
diff --git a/catalyst/main.py b/catalyst/main.py |
8 |
index 3550809..cb90e66 100644 |
9 |
--- a/catalyst/main.py |
10 |
+++ b/catalyst/main.py |
11 |
@@ -174,6 +174,9 @@ $ catalyst -f stage1-specfile.spec""" |
12 |
group.add_argument('--trace', |
13 |
default=False, action='store_true', |
14 |
help='trace program output (akin to `sh -x`)') |
15 |
+ group.add_argument('--profile', |
16 |
+ default=False, action='store_true', |
17 |
+ help='profile program execution') |
18 |
|
19 |
group = parser.add_argument_group('Temporary file management') |
20 |
group.add_argument('-a', '--clear-autoresume', |
21 |
@@ -243,6 +246,25 @@ def trace(func, *args, **kwargs): |
22 |
return tracer.runfunc(func, *args, **kwargs) |
23 |
|
24 |
|
25 |
+def profile(func, *args, **kwargs): |
26 |
+ """Run |func| through the profile module""" |
27 |
+ # Should make this an option. |
28 |
+ sort_keys = ('time',) |
29 |
+ |
30 |
+ # Collect the profile. |
31 |
+ import cProfile |
32 |
+ profiler = cProfile.Profile(subcalls=True, builtins=True) |
33 |
+ try: |
34 |
+ ret = profiler.runcall(func, *args, **kwargs) |
35 |
+ finally: |
36 |
+ # Then process the results. |
37 |
+ import pstats |
38 |
+ stats = pstats.Stats(profiler, stream=sys.stderr) |
39 |
+ stats.strip_dirs().sort_stats(*sort_keys).print_stats() |
40 |
+ |
41 |
+ return ret |
42 |
+ |
43 |
+ |
44 |
def main(argv): |
45 |
"""The main entry point for frontends to use""" |
46 |
parser = get_parser() |
47 |
@@ -250,6 +272,8 @@ def main(argv): |
48 |
|
49 |
if opts.trace: |
50 |
return trace(_main, parser, opts) |
51 |
+ elif opts.profile: |
52 |
+ return profile(_main, parser, opts) |
53 |
else: |
54 |
return _main(parser, opts) |
55 |
|
56 |
-- |
57 |
2.6.2 |