Merge pull request #33 from aperiodic/profiling-fixes

Compile->runtime profiling keyword names (aperiodic)
This commit is contained in:
Peter Taoussanis 2013-10-21 22:57:26 -07:00
commit a29879d1f9
1 changed files with 15 additions and 17 deletions

View File

@ -26,13 +26,12 @@
the raw profiling stats under a special :profiling-stats key (useful for the raw profiling stats under a special :profiling-stats key (useful for
queryable db logging)." queryable db logging)."
[level name & body] [level name & body]
(let [name (utils/fq-keyword name)]
`(let [{result# :result stats# :stats} (with-pdata ~level ~@body)] `(let [{result# :result stats# :stats} (with-pdata ~level ~@body)]
(when stats# (when stats#
(timbre/log* print-str ~level {:profile-stats stats#} (timbre/log* print-str ~level {:profile-stats stats#}
(str "Profiling " ~name) (str "Profiling " (utils/fq-keyword ~name))
(str "\n" (format-pdata stats#)))) (str "\n" (format-pdata stats#))))
result#))) result#))
(defmacro sampling-profile (defmacro sampling-profile
"Like `profile`, but only enables profiling every 1/`proportion` calls. "Like `profile`, but only enables profiling every 1/`proportion` calls.
@ -46,16 +45,15 @@
"Profile spy. When in the context of a *pdata* binding, records execution time "Profile spy. When in the context of a *pdata* binding, records execution time
of named body. Always returns the body's result." of named body. Always returns the body's result."
[name & body] [name & body]
(let [name (utils/fq-keyword name)]
`(if-not *pdata* `(if-not *pdata*
(do ~@body) (do ~@body)
(let [name# ~name (let [name# (utils/fq-keyword ~name)
start-time# (System/nanoTime)] start-time# (System/nanoTime)]
(try (try
(do ~@body) (do ~@body)
(finally (finally
(let [elapsed# (- (System/nanoTime) start-time#)] (let [elapsed# (- (System/nanoTime) start-time#)]
(swap! *pdata* #(assoc % name# (conj (% name# []) elapsed#)))))))))) (swap! *pdata* #(assoc % name# (conj (% name# []) elapsed#)))))))))
(defmacro p [name & body] `(pspy ~name ~@body)) ; Alias (defmacro p [name & body] `(pspy ~name ~@body)) ; Alias