Skip to content

Commit

Permalink
Add athens.tracing dev tool for profiling
Browse files Browse the repository at this point in the history
Added athens.tracing which depends on tufte for profiling CLJS
functions. It's not a great integration, as currently Athens is on an
old version of re-frame-10x that does not have the appropriate
monkeypatchin hooks.

The assumption is that a developer will enable it by uncommenting the
`athens.tracing` in shadow-cljs preloads and checking the results in
console.log.
  • Loading branch information
pithyless committed Mar 9, 2021
1 parent dfdae8f commit 75b1d57
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions project.clj
Expand Up @@ -68,6 +68,7 @@
{:dependencies [[binaryage/devtools "1.0.0"]
[day8.re-frame/re-frame-10x "0.6.0"]
[day8.re-frame/tracing "0.5.3"]
[com.taoensso/tufte "2.2.0"]
[cider/cider-nrepl "0.25.1"]]

:source-paths ["dev"]}
Expand Down
3 changes: 2 additions & 1 deletion shadow-cljs.edn
Expand Up @@ -29,7 +29,8 @@
:closure-defines {re-frame.trace.trace-enabled? true}
:output-feature-set :es-next}
:devtools {:preloads [devtools.preload
day8.re-frame-10x.preload]}}
day8.re-frame-10x.preload
#_athens.tracing]}}

;; backend for electron (node.js)
:main {:target :node-script
Expand Down
62 changes: 62 additions & 0 deletions src/cljs/athens/tracing.cljs
@@ -0,0 +1,62 @@
(ns athens.tracing
(:require
[reagent.impl.batching :as batching]
;; This exists only in newer versions of re-frame-10x
;; [day8.reagent.impl.batching :as day8.batching]
[taoensso.tufte :as tufte]))


(defonce reagent-next-tick batching/next-tick)

;; This exists only in newer version of re-frame-10x:
;; (defonce reframe10x-next-tick day8.batching/next-tick)


(defonce stats-accumulator
(tufte/add-accumulating-handler! {:ns-pattern "*"}))


(defonce stats-total
(atom nil))


(defn format-stats [stats]
(-> stats
(tufte/format-pstats
{:columns [:n-calls :p50 :p95 :p99 :max :clock :total]
:format-id-fn str})
(println)))


(defn next-tick
[f]
;; Schedule a trace to be emitted after a render if
;; there is nothing else scheduled after that render.
;; This signals the end of the epoch.

(reagent-next-tick
;; TODO: ideally, should also be wrapping re-frame-10x:
;; reframe10x-next-tick day8.batching/next-tick
(fn []
(tufte/profile {} (f))
(when (false? (.-scheduled? batching/render-queue))
(when-some [m (not-empty @stats-accumulator)]
(when (some-> m vals first deref :stats)
(let [stats (-> m vals first)]
(println "Epoch Stats:")
(format-stats stats)
(if @stats-total
(swap! stats-total tufte/merge-pstats stats)
(reset! stats-total stats))
(println "Total Stats:")
(format-stats @stats-total))))))))


(defn patch-next-tick
[]
(println "Patching with tufte profiling")
(set! batching/next-tick next-tick))


;; TODO: assuming this namespace will only be loaded via shadow preloads
(patch-next-tick)

0 comments on commit 75b1d57

Please sign in to comment.