Skip to content

Instantly share code, notes, and snippets.

@Chouser
Forked from fogus/ref-history-stress.clj
Created August 13, 2010 02:10
Show Gist options
  • Save Chouser/522128 to your computer and use it in GitHub Desktop.
Save Chouser/522128 to your computer and use it in GitHub Desktop.
; STM history stress-test
(defn stress-ref [r]
(let [slow-tries (atom 0)]
(future
(dosync
(swap! slow-tries inc)
(Thread/sleep 200)
@r)
(println (format "r is: %s, history: %d, after: %d tries"
@r (.getHistoryCount r) @slow-tries)))
(dotimes [i 500]
(Thread/sleep 10)
(dosync (alter r inc)))
:done))
(stress-ref (ref 0)) ; default ref settings :min-history 0 :max-history 10
;=> :done
; a is: 500 history: 10 after: 26 tries
; This indicates that the slow transaction never succeeded until the dotimes
; loop was complete (and returned :done)
(stress-ref (ref 0 :max-history 30))
; a is: 415 history: 19 after: 21 tries
;=> :done
; Here the slow transaction completed first, after growing the history to 19.
; Success! But it took 21 tries to grow the history from 0, so we can help out
; by starting with a larger min-history:
(stress-ref (ref 0 :min-history 15 :max-history 30))
; a is: 119 history: 20 after: 6 tries
;=> :done
; Only 6 retries of the slow transaction this time, enough to grow the history
; from 15 to 20.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment