Skip to content

Instantly share code, notes, and snippets.

@sw1nn
Created May 18, 2022 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sw1nn/1d3882a76535497fa7a9d4e07132896f to your computer and use it in GitHub Desktop.
Save sw1nn/1d3882a76535497fa7a9d4e07132896f to your computer and use it in GitHub Desktop.
Repl fns to generate a flame graph. (Not super sophisticated!)
;; Assumes https://github.com/jstepien/flames available
;; requires http://riemann.io to be installed.
(def flames (atom nil))
(defn flames-start! []
;; We resolve explicitly here, to avoid warnings when not working
;; with flamegraphs
(if-not @flames
(let [config {:port 54321, :host "localhost"}]
(reset! flames ((requiring-resolve 'flames.core/start!) config))
(printf "You must have http://riemann.io profiler installed.\nView flames graph at http://%s:%d/flames.svg\n" (:host config) (:port config)))
(throw (IllegalStateException. "Stop the existing capture first!"))))
(defn flames-stop! []
;; We resolve explicitly here, to avoid warnings when not working
;; with flamegraphs
(when @flames
(reset! flames ((requiring-resolve 'flames.core/stop!) @flames))))
(defmacro flames-do
"
Just like (do ...) but capturing a flamegraph whilst executing the body.
(flames-do
(f arg1 arg2)
(g arg3 arg4))
"
[& body]
`(with-open [_# (flames-start!)]
~@body
(println "Capture Flame graph now:\ncurl -sO http://localhost:54321/flames.svg\n\nThen press ENTER")
(read-line)
nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment