5353import re
5454import sys
5555import threading
56+ import time
5657import token
5758import tokenize
5859import 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
102105Filters, 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
436439class 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 :
0 commit comments