1 | |
2 | use std::time::Duration; |
3 | |
4 | use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput}; |
5 | |
6 | const REQ_SHORT: &[u8] = b"\ |
7 | GET / HTTP/1.0 \r\n\ |
8 | Host: example.com \r\n\ |
9 | Cookie: session=60; user_id=1 \r\n\r\n" ; |
10 | |
11 | const REQ: &[u8] = b"\ |
12 | GET /wp-content/uploads/2010/03/hello-kitty-darth-vader-pink.jpg HTTP/1.1 \r\n\ |
13 | Host: www.kittyhell.com \r\n\ |
14 | User-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\ |
15 | Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \r\n\ |
16 | Accept-Language: ja,en-us;q=0.7,en;q=0.3 \r\n\ |
17 | Accept-Encoding: gzip,deflate \r\n\ |
18 | Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7 \r\n\ |
19 | Keep-Alive: 115 \r\n\ |
20 | Connection: keep-alive \r\n\ |
21 | Cookie: 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 | |
23 | fn 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 | |
34 | fn 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 | |
45 | const RESP_SHORT: &[u8] = b"\ |
46 | HTTP/1.0 200 OK \r\n\ |
47 | Date: Wed, 21 Oct 2015 07:28:00 GMT \r\n\ |
48 | Set-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. |
51 | const RESP: &[u8] = b"\ |
52 | HTTP/1.1 200 OK \r\n\ |
53 | Date: Wed, 21 Oct 2015 07:28:00 GMT \r\n\ |
54 | Host: www.kittyhell.com \r\n\ |
55 | User-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\ |
56 | Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \r\n\ |
57 | Accept-Language: ja,en-us;q=0.7,en;q=0.3 \r\n\ |
58 | Accept-Encoding: gzip,deflate \r\n\ |
59 | Accept-Charset: Shift_JIS,utf-8;q=0.7,*;q=0.7 \r\n\ |
60 | Keep-Alive: 115 \r\n\ |
61 | Connection: keep-alive \r\n\ |
62 | Cookie: 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 | |
64 | fn 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 | |
75 | fn 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 | |
86 | criterion_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 | } |
91 | criterion_main!(benches); |
92 | |