add start/stop functions for the statistics logger
This commit is contained in:
parent
eb55744429
commit
323e7497c3
|
@ -26,23 +26,24 @@
|
||||||
|
|
||||||
(defonce view-system (atom {}))
|
(defonce view-system (atom {}))
|
||||||
|
|
||||||
|
(defonce statistics (atom {}))
|
||||||
|
|
||||||
(def refresh-queue-size
|
(def refresh-queue-size
|
||||||
(if-let [n (:views-refresh-queue-size env)]
|
(if-let [n (:views-refresh-queue-size env)]
|
||||||
(Long/parseLong n)
|
(Long/parseLong n)
|
||||||
1000))
|
1000))
|
||||||
|
|
||||||
(defonce statistics (atom {}))
|
|
||||||
|
|
||||||
(defn reset-stats!
|
(defn reset-stats!
|
||||||
[]
|
[]
|
||||||
(swap! statistics (fn [s] {:enabled (boolean (:enabled s)), :refreshes 0, :dropped 0, :deduplicated 0})))
|
(swap! statistics assoc
|
||||||
|
:refreshes 0
|
||||||
(defn collect-stats? [] (:enabled @statistics))
|
:dropped 0
|
||||||
|
:deduplicated 0))
|
||||||
(reset-stats!)
|
|
||||||
|
|
||||||
|
(defn collect-stats?
|
||||||
|
[]
|
||||||
|
(boolean (:logger @statistics)))
|
||||||
|
|
||||||
(def refresh-queue (ArrayBlockingQueue. refresh-queue-size))
|
(def refresh-queue (ArrayBlockingQueue. refresh-queue-size))
|
||||||
|
|
||||||
|
@ -300,20 +301,44 @@
|
||||||
:refresh-watcher nil
|
:refresh-watcher nil
|
||||||
:workers nil))
|
:workers nil))
|
||||||
|
|
||||||
(defn log-statistics!
|
(defn logger-thread
|
||||||
"Run a thread that logs statistics every msecs."
|
"Returns a logger thread function. A logger periodically writes view system
|
||||||
|
statistics to the log that are collected only when logging is enabled."
|
||||||
[msecs]
|
[msecs]
|
||||||
(swap! statistics assoc-in [:enabled] true)
|
|
||||||
(let [secs (/ msecs 1000)]
|
(let [secs (/ msecs 1000)]
|
||||||
(.start (Thread. (fn []
|
(fn []
|
||||||
(Thread/sleep msecs)
|
(try
|
||||||
(let [stats @statistics]
|
(Thread/sleep msecs)
|
||||||
(reset-stats!)
|
(let [stats @statistics]
|
||||||
(info "subscribed views:" (active-view-count)
|
(reset-stats!)
|
||||||
(format "refreshes/sec: %.1f" (double (/ (:refreshes stats) secs)))
|
(info "subscribed views:" (active-view-count)
|
||||||
(format "dropped/sec: %.1f" (double (/ (:dropped stats) secs)))
|
(format "refreshes/sec: %.1f" (double (/ (:refreshes stats) secs)))
|
||||||
(format "deduped/sec: %.1f" (double (/ (:deduplicated stats) secs))))
|
(format "dropped/sec: %.1f" (double (/ (:dropped stats) secs)))
|
||||||
(recur)))))))
|
(format "deduped/sec: %.1f" (double (/ (:deduplicated stats) secs)))))
|
||||||
|
(catch InterruptedException e))
|
||||||
|
(if-not (:stop? @statistics)
|
||||||
|
(recur)))))
|
||||||
|
|
||||||
|
(defn start-logger!
|
||||||
|
"Starts a logger thread that will enable collection of view statistics
|
||||||
|
which the logger will periodically write out to the log."
|
||||||
|
[log-interval]
|
||||||
|
(if (:logger @statistics)
|
||||||
|
(error "cannot start new logger thread until existing thread is stopped")
|
||||||
|
(let [logger (Thread. ^Runnable (logger-thread log-interval))]
|
||||||
|
(swap! statistics assoc
|
||||||
|
:logger logger
|
||||||
|
:stop? false)
|
||||||
|
(reset-stats!)
|
||||||
|
(.start logger))))
|
||||||
|
|
||||||
|
(defn stop-logger!
|
||||||
|
"Stops the logger thread."
|
||||||
|
[]
|
||||||
|
(swap! statistics assoc :stop? true)
|
||||||
|
(if-let [^Thread logger (:logger @statistics)]
|
||||||
|
(.interrupt logger))
|
||||||
|
(swap! statistics assoc :logger nil))
|
||||||
|
|
||||||
(defn hint
|
(defn hint
|
||||||
"Create a hint."
|
"Create a hint."
|
||||||
|
|
Loading…
Reference in a new issue