Commits

Steve Losh committed b78e233

Add some sanity tests so I don't go crazy.

Comments (0)

Files changed (8)

src/metrics/histograms.clj

 
 (defn percentiles
   ([^HistogramMetric h]
-   (percentiles h [0.75 0.90 0.95 1.0]))
+   (percentiles h [0.75 0.95 0.99 0.999 1.0]))
   ([^HistogramMetric h ps]
    (zipmap ps
            (.percentiles h (double-array ps)))))

test/metrics/test/core.clj

-(ns metrics-clojure.test.core
-  (:use [metrics-clojure.core])
+(ns metrics.test.core
+  (:use [metrics.core])
   (:use [clojure.test]))
 
-(deftest replace-me ;; FIXME: write
-  (is false "No tests have been written."))

test/metrics/test/counters.clj

+(ns metrics.test.counters
+  (:require [metrics.counters :as counters])
+  (:use [clojure.test]))
+
+
+(deftest test-inc
+  (let [c (counters/counter ["test" "counters" "test-inc"])]
+    (is (= (counters/value c) 0))
+    (counters/inc! c)
+    (is (= (counters/value c) 1))
+    (counters/inc! c 2)
+    (is (= (counters/value c) 3))))
+
+(deftest test-dec
+  (let [c (counters/counter ["test" "counters" "test-dec"])]
+    (is (= (counters/value c) 0))
+    (counters/dec! c)
+    (is (= (counters/value c) -1))
+    (counters/dec! c 2)
+    (is (= (counters/value c) -3))))
+
+(deftest test-clear
+  (let [c (counters/counter ["test" "counters" "test-clear"])]
+    (is (= (counters/value c) 0))
+    (counters/inc! c 100)
+    (is (= (counters/value c) 100))
+    (counters/clear! c)
+    (is (= (counters/value c) 0))))

test/metrics/test/gauges.clj

+(ns metrics.test.gauges
+  (:require [metrics.gauges :as gauges])
+  (:use [clojure.test]))
+
+
+(deftest test-gauge
+  (let [g (gauges/gauge ["test" "gauges" "test-gauge"]
+                        (+ 100 1))]
+    (is (= (gauges/value g) 101))))
+
+(deftest test-gauge-fn
+  (let [g (gauges/gauge-fn ["test" "gauges" "test-gauge-fn"]
+                           #(+ 100 2))]
+    (is (= (gauges/value g) 102))))

test/metrics/test/histograms.clj

+(ns metrics.test.histograms
+  (:require [metrics.histograms :as histograms])
+  (:use [metrics.test.utils])
+  (:use [clojure.test]))
+
+
+(deftest test-mean
+  (let [h (histograms/histogram ["test" "histograms" "test-mean"])]
+    (is (= (histograms/mean h) 0))
+    (histograms/update! h 5)
+    (is (= (histograms/mean h) 5))
+    (histograms/update! h 15)
+    (is (= (histograms/mean h) 10))))
+
+(deftest test-number-recorded
+  (let [h (histograms/histogram ["test" "histograms" "test-number-recorded"])]
+    (is (= (histograms/number-recorded h) 0))
+    (histograms/update! h 0)
+    (is (= (histograms/number-recorded h) 1))
+    (histograms/update! h 0)
+    (is (= (histograms/number-recorded h) 2))))
+
+(deftest test-std-dev
+  (let [h (histograms/histogram ["test" "histograms" "test-std-dev"])]
+    (is (= (histograms/std-dev h) 0))
+    (histograms/update! h 2)
+    (histograms/update! h 4)
+    (histograms/update! h 6)
+    (is (= (histograms/std-dev h) 2))))
+
+(deftest test-smallest
+  (let [h (histograms/histogram ["test" "histograms" "test-smallest"])]
+    (is (= (histograms/smallest h) 0))
+    (histograms/update! h 1)
+    (histograms/update! h 9)
+    (histograms/update! h 3)
+    (is (= (histograms/smallest h) 1))))
+
+(deftest test-largest
+  (let [h (histograms/histogram ["test" "histograms" "test-largest"])]
+    (is (= (histograms/largest h) 0))
+    (histograms/update! h 1)
+    (histograms/update! h 9)
+    (histograms/update! h 3)
+    (is (= (histograms/largest h) 9))))
+
+(deftest test-percentiles
+  (let [h (histograms/histogram ["test" "histograms" "test-percentiles"])]
+    (is (maps-within-one (histograms/percentiles h)
+                    {0.75 0, 0.95 0, 0.99 0, 0.999 0, 1.00 0}))
+
+    (dorun (map (partial histograms/update! h) (range 1 101)))
+
+    (is (maps-within-one (histograms/percentiles h)
+                    {0.75 75, 0.95 95, 0.99 99, 0.999 100, 1.00 100}))
+
+    (is (maps-within-one (histograms/percentiles h [0.10 0.50])
+                    {0.10 10, 0.50 50}))))
+
+(deftest test-clear
+  (let [h (histograms/histogram ["test" "histograms" "test-clear"])]
+    (is (= (histograms/mean h) 0))
+    (histograms/update! h 5)
+    (is (= (histograms/mean h) 5))
+    (histograms/clear! h)
+    (is (= (histograms/mean h) 0))))
+

test/metrics/test/meters.clj

+(ns metrics.test.meters
+  (:require [metrics.meters :as meters])
+  (:use [metrics.test.utils])
+  (:use [clojure.test]))
+
+
+(deftest test-rate-mean
+  (let [m (meters/meter ["test" "meters" "test-rate-mean"] "test-events")]
+    (is (= (meters/rate-mean m) 0))
+    (meters/mark! m)
+    (is (> (meters/rate-mean m) 0))))
+
+(deftest test-rate-mean-update-multiple
+  (let [m (meters/meter ["test" "meters" "test-rate-mean-update-multiple"] "test-events")]
+    (is (= (meters/rate-mean m) 0))
+    (meters/mark! m 10)
+    (is (> (meters/rate-mean m) 0))))
+
+(deftest test-rate-one
+  (let [m (meters/meter ["test" "meters" "test-rate-one"] "test-events")]
+    (is (= (meters/rate-one m) 0))
+    (meters/mark! m 20000)
+    (Thread/sleep 8000)
+    (is (> (meters/rate-one m) 0))))
+
+(deftest test-rate-five
+  (let [m (meters/meter ["test" "meters" "test-rate-five"] "test-events")]
+    (is (= (meters/rate-five m) 0))
+    (meters/mark! m 20000)
+    (Thread/sleep 8000)
+    (is (> (meters/rate-five m) 0))))
+
+(deftest test-rate-fifteen
+  (let [m (meters/meter ["test" "meters" "test-rate-fifteen"] "test-events")]
+    (is (= (meters/rate-fifteen m) 0))
+    (meters/mark! m 20000)
+    (Thread/sleep 8000)
+    (is (> (meters/rate-fifteen m) 0))))
+
+(deftest test-rates
+  (let [m (meters/meter ["test" "meters" "test-rates"] "test-events")]
+    (is (every? zero? (vals (meters/rates m))))
+    (meters/mark! m 20000)
+    (Thread/sleep 8000)
+    (is (every? #(> % 0) (vals (meters/rates m))))))

test/metrics/test/timers.clj

+(ns metrics.test.timers
+  (:require [metrics.timers :as timers])
+  (:use [metrics.test.utils])
+  (:use [clojure.test]))
+
+
+(defn- sleep-100 []
+  (Thread/sleep 100)
+  100)
+
+(defn- sleep-200 []
+  (Thread/sleep 200)
+  200)
+
+
+(deftest test-rate-mean
+  (let [t (timers/timer ["test" "timers" "test-rate-mean"])]
+    (is (= (timers/rate-mean t) 0))
+    (is (= (timers/time! t (sleep-100)) 100))
+    (is (> (timers/rate-mean t) 0))))
+
+(deftest test-rate-mean-fn
+  (let [t (timers/timer ["test" "timers" "test-rate-mean-fn"])]
+    (is (= (timers/rate-mean t) 0))
+    (is (= (timers/time-fn! t sleep-100) 100))
+    (is (> (timers/rate-mean t) 0))))
+
+(deftest test-rate-one
+  (let [t (timers/timer ["test" "timers" "test-rate-one"])]
+    (is (= (timers/rate-one t) 0))
+    (is (= (timers/time! t (sleep-100)) 100))
+    (Thread/sleep 8000)
+    (is (> (timers/rate-one t) 0))))
+
+(deftest test-rate-five
+  (let [t (timers/timer ["test" "timers" "test-rate-five"])]
+    (is (= (timers/rate-five t) 0))
+    (is (= (timers/time! t (sleep-100)) 100))
+    (Thread/sleep 8000)
+    (is (> (timers/rate-five t) 0))))
+
+(deftest test-rate-fifteen
+  (let [t (timers/timer ["test" "timers" "test-rate-fifteen"])]
+    (is (= (timers/rate-fifteen t) 0))
+    (is (= (timers/time! t (sleep-100)) 100))
+    (Thread/sleep 8000)
+    (is (> (timers/rate-fifteen t) 0))))
+
+
+(deftest test-largest
+  (let [t (timers/timer ["test" "timers" "test-largest"])]
+    (is (= (timers/largest t) 0))
+    (is (= (timers/time! t (sleep-100)) 100))
+    (is (within-ten (timers/largest t) 100))
+    (is (= (timers/time! t (sleep-200)) 200))
+    (is (within-ten (timers/largest t) 200))))
+(deftest test-smallest
+  (let [t (timers/timer ["test" "timers" "test-smallest"])]
+    (is (= (timers/smallest t) 0))
+    (is (= (timers/time! t (sleep-100)) 100))
+    (is (within-ten (timers/smallest t) 100))
+    (is (= (timers/time! t (sleep-200)) 200))
+    (is (within-ten (timers/smallest t) 100))))

test/metrics/test/utils.clj

+(ns metrics.test.utils)
+
+(defn abs [n]
+  (if (> n 0) n (* -1 n)))
+
+(defn within-one [a b]
+  (<= (abs (- a b))
+      1))
+
+(defn within-ten [a b]
+  (<= (abs (- a b))
+      10))
+
+(defn maps-within-one [a b]
+  (when (= (set (keys a)) (set (keys b)))
+    (every? identity
+            (map #(within-one (a %) (b %))
+                 (keys a)))))
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.