Merge pull request #111 from reem/header-benchmarks

Add benchmarks for all implemented headers except set-cookie.
This commit is contained in:
Sean McArthur
2014-11-10 16:44:42 -08:00
14 changed files with 70 additions and 0 deletions

View File

@@ -65,3 +65,5 @@ impl HeaderFormat for Accept {
}
}
bench_header!(bench, Accept, { vec![b"text/plain; q=0.5, text/html".to_vec()] })

View File

@@ -178,3 +178,6 @@ mod tests {
}
}
bench_header!(raw, Authorization<String>, { vec![b"foo bar baz".to_vec()] })
bench_header!(basic, Authorization<Basic>, { vec![b"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==".to_vec()] })

View File

@@ -62,3 +62,6 @@ impl HeaderFormat for Connection {
}
}
bench_header!(close, Connection, { vec![b"close".to_vec()] })
bench_header!(keep_alive, Connection, { vec![b"keep-alive".to_vec()] })
bench_header!(header, Connection, { vec![b"authorization".to_vec()] })

View File

@@ -34,3 +34,5 @@ impl ContentLength {
len
}
}
bench_header!(bench, ContentLength, { vec![b"42349984".to_vec()] })

View File

@@ -27,3 +27,4 @@ impl HeaderFormat for ContentType {
}
}
bench_header!(bench, ContentType, { vec![b"application/json; charset=utf-8".to_vec()] })

View File

@@ -94,3 +94,5 @@ fn test_fmt() {
assert_eq!(headers.to_string()[], "Cookie: foo=bar; baz=quux\r\n");
}
bench_header!(bench, Cookies, { vec![b"foo=bar; baz=quux".to_vec()] })

View File

@@ -69,3 +69,7 @@ impl FromStr for Date {
}
}
bench_header!(imf_fixdate, Date, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] })
bench_header!(rfc_850, Date, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] })
bench_header!(asctime, Date, { vec![b"Sun Nov 6 08:49:37 1994".to_vec()] })

View File

@@ -94,3 +94,6 @@ mod tests {
}));
}
}
bench_header!(bench, Host, { vec![b"foo.com:3000".to_vec()] })

View File

@@ -33,3 +33,5 @@ impl HeaderFormat for Location {
}
}
bench_header!(bench, Location, { vec![b"http://foo.com/hello:3000".to_vec()] })

View File

@@ -25,8 +25,47 @@ use std::fmt::{mod, Show};
use std::from_str::FromStr;
use std::str::from_utf8;
macro_rules! bench_header(
($name:ident, $ty:ty, $value:expr) => {
#[cfg(test)]
mod $name {
use test::Bencher;
use std::fmt::{mod, Show};
use super::*;
use header::{Header, HeaderFormat};
struct HeaderFormatter($ty);
impl Show for HeaderFormatter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt_header(f)
}
}
#[bench]
fn bench_parse(b: &mut Bencher) {
let val = $value;
b.iter(|| {
let _: $ty= Header::parse_header(val[]).unwrap();
});
}
#[bench]
fn bench_format(b: &mut Bencher) {
let val = HeaderFormatter(Header::parse_header($value[]).unwrap());
b.iter(|| {
format!("{}", val);
});
}
}
}
)
/// Exposes the Accept header.
pub mod accept;
/// Exposes the Authorization header.
pub mod authorization;

View File

@@ -25,3 +25,5 @@ impl HeaderFormat for Server {
}
}
bench_header!(bench, Server, { vec![b"Some String".to_vec()] })

View File

@@ -84,3 +84,6 @@ impl HeaderFormat for TransferEncoding {
}
}
bench_header!(normal, TransferEncoding, { vec![b"chunked, gzip".to_vec()] })
bench_header!(ext, TransferEncoding, { vec![b"ext".to_vec()] })

View File

@@ -51,3 +51,5 @@ impl HeaderFormat for Upgrade {
}
}
bench_header!(bench, Upgrade, { vec![b"HTTP/2.0, RTA/x11, websocket".to_vec()] })

View File

@@ -25,3 +25,5 @@ impl HeaderFormat for UserAgent {
}
}
bench_header!(bench, UserAgent, { vec![b"cargo bench".to_vec()] })