mirror of
https://github.com/penpot/penpot.git
synced 2025-05-12 19:26:38 +02:00
✨ More improvements to perf namespace.
This commit is contained in:
parent
032252469b
commit
a91a8401d6
2 changed files with 76 additions and 10 deletions
17
frontend/src/uxbox/util/perf.clj
Normal file
17
frontend/src/uxbox/util/perf.clj
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
;;
|
||||||
|
;; Copyright (c) 2020 UXBOX Labs SL
|
||||||
|
|
||||||
|
(ns uxbox.util.perf
|
||||||
|
"Performance profiling for react components.")
|
||||||
|
|
||||||
|
(defmacro with-measure
|
||||||
|
[name & body]
|
||||||
|
`(let [start# (uxbox.util.perf/timestamp)
|
||||||
|
res# (do ~@body)
|
||||||
|
end# (uxbox.util.perf/timestamp)]
|
||||||
|
(uxbox.util.perf/register-measure ~name (- end# start#))
|
||||||
|
res#))
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
(ns uxbox.util.perf
|
(ns uxbox.util.perf
|
||||||
"Performance profiling for react components."
|
"Performance profiling for react components."
|
||||||
|
(:require-macros [uxbox.util.perf])
|
||||||
(:require [uxbox.util.math :as math]
|
(:require [uxbox.util.math :as math]
|
||||||
[rumext.alpha :as mf]
|
[rumext.alpha :as mf]
|
||||||
[goog.functions :as f]
|
[goog.functions :as f]
|
||||||
|
@ -35,29 +36,77 @@
|
||||||
;; (println (str "[perf|" ~name "] => " time#))
|
;; (println (str "[perf|" ~name "] => " time#))
|
||||||
;; res#)))
|
;; res#)))
|
||||||
|
|
||||||
|
(defn tdigest
|
||||||
|
[]
|
||||||
|
(specify! (td/TDigest.)
|
||||||
|
ITransientCollection
|
||||||
|
(-conj! [this n]
|
||||||
|
(.push this n)
|
||||||
|
this)
|
||||||
|
|
||||||
|
(-persistent! [this]
|
||||||
|
this)))
|
||||||
|
|
||||||
|
(defn timestamp
|
||||||
|
[]
|
||||||
|
(js/performance.now))
|
||||||
|
|
||||||
|
(def registry (js/Map.))
|
||||||
|
|
||||||
|
(def register-measure
|
||||||
|
(let [insert!
|
||||||
|
(fn [name measure]
|
||||||
|
(let [td (.get registry name)]
|
||||||
|
(if td
|
||||||
|
(conj! td measure)
|
||||||
|
(.set registry name (conj! (tdigest) measure)))))
|
||||||
|
|
||||||
|
print-single-summary!
|
||||||
|
(fn [name td]
|
||||||
|
(js/console.log (str "[measure: " name "] "
|
||||||
|
"samples=" (unchecked-get td "n") "\n"
|
||||||
|
"Q50=" (.percentile td 0.50) "\n"
|
||||||
|
"Q75=" (.percentile td 0.75) "\n"
|
||||||
|
"Q95=" (.percentile td 0.90) "\n"
|
||||||
|
"MAX=" (.percentile td 1))))
|
||||||
|
print-summary!
|
||||||
|
(f/debounce
|
||||||
|
#(.forEach registry (fn [td name] (print-single-summary! name td)))
|
||||||
|
500)]
|
||||||
|
(fn [name measure]
|
||||||
|
(insert! name measure)
|
||||||
|
(print-summary!))))
|
||||||
|
|
||||||
|
(defn mesurable
|
||||||
|
[name f]
|
||||||
|
(fn [& args]
|
||||||
|
(uxbox.util.perf/with-measure name
|
||||||
|
(apply f args))))
|
||||||
|
|
||||||
|
|
||||||
(defn on-render-factory
|
(defn on-render-factory
|
||||||
[label]
|
[label]
|
||||||
(let [buf (td/TDigest.)
|
(let [td (tdigest)
|
||||||
log (f/debounce
|
log (f/debounce
|
||||||
(fn [phase buf]
|
(fn [phase td]
|
||||||
(js/console.log (str "[profile: " label " (" phase ")] "
|
(js/console.log (str "[profile: " label " (" phase ")] "
|
||||||
"samples=" (unchecked-get buf "n") "\n"
|
"samples=" (unchecked-get td "n") "\n"
|
||||||
"Q50=" (.percentile buf 0.50) "\n"
|
"Q50=" (.percentile td 0.50) "\n"
|
||||||
"Q75=" (.percentile buf 0.75) "\n"
|
"Q75=" (.percentile td 0.75) "\n"
|
||||||
"Q95=" (.percentile buf 0.90) "\n"
|
"Q95=" (.percentile td 0.90) "\n"
|
||||||
"MAX=" (.percentile buf 1))))
|
"MAX=" (.percentile td 1))))
|
||||||
300)]
|
300)]
|
||||||
(fn [id phase adur, bdur, st, ct, itx]
|
(fn [id phase adur, bdur, st, ct, itx]
|
||||||
(.push buf adur)
|
(conj! td adur)
|
||||||
(log phase buf))))
|
(log phase td))))
|
||||||
|
|
||||||
(mf/defc profiler
|
(mf/defc profiler
|
||||||
{::mf/wrap-props false}
|
{::mf/wrap-props false}
|
||||||
[props]
|
[props]
|
||||||
(let [children (unchecked-get props "children")
|
(let [children (unchecked-get props "children")
|
||||||
label (unchecked-get props "label")
|
label (unchecked-get props "label")
|
||||||
enabled? (or (unchecked-get props "enabled") true)
|
enabled? (unchecked-get props "enabled")
|
||||||
|
enabled? (if (nil? enabled?) true enabled?)
|
||||||
on-render (mf/use-memo
|
on-render (mf/use-memo
|
||||||
(mf/deps label)
|
(mf/deps label)
|
||||||
#(on-render-factory label))]
|
#(on-render-factory label))]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue