Browse Source

minor tweaks

master
Jonathan Strong 6 years ago
parent
commit
929fdb2183
2 changed files with 45 additions and 18 deletions
  1. +35
    -18
      src/influx.rs
  2. +10
    -0
      src/lib.rs

+ 35
- 18
src/influx.rs View File

@@ -37,6 +37,8 @@ const ZMQ_SND_HWM: i32 = 0;

const BUFFER_SIZE: u8 = 80;

pub use super::{dur_nanos, dt_nanos};

pub type Map<K, V> = OrderMap<K, V, BuildHasherDefault<FnvHasher>>;

pub fn new_map<K, V>(capacity: usize) -> Map<K, V> {
@@ -154,7 +156,7 @@ macro_rules! measure {
}};

($m:tt, $name:tt, $( $t:tt ( $($tail:tt)* ) ),+ $(,)*) => {
measure!($m, $name, $($t [ $($tail)* ] ),+ $(,)*)
measure!($m, $name, $($t [ $($tail)* ] ),+)
};

($m:tt, $name:tt, $( $t:tt [ $($tail:tt)* ] ),+ $(,)*) => {{
@@ -188,6 +190,10 @@ impl InfluxWriter {
self.tx.send(m)
}

pub fn tx(&self) -> Sender<OwnedMeasurement> {
self.tx.clone()
}

pub fn new(host: &'static str, db: &'static str, log_path: &str, buffer_size: u8) -> Self {
let (kill_switch, terminate) = channel();
let (tx, rx) = channel();
@@ -486,18 +492,6 @@ pub fn serialize_owned(measurement: &OwnedMeasurement, line: &mut String) {
}
}

#[test]
fn it_checks_that_fields_are_separated_correctly() {
let m = measure!(@make_meas test, t[a; "one"], t[b; "two"], f[x; 1.1], f[y; -1.1]);
assert_eq!(m.key, "test");
assert_eq!(m.tags.get("a"), Some(&"one"));
assert_eq!(m.fields.get("x"), Some(&OwnedValue::Float(1.1)));

let mut buf = String::new();
serialize_owned(&m, &mut buf);
assert!(buf.contains("b=two x=1.1,y=-1.1"), "buf = {}", buf);
}


pub fn writer(warnings: Sender<Warning>) -> thread::JoinHandle<()> {
thread::spawn(move || {
@@ -619,11 +613,7 @@ impl OwnedMeasurement {
}
}

pub fn dur_nanos(d: ::std::time::Duration) -> i64 {
(d.as_secs() * 1_000_000_000_u64 + (d.subsec_nanos() as u64)) as i64
}


#[cfg(test)]
mod tests {
use super::*;
use test::{black_box, Bencher};
@@ -758,11 +748,38 @@ mod tests {
assert_eq!(meas.timestamp, Some(1));
}

#[test]
fn it_checks_that_fields_are_separated_correctly() {
let m = measure!(@make_meas test, t[a; "one"], t[b; "two"], f[x; 1.1], f[y; -1.1]);
assert_eq!(m.key, "test");
assert_eq!(m.tags.get("a"), Some(&"one"));
assert_eq!(m.fields.get("x"), Some(&OwnedValue::Float(1.1)));

let mut buf = String::new();
serialize_owned(&m, &mut buf);
assert!(buf.contains("b=two x=1.1,y=-1.1"), "buf = {}", buf);
}

#[test]
fn try_to_break_measure_macro() {
let (tx, _) = channel();
measure!(tx, one, tag[x=>"y"], int[n;1]);
measure!(tx, one, tag[x;"y"], int[n;1],);

struct A {
pub one: i32,
pub two: i32,
}

struct B {
pub a: A
}

let b = B { a: A { one: 1, two: 2 } };

let m = measure!(@make_meas test, t(name, "a"), i(a, b.a.one));

assert_eq!(m.fields.get("a"), Some(&OwnedValue::Integer(1)));
}

#[bench]


+ 10
- 0
src/lib.rs View File

@@ -50,3 +50,13 @@ pub fn file_logger(path: &str, level: Severity) -> slog::Logger {
builder.timezone(TimeZone::Utc);
builder.build().unwrap()
}

pub fn dt_nanos(t: DateTime<Utc>) -> i64 {
(t.timestamp() as i64) * 1_000_000_000_i64 + (t.timestamp_subsec_nanos() as i64)
}

pub fn dur_nanos(d: ::std::time::Duration) -> i64 {
(d.as_secs() * 1_000_000_000_u64 + (d.subsec_nanos() as u64)) as i64
}



Loading…
Cancel
Save