Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format code using 'cargo fmt' #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions examples/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! A simple example demonstrating how to use Telemetry to measure and
//! store performance data, then eventually dump it to console/disk.

use std::thread;
use std::sync::mpsc::channel;
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Write;
use std::sync::mpsc::channel;
use std::thread;

extern crate rustc_serialize;
use rustc_serialize::json::Json;
Expand All @@ -17,15 +17,17 @@ extern crate telemetry;
use telemetry::plain::*;
use telemetry::Flatten;


// A stopwatch for microsecond precision.
struct StopwatchUS {
pub value: time::Duration
pub value: time::Duration,
}
impl StopwatchUS {
fn span<F>(f: F) -> StopwatchUS where F: FnOnce() {
fn span<F>(f: F) -> StopwatchUS
where
F: FnOnce(),
{
StopwatchUS {
value: time::Duration::span(f)
value: time::Duration::span(f),
}
}
}
Expand All @@ -35,15 +37,15 @@ impl telemetry::Flatten for StopwatchUS {
Some(x) if x >= std::u32::MAX as i64 => std::u32::MAX,
Some(x) if x <= 0 => 0,
Some(x) => x as u32,
None => std::u32::MAX
None => std::u32::MAX,
}
}
}

struct Histograms {
/// The duration of execution of a recursive implementation of
/// Fibonacci's function, in microseconds.
fibonacci_us: telemetry::plain::Linear<StopwatchUS>
fibonacci_us: telemetry::plain::Linear<StopwatchUS>,
}

fn fibonacci(i: u32) -> u32 {
Expand All @@ -61,17 +63,20 @@ fn main() {
fibonacci_us: telemetry::plain::Linear::new(
&telemetry,
"FIBONACCI_DURATION_US".to_string(),
0 /* min */,
1_000_000 /* max */,
20 /* buckets */)
0, /* min */
1_000_000, /* max */
20, /* buckets */
),
};

// Measure a number of durations.
let mut handles = Vec::new();
for _ in 1..10 {
let hist = histograms.fibonacci_us.clone();
handles.push(thread::spawn(move || {
hist.record(StopwatchUS::span(|| {fibonacci(30);}));
hist.record(StopwatchUS::span(|| {
fibonacci(30);
}));
}));
}

Expand All @@ -81,7 +86,11 @@ fn main() {

// Now look at the histogram.
let (sender, receiver) = channel();
telemetry.to_json(telemetry::Subset::AllPlain, telemetry::SerializationFormat::SimpleJson, sender.clone());
telemetry.to_json(
telemetry::Subset::AllPlain,
telemetry::SerializationFormat::SimpleJson,
sender.clone(),
);
let plain = receiver.recv().unwrap();

print!("{}\n", plain);
Expand All @@ -93,8 +102,14 @@ fn main() {

// If we had keyed histograms, we should put them here, too.
// Now, add metadata.
storage.insert("application".to_string(), Json::String("telemetry example app".to_string()));
storage.insert("version".to_string(), Json::Array(vec![Json::I64(0), Json::I64(1), Json::I64(0)]));
storage.insert(
"application".to_string(),
Json::String("telemetry example app".to_string()),
);
storage.insert(
"version".to_string(),
Json::Array(vec![Json::I64(0), Json::I64(1), Json::I64(0)]),
);

print!("{}\n", Json::Object(storage.clone()).pretty());

Expand Down
13 changes: 5 additions & 8 deletions src/indexing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::marker::PhantomData;
use std::sync::atomic::{AtomicUsize, Ordering};


/// Witness type, used to specify that the data is specific to a plain histogram.
#[derive(Clone)]
pub struct Plain;
Expand All @@ -13,12 +12,12 @@ pub struct Map;
/// Witness type, used to specify that the data is specific to a map
/// histogram with keys of a specific type `T`.
pub struct Keyed<T> {
pub witness: PhantomData<T>
pub witness: PhantomData<T>,
}
impl<T> Clone for Keyed<T> {
fn clone(&self) -> Self {
Keyed {
witness: PhantomData
witness: PhantomData,
}
}
}
Expand All @@ -32,7 +31,7 @@ impl<T> Clone for Key<T> {
fn clone(&self) -> Self {
Key {
witness: PhantomData,
index: self.index
index: self.index,
}
}
}
Expand All @@ -54,17 +53,15 @@ impl KeyGenerator<Plain> {
pub fn next(&self) -> Key<Plain> {
Key {
index: self.counter.fetch_add(1, Ordering::Relaxed),
witness: PhantomData
witness: PhantomData,
}
}
}
impl KeyGenerator<Map> {
pub fn next<T>(&self) -> Key<Keyed<T>> {
Key {
index: self.counter.fetch_add(1, Ordering::Relaxed),
witness: PhantomData
witness: PhantomData,
}
}
}


Loading