Skip to content
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
23 changes: 14 additions & 9 deletions examples/acme.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
extern crate hornet;
extern crate hornet;
extern crate rand;

use hornet::client::Client;
use hornet::client::metric::*;
use hornet::client::Client;
use rand::random;
use std::thread;
use std::time::Duration;

fn main() {

let products = ["Anvils", "Rockets", "Giant_Rubber_Bands"];
let indom = Indom::new(
&products,
"Acme products",
"Most popular products produced by the Acme Corporation"
).unwrap();

"Most popular products produced by the Acme Corporation",
)
.unwrap();

/* create three instance metrics */

let mut counts = InstanceMetric::new(
Expand Down Expand Up @@ -52,8 +52,10 @@ fn main() {

/* create a client, register the metrics with it, and export them */

Client::new("acme").unwrap()
.export(&mut [&mut counts, &mut times, &mut queue_times]).unwrap();
Client::new("acme")
.unwrap()
.export(&mut [&mut counts, &mut times, &mut queue_times])
.unwrap();

/* update metrics */

Expand All @@ -74,7 +76,10 @@ fn main() {
let queued_product = products[i];

let queue_time = *queue_times.val(queued_product).unwrap();
queue_times.set_val(queued_product, queue_time + 1).unwrap().unwrap();
queue_times
.set_val(queued_product, queue_time + 1)
.unwrap()
.unwrap();
}
}
}
Expand Down
22 changes: 9 additions & 13 deletions examples/growth.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
extern crate hornet;
extern crate hornet;
extern crate rand;

use hornet::client::Client;
use hornet::client::metric::*;
use hornet::client::Client;
use std::thread;
use std::time::Duration;

/* this examples demonstrates use of Counter and GaugeVector */

fn main() {
let mut n = Counter::new("n", 0, "Input to various functions", "").unwrap();

let mut n = Counter::new(
"n",
0,
"Input to various functions", "").unwrap();

let mut f_n = GaugeVector::new(
let mut f_n = GaugeVector::new(
"functions",
0.0,
&["log2(n)", "nlog2(n)", "n^2", "n^3", "n^4", "2^n", "10^n"],
"Growth of various functions", "").unwrap();
"Growth of various functions",
"",
)
.unwrap();

let client = Client::new("growth").unwrap();
client.export(&mut [&mut n, &mut f_n]).unwrap();
println!("Values mapped at {}", client.mmv_path().to_str().unwrap());

for _ in 0..60 {

let val = n.val() as f64;

f_n.set("log2(n)", val.log2()).unwrap().unwrap();
f_n.set("nlog2(n)", val*val.log2()).unwrap().unwrap();
f_n.set("nlog2(n)", val * val.log2()).unwrap().unwrap();
f_n.set("n^2", val.powi(2)).unwrap().unwrap();
f_n.set("n^3", val.powi(3)).unwrap().unwrap();
f_n.set("n^4", val.powi(4)).unwrap().unwrap();
Expand All @@ -40,7 +38,5 @@ fn main() {
n.up().unwrap();

thread::sleep(Duration::from_secs(1));

}

}
28 changes: 16 additions & 12 deletions examples/histogram.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
extern crate hornet;
extern crate hornet;
extern crate rand;

use hornet::client::Client;
use hornet::client::metric::*;
use rand::thread_rng;
use hornet::client::Client;
use rand::distributions::{IndependentSample, Range};
use rand::thread_rng;

/*
For detailed usage and behaviour of the underlying HDR histogram object,
check out jonhoo's hdrsample crate at https://github.com/jonhoo/hdrsample
check out jonhoo's hdrsample crate at https://github.com/jonhoo/hdrsample
*/

fn main() {

/* pick parameters for the histogram */

let low = 1;
let high = 100;
let significant_figures = 5;

/* create a histogram metric */
/* create a histogram metric */

let mut hist = Histogram::new(
let mut hist = Histogram::new(
"histogram",
low,
high,
significant_figures,
Unit::new().count(Count::One, 1).unwrap(),
"Simple histogram example", ""
).unwrap();
"Simple histogram example",
"",
)
.unwrap();

/* export it to an mmv */

let client = Client::new("histogram").unwrap();
client.export(&mut [&mut hist]).unwrap();
println!("Histogram mapped at {}", client.mmv_path().to_str().unwrap());
println!(
"Histogram mapped at {}",
client.mmv_path().to_str().unwrap()
);

/* record 100 random values */

Expand All @@ -46,7 +50,7 @@ fn main() {
}

/* record a single random value 100 times */

hist.record_n(range.ind_sample(&mut thread_rng), 100).unwrap();

hist.record_n(range.ind_sample(&mut thread_rng), 100)
.unwrap();
}
21 changes: 10 additions & 11 deletions examples/http_download.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extern crate hornet;
extern crate curl;
extern crate hornet;

use hornet::client::Client;
use hornet::client::metric::*;
use curl::easy::Easy;
use hornet::client::metric::*;
use hornet::client::Client;

/*
this example uses the Timer metric to measure time spent
Expand All @@ -13,18 +13,17 @@ use curl::easy::Easy;
const URL: &'static str = "https://codeload.github.com/torvalds/linux/zip/master";

fn main() {

let mut timer = Timer::new(
"time",
Time::Sec,
"Time elapsed downloading", "").unwrap();
let mut timer = Timer::new("time", Time::Sec, "Time elapsed downloading", "").unwrap();

let mut bytes = Metric::new(
"bytes",
0,
Semantics::Discrete,
Unit::new().space(Space::Byte, 1).unwrap(),
"Bytes downloaded so far", "").unwrap();
"Bytes downloaded so far",
"",
)
.unwrap();

let client = Client::new("download").unwrap();
client.export(&mut [&mut timer, &mut bytes]).unwrap();
Expand All @@ -38,11 +37,11 @@ fn main() {
timer.start().ok();
bytes.set_val(bytes_downloaded as u64).unwrap();
true
}).unwrap();
})
.unwrap();

println!("Downloading from {}", URL);
println!("Progress mapped at {}", client.mmv_path().to_str().unwrap());

easy.perform().unwrap();

}
51 changes: 21 additions & 30 deletions examples/http_server.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
extern crate hornet;
extern crate hyper;
extern crate futures;
extern crate hornet;
extern crate hyper;

use std::sync::{Mutex, Arc};
use hornet::client::Client;
use hornet::client::metric::*;
use futures::future::FutureResult;
use hornet::client::metric::*;
use hornet::client::Client;
use hyper::header::{ContentLength, ContentType};
use hyper::server::{Http, Request, Response, Service};
use hyper::{Get, StatusCode};
use hyper::server::{Http, Service, Request, Response};
use std::sync::{Arc, Mutex};

/*
records count of HTTP GET requests on localhost:8000
Expand All @@ -20,7 +20,7 @@ use hyper::server::{Http, Service, Request, Response};
static URL: &'static str = "127.0.0.1:8000";

struct HTTPCounterService {
arc: Arc<Mutex<Counter>>
arc: Arc<Mutex<Counter>>,
}

impl Service for HTTPCounterService {
Expand All @@ -32,7 +32,6 @@ impl Service for HTTPCounterService {
fn call(&self, req: Request) -> Self::Future {
futures::future::ok(match (req.method(), req.path()) {
(&Get, "/") => {

let mut counter = self.arc.lock().unwrap();

/* increase the counter value by one */
Expand All @@ -43,54 +42,46 @@ impl Service for HTTPCounterService {
.with_header(ContentLength(body.len() as u64))
.with_header(ContentType::plaintext())
.with_body(body)

},
_ => {
Response::new()
.with_status(StatusCode::NotFound)
}
_ => Response::new().with_status(StatusCode::NotFound),
})
}

}

fn main() {
/* create a counter metric */

/* create a counter metric */

let mut counter = Counter::new(
let mut counter = Counter::new(
"get",
0, // initial value
"GET request count", // short description
&format!("Count of GET requests on http://{}/", URL) // long description
).unwrap();
0, // initial value
"GET request count", // short description
&format!("Count of GET requests on http://{}/", URL), // long description
)
.unwrap();

/* export it to an mmv */

let client = Client::new("localhost.http").unwrap();
client.export(&mut [&mut counter]).unwrap();

/*
/*
since the counter could be updated concurrently, wrap it
in a mutex. to have shared ownership of the mutex itself,
wrap it in an atomic reference counting pointer
*/

let mutex = Mutex::new(counter);
let arc = Arc::new(mutex);

/* create and run the server */

let addr = URL.parse().unwrap();
let server = Http::new().bind(&addr, move || {
Ok(HTTPCounterService {
arc: arc.clone()
})
}).unwrap();
let server = Http::new()
.bind(&addr, move || Ok(HTTPCounterService { arc: arc.clone() }))
.unwrap();

println!("Listening on http://{}", server.local_addr().unwrap());
println!("Counter mapped at {}", client.mmv_path().to_str().unwrap());

server.run().unwrap();

server.run().unwrap();
}
29 changes: 15 additions & 14 deletions examples/iron_middleware.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
extern crate iron;
extern crate hornet;
extern crate iron;

use std::sync::Mutex;
use hornet::client::Client;
use hornet::client::metric::*;
use iron::prelude::*;
use iron::middleware::BeforeMiddleware;
use hornet::client::Client;
use iron::method::Method;
use iron::middleware::BeforeMiddleware;
use iron::prelude::*;
use iron::status;
use std::sync::Mutex;

/*
this examples demonstrates usage of CountVector metric
Expand All @@ -21,7 +21,7 @@ fn method_str(method: &Method) -> String {
}

struct MethodCounter {
pub metric: Mutex<CountVector>
pub metric: Mutex<CountVector>,
}

impl MethodCounter {
Expand All @@ -37,20 +37,23 @@ impl MethodCounter {
&method_str(&Method::Delete),
&method_str(&Method::Head),
&method_str(&Method::Trace),
&method_str(&Method::Connect)
&method_str(&Method::Connect),
],
"Counts of recieved HTTP request methods", "").unwrap();

"Counts of recieved HTTP request methods",
"",
)
.unwrap();

MethodCounter {
metric: Mutex::new(metric)
metric: Mutex::new(metric),
}
}
}

impl BeforeMiddleware for MethodCounter {
fn before(&self, req: &mut Request) -> IronResult<()> {
match &req.method {
&Method::Extension(_) => {},
&Method::Extension(_) => {}
_ => {
let mut counter = self.metric.lock().unwrap();
counter.up(&method_str(&req.method)).unwrap().unwrap();
Expand All @@ -73,9 +76,7 @@ fn main() {
client.export(&mut [&mut *metric]).unwrap();
}

let mut chain = Chain::new(|_: &mut Request| {
Ok(Response::with((status::Ok, "Hello World!")))
});
let mut chain = Chain::new(|_: &mut Request| Ok(Response::with((status::Ok, "Hello World!"))));
chain.link_before(method_counter);

println!("Listening on http://{}", URL);
Expand Down
Loading