|
2 | 2 | (:require [clojure.pprint :as pp] |
3 | 3 | [clojure.string :as str] |
4 | 4 | [clojure.tools.cli :refer [parse-opts]] |
| 5 | + [clojure.java.jdbc :as jdbc] |
5 | 6 | [excel-templates.build :as excel] |
6 | 7 | [hitchhiker.redis :as redis] |
| 8 | + [hitchhiker.sqlite :as sqlite] |
7 | 9 | [hitchhiker.tree.core :as core] |
8 | 10 | [hitchhiker.tree.messaging :as msg]) |
9 | 11 | (:import [java.io File FileWriter])) |
|
129 | 131 | :validate [#(#{"fractal" "b-tree" "sorted-set"} %) "Data structure must be fractal, b-tree, or sorted set"]] |
130 | 132 | [nil "--backend testing" "Runs the benchmark with the specified backend" |
131 | 133 | :default "testing" |
132 | | - :validate [#(#{"redis" "testing"} %) "Backend must be redis or testing"]] |
| 134 | + :validate [#(#{"redis" "sqlite" "testing"} %) "Backend must be redis, sqlite or testing"]] |
133 | 135 | ["-d" "--delete-pattern PATTERN" "Specifies how the operations will be reordered on delete" |
134 | 136 | :default "forward" |
135 | 137 | :validate [#(#{"forward" "reverse" "shuffle" "zero"} %) "Incorrect delete pattern"] |
|
194 | 196 |
|
195 | 197 | (defn -main |
196 | 198 | [& [root & args]] |
197 | | - (let [outputs (atom [])] |
198 | | - (doseq [args (or (->> args |
199 | | - (partition-by #(= % "--")) |
200 | | - (map-indexed vector) |
201 | | - (filter (comp even? first)) |
202 | | - (map second) |
203 | | - (seq)) |
204 | | - [[]])] ; always do one iteration |
205 | | - (let [{:keys [options arguments errors summary]} (parse-opts args options) |
206 | | - tree-to-test (atom {}) |
207 | | - results (atom [])] |
208 | | - (cond |
209 | | - (or (= "-h" root) |
210 | | - (= "--help" root) |
211 | | - (nil? root) |
212 | | - (:help options)) (exit 0 (usage summary)) |
213 | | - (not= (count arguments) 0) (exit 1 (usage summary)) |
214 | | - errors (exit 1 (error-msg errors))) |
215 | | - (let [backend (case (:backend options) |
216 | | - "testing" (core/->TestingBackend) |
217 | | - "redis" (do (redis/start-expiry-thread!) |
218 | | - (redis/->RedisBackend))) |
219 | | - delete-xform (case (:delete-pattern options) |
220 | | - "forward" identity |
221 | | - "reverse" reverse |
222 | | - "shuffle" shuffle |
223 | | - "zero" #(repeat (count %) 0.0)) |
224 | | - [tree-name structure] |
225 | | - (case (:data-structure options) |
226 | | - "b-tree" ["b-tree" (core-b-tree (:tree-width options) backend)] |
227 | | - "fractal" ["fractal" (msg-b-tree (:tree-width options) backend)] |
228 | | - "sorted-set" ["sorted-set" (sorted-set-repr)]) |
229 | | - flush-freq (:flush-freq options) |
230 | | - codename (str tree-name |
231 | | - "__flush_" |
232 | | - flush-freq |
233 | | - "__b_" |
234 | | - (:tree-width options) |
235 | | - "__" |
236 | | - (:backend options) |
237 | | - "__n_" |
238 | | - (:num-operations options) |
239 | | - "__del_" |
240 | | - (:delete-pattern options))] |
241 | | - (doseq [ds (generate-test-datasets) |
242 | | - :let [codename (str codename |
243 | | - "_" |
244 | | - (:name ds)) |
245 | | - out (create-output-dir |
246 | | - root |
247 | | - codename) |
248 | | - _ (println "Doing" codename) |
249 | | - bench-res (benchmark (:num-operations options) ds flush-freq structure out delete-xform)]] |
250 | | - (swap! results conj |
251 | | - {:tree tree-name |
252 | | - :ds (:name ds) |
253 | | - :freq flush-freq |
254 | | - :n (:num-operations options) |
255 | | - :b (:tree-width options) |
256 | | - :delete-pattern (:delete-pattern options) |
257 | | - :results bench-res})) |
258 | | - ;(println "results") |
259 | | - ;(clojure.pprint/pprint @results) |
260 | | - (swap! outputs conj (template-one-sheet @results))))) |
261 | | - (excel/render-to-file |
262 | | - "template_benchmark.xlsx" |
263 | | - (.getPath (File. root "analysis.xlsx")) |
264 | | - {"SingleDS" |
265 | | - (map-indexed (fn [i s] |
266 | | - (assoc s :sheet-name (str "Trial " (inc i)))) |
267 | | - @outputs)}))) |
| 199 | + (jdbc/with-db-connection [db (sqlite/db-spec ":memory:")] |
| 200 | + (let [outputs (atom [])] |
| 201 | + (doseq [args (or (->> args |
| 202 | + (partition-by #(= % "--")) |
| 203 | + (map-indexed vector) |
| 204 | + (filter (comp even? first)) |
| 205 | + (map second) |
| 206 | + (seq)) |
| 207 | + [[]])] ; always do one iteration |
| 208 | + (let [{:keys [options arguments errors summary]} (parse-opts args options) |
| 209 | + tree-to-test (atom {}) |
| 210 | + results (atom [])] |
| 211 | + (cond |
| 212 | + (or (= "-h" root) |
| 213 | + (= "--help" root) |
| 214 | + (nil? root) |
| 215 | + (:help options)) (exit 0 (usage summary)) |
| 216 | + (not= (count arguments) 0) (exit 1 (usage summary)) |
| 217 | + errors (exit 1 (error-msg errors))) |
| 218 | + (let [backend (case (:backend options) |
| 219 | + "testing" (core/->TestingBackend) |
| 220 | + "redis" (do (redis/start-expiry-thread!) |
| 221 | + (redis/->RedisBackend)) |
| 222 | + "sqlite" (do (sqlite/ensure-schema db) |
| 223 | + (sqlite/->SQLiteBackend db))) |
| 224 | + delete-xform (case (:delete-pattern options) |
| 225 | + "forward" identity |
| 226 | + "reverse" reverse |
| 227 | + "shuffle" shuffle |
| 228 | + "zero" #(repeat (count %) 0.0)) |
| 229 | + [tree-name structure] |
| 230 | + (case (:data-structure options) |
| 231 | + "b-tree" ["b-tree" (core-b-tree (:tree-width options) backend)] |
| 232 | + "fractal" ["fractal" (msg-b-tree (:tree-width options) backend)] |
| 233 | + "sorted-set" ["sorted-set" (sorted-set-repr)]) |
| 234 | + flush-freq (:flush-freq options) |
| 235 | + codename (str tree-name |
| 236 | + "__flush_" |
| 237 | + flush-freq |
| 238 | + "__b_" |
| 239 | + (:tree-width options) |
| 240 | + "__" |
| 241 | + (:backend options) |
| 242 | + "__n_" |
| 243 | + (:num-operations options) |
| 244 | + "__del_" |
| 245 | + (:delete-pattern options))] |
| 246 | + (doseq [ds (generate-test-datasets) |
| 247 | + :let [codename (str codename |
| 248 | + "_" |
| 249 | + (:name ds)) |
| 250 | + out (create-output-dir |
| 251 | + root |
| 252 | + codename) |
| 253 | + _ (println "Doing" codename) |
| 254 | + bench-res (benchmark (:num-operations options) ds flush-freq structure out delete-xform)]] |
| 255 | + (swap! results conj |
| 256 | + {:tree tree-name |
| 257 | + :ds (:name ds) |
| 258 | + :freq flush-freq |
| 259 | + :n (:num-operations options) |
| 260 | + :b (:tree-width options) |
| 261 | + :delete-pattern (:delete-pattern options) |
| 262 | + :results bench-res})) |
| 263 | + ;(println "results") |
| 264 | + ;(clojure.pprint/pprint @results) |
| 265 | + (swap! outputs conj (template-one-sheet @results))))) |
| 266 | + (excel/render-to-file |
| 267 | + "template_benchmark.xlsx" |
| 268 | + (.getPath (File. root "analysis.xlsx")) |
| 269 | + {"SingleDS" |
| 270 | + (map-indexed (fn [i s] |
| 271 | + (assoc s :sheet-name (str "Trial " (inc i)))) |
| 272 | + @outputs)})))) |
0 commit comments