Skip to content

Commit

Permalink
add bench_header utility
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Jan 2, 2019
1 parent 3f50104 commit a987a56
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ bytes = "0.4"
mime = "0.3"
sha-1 = "0.8"
time = "0.1"

[features]
nightly = []
5 changes: 4 additions & 1 deletion src/common/content_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,8 @@ mod tests {
Some(ContentType::json()),
);
}

bench_header!(bench_plain, ContentType, "text/plain");
bench_header!(bench_json, ContentType, "application/json");
bench_header!(bench_formdata, ContentType, "multipart/form-data; boundary=---------------abcd");
}
//bench_header!(bench, ContentType, { vec![b"application/json".to_vec()] });
39 changes: 39 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,45 @@ fn test_encode<T: ::Header>(header: T) -> ::http::HeaderMap {
map
}

#[cfg(test)]
macro_rules! bench_header {
($mod:ident, $ty:ident, $value:expr) => {
#[cfg(feature = "nightly")]
mod $mod {
use super::$ty;
use ::HeaderMapExt;

#[bench]
fn bench_decode(b: &mut ::test::Bencher) {
let mut map = ::http::HeaderMap::new();
map.append(
<$ty as ::Header>::name(),
$value.parse().expect("HeaderValue::from_str($value)")
);
b.bytes = $value.len() as u64;
b.iter(|| {
map.typed_get::<$ty>().unwrap();
});
}

#[bench]
fn bench_encode(b: &mut ::test::Bencher) {
let mut map = ::http::HeaderMap::new();
map.append(
<$ty as ::Header>::name(),
$value.parse().expect("HeaderValue::from_str($value)")
);
let typed = map.typed_get::<$ty>().unwrap();
b.bytes = $value.len() as u64;
b.iter(|| {
map.typed_insert(typed.clone());
map.clear();
});
}
}
}
}

//mod accept;
//mod accept_charset;
//mod accept_encoding;
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![cfg_attr(test, deny(warnings))]
#![cfg_attr(all(test, feature = "nightly"), feature(test))]

extern crate base64;
#[macro_use]
Expand All @@ -80,6 +81,8 @@ extern crate headers_derive;
extern crate http;
extern crate mime;
extern crate sha1;
#[cfg(all(test, feature = "nightly"))]
extern crate test;
extern crate time;

pub use headers_core::{
Expand Down

0 comments on commit a987a56

Please sign in to comment.