-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ::path-cache to registry collectors map metadata (#59)
- Loading branch information
Showing
9 changed files
with
174 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
(ns bench | ||
(:require [clojure.test.check.generators :as gen] | ||
[iapetos.core :as prometheus] | ||
[iapetos.registry :as r] | ||
[iapetos.registry.collectors :as c] | ||
[iapetos.test.generators :as g] | ||
[jmh.core :as jmh]) | ||
(:import [iapetos.registry IapetosRegistry])) | ||
|
||
(defn metrics | ||
[metric-count] | ||
(gen/sample g/metric metric-count)) | ||
|
||
(def dirty-string-metric | ||
(gen/let [first-char gen/char-alpha | ||
invalid-chars (gen/return (apply str (map char (range 33 45)))) | ||
last-char gen/char-alphanumeric | ||
rest-chars gen/string-alphanumeric] | ||
(gen/return | ||
(str | ||
(apply str first-char invalid-chars rest-chars) | ||
last-char)))) | ||
|
||
(defn dirty-metrics | ||
[metric-count] | ||
(gen/sample dirty-string-metric metric-count)) | ||
|
||
;; JMH fns | ||
|
||
(defn collectors | ||
[^IapetosRegistry registry] | ||
(.-collectors registry)) | ||
|
||
(defn register-collectors | ||
[metrics] | ||
(reduce (fn [reg metric] | ||
(r/register reg metric (prometheus/counter metric))) | ||
(r/create) | ||
metrics)) | ||
|
||
(defn lookup | ||
[collectors metric] | ||
(c/lookup collectors metric {})) | ||
|
||
(def bench-env | ||
{:benchmarks [{:name :registry-lookup | ||
:fn `lookup | ||
:args [:state/collectors :state/metric]} | ||
|
||
{:name :dirty-registry-lookup | ||
:fn `lookup | ||
:args [:state/dirty-collectors :state/dirty-metric]}] | ||
|
||
:states {:dirty-metrics {:fn `dirty-metrics :args [:param/metric-count]} | ||
:dirty-metric {:fn `rand-nth :args [:state/dirty-metrics]} | ||
:dirty-registry {:fn `register-collectors :args [:state/dirty-metrics]} | ||
:dirty-collectors {:fn `collectors :args [:state/dirty-registry]} | ||
|
||
:metrics {:fn `metrics :args [:param/metric-count]} | ||
:metric {:fn `rand-nth :args [:state/metrics]} | ||
:registry {:fn `register-collectors :args [:state/metrics]} | ||
:collectors {:fn `collectors :args [:state/registry]}} | ||
|
||
:params {:metric-count 500} | ||
|
||
:options {:registry-lookup {:measurement {:iterations 1000}} | ||
:dirty-registry-lookup {:measurement {:iterations 1000}} | ||
:jmh/default {:mode :average | ||
:output-time-unit :us | ||
:measurement {:iterations 1000 | ||
:count 1}}}}) | ||
|
||
(def bench-opts | ||
{:type :quick | ||
:params {:metric-count 500}}) | ||
|
||
(comment | ||
|
||
(map #(select-keys % [:fn :mode :name :score]) (jmh/run bench-env bench-opts)) | ||
|
||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(ns iapetos.registry.collectors-test | ||
(:require [clojure.test.check | ||
[generators :as gen] | ||
[properties :as prop] | ||
[clojure-test :refer [defspec]]] | ||
[clojure.test :refer :all] | ||
[iapetos.test.generators :as g] | ||
[iapetos.core :as prometheus] | ||
[iapetos.collector :as collector] | ||
[iapetos.registry.collectors :as c]) | ||
(:import [iapetos.registry IapetosRegistry])) | ||
|
||
(defn- all-collectors | ||
[^IapetosRegistry registry] | ||
(for [[_namespace vs] (.-collectors registry) | ||
[_subsystem vs] vs | ||
[_name collector] vs] | ||
collector)) | ||
|
||
(defn- path-cache | ||
[^IapetosRegistry registry] | ||
(::c/path-cache (meta (.-collectors registry)))) | ||
|
||
(defspec t-registry-should-cache-the-path-of-registered-collectors 50 | ||
(prop/for-all | ||
[registry (gen/return (prometheus/collector-registry)) | ||
collector-constructor g/collector-constructor | ||
collector-name g/metric] | ||
(let [collector (collector-constructor collector-name) | ||
registry (prometheus/register registry collector) | ||
cache (path-cache registry)] | ||
(is (every? (fn [{:keys [path cache-key] :as _collector}] | ||
(= path (get cache cache-key))) | ||
(all-collectors registry)))))) | ||
|
||
(defspec t-registry-should-evict-path-from-cache-for-unregistered-collectors 50 | ||
(prop/for-all | ||
[registry (gen/return (prometheus/collector-registry)) | ||
collector-constructor g/collector-constructor | ||
collector-name g/metric] | ||
(let [collector (collector-constructor collector-name) | ||
registry (-> registry | ||
(prometheus/register collector) | ||
(prometheus/unregister collector))] | ||
(is (empty? (path-cache registry)))))) |