Skip to content

Commit

Permalink
Merge branch 'eranharel-add-timer-update-func-139'
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Nov 30, 2018
2 parents 208c07b + fe6b5f4 commit f10e54b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 5 additions & 1 deletion metrics-clojure-core/src/metrics/timers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require [metrics.core :refer [default-registry metric-name]]
[metrics.utils :refer [get-percentiles desugared-title snapshot]])
(:import [com.codahale.metrics MetricRegistry Reservoir Timer Timer$Context]
java.util.concurrent.TimeUnit))
[java.util.concurrent TimeUnit]))


(defn ^com.codahale.metrics.Timer timer
Expand Down Expand Up @@ -120,6 +120,10 @@
[^Timer t ^clojure.lang.IFn f]
(.time t ^Callable f))

(defn update!
[^Timer t ^long duration, ^TimeUnit unit]
(.update t duration unit))

(defn start
"Start a timer, returning the context object that will be used to
stop this particular instance."
Expand Down
13 changes: 11 additions & 2 deletions metrics-clojure-core/test/metrics/test/timers_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(:require [metrics.core :as mc]
[metrics.timers :as mt]
[metrics.test.test-utils :refer :all]
[clojure.test :refer :all]))
[clojure.test :refer :all])
(:import [java.util.concurrent TimeUnit]))

(def ^:const expiration-delay 6000)

Expand All @@ -18,7 +19,7 @@

(let [reg (mc/new-registry)]
(mt/deftimer reg ["test" "timers" "deftimered"])
(mt/deftimer reg (mc/uniform-reservior) deftimered-3-arity)
(mt/deftimer reg (mc/uniform-reservoir) deftimered-3-arity)

(deftest test-deftimer
(is (= (mt/rate-mean deftimered) 0.0))
Expand Down Expand Up @@ -116,3 +117,11 @@
(is (= java.lang.Long
(.getClass (mt/number-recorded t)))))))

(deftest test-update-timer
(let [r (mc/new-registry)
t (mt/timer r ["test" "timers" "test-update-timer"])
expected-time 666
expected-time-unit TimeUnit/SECONDS]
(is (zero? (mt/largest t)))
(mt/update! t expected-time expected-time-unit)
(is (= (.toNanos expected-time-unit expected-time) (mt/largest t)))))

0 comments on commit f10e54b

Please sign in to comment.