Skip to content

Commit

Permalink
Added some simple benchmarking for CLJ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janetacarr committed Jan 15, 2024
1 parent c43ce12 commit 4e91889
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions test/quadtree_cljc/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
retrieve
retrieve-intersections
retrieve-points]]
#?(:clj [clojure.test :refer [deftest is testing run-tests]]
:cljs [cljs.test :refer-macros [deftest is testing run-tests]])))
#?@(:clj [[clojure.test :refer [deftest is testing run-tests]]
[criterium.core :as bench]]
:cljs [[cljs.test :refer-macros [deftest is testing run-tests]]])))

(deftest total-nodes-test
(testing "Correct use case for total-nodes"
Expand Down Expand Up @@ -349,3 +350,48 @@
:level 0,
:objects [],
:nodes []} (insert-all basic-quadtree []))))))

(defn- ->random-small-entity
"Simulates a small entity in a 1080p world"
[]
(hash-map :x (rand-int 1980)
:y (rand-int 1080)
:width 10
:height 10))

(defn- ->random-medium-entity
"Simulates a medium entity in a 1080p world"
[]
(hash-map :x (rand-int 1980)
:y (rand-int 1080)
:width 50
:height 50))

(defn- ->random-large-entity
"Simulates a large entity in a 1080p world"
[]
(hash-map :x (rand-int 1980)
:y (rand-int 1080)
:width 100
:height 100))

(defn- generate-entities
"Generate a vector of entities from the `entity-f`.
Optionally, pass `n` number of entities to generate."
([entity-f]
(generate-entities entity-f 100))
([entity-f n]
(vec (repeatedly n entity-f))))

#?(:clj
(deftest benchmarking
(testing "1000, 100 max-levels entties in tree"
(let [entities (generate-entities ->random-large-entity)
player (->random-small-entity)
{:keys [mean]} (bench/benchmark (-> (->bounds 0 0 1920 1080)
(->quadtree 1020 100)
(insert-all entities)
(retrieve-intersections player))
{:verbose true})]
(is (> 0.5E-4 (first mean))
"Benchmark should be sub 500us for 1000 large entities")))))

0 comments on commit 4e91889

Please sign in to comment.