mirror of
https://github.com/penpot/penpot.git
synced 2025-03-11 23:31:21 -05:00
🎉 Add benchmark
helper function (cljs only).
This commit is contained in:
parent
0f7166d34a
commit
73042115e0
1 changed files with 31 additions and 0 deletions
|
@ -19,3 +19,34 @@
|
|||
(defn measure
|
||||
[key]
|
||||
(- (timestamp) (get @measures key)))
|
||||
|
||||
#?(:cljs
|
||||
(defn benchmark
|
||||
"A helper function for perform a unitari benchmark on JS/CLJS. It
|
||||
uses browser native api so it only suitable to be executed in
|
||||
browser."
|
||||
[& {:keys [f warmup iterations name]
|
||||
:or {iterations 10000}}]
|
||||
(let [end-mark (str name ":end")]
|
||||
(println "=> benchmarking:" name)
|
||||
(println "--> warming up:" iterations)
|
||||
(loop [i iterations]
|
||||
(when (pos? i)
|
||||
(f)
|
||||
(recur (dec i))))
|
||||
(println "--> benchmarking:" iterations)
|
||||
(js/performance.mark name)
|
||||
(loop [i iterations]
|
||||
(when (pos? i)
|
||||
(f)
|
||||
(recur (dec i))))
|
||||
(js/performance.measure end-mark name)
|
||||
(let [[result] (js/performance.getEntriesByName end-mark)
|
||||
duration (mth/precision (.-duration ^js result) 6)
|
||||
avg (mth/precision (/ duration iterations) 6)]
|
||||
(println "--> TOTAL:" (str duration "ms") "AVG:" (str avg "ms"))
|
||||
(js/performance.clearMarks name)
|
||||
(js/performance.clearMeasures end-mark)
|
||||
#js {:duration duration
|
||||
:avg avg}))))
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue