Gentoo Archives: gentoo-catalyst

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