Profiling example

Do we have a wiki where we can put snippets of code? Or perhaps someone has time to format this as part of the docs? I wanted to run a profiler last night, and it looks like a piece of example code would be helpful.

#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))))
4 Likes

How about the Artifacts page on the racket wiki ?

1 Like

Brilliant. Done. Now we just need the documentation search to search through the code in the artifacts, too...

1 Like

Probably easier to make an artifacts package - similar to Syntax Parse Examples

Oh! That's an interesting idea. A package that no one would actually install, but it's there for the docs? Actually... I think you've just condensed the highlight on an earlier idea: I'm not aware of a code search for Racket that would allow you to find all uses of a particular function in our code base. You could use syntax expansion to do a really careful job, or you could just use text indexing and get it done quickly. This is another of those projects that could be really useful but hasn't gotten done....

Would that count as an intro project?

This seems very similar to the raco profile command.

1 Like

Not exactly what you want, but on GitHub it is possible to add language:racket to the search and get racket only results.

2 Likes

Yes. I quite successfully used raco profile in the past and that wasn't nearly as complicated as in the code snippet. :slight_smile:

That said, I don't know whether there are situations where the above code would work, but not raco profile.

1 Like

Oh dear heaven. How embarrassing. Yep, didn't see that at all. It looks to me like I searched for "profiling" rather than "profile", which leads to completely different search results. I should try to add the search term "profiling" to the docs for raco profile. Sigh.