|
| 1 | +(ns bench |
| 2 | + (:require [clojure.test.check.generators :as gen] |
| 3 | + [iapetos.core :as prometheus] |
| 4 | + [iapetos.registry :as r] |
| 5 | + [iapetos.registry.collectors :as c] |
| 6 | + [iapetos.test.generators :as g] |
| 7 | + [jmh.core :as jmh]) |
| 8 | + (:import [iapetos.registry IapetosRegistry])) |
| 9 | + |
| 10 | +(defn metrics |
| 11 | + [metric-count] |
| 12 | + (gen/sample g/metric metric-count)) |
| 13 | + |
| 14 | +(def dirty-string-metric |
| 15 | + (gen/let [first-char gen/char-alpha |
| 16 | + invalid-chars (gen/return (apply str (map char (range 33 45)))) |
| 17 | + last-char gen/char-alphanumeric |
| 18 | + rest-chars gen/string-alphanumeric] |
| 19 | + (gen/return |
| 20 | + (str |
| 21 | + (apply str first-char invalid-chars rest-chars) |
| 22 | + last-char)))) |
| 23 | + |
| 24 | +(defn dirty-metrics |
| 25 | + [metric-count] |
| 26 | + (gen/sample dirty-string-metric metric-count)) |
| 27 | + |
| 28 | +;; JMH fns |
| 29 | + |
| 30 | +(defn collectors |
| 31 | + [^IapetosRegistry registry] |
| 32 | + (.-collectors registry)) |
| 33 | + |
| 34 | +(defn register-collectors |
| 35 | + [metrics] |
| 36 | + (reduce (fn [reg metric] |
| 37 | + (r/register reg metric (prometheus/counter metric))) |
| 38 | + (r/create) |
| 39 | + metrics)) |
| 40 | + |
| 41 | +(defn lookup |
| 42 | + [collectors metric] |
| 43 | + (c/lookup collectors metric {})) |
| 44 | + |
| 45 | +(def bench-env |
| 46 | + {:benchmarks [{:name :registry-lookup |
| 47 | + :fn `lookup |
| 48 | + :args [:state/collectors :state/metric]} |
| 49 | + |
| 50 | + {:name :dirty-registry-lookup |
| 51 | + :fn `lookup |
| 52 | + :args [:state/dirty-collectors :state/dirty-metric]}] |
| 53 | + |
| 54 | + :states {:dirty-metrics {:fn `dirty-metrics :args [:param/metric-count]} |
| 55 | + :dirty-metric {:fn `rand-nth :args [:state/dirty-metrics]} |
| 56 | + :dirty-registry {:fn `register-collectors :args [:state/dirty-metrics]} |
| 57 | + :dirty-collectors {:fn `collectors :args [:state/dirty-registry]} |
| 58 | + |
| 59 | + :metrics {:fn `metrics :args [:param/metric-count]} |
| 60 | + :metric {:fn `rand-nth :args [:state/metrics]} |
| 61 | + :registry {:fn `register-collectors :args [:state/metrics]} |
| 62 | + :collectors {:fn `collectors :args [:state/registry]}} |
| 63 | + |
| 64 | + :params {:metric-count 500} |
| 65 | + |
| 66 | + :options {:registry-lookup {:measurement {:iterations 1000}} |
| 67 | + :dirty-registry-lookup {:measurement {:iterations 1000}} |
| 68 | + :jmh/default {:mode :average |
| 69 | + :output-time-unit :us |
| 70 | + :measurement {:iterations 1000 |
| 71 | + :count 1}}}}) |
| 72 | + |
| 73 | +(def bench-opts |
| 74 | + {:type :quick |
| 75 | + :params {:metric-count 500}}) |
| 76 | + |
| 77 | +(comment |
| 78 | + |
| 79 | + (map #(select-keys % [:fn :mode :name :score]) (jmh/run bench-env bench-opts)) |
| 80 | + |
| 81 | + ) |
0 commit comments