From 4e9188915d4105a4f43f557e528b9e18f146ea35 Mon Sep 17 00:00:00 2001 From: Janet Carr Date: Mon, 15 Jan 2024 16:25:24 -0500 Subject: [PATCH] Added some simple benchmarking for CLJ tests --- test/quadtree_cljc/core_test.cljc | 50 +++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/test/quadtree_cljc/core_test.cljc b/test/quadtree_cljc/core_test.cljc index e0ad37c..e24cf56 100644 --- a/test/quadtree_cljc/core_test.cljc +++ b/test/quadtree_cljc/core_test.cljc @@ -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" @@ -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")))))