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