about summary refs log tree commit diff
path: root/tools/clojure
diff options
context:
space:
mode:
Diffstat (limited to 'tools/clojure')
-rw-r--r--tools/clojure/README.md4
-rw-r--r--tools/clojure/project.clj12
-rw-r--r--tools/clojure/src/immer_benchmark.clj131
3 files changed, 147 insertions, 0 deletions
diff --git a/tools/clojure/README.md b/tools/clojure/README.md
new file mode 100644
index 000000000000..2628aa2e5f40
--- /dev/null
+++ b/tools/clojure/README.md
@@ -0,0 +1,4 @@
+
+```
+lein run
+```
diff --git a/tools/clojure/project.clj b/tools/clojure/project.clj
new file mode 100644
index 000000000000..735deab2f9eb
--- /dev/null
+++ b/tools/clojure/project.clj
@@ -0,0 +1,12 @@
+(defproject immer-benchmarks "0.1.0-SNAPSHOT"
+  :description "Benchmarks for immutable data structures"
+  :url "http://example.com/FIXME"
+  :dependencies [[org.clojure/clojure "1.8.0"]
+                 [org.clojure/core.rrb-vector "0.0.11"]
+                 [criterium "0.4.4"]]
+  :plugins [[lein-git-deps "0.0.1-SNAPSHOT"]]
+  ;;:git-dependencies [["https://github.com/clojure/core.rrb-vector.git"]]
+  :source-paths [;;".lein-git-deps/core.rrb-vector/src/main/clojure"
+                 "src"]
+
+  :main immer-benchmark)
diff --git a/tools/clojure/src/immer_benchmark.clj b/tools/clojure/src/immer_benchmark.clj
new file mode 100644
index 000000000000..f3d6f73e6c18
--- /dev/null
+++ b/tools/clojure/src/immer_benchmark.clj
@@ -0,0 +1,131 @@
+(ns immer-benchmark
+  (:require [criterium.core :as c]
+            [clojure.core.rrb-vector :as fv]))
+
+(defn h0 [& args]
+  (apply println "\n####" args))
+
+(defn h1 [& args]
+  (apply println "\n====" args))
+
+(defn h2 [& args]
+  (apply println "\n----" args))
+
+(defn run-benchmarks [N S]
+  (h0 "Running benchmarks:  N =" N " S =" S)
+
+  (def c-steps 10)
+
+  (defn vector-push-f [v]
+    (loop [v v
+           i 0]
+      (if (< i N)
+        (recur (fv/catvec (fv/vector i) v)
+               (inc i))
+        v)))
+
+  (defn vector-push [v]
+    (loop [v v
+           i 0]
+      (if (< i N)
+        (recur (conj v i)
+               (inc i))
+        v)))
+
+  (defn vector-push! [v]
+    (loop [v (transient v)
+           i 0]
+      (if (< i N)
+        (recur (conj! v i)
+               (inc i))
+        (persistent! v))))
+
+  (defn vector-concat [v vc]
+    (loop [v v
+           i 0]
+      (if (< i c-steps)
+        (recur (fv/catvec v vc)
+               (inc i))
+        v)))
+
+  (defn vector-update [v]
+    (loop [v v
+           i 0]
+      (if (< i N)
+        (recur (assoc v i (+ i 1))
+               (inc i))
+        v)))
+
+  (defn vector-update-random [v]
+    (loop [v v
+           i 0]
+      (if (< i N)
+        (recur (assoc v (rand-int N) i)
+               (inc i))
+        v)))
+
+  (defn vector-update! [v]
+    (loop [v (transient v)
+           i 0]
+      (if (< i N)
+        (recur (assoc! v i (+ i 1))
+               (inc i))
+        (persistent! v))))
+
+  (defn vector-update-random! [v]
+    (loop [v (transient v)
+           i 0]
+      (if (< i N)
+        (recur (assoc! v (rand-int N) i)
+               (inc i))
+        (persistent! v))))
+
+  (defn vector-iter [v]
+    (reduce + 0 v))
+
+  (defn the-benchmarks [empty-v]
+    (def full-v (into empty-v (range N)))
+    (h2 "iter")
+    (c/bench (vector-iter full-v) :samples S)
+    (if (> N 100000)
+      (h2 "skipping updates 'cuz N > 100000 (would run out of mem)")
+      (do
+        (h2 "update!")
+        (c/bench (vector-update! full-v) :samples S)
+        (h2 "update-random!")
+        (c/bench (vector-update-random! full-v) :samples S)
+        (h2 "push!")
+        (c/bench (vector-push! empty-v) :samples S)
+        (h2 "update")
+        (c/bench (vector-update full-v) :samples S)
+        (h2 "update-random")
+        (c/bench (vector-update-random full-v) :samples S)
+        (h2 "push")
+        (c/bench (vector-push empty-v) :samples S))))
+
+  (defn the-rrb-benchmarks [empty-v]
+    (if (> N 1000)
+      (h2 "skipping relaxed test 'cuz N > 1000 (rrb-vector bug)")
+      (do
+        (def full-v (vector-push-f empty-v))
+        (h2 "iter/f")
+        (c/bench (vector-iter full-v) :samples S)
+        (h2 "update/f")
+        (c/bench (vector-update full-v) :samples S)
+        (h2 "update-random/f")
+        (c/bench (vector-update-random full-v) :samples S)))
+    (def short-v (into empty-v (range (/ N c-steps))))
+    (h2 "concat")
+    (c/bench (vector-concat empty-v short-v)) :samples S)
+
+  (h1 "vector")
+  (the-benchmarks (vector))
+
+  (h1 "rrb-vector")
+  (the-benchmarks (fv/vector))
+  (the-rrb-benchmarks (fv/vector)))
+
+(defn -main []
+  (run-benchmarks 1000 100)
+  (run-benchmarks 100000 20)
+  (run-benchmarks 10000000 3))