mercurial/lsprof.py
changeset 43077 687b865b95ad
parent 43076 2372284d9457
child 43089 c59eb1560c44
equal deleted inserted replaced
43076:2372284d9457 43077:687b865b95ad
     6 Profiler = _lsprof.Profiler
     6 Profiler = _lsprof.Profiler
     7 
     7 
     8 # PyPy doesn't expose profiler_entry from the module.
     8 # PyPy doesn't expose profiler_entry from the module.
     9 profiler_entry = getattr(_lsprof, 'profiler_entry', None)
     9 profiler_entry = getattr(_lsprof, 'profiler_entry', None)
    10 
    10 
    11 __all__ = ['profile', 'Stats']
    11 __all__ = [b'profile', b'Stats']
    12 
    12 
    13 
    13 
    14 def profile(f, *args, **kwds):
    14 def profile(f, *args, **kwds):
    15     """XXX docstring"""
    15     """XXX docstring"""
    16     p = Profiler()
    16     p = Profiler()
    31     def sort(self, crit=r"inlinetime"):
    31     def sort(self, crit=r"inlinetime"):
    32         """XXX docstring"""
    32         """XXX docstring"""
    33         # profiler_entries isn't defined when running under PyPy.
    33         # profiler_entries isn't defined when running under PyPy.
    34         if profiler_entry:
    34         if profiler_entry:
    35             if crit not in profiler_entry.__dict__:
    35             if crit not in profiler_entry.__dict__:
    36                 raise ValueError("Can't sort by %s" % crit)
    36                 raise ValueError(b"Can't sort by %s" % crit)
    37         elif self.data and not getattr(self.data[0], crit, None):
    37         elif self.data and not getattr(self.data[0], crit, None):
    38             raise ValueError("Can't sort by %s" % crit)
    38             raise ValueError(b"Can't sort by %s" % crit)
    39 
    39 
    40         self.data.sort(key=lambda x: getattr(x, crit), reverse=True)
    40         self.data.sort(key=lambda x: getattr(x, crit), reverse=True)
    41         for e in self.data:
    41         for e in self.data:
    42             if e.calls:
    42             if e.calls:
    43                 e.calls.sort(key=lambda x: getattr(x, crit), reverse=True)
    43                 e.calls.sort(key=lambda x: getattr(x, crit), reverse=True)
    47         if file is None:
    47         if file is None:
    48             file = sys.stdout
    48             file = sys.stdout
    49         d = self.data
    49         d = self.data
    50         if top is not None:
    50         if top is not None:
    51             d = d[:top]
    51             d = d[:top]
    52         cols = "% 12d %12d %11.4f %11.4f   %s\n"
    52         cols = b"% 12d %12d %11.4f %11.4f   %s\n"
    53         hcols = "% 12s %12s %12s %12s %s\n"
    53         hcols = b"% 12s %12s %12s %12s %s\n"
    54         file.write(
    54         file.write(
    55             hcols
    55             hcols
    56             % (
    56             % (
    57                 "CallCount",
    57                 b"CallCount",
    58                 "Recursive",
    58                 b"Recursive",
    59                 "Total(s)",
    59                 b"Total(s)",
    60                 "Inline(s)",
    60                 b"Inline(s)",
    61                 "module:lineno(function)",
    61                 b"module:lineno(function)",
    62             )
    62             )
    63         )
    63         )
    64         count = 0
    64         count = 0
    65         for e in d:
    65         for e in d:
    66             file.write(
    66             file.write(
    84                         % (
    84                         % (
    85                             se.callcount,
    85                             se.callcount,
    86                             se.reccallcount,
    86                             se.reccallcount,
    87                             se.totaltime,
    87                             se.totaltime,
    88                             se.inlinetime,
    88                             se.inlinetime,
    89                             "    %s" % label(se.code),
    89                             b"    %s" % label(se.code),
    90                         )
    90                         )
    91                     )
    91                     )
    92                     count += 1
    92                     count += 1
    93                     ccount += 1
    93                     ccount += 1
    94                     if limit is not None and count == limit:
    94                     if limit is not None and count == limit:
   145 if __name__ == '__main__':
   145 if __name__ == '__main__':
   146     import os
   146     import os
   147 
   147 
   148     sys.argv = sys.argv[1:]
   148     sys.argv = sys.argv[1:]
   149     if not sys.argv:
   149     if not sys.argv:
   150         print("usage: lsprof.py <script> <arguments...>", file=sys.stderr)
   150         print(b"usage: lsprof.py <script> <arguments...>", file=sys.stderr)
   151         sys.exit(2)
   151         sys.exit(2)
   152     sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))
   152     sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0])))
   153     stats = profile(execfile, sys.argv[0], globals(), locals())
   153     stats = profile(execfile, sys.argv[0], globals(), locals())
   154     stats.sort()
   154     stats.sort()
   155     stats.pprint()
   155     stats.pprint()