Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created June 28, 2010 20:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Chouser/456326 to your computer and use it in GitHub Desktop.
Save Chouser/456326 to your computer and use it in GitHub Desktop.
; 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment