-
-
Save Chouser/522128 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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