Profiling - racket/racket GitHub Wiki

This is a very short example file, showing a simple example of profiling.

#lang racket

;; this is a minimum-viable example of how to profile a
;; racket program. The target program should be defined in its own
;; module, in its own file.

;; running this program will destroy whatever's in
;; /tmp/profiler-results.txt.

(require errortrace)

;; note: you should probably only run this at the command-line
;; (that is, not in DrRacket), to remove additional sources
;; of randomness, and you should delete any compiled files,
;; as described by the documentation.

(profiling-enabled #t)

;; optional, nice for a long-running and silent process.
(printf "starting...\n")
(flush-output)

;; the target file is in the same directory, in this
;; example. 
(dynamic-require "my-target-file.rkt" #f)

(define p (get-profile-results))

;; syntax objects have long printed representations.
;; this extracts just the path, line, and column.
;; there's a lot more information you might want,
;; but this is probably enough for some uses.
(define (syntax->printable stx)
  (list (path->string (syntax-source stx))
        (syntax-line stx)
        (syntax-column stx)))


(define printable
  (for/list ([tup (in-list p)])
    (match-define (list a b c stx d) tup)
    (list a b c (syntax->printable stx) d)))


(call-with-output-file "/tmp/profiler-results.txt"
  #:exists 'truncate
  (λ (port)
    (for ((p (in-list printable)))
      (writeln p port))))