Skip to content

Commit 5ec8529

Browse files
committed
Merge pull request #111 from reem/header-benchmarks
Add benchmarks for all implemented headers except set-cookie.
2 parents f5436d0 + c828466 commit 5ec8529

14 files changed

+70
-0
lines changed

common/accept.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@ impl HeaderFormat for Accept {
6565
}
6666
}
6767

68+
bench_header!(bench, Accept, { vec![b"text/plain; q=0.5, text/html".to_vec()] })
69+

common/authorization.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,6 @@ mod tests {
178178
}
179179

180180
}
181+
182+
bench_header!(raw, Authorization<String>, { vec![b"foo bar baz".to_vec()] })
183+
bench_header!(basic, Authorization<Basic>, { vec![b"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==".to_vec()] })

common/connection.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ impl HeaderFormat for Connection {
6262
}
6363
}
6464

65+
bench_header!(close, Connection, { vec![b"close".to_vec()] })
66+
bench_header!(keep_alive, Connection, { vec![b"keep-alive".to_vec()] })
67+
bench_header!(header, Connection, { vec![b"authorization".to_vec()] })

common/content_length.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ impl ContentLength {
3434
len
3535
}
3636
}
37+
38+
bench_header!(bench, ContentLength, { vec![b"42349984".to_vec()] })

common/content_type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ impl HeaderFormat for ContentType {
2727
}
2828
}
2929

30+
bench_header!(bench, ContentType, { vec![b"application/json; charset=utf-8".to_vec()] })

common/cookie.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,5 @@ fn test_fmt() {
9494

9595
assert_eq!(headers.to_string()[], "Cookie: foo=bar; baz=quux\r\n");
9696
}
97+
98+
bench_header!(bench, Cookies, { vec![b"foo=bar; baz=quux".to_vec()] })

common/date.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ impl FromStr for Date {
6969
}
7070
}
7171

72+
bench_header!(imf_fixdate, Date, { vec![b"Sun, 07 Nov 1994 08:48:37 GMT".to_vec()] })
73+
bench_header!(rfc_850, Date, { vec![b"Sunday, 06-Nov-94 08:49:37 GMT".to_vec()] })
74+
bench_header!(asctime, Date, { vec![b"Sun Nov 6 08:49:37 1994".to_vec()] })
75+

common/host.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,6 @@ mod tests {
9494
}));
9595
}
9696
}
97+
98+
bench_header!(bench, Host, { vec![b"foo.com:3000".to_vec()] })
99+

common/location.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ impl HeaderFormat for Location {
3333
}
3434
}
3535

36+
bench_header!(bench, Location, { vec![b"http://foo.com/hello:3000".to_vec()] })
37+

common/mod.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,47 @@ use std::fmt::{mod, Show};
2525
use std::from_str::FromStr;
2626
use std::str::from_utf8;
2727

28+
macro_rules! bench_header(
29+
($name:ident, $ty:ty, $value:expr) => {
30+
#[cfg(test)]
31+
mod $name {
32+
use test::Bencher;
33+
use std::fmt::{mod, Show};
34+
35+
use super::*;
36+
37+
use header::{Header, HeaderFormat};
38+
39+
struct HeaderFormatter($ty);
40+
41+
impl Show for HeaderFormatter {
42+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43+
self.0.fmt_header(f)
44+
}
45+
}
46+
47+
#[bench]
48+
fn bench_parse(b: &mut Bencher) {
49+
let val = $value;
50+
b.iter(|| {
51+
let _: $ty= Header::parse_header(val[]).unwrap();
52+
});
53+
}
54+
55+
#[bench]
56+
fn bench_format(b: &mut Bencher) {
57+
let val = HeaderFormatter(Header::parse_header($value[]).unwrap());
58+
b.iter(|| {
59+
format!("{}", val);
60+
});
61+
}
62+
}
63+
}
64+
)
65+
2866
/// Exposes the Accept header.
2967
pub mod accept;
68+
3069
/// Exposes the Authorization header.
3170
pub mod authorization;
3271

common/server.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ impl HeaderFormat for Server {
2525
}
2626
}
2727

28+
bench_header!(bench, Server, { vec![b"Some String".to_vec()] })
29+

common/transfer_encoding.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,6 @@ impl HeaderFormat for TransferEncoding {
8484
}
8585
}
8686

87+
bench_header!(normal, TransferEncoding, { vec![b"chunked, gzip".to_vec()] })
88+
bench_header!(ext, TransferEncoding, { vec![b"ext".to_vec()] })
89+

common/upgrade.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ impl HeaderFormat for Upgrade {
5151
}
5252
}
5353

54+
bench_header!(bench, Upgrade, { vec![b"HTTP/2.0, RTA/x11, websocket".to_vec()] })
55+

common/user_agent.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ impl HeaderFormat for UserAgent {
2525
}
2626
}
2727

28+
bench_header!(bench, UserAgent, { vec![b"cargo bench".to_vec()] })
29+

0 commit comments

Comments
 (0)