Skip to content

Commit

Permalink
test(http): addn a benchmark for encoding a response head
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed May 25, 2017
1 parent 2cbf4ef commit 78a8eed
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/http/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
27 changes: 25 additions & 2 deletions src/http/h1/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);


Expand Down Expand Up @@ -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);
})
}
}
4 changes: 2 additions & 2 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 78a8eed

Please sign in to comment.