viff

changeset 862:14be5568022c

Added a --profile flag. This will make the program dump profiling information when the reactor is stopped. The data for player X is stored in viff-X.prof and can be inspected afterwards with the hotshot.stats module: http://docs.python.org/lib/hotshot-example.html
author Martin Geisler <mg@daimi.au.dk>
date Thu, 31 Jul 2008 19:23:05 +0200
parents f0aa454c9b91
children 5e2a190baf4a
files viff/runtime.py
diffstat 1 files changed, 29 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/viff/runtime.py	Wed Jul 30 22:46:53 2008 +0200
     1.2 +++ b/viff/runtime.py	Thu Jul 31 19:23:05 2008 +0200
     1.3 @@ -417,6 +417,8 @@
     1.4                                 "(if the OpenSSL bindings are available)."))
     1.5          group.add_option("--deferred-debug", action="store_true",
     1.6                           help="Enable extra debug output for deferreds.")
     1.7 +        group.add_option("--profile", action="store_true",
     1.8 +                         help="Collect and print profiling information.")
     1.9  
    1.10          try:
    1.11              # Using __import__ since we do not use the module, we are
    1.12 @@ -429,7 +431,8 @@
    1.13          parser.set_defaults(bit_length=32,
    1.14                              security_parameter=30,
    1.15                              ssl=have_openssl,
    1.16 -                            deferred_debug=False)
    1.17 +                            deferred_debug=False,
    1.18 +                            profile=False)
    1.19  
    1.20      def __init__(self, player, threshold, options=None):
    1.21          """Initialize runtime.
    1.22 @@ -1556,6 +1559,31 @@
    1.23      Please see the example applications for more examples.
    1.24  
    1.25      """
    1.26 +    if options and options.profile:
    1.27 +        # To collect profiling information we monkey patch reactor.run
    1.28 +        # to do the collecting. It would be nicer to simply start the
    1.29 +        # profiler here and stop it upon shutdown, but this triggers
    1.30 +        # http://bugs.python.org/issue1375 since the start and stop
    1.31 +        # calls are in different stack frames.
    1.32 +        import hotshot
    1.33 +        prof = hotshot.Profile("viff-%d.prof" % id)
    1.34 +        old_run = reactor.run
    1.35 +        def new_run(*args, **kwargs):
    1.36 +            print "Starting reactor with profiling"
    1.37 +            prof.runcall(old_run, *args, **kwargs)
    1.38 +
    1.39 +            import sys
    1.40 +            import hotshot.stats
    1.41 +            print "Loading profiling statistics...",
    1.42 +            sys.stdout.flush()
    1.43 +            stats = hotshot.stats.load("viff-%d.prof" % id)
    1.44 +            print "done."
    1.45 +            print
    1.46 +            stats.strip_dirs()
    1.47 +            stats.sort_stats("time", "calls")
    1.48 +            stats.print_stats(40)
    1.49 +        reactor.run = new_run
    1.50 +
    1.51      # This will yield a Runtime when all protocols are connected.
    1.52      result = Deferred()
    1.53