How to use the profiler?

I'm looking at the docs on the profiler and having some trouble. What I see is the following:

  • profile will accept some expressions, run them, gather timing data, analyze it, and render it. It's a macro wrapper around profile-thunk, which does the same end-to-end profiling but takes a thunk.
  • create-sampler accepts a thread, a custodian, or a list thereof. It will return a function that is able to gather data in response to messages. No idea what the difference is between passing a thread versus a custodian, why I would do one over the other, or what the implications are. If I pass a thread, maybe I should start off with the thread suspended so that the timing data will be accurate? Maybe it doesn't matter? Dunno.
  • analyze-samples accepts profile data and analyzes it. The profile data comes from...somewhere? Presumably create-sampler is involved but I have no idea how. It emits a profile struct.
  • render and its variants take a profile struct that was generated by analyze-samples and spit out the encapsulated information in the desired representation (text, JSON, graphviz, etc).

I've googled around but cannot find any examples of the profiler that clear up my confusion.

Here's some sample code. If I want to run this in the profiler and then spit the results out in, for example, graphviz, how do I do that?

(require profile

(define png-file "out.png")
(define str (port->string (open-input-file "./sample-text.txt")))
(when (file-exists? png-file) (delete-file png-file))

(profile-thunk (thunk (qr-write str png-file #:error_level "L")))

;; A bunch of text-based data comes pouring forth.  Excellent.  Still, it's a little hard      
;; to follow and I'd like to be able to use the hide-* arguments to streamline what gets       
;; shown.  Let's try again.                                                                    

(when (file-exists? png-file) (delete-file png-file))
; (define results ...gather the profile data here...)                                          
; (define prof (analyze-samples results))                                                      
; (render prof #:hide-self 5/100) ; should produce a graphviz as render-graphviz is required   

(NB: I recognize that the graphviz renderer is marked experimental.)


I think you want to write something like this:

(require (prefix-in g: profile/render-graphviz))
  (qr-write ...)
  #:render (lambda (a b) (g:render #:hide-self .05 a b)))

That will print the results as a graphviz graph on stdout.

Cool, thank you.

How does create-sampler work? Are there examples of it anywhere that I could look at?

Here's the sampler used in the default behavior: profile/main.rkt at master · racket/profile · GitHub

Thanks, that helps.

Also, I note that posts need to be 20 characters so allow me to appease our software overlords.