# How to use the profiler?

**URL:** <https://racket.discourse.group/t/how-to-use-the-profiler/1345>\
**Category:** Questions & Answers\
**Created:** [October 3, 2022, 7:57pm UTC](https://racket.discourse.group/t/how-to-use-the-profiler/1345 "2022-10-03T19:57:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [October 3, 2022, 7:57pm UTC](https://racket.discourse.group/t/how-to-use-the-profiler/1345/1 "2022-10-03T19:57:43Z")

</div>

I'm looking at the docs on the [profiler](https://docs.racket-lang.org/profile/index.html#%28part._sampler%29) 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?

```scheme

(require profile
         profile/render-graphviz
         simple-qr
         )

(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.)

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [October 3, 2022, 8:55pm UTC](https://racket.discourse.group/t/how-to-use-the-profiler/1345/2 "2022-10-03T20:55:02Z")

</div>

I think you want to write something like this:

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

```

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

---

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [October 4, 2022, 3:25pm UTC](https://racket.discourse.group/t/how-to-use-the-profiler/1345/3 "2022-10-04T15:25:48Z")

</div>

Cool, thank you.

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

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [October 4, 2022, 8:08pm UTC](https://racket.discourse.group/t/how-to-use-the-profiler/1345/4 "2022-10-04T20:08:40Z")

</div>

Here's the sampler used in the default behavior: [profile/main.rkt at master · racket/profile · GitHub](https://github.com/racket/profile/blob/master/profile-lib/main.rkt#L25-L29)

---

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [October 4, 2022, 8:35pm UTC](https://racket.discourse.group/t/how-to-use-the-profiler/1345/5 "2022-10-04T20:35:29Z")

</div>

Thanks, that helps.

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