Skip to content

Commit

Permalink
move statistics out of it's own atom into view-system
Browse files Browse the repository at this point in the history
  • Loading branch information
gered committed May 28, 2016
1 parent 24646b1 commit f857edd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
25 changes: 12 additions & 13 deletions src/views/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,19 @@

(defonce view-system (atom {}))

(defonce statistics (atom {}))



(defn reset-stats!
[]
(swap! statistics assoc
(swap! view-system update-in [:statistics] assoc
:refreshes 0
:dropped 0
:deduplicated 0))

(defn collect-stats?
[]
(boolean (:logger @statistics)))
(boolean (get-in @view-system [:statistics :logger])))

(defn ->view-sig
([namespace view-id parameters]
Expand Down Expand Up @@ -199,10 +198,10 @@
(if (relevant? v namespace parameters hints)
(if-not (.contains refresh-queue view-sig)
(when-not (.offer refresh-queue view-sig)
(when (collect-stats?) (swap! statistics update-in [:dropped] inc))
(when (collect-stats?) (swap! view-system update-in [:statistics :dropped] inc))
(error "refresh-queue full, dropping refresh request for" view-sig))
(do
(when (collect-stats?) (swap! statistics update-in [:deduplicated] inc))
(when (collect-stats?) (swap! view-system update-in [:statistics :deduplicated] inc))
(trace "already queued for refresh" view-sig))))
(catch Exception e
(error e "error determining if view is relevant" view-sig))))))
Expand Down Expand Up @@ -245,7 +244,7 @@

(defn do-view-refresh!
[{:keys [namespace view-id parameters] :as view-sig}]
(if (collect-stats?) (swap! statistics update-in [:refreshes] inc))
(if (collect-stats?) (swap! view-system update-in [:statistics :refreshes] inc))
(try
(let [view (get-in @view-system [:views view-id])
vdata (data view namespace parameters)
Expand Down Expand Up @@ -342,25 +341,25 @@
(fn []
(try
(Thread/sleep msecs)
(let [stats @statistics]
(let [stats (:statistics @view-system)]
(reset-stats!)
(info "subscribed views:" (active-view-count)
(format "refreshes/sec: %.1f" (double (/ (:refreshes stats) secs)))
(format "dropped/sec: %.1f" (double (/ (:dropped stats) secs)))
(format "deduped/sec: %.1f" (double (/ (:deduplicated stats) secs)))))
(catch InterruptedException e))
(if-not (:stop? @statistics)
(if-not (get-in @view-system [:statistics :stop?])
(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]
(trace "starting logger. logging at" log-interval "secs intervals")
(if (:logger @statistics)
(if (get-in @view-system [:statistics :logger])
(error "cannot start new logger thread until existing thread is stopped")
(let [logger (Thread. ^Runnable (logger-thread log-interval))]
(swap! statistics assoc
(swap! view-system update-in [:statistics] assoc
:logger logger
:stop? false)
(reset-stats!)
Expand All @@ -370,11 +369,11 @@
"Stops the logger thread."
[& [wait-for-thread?]]
(trace "stopping logger")
(let [^Thread logger-thread (:logger @statistics)]
(swap! statistics assoc :stop? true)
(let [^Thread logger-thread (get-in @view-system [:statistics :logger])]
(swap! view-system assoc-in [:statistics :stop?] true)
(if logger-thread (.interrupt logger-thread))
(if wait-for-thread? (.join logger-thread))
(swap! statistics assoc :logger nil)))
(swap! view-system assoc-in [:statistics :logger] nil)))

(defn hint
"Create a hint."
Expand Down
8 changes: 3 additions & 5 deletions test/views/basic_system_init_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

(defn reset-state-fixture! [f]
(reset! view-system {})
(reset! statistics {})
(f))

(use-fixtures :each reset-state-fixture!)
Expand Down Expand Up @@ -43,7 +42,6 @@
; 2. shutdown views (and wait for all threads to also finish)
(shutdown! true)
(is (empty? @view-system))
(is (empty? @statistics))
(is (not (.isAlive ^Thread refresh-watcher)))
(doseq [^Thread t workers]
(is (not (.isAlive t)))))))
Expand All @@ -53,14 +51,14 @@
(assoc :stats-log-interval 10000))]
; 1. init views
(init! views dummy-send-fn options)
(is (seq @statistics))
(is (seq (:statistics @view-system)))
(is (:logging? @view-system))
(is (collect-stats?))
(let [logger-thread (:logger @statistics)]
(let [logger-thread (get-in @view-system [:statistics :logger])]
(is (.isAlive ^Thread logger-thread))
; 2. shutdown views
(shutdown! true)
(is (nil? (:logger @statistics)))
(is (nil? (get-in @view-system [:statistics :logger])))
(is (not (.isAlive ^Thread logger-thread))))))

(deftest can-add-new-views-after-init
Expand Down
9 changes: 4 additions & 5 deletions test/views/hint_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

(defn reset-system-fixture [f]
(reset! view-system {})
(reset! statistics {})
(f)
(shutdown! true))

Expand Down Expand Up @@ -232,13 +231,13 @@
(let [subscribe-result (subscribe! view-sig subscriber-key nil)]
; 4. block until subscription finishes
(while (not (realized? subscribe-result)))
(is (= 0 (:deduplicated @statistics)))
(is (= 0 (get-in @view-system [:statistics :deduplicated])))
; 5. add duplicate hints by changing the same set of data twice
; (hints will stay in the queue forever because we stopped the worker threads)
(memory-db-assoc-in! :a [:foo] 6)
(memory-db-assoc-in! :a [:foo] 7)
(wait-for-refresh-views)
(is (= 1 (:deduplicated @statistics)))
(is (= 1 (get-in @view-system [:statistics :deduplicated])))
(is (= [view-sig]
(vec (:refresh-queue @view-system)))))))

Expand All @@ -265,12 +264,12 @@
; 4. block until subscription finishes
(while (or (not (realized? subscribe-a))
(not (realized? subscribe-b))))
(is (= 0 (:dropped @statistics)))
(is (= 0 (get-in @view-system [:statistics :dropped])))
; 5. change some data affecting the subscribed view, resulting in more then 1 hint
; being added to the refresh queue
(memory-db-assoc-in! :a [:foo] 101010)
(memory-db-assoc-in! :b [:foo] 010101)
(wait-for-refresh-views)
(is (= 1 (:dropped @statistics)))
(is (= 1 (get-in @view-system [:statistics :dropped])))
(is (= [view-sig-a]
(vec (:refresh-queue @view-system))))))))
1 change: 0 additions & 1 deletion test/views/subscription_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

(defn reset-system-fixture [f]
(reset! view-system {})
(reset! statistics {})
(f)
(shutdown! true))

Expand Down

0 comments on commit f857edd

Please sign in to comment.