08 January 2009

Profiling Using MzScheme and Errortrace

Lately I've needed to do some profiling in PLT Scheme, and the usual process in DrScheme hasn't been working for me. If you don't know, here's the usual process:

  1. Language --> Choose Language
  2. Show Details
  3. Debugging and Profiling
  4. Run Program
  5. View --> Show Profile
Robby suggested using the errortrace library directly from mzscheme. It worked perfectly. Here's how you can duplicate my success. (More information is available in the errortrace docs.)

First, get your program (or the parts of it you want to profile) in shape to be run easily from the REPL. I already have a run-tests.ss module which runs all my tests and exercises the code in a pretty realistic way, so I just entered (require "run-tests.ss"); you should do something similar. Then:

  1. Fire up mzscheme:
    mzscheme -i -l errortrace
    The -i tells mzscheme to start in interpreter mode; the -l errortrace says "load errortrace first thing".
  2. Set a couple of parameters for errortrace:
    (instrumenting-enabled #t)
    (profiling-enabled #t)
    (profiling-record-enabled #t)
  3. Run your program. For example:
    (require "run-tests.ss")
  4. Output the profile information:
    (output-profile-results #t #t)
    (with-output-to-file "profile.dat"
      (lambda () (output-profile-results #t #t)))
    The two #ts tell errortrace to output the path-to-function-source information and to sort in terms of total time taken.
And that's it! I now know where my slow program is spending its time (and I think I even understand why). Fixing it is another matter, though....