test(http): addn a benchmark for encoding a response head

This commit is contained in:
Sean McArthur
2017-05-25 11:41:56 -07:00
parent 2cbf4ef85f
commit 78a8eed7f1
3 changed files with 28 additions and 5 deletions

View File

@@ -236,7 +236,7 @@ where I: AsyncRead + AsyncWrite,
let wants_keep_alive = head.should_keep_alive();
self.state.keep_alive &= wants_keep_alive;
let mut buf = Vec::new();
let encoder = T::encode(&mut head, &mut buf);
let encoder = T::encode(head, &mut buf);
//TODO: handle when there isn't enough room to buffer the head
assert!(self.io.buffer(buf) > 0);
self.state.writing = if body {

View File

@@ -86,7 +86,7 @@ impl Http1Transaction for ServerTransaction {
}
fn encode(head: &mut MessageHead<Self::Outgoing>, dst: &mut Vec<u8>) -> Encoder {
fn encode(mut head: MessageHead<Self::Outgoing>, dst: &mut Vec<u8>) -> Encoder {
use ::header;
trace!("writing head: {:?}", head);
@@ -213,7 +213,7 @@ impl Http1Transaction for ClientTransaction {
}
}
fn encode(head: &mut MessageHead<Self::Outgoing>, dst: &mut Vec<u8>) -> Encoder {
fn encode(mut head: MessageHead<Self::Outgoing>, dst: &mut Vec<u8>) -> Encoder {
trace!("writing head: {:?}", head);
@@ -421,4 +421,27 @@ mod tests {
}
}
}
#[cfg(feature = "nightly")]
#[bench]
fn bench_server_transaction_encode(b: &mut Bencher) {
use ::http::MessageHead;
use ::header::{Headers, ContentLength};
use ::{StatusCode, HttpVersion};
b.bytes = 75;
let mut head = MessageHead {
subject: StatusCode::Ok,
headers: Headers::new(),
version: HttpVersion::Http11,
};
head.headers.set(ContentLength(0));
b.iter(|| {
let mut vec = Vec::new();
ServerTransaction::encode(head.clone(), &mut vec);
assert_eq!(vec.len(), 75);
::test::black_box(vec);
})
}
}

View File

@@ -42,7 +42,7 @@ macro_rules! nonblocking {
*/
/// An Incoming Message head. Includes request/status line, and headers.
#[derive(Debug, Default, PartialEq)]
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MessageHead<S> {
/// HTTP version of the message.
pub version: HttpVersion,
@@ -145,7 +145,7 @@ pub trait Http1Transaction {
type Outgoing: Default;
fn parse(bytes: &mut BytesMut) -> ParseResult<Self::Incoming>;
fn decoder(head: &MessageHead<Self::Incoming>) -> ::Result<h1::Decoder>;
fn encode(head: &mut MessageHead<Self::Outgoing>, dst: &mut Vec<u8>) -> h1::Encoder;
fn encode(head: MessageHead<Self::Outgoing>, dst: &mut Vec<u8>) -> h1::Encoder;
fn should_set_length(head: &MessageHead<Self::Outgoing>) -> bool;
}