# Profiling example

**URL:** https://racket.discourse.group/t/profiling-example/954
**Category:** Show & Tell
**Created:** [May 2, 2022, 3:02pm UTC](https://racket.discourse.group/t/profiling-example/954 "2022-05-02T15:02:45Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)
#### Post date: [May 2, 2022, 3:02pm UTC](https://racket.discourse.group/t/profiling-example/954/1 "2022-05-02T15:02:45Z")

</div>

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.

```scheme
#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))))

```

---

<div class="post-metadata">

### Author: ![spdegabrielle](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/spdegabrielle/32/95_2.png) [@spdegabrielle](https://racket.discourse.group/u/spdegabrielle)
#### Post date: [May 2, 2022, 3:18pm UTC](https://racket.discourse.group/t/profiling-example/954/2 "2022-05-02T15:18:03Z")

</div>

How about the Artifacts page on the racket wiki ?

> **[Artifacts · racket/racket Wiki](https://github.com/racket/racket/wiki/Artifacts)**
>
> The Racket repository. Contribute to racket/racket development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)
#### Post date: [May 2, 2022, 5:01pm UTC](https://racket.discourse.group/t/profiling-example/954/3 "2022-05-02T17:01:19Z")

</div>

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

---

<div class="post-metadata">

### Author: ![spdegabrielle](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/spdegabrielle/32/95_2.png) [@spdegabrielle](https://racket.discourse.group/u/spdegabrielle)
#### Post date: [May 2, 2022, 5:04pm UTC](https://racket.discourse.group/t/profiling-example/954/4 "2022-05-02T17:04:02Z")

</div>

Probably easier to make an artifacts package - similar to [Syntax Parse Examples](https://docs.racket-lang.org/syntax-parse-example/index.html)

> **[GitHub - bennn/syntax-parse-example: Collection of syntax/parse macros](https://github.com/bennn/syntax-parse-example)**
>
> Collection of syntax/parse macros. Contribute to bennn/syntax-parse-example development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)
#### Post date: [May 2, 2022, 5:17pm UTC](https://racket.discourse.group/t/profiling-example/954/5 "2022-05-02T17:17:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![spdegabrielle](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/spdegabrielle/32/95_2.png) [@spdegabrielle](https://racket.discourse.group/u/spdegabrielle)
#### Post date: [May 2, 2022, 5:33pm UTC](https://racket.discourse.group/t/profiling-example/954/6 "2022-05-02T17:33:15Z")

</div>

Would that count as an intro project?

> **[Intro Projects · racket/racket Wiki](https://github.com/racket/racket/wiki/Intro-Projects)**
>
> The Racket repository. Contribute to racket/racket development by creating an account on GitHub.

---

<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: [May 2, 2022, 6:28pm UTC](https://racket.discourse.group/t/profiling-example/954/7 "2022-05-02T18:28:30Z")

</div>

This seems very similar to the `raco profile` command.

---

<div class="post-metadata">

### Author: ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)
#### Post date: [May 2, 2022, 6:30pm UTC](https://racket.discourse.group/t/profiling-example/954/8 "2022-05-02T18:30:28Z")

</div>

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

---

<div class="post-metadata">

### Author: ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)
#### Post date: [May 2, 2022, 9:42pm UTC](https://racket.discourse.group/t/profiling-example/954/9 "2022-05-02T21:42:37Z")

</div>

Yes. I quite successfully used [raco profile](https://docs.racket-lang.org/profile/index.html) in the past and that wasn't nearly as complicated as in the code snippet. 🙂

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

---

<div class="post-metadata">

### Author: ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)
#### Post date: [May 3, 2022, 5:18am UTC](https://racket.discourse.group/t/profiling-example/954/10 "2022-05-03T05:18:01Z")

</div>

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.
