test(header): add ContentType to server encode benchmark
This commit is contained in:
@@ -202,8 +202,8 @@ impl<'a, 'b> Formatter<'a, 'b> {
|
||||
pub fn fmt_line(&mut self, line: &fmt::Display) -> fmt::Result {
|
||||
use std::fmt::Write;
|
||||
match self.0 {
|
||||
Multi::Line(ref name, ref mut f) => {
|
||||
try!(f.write_str(*name));
|
||||
Multi::Line(name, ref mut f) => {
|
||||
try!(f.write_str(name));
|
||||
try!(f.write_str(": "));
|
||||
try!(write!(NewlineReplacer(*f), "{}", line));
|
||||
f.write_str("\r\n")
|
||||
@@ -256,6 +256,7 @@ impl<'a, H: Header> fmt::Debug for HeaderValueString<'a, H> {
|
||||
struct NewlineReplacer<'a, F: fmt::Write + 'a>(&'a mut F);
|
||||
|
||||
impl<'a, F: fmt::Write + 'a> fmt::Write for NewlineReplacer<'a, F> {
|
||||
#[inline]
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
let mut since = 0;
|
||||
for (i, &byte) in s.as_bytes().iter().enumerate() {
|
||||
@@ -271,6 +272,11 @@ impl<'a, F: fmt::Write + 'a> fmt::Write for NewlineReplacer<'a, F> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result {
|
||||
fmt::write(self, args)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -539,6 +545,7 @@ impl PartialEq for Headers {
|
||||
}
|
||||
|
||||
impl fmt::Display for Headers {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
for header in self.iter() {
|
||||
try!(fmt::Display::fmt(&header, f));
|
||||
@@ -548,6 +555,7 @@ impl fmt::Display for Headers {
|
||||
}
|
||||
|
||||
impl fmt::Debug for Headers {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_map()
|
||||
.entries(self.iter().map(|view| (view.0.as_ref(), ValueString(view.1))))
|
||||
@@ -610,12 +618,14 @@ impl<'a> HeaderView<'a> {
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for HeaderView<'a> {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.1.write_h1(&mut Formatter(Multi::Line(self.0.as_ref(), f)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> fmt::Debug for HeaderView<'a> {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Display::fmt(self, f)
|
||||
}
|
||||
@@ -659,8 +669,9 @@ impl<'a> FromIterator<HeaderView<'a>> for Headers {
|
||||
struct HeaderName(Ascii<Cow<'static, str>>);
|
||||
|
||||
impl fmt::Display for HeaderName {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Display::fmt(&self.0, f)
|
||||
fmt::Display::fmt(self.0.as_ref(), f)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -110,7 +110,6 @@ impl Http1Transaction for ServerTransaction {
|
||||
Encoder::chunked()
|
||||
};
|
||||
|
||||
|
||||
let init_cap = 30 + head.headers.len() * AVERAGE_HEADER_SIZE;
|
||||
dst.reserve(init_cap);
|
||||
debug!("writing headers = {:?}", head.headers);
|
||||
@@ -311,11 +310,13 @@ impl<'a> Iterator for HeadersAsBytesIter<'a> {
|
||||
struct FastWrite<'a>(&'a mut Vec<u8>);
|
||||
|
||||
impl<'a> fmt::Write for FastWrite<'a> {
|
||||
#[inline]
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
extend(self.0, s.as_bytes());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result {
|
||||
fmt::write(self, args)
|
||||
}
|
||||
@@ -424,21 +425,24 @@ mod tests {
|
||||
#[bench]
|
||||
fn bench_server_transaction_encode(b: &mut Bencher) {
|
||||
use ::http::MessageHead;
|
||||
use ::header::{Headers, ContentLength};
|
||||
use ::header::{Headers, ContentLength, ContentType};
|
||||
use ::{StatusCode, HttpVersion};
|
||||
b.bytes = 75;
|
||||
|
||||
let len = 108;
|
||||
b.bytes = len as u64;
|
||||
|
||||
let mut head = MessageHead {
|
||||
subject: StatusCode::Ok,
|
||||
headers: Headers::new(),
|
||||
version: HttpVersion::Http11,
|
||||
};
|
||||
head.headers.set(ContentLength(0));
|
||||
head.headers.set(ContentLength(10));
|
||||
head.headers.set(ContentType::json());
|
||||
|
||||
b.iter(|| {
|
||||
let mut vec = Vec::new();
|
||||
ServerTransaction::encode(head.clone(), &mut vec);
|
||||
assert_eq!(vec.len(), 75);
|
||||
assert_eq!(vec.len(), len);
|
||||
::test::black_box(vec);
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user