public
Last active

  • Download Gist
ref-history-stress.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
; STM history stress-test
 
(defn stress [hmin hmax]
(let [r (ref 0 :min-history hmin :max-history hmax)
slow-tries (atom 0)]
(future
(dosync
(swap! slow-tries inc)
(Thread/sleep 200)
@r)
(println "a is:" @r "history:" (.getHistoryCount r)
"after:" @slow-tries "tries"))
(dotimes [i 500]
(Thread/sleep 10)
(dosync (alter r inc)))
:done))
 
 
(stress 0 10) ; default ref settings
;=> :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 0 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 15 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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.