Mercurial > lbo > hg > myi3stat
changeset 19:9d7e78d13bd4
Use millisecond resolution for intervals
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 14 Feb 2016 16:22:50 +0100 |
parents | b50a0aeb059a |
children | d69c429b176c |
files | README.md src/framework.rs src/main.rs |
diffstat | 3 files changed, 13 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/README.md Sun Feb 14 15:23:51 2016 +0100 +++ b/README.md Sun Feb 14 16:22:50 2016 +0100 @@ -35,7 +35,7 @@ pub fn set(&mut self, k: String, v: State); // Obtain current timestamp. pub fn now() -> i64; - // Time (Unix epoch) of last call. + // Time (in milliseconds since Unix epoch) of last call. pub last_called: i64; } pub enum State { @@ -48,8 +48,9 @@ Every time your metric is asked to `render()`, it is given the same `MetricState` object; `last_called` is set to the timestamp of the previous -invocation (so you can compute a rate, for example); it has second-resolution. -You can arbitrarily get and set values on the `MetricState`. +invocation (so you can compute a rate, for example); it has millisecond +resolution and is 0 on first invocation. You can arbitrarily get and set values +on the `MetricState`. Typically you will set some constant configuration parameters at the invocation of your `init()` method, and use them later to determine how exactly you'll
--- a/src/framework.rs Sun Feb 14 15:23:51 2016 +0100 +++ b/src/framework.rs Sun Feb 14 16:22:50 2016 +0100 @@ -5,7 +5,7 @@ use std::time::Duration; extern crate chrono; -use self::chrono::Local; +use self::chrono as chron; #[derive(Clone)] pub enum Color { @@ -96,8 +96,11 @@ pub fn set(&mut self, k: String, v: State) { self.state.insert(k, v); } + /// Returns timestamp in epoch milliseconds. pub fn now() -> i64 { - Local::now().timestamp() + use self::chrono::Timelike; + let t = chron::Local::now(); + t.timestamp() + t.nanosecond() as i64 / 1000000 } } @@ -135,7 +138,7 @@ } pub fn render_loop(mut metrics: Vec<ActiveMetric>, interval: i32) { - let ival_duration = Duration::new(interval as u64, 0); + let ival_duration = Duration::new((interval / 1000) as u64, 1000000 * (interval as u32 % 1000)); let intro = "{\"version\":1}\n[[]\n"; print!("{}", intro);
--- a/src/main.rs Sun Feb 14 15:23:51 2016 +0100 +++ b/src/main.rs Sun Feb 14 16:22:50 2016 +0100 @@ -30,7 +30,7 @@ "METRIC1,METRIC2,METRIC3"); options.optopt("", "interval", - "Interval in seconds between individual render cycles. Default: 1", + "Interval in milliseconds between individual render cycles. Default: 1000", "SECONDS"); options.optflag("h", "help", "Print a help text"); @@ -111,9 +111,9 @@ }); let interval = i32::from_str_radix(&matches.opt_str("interval") - .unwrap_or(String::from("1")), + .unwrap_or(String::from("1000")), 10) - .unwrap_or(1); + .unwrap_or(1000); (metrics, interval) }