🎉 Adding performance logs flag

This commit is contained in:
Alejandro Alonso 2026-01-23 13:34:19 +01:00
parent 5da9bbea62
commit e3148ea20e
2 changed files with 39 additions and 34 deletions

View File

@ -134,6 +134,8 @@
:subscriptions :subscriptions
:subscriptions-old :subscriptions-old
:inspect-styles :inspect-styles
;; Enable performance logs in devconsole (disabled by default)
:perf-logs
;; Security layer middleware that filters request by fetch ;; Security layer middleware that filters request by fetch
;; metadata headers ;; metadata headers

View File

@ -476,23 +476,24 @@
(when (and (some? (.-PerformanceObserver js/window)) (nil? @longtask-observer*)) (when (and (some? (.-PerformanceObserver js/window)) (nil? @longtask-observer*))
(let [observer (js/PerformanceObserver. (let [observer (js/PerformanceObserver.
(fn [list _] (fn [list _]
(doseq [entry (.getEntries list)] (when (contains? cf/flags :perf-logs)
(let [dur (.-duration entry) (doseq [entry (.getEntries list)]
start (.-startTime entry) (let [dur (.-duration entry)
attrib (.-attribution entry) start (.-startTime entry)
attrib-count (when attrib (.-length attrib)) attrib (.-attribution entry)
first-attrib (when (and attrib-count (> attrib-count 0)) (aget attrib 0)) attrib-count (when attrib (.-length attrib))
attrib-name (when first-attrib (.-name first-attrib)) first-attrib (when (and attrib-count (> attrib-count 0)) (aget attrib 0))
attrib-ctype (when first-attrib (.-containerType first-attrib)) attrib-name (when first-attrib (.-name first-attrib))
attrib-cid (when first-attrib (.-containerId first-attrib)) attrib-ctype (when first-attrib (.-containerType first-attrib))
attrib-csrc (when first-attrib (.-containerSrc first-attrib))] attrib-cid (when first-attrib (.-containerId first-attrib))
attrib-csrc (when first-attrib (.-containerSrc first-attrib))]
(.warn js/console (str "[perf] long task " (Math/round dur) "ms at " (Math/round start) "ms" (.warn js/console (str "[perf] long task " (Math/round dur) "ms at " (Math/round start) "ms"
(when first-attrib (when first-attrib
(str " attrib:name=" attrib-name (str " attrib:name=" attrib-name
" ctype=" attrib-ctype " ctype=" attrib-ctype
" cid=" attrib-cid " cid=" attrib-cid
" csrc=" attrib-csrc))))))))] " csrc=" attrib-csrc)))))))))]
(.observe observer #js{:entryTypes #js["longtask"]}) (.observe observer #js{:entryTypes #js["longtask"]})
(reset! longtask-observer* observer)))) (reset! longtask-observer* observer))))
@ -505,28 +506,30 @@
(let [last (atom (.now js/performance)) (let [last (atom (.now js/performance))
id (js/setInterval id (js/setInterval
(fn [] (fn []
(let [now (.now js/performance) (when (contains? cf/flags :perf-logs)
expected (+ @last interval-ms) (let [now (.now js/performance)
drift (- now expected) expected (+ @last interval-ms)
current-op @current-op* drift (- now expected)
measures (.getEntriesByType js/performance "measure") current-op @current-op*
mlen (.-length measures) measures (.getEntriesByType js/performance "measure")
last-measure (when (> mlen 0) (aget measures (dec mlen))) mlen (.-length measures)
meas-name (when last-measure (.-name last-measure)) last-measure (when (> mlen 0) (aget measures (dec mlen)))
meas-detail (when last-measure (.-detail last-measure)) meas-name (when last-measure (.-name last-measure))
meas-count (when meas-detail (unchecked-get meas-detail "count"))] meas-detail (when last-measure (.-detail last-measure))
(reset! last now) meas-count (when meas-detail (unchecked-get meas-detail "count"))]
(when (> drift threshold-ms) (reset! last now)
(.warn js/console (when (> drift threshold-ms)
(str "[perf] event loop stall: " (Math/round drift) "ms" (.warn js/console
(when current-op (str " op=" current-op)) (str "[perf] event loop stall: " (Math/round drift) "ms"
(when meas-name (str " last=" meas-name)) (when current-op (str " op=" current-op))
(when meas-count (str " count=" meas-count))))))) (when meas-name (str " last=" meas-name))
(when meas-count (str " count=" meas-count))))))))
interval-ms)] interval-ms)]
(reset! stall-timer* id)))) (reset! stall-timer* id))))
(defn init! (defn init!
"Install perf observers in dev builds. Safe to call multiple times." "Install perf observers in dev builds. Safe to call multiple times.
Perf logs are disabled by default. Enable them with the :perf-logs flag in config."
[] []
(when ^boolean js/goog.DEBUG (when ^boolean js/goog.DEBUG
(install-long-task-observer!) (install-long-task-observer!)