1
2use std::time::Duration;
3
4use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
5
6const REQ_SHORT: &[u8] = b"\
7GET / HTTP/1.0\r\n\
8Host: example.com\r\n\
9Cookie: session=60; user_id=1\r\n\r\n";
10
11const REQ: &[u8] = b"\
12GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1\r\n\
13Host: www.kittyhell.com\r\n\
14User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; ja-JP-mac; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 Pathtraq/0.9\r\n\
15Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n\
16Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n\
17Accept-Encoding: gzip,deflate\r\n\
18Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n\
19Keep-Alive: 115\r\n\
20Connection: keep-alive\r\n\
21Cookie: wp_ozh_wsa_visits=2; wp_ozh_wsa_visit_lasttime=xxxxxxxxxx; __utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; __utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor.com|utmcct=/reader/|utmcmd=referral|padding=under256\r\n\r\n";
22
23fn req(c: &mut Criterion) {
24 let mut headers = [httparse::Header{ name: "", value: &[] }; 16];
25 let mut req = httparse::Request::new(&mut headers);
26
27 c.benchmark_group("req")
28 .throughput(Throughput::Bytes(REQ.len() as u64))
29 .bench_function("req", |b| b.iter(|| {
30 assert_eq!(black_box(req.parse(REQ).unwrap()), httparse::Status::Complete(REQ.len()));
31 }));
32}
33
34fn req_short(c: &mut Criterion) {
35 let mut headers = [httparse::Header{ name: "", value: &[] }; 16];
36 let mut req = httparse::Request::new(&mut headers);
37
38 c.benchmark_group("req_short")
39 .throughput(Throughput::Bytes(REQ_SHORT.len() as u64))
40 .bench_function("req_short", |b| b.iter(|| {
41 assert_eq!(black_box(req.parse(REQ_SHORT).unwrap()), httparse::Status::Complete(REQ_SHORT.len()));
42 }));
43}
44
45const RESP_SHORT: &[u8] = b"\
46HTTP/1.0 200 OK\r\n\
47Date: Wed, 21 Oct 2015 07:28:00 GMT\r\n\
48Set-Cookie: session=60; user_id=1\r\n\r\n";
49
50// These particular headers don't all make semantic sense for a response, but they're syntactically valid.
51const RESP: &[u8] = b"\
52HTTP/1.1 200 OK\r\n\
53Date: Wed, 21 Oct 2015 07:28:00 GMT\r\n\
54Host: www.kittyhell.com\r\n\
55User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; ja-JP-mac; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 Pathtraq/0.9\r\n\
56Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n\
57Accept-Language: ja,en-us;q=0.7,en;q=0.3\r\n\
58Accept-Encoding: gzip,deflate\r\n\
59Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7\r\n\
60Keep-Alive: 115\r\n\
61Connection: keep-alive\r\n\
62Cookie: wp_ozh_wsa_visits=2; wp_ozh_wsa_visit_lasttime=xxxxxxxxxx; __utma=xxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.xxxxxxxxxx.x; __utmz=xxxxxxxxx.xxxxxxxxxx.x.x.utmccn=(referral)|utmcsr=reader.livedoor.com|utmcct=/reader/|utmcmd=referral|padding=under256\r\n\r\n";
63
64fn resp(c: &mut Criterion) {
65 let mut headers = [httparse::Header{ name: "", value: &[] }; 16];
66 let mut resp = httparse::Response::new(&mut headers);
67
68 c.benchmark_group("resp")
69 .throughput(Throughput::Bytes(RESP.len() as u64))
70 .bench_function("resp", |b| b.iter(|| {
71 assert_eq!(black_box(resp.parse(RESP).unwrap()), httparse::Status::Complete(RESP.len()));
72 }));
73}
74
75fn resp_short(c: &mut Criterion) {
76 let mut headers = [httparse::Header{ name: "", value: &[] }; 16];
77 let mut resp = httparse::Response::new(&mut headers);
78
79 c.benchmark_group("resp_short")
80 .throughput(Throughput::Bytes(RESP_SHORT.len() as u64))
81 .bench_function("resp_short", |b| b.iter(|| {
82 assert_eq!(black_box(resp.parse(RESP_SHORT).unwrap()), httparse::Status::Complete(RESP_SHORT.len()));
83 }));
84}
85
86criterion_group!{
87 name = benches;
88 config = Criterion::default().sample_size(100).measurement_time(Duration::from_secs(10));
89 targets = req, req_short, resp, resp_short
90}
91criterion_main!(benches);
92