Profile Clojure Step by Step

Posted on Apr 8, 2022

Profile in CodeScene with async-profiler:

  1. Check that the following is set to 1:
    cat /proc/sys/kernel/perf_event_paranoid

  2. To set it do this:
    echo 1 | sudo tee /proc/sys/kernel/perf_event_paranoid

  3. Add the following to your project.clj (if you’re using leinigen):

:async-profiler {:jvm-opts ["-Djdk.attach.allowAttachSelf" "-XX:+UnlockDiagnosticVMOptions" "-XX:+DebugNonSafepoints"]
                 :dependencies [[com.clojure-goes-fast/clj-async-profiler "1.0.0-alpha1"]]}
  1. Require the profiler in your ns, and add (proc/profile) around the interesting code
    (require '[clj-async-profiler.core :as prof])

;; Profile your expression:
(prof/profile (dotimes [i 10000] (reduce + (range i))))

  1. Inspect the result:
  • The resulting flamegraph will be stored in /tmp/clj-async-profiler/results/
  • You can view the SVG directly from there or start a local webserver:
    (prof/serve-files 8080)