|
| 1 | +/* |
| 2 | + * Copyright 2019-2025 Didier Plaindoux |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +use bencher::{Bencher, benchmark_group, benchmark_main, black_box}; |
| 17 | +use celma_v0_core::parser::and::AndOperation; |
| 18 | +use celma_v0_core::parser::char::{alpha, digit}; |
| 19 | +use celma_v0_core::parser::core::eos; |
| 20 | +use celma_v0_core::parser::response::Response::{Reject, Success}; |
| 21 | +use celma_v0_core::parser::specs::Parse; |
| 22 | +use celma_v0_core::stream::char_stream::CharStream; |
| 23 | +use celma_v0_core::stream::position::Position; |
| 24 | +use celma_v0_core::stream::specs::Stream; |
| 25 | +use celma_v0_macro::parsec_rules; |
| 26 | + |
| 27 | +parsec_rules!( |
| 28 | + let http_header = request (header)* EOL -> {} |
| 29 | + let request = VERB S URI S VERSION EOL -> {} |
| 30 | + let header = NAME ':' S VALUE EOL -> {} |
| 31 | +); |
| 32 | + |
| 33 | +parsec_rules!( |
| 34 | + let VERB = ("GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "CONNECT" | "PATCH") -> {} |
| 35 | + let URI = ^(' ')+ -> {} |
| 36 | + let VERSION = "HTTP/" digit+ ('.' digit+)? -> {} |
| 37 | + let S = (' ' | '\t')+ -> {} |
| 38 | + let EOL = ('\r'? '\n') -> {} |
| 39 | + let NAME = (alpha | '-')+ -> {} |
| 40 | + let VALUE = ^EOL+ -> {} // Not precise enough |
| 41 | +); |
| 42 | + |
| 43 | +// ------------------------------------------------------------------------------------------------- |
| 44 | +// HTTP benchmarks |
| 45 | +// ------------------------------------------------------------------------------------------------- |
| 46 | + |
| 47 | +fn http_data(b: &mut Bencher) { |
| 48 | + let data = include_str!("data/request.http"); |
| 49 | + b.bytes = data.len() as u64; |
| 50 | + parse(b, data) |
| 51 | +} |
| 52 | + |
| 53 | +fn parse(b: &mut Bencher, buffer: &str) { |
| 54 | + let stream = CharStream::new_with_position(buffer, <usize>::new()); |
| 55 | + |
| 56 | + b.iter(|| { |
| 57 | + let response = http_header() |
| 58 | + .and_left(eos()) |
| 59 | + .parse(black_box(stream.clone())); |
| 60 | + |
| 61 | + match response { |
| 62 | + Success(_, _, _) => (), |
| 63 | + Reject(s, _) => panic!("parse error for {:?} at {:?}", s.next().0, s.position()), |
| 64 | + } |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +benchmark_group!(benches, http_data,); |
| 69 | + |
| 70 | +benchmark_main!(benches); |
0 commit comments