0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-26 08:29:42 -05:00
penpot/dev/bench/core.cljs

95 lines
2 KiB
Text
Raw Normal View History

2016-04-07 22:51:40 +03:00
(ns bench.core
2016-04-11 17:32:40 +03:00
(:require [kdtree.core :as k]
[cljs.pprint :refer (pprint)]
2016-04-09 20:32:15 +03:00
[cljs.nodejs :as node]))
2016-04-07 22:51:40 +03:00
(enable-console-print!)
2016-04-11 17:32:40 +03:00
;; --- Helpers
2016-04-07 22:51:40 +03:00
(defn generate-points
2016-04-11 17:32:40 +03:00
([n] (generate-points n 1))
([n step]
(into-array
(for [x (range 0 n step)
y (range 0 n step)]
#js [x y]))))
2016-04-07 22:51:40 +03:00
2016-04-11 17:32:40 +03:00
;; --- Index Initialization Bechmark
2016-04-07 22:51:40 +03:00
2016-04-11 17:32:40 +03:00
(defn- bench-init-10000
2016-04-07 22:51:40 +03:00
[]
2016-04-09 20:32:15 +03:00
(js/console.time "init:10000")
2016-04-11 17:32:40 +03:00
(let [points (generate-points 1000 10)]
(println (count points))
2016-04-09 20:32:15 +03:00
(k/create2d points)
(js/console.timeEnd "init:10000")))
2016-04-07 22:51:40 +03:00
2016-04-11 17:32:40 +03:00
(defn- bench-init-250000
2016-04-07 22:51:40 +03:00
[]
2016-04-11 17:32:40 +03:00
(js/console.time "init:250000")
(let [points (generate-points 5000 10)]
(println (count points))
2016-04-09 20:32:15 +03:00
(k/create2d points)
2016-04-11 17:32:40 +03:00
(js/console.timeEnd "init:250000")))
2016-04-07 22:51:40 +03:00
(defn bench-init
[]
2016-04-09 20:32:15 +03:00
(bench-init-10000)
2016-04-11 17:32:40 +03:00
(bench-init-250000))
;; --- Nearest Search Benchmark
2016-04-07 22:51:40 +03:00
2016-04-11 17:32:40 +03:00
(defn- bench-knn-160000
2016-04-07 22:51:40 +03:00
[]
2016-04-11 17:32:40 +03:00
(let [tree (k/create2d (generate-points 4000 10))]
2016-04-07 22:51:40 +03:00
(dotimes [i 100]
2016-04-09 20:32:15 +03:00
(js/console.time "knn:160000")
(let [pt #js [(rand-int 400)
(rand-int 400)]]
(.nearest tree pt 2))
(js/console.timeEnd "knn:160000"))))
2016-04-07 22:51:40 +03:00
2016-04-11 17:32:40 +03:00
(defn- bench-knn-360000
2016-04-07 22:51:40 +03:00
[]
2016-04-11 17:32:40 +03:00
(let [tree (k/create2d (generate-points 6000 10))]
2016-04-07 22:51:40 +03:00
(dotimes [i 100]
2016-04-09 20:32:15 +03:00
(js/console.time "knn:360000")
(let [pt #js [(rand-int 600)
(rand-int 600)]]
(.nearest tree pt 2))
(js/console.timeEnd "knn:360000"))))
2016-04-07 22:51:40 +03:00
(defn bench-knn
[]
2016-04-09 20:32:15 +03:00
(bench-knn-160000)
(bench-knn-360000))
2016-04-07 22:51:40 +03:00
2016-04-11 17:32:40 +03:00
;; --- Accuracity tests
(defn test-accuracity
[]
(let [tree (k/create2d (generate-points 4000 20))]
(print "[1742 1419]")
(pprint (js->clj (.nearest tree #js [1742 1419] 6)))
(print "[1742 1420]")
(pprint (js->clj (.nearest tree #js [1742 1420] 6)))
))
2016-04-07 22:51:40 +03:00
(defn main
[& [type]]
2016-04-11 17:32:40 +03:00
(cond
2016-04-07 22:51:40 +03:00
(= type "init")
(bench-init)
(= type "knn")
(bench-knn)
2016-04-11 17:32:40 +03:00
(= type "test")
(test-accuracity)
2016-04-07 22:51:40 +03:00
:else
(println "not implemented")))
(set! *main-cli-fn* main)