Skip to content

Commit ca37661

Browse files
committed
Add a timing flag to Trace so you can see where slowness occurs
like waiting for socket timeouts in test_smtplib :-).
1 parent aa0ef52 commit ca37661

3 files changed

Lines changed: 26 additions & 6 deletions

File tree

Doc/library/trace.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Programming Interface
8080
---------------------
8181

8282

83-
.. class:: Trace([count=1[, trace=1[, countfuncs=0[, countcallers=0[, ignoremods=()[, ignoredirs=()[, infile=None[, outfile=None]]]]]]]])
83+
.. class:: Trace([count=1[, trace=1[, countfuncs=0[, countcallers=0[, ignoremods=()[, ignoredirs=()[, infile=None[, outfile=None[, timing=False]]]]]]]]])
8484

8585
Create an object to trace execution of a single statement or expression. All
8686
parameters are optional. *count* enables counting of line numbers. *trace*
@@ -89,7 +89,8 @@ Programming Interface
8989
*ignoremods* is a list of modules or packages to ignore. *ignoredirs* is a list
9090
of directories whose modules or packages should be ignored. *infile* is the
9191
file from which to read stored count information. *outfile* is a file in which
92-
to write updated count information.
92+
to write updated count information. *timing* enables a timestamp relative
93+
to when tracing was started to be displayed.
9394

9495

9596
.. method:: Trace.run(cmd)

Lib/trace.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import re
5454
import sys
5555
import threading
56+
import time
5657
import token
5758
import tokenize
5859
import types
@@ -98,6 +99,8 @@ def usage(outfile):
9899
with '>>>>>> '.
99100
-s, --summary Write a brief summary on stdout for each file.
100101
(Can only be used with --count or --report.)
102+
-g, --timing Prefix each line with the time since the program started.
103+
Only used while tracing.
101104
102105
Filters, may be repeated multiple times:
103106
--ignore-module=<mod> Ignore the given module(s) and its submodules
@@ -435,7 +438,8 @@ def find_executable_linenos(filename):
435438

436439
class Trace:
437440
def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0,
438-
ignoremods=(), ignoredirs=(), infile=None, outfile=None):
441+
ignoremods=(), ignoredirs=(), infile=None, outfile=None,
442+
timing=False):
439443
"""
440444
@param count true iff it should count number of times each
441445
line is executed
@@ -451,6 +455,7 @@ def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0,
451455
@param infile file from which to read stored counts to be
452456
added into the results
453457
@param outfile file in which to write the results
458+
@param timing true iff timing information be displayed
454459
"""
455460
self.infile = infile
456461
self.outfile = outfile
@@ -463,6 +468,9 @@ def __init__(self, count=1, trace=1, countfuncs=0, countcallers=0,
463468
self._calledfuncs = {}
464469
self._callers = {}
465470
self._caller_cache = {}
471+
self.start_time = None
472+
if timing:
473+
self.start_time = time.time()
466474
if countcallers:
467475
self.globaltrace = self.globaltrace_trackcallers
468476
elif countfuncs:
@@ -613,6 +621,8 @@ def localtrace_trace_and_count(self, frame, why, arg):
613621
key = filename, lineno
614622
self.counts[key] = self.counts.get(key, 0) + 1
615623

624+
if self.start_time:
625+
print '%.2f' % (time.time() - self.start_time),
616626
bname = os.path.basename(filename)
617627
print "%s(%d): %s" % (bname, lineno,
618628
linecache.getline(filename, lineno)),
@@ -624,6 +634,8 @@ def localtrace_trace(self, frame, why, arg):
624634
filename = frame.f_code.co_filename
625635
lineno = frame.f_lineno
626636

637+
if self.start_time:
638+
print '%.2f' % (time.time() - self.start_time),
627639
bname = os.path.basename(filename)
628640
print "%s(%d): %s" % (bname, lineno,
629641
linecache.getline(filename, lineno)),
@@ -653,13 +665,13 @@ def main(argv=None):
653665
if argv is None:
654666
argv = sys.argv
655667
try:
656-
opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:lT",
668+
opts, prog_argv = getopt.getopt(argv[1:], "tcrRf:d:msC:lTg",
657669
["help", "version", "trace", "count",
658670
"report", "no-report", "summary",
659671
"file=", "missing",
660672
"ignore-module=", "ignore-dir=",
661673
"coverdir=", "listfuncs",
662-
"trackcalls"])
674+
"trackcalls", "timing"])
663675

664676
except getopt.error, msg:
665677
sys.stderr.write("%s: %s\n" % (sys.argv[0], msg))
@@ -679,6 +691,7 @@ def main(argv=None):
679691
summary = 0
680692
listfuncs = False
681693
countcallers = False
694+
timing = False
682695

683696
for opt, val in opts:
684697
if opt == "--help":
@@ -697,6 +710,10 @@ def main(argv=None):
697710
listfuncs = True
698711
continue
699712

713+
if opt == "-g" or opt == "--timing":
714+
timing = True
715+
continue
716+
700717
if opt == "-t" or opt == "--trace":
701718
trace = 1
702719
continue
@@ -779,7 +796,7 @@ def main(argv=None):
779796
t = Trace(count, trace, countfuncs=listfuncs,
780797
countcallers=countcallers, ignoremods=ignore_modules,
781798
ignoredirs=ignore_dirs, infile=counts_file,
782-
outfile=counts_file)
799+
outfile=counts_file, timing=timing)
783800
try:
784801
t.run('execfile(%r)' % (progname,))
785802
except IOError, err:

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ Core and builtins
441441
Library
442442
-------
443443

444+
- Add a timing parameter when using trace.Trace to print out timestamps.
445+
444446
- #1627: httplib now ignores negative Content-Length headers.
445447

446448
- #900744: If an invalid chunked-encoding header is sent by a server,

0 commit comments

Comments
 (0)