Skip to content

Instantly share code, notes, and snippets.

@jiacai2050
Created April 29, 2018 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiacai2050/082a05394ac4ad8f59953d95519740d5 to your computer and use it in GitHub Desktop.
Save jiacai2050/082a05394ac4ad8f59953d95519740d5 to your computer and use it in GitHub Desktop.
(ns demo.specter-benchmark
(:use [com.rpl.specter]
[criterium.core]))
;; one big app state, look like
;; {app-id1 {peer-id1 #{ch-di1}}}
(def clients (atom {}))
(def num-app 10)
(def num-peer 20)
(def num-channel 5)
(declare rand-id)
(def apps (vec (take num-app (repeatedly #(str (rand-id) "-appid")))))
(def peers (vec (take num-peer (repeatedly #(str (rand-id) "-peerid")))))
(def channels (vec (take num-channel (repeatedly #(str (rand-id) "-chid")))))
(def login-tuple (vec (for [app-id apps
peer-id peers
channel-id channels]
[app-id peer-id channel-id])))
(defn bench-specter []
(doseq [[app-id peer-id channel-id] login-tuple]
(setval [ATOM app-id peer-id NIL->SET (subset #{})]
#{channel-id}
clients)))
(def conj-set (fnil conj #{}))
(defn bench-vanilla-clj []
(doseq [[app-id peer-id channel-id] login-tuple]
(swap! clients update-in [app-id peer-id] conj-set channel-id)))
(defn -main [& args]
(reset! clients {})
(with-progress-reporting (quick-bench (bench-vanilla-clj)))
(reset! clients {})
(with-progress-reporting (quick-bench (bench-specter)))
)
(def rand-id-seed
(mapv char (concat (range 48 58)
(range 65 90)
(range 97 123))))
(defn rand-id
([] (rand-id 10))
([len]
(clojure.string/join (take len (repeatedly #(rand-nth rand-id-seed))))))
@jiacai2050
Copy link
Author

vanilla-clj

Evaluation count : 888 in 6 samples of 148 calls.
             Execution time mean : 681.139939 µs
    Execution time std-deviation : 15.364454 µs
   Execution time lower quantile : 666.116845 µs ( 2.5%)
   Execution time upper quantile : 704.245623 µs (97.5%)
                   Overhead used : 1.757134 ns

specter

Evaluation count : 516 in 6 samples of 86 calls.
             Execution time mean : 1.180217 ms
    Execution time std-deviation : 34.616710 µs
   Execution time lower quantile : 1.147961 ms ( 2.5%)
   Execution time upper quantile : 1.234786 ms (97.5%)
                   Overhead used : 1.757134 ns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment