Skip to content

Commit

Permalink
bring back the INI bench without CompleteByteSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Mar 6, 2018
1 parent 8aaab61 commit 48ab899
Show file tree
Hide file tree
Showing 2 changed files with 346 additions and 85 deletions.
152 changes: 67 additions & 85 deletions benches/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,45 @@ extern crate test;
#[macro_use]
extern crate nom;

use nom::{alphanumeric, multispace, space};
use nom::types::CompleteByteSlice;
use nom::{IResult, space, alphanumeric, multispace};

use std::str;
use std::collections::HashMap;

named!(category<CompleteByteSlice, &str>, map_res!(
named!(category<&str>, map_res!(
delimited!(
char!('['),
take_while!(call!(|c| c != b']')),
take_while!(call!(|c| c != ']' as u8)),
char!(']')
),
complete_byte_slice_to_str
str::from_utf8
));

fn complete_byte_slice_to_str<'a>(s: CompleteByteSlice<'a>) -> Result<&'a str, str::Utf8Error> {
str::from_utf8(s.0)
}

named!(key_value <CompleteByteSlice,(&str,&str)>,
named!(key_value <&[u8],(&str,&str)>,
do_parse!(
key: map_res!(alphanumeric, complete_byte_slice_to_str)
key: map_res!(alphanumeric, str::from_utf8)
>> opt!(space)
>> char!('=')
>> opt!(space)
>> val: map_res!(
take_while!(call!(|c| c != b'\n' && c != b';')),
complete_byte_slice_to_str
take_while!(call!(|c| c != '\n' as u8 && c != ';' as u8)),
str::from_utf8
)
>> opt!(pair!(char!(';'), take_while!(call!(|c| c != b'\n'))))
>> opt!(pair!(char!(';'), take_while!(call!(|c| c != '\n' as u8))))
>> (key, val)
)
);

named!(keys_and_values<CompleteByteSlice, HashMap<&str, &str> >,

named!(keys_and_values<&[u8], HashMap<&str, &str> >,
map!(
many0!(terminated!(key_value, opt!(multispace))),
|vec: Vec<_>| vec.into_iter().collect()
)
);

named!(category_and_keys<CompleteByteSlice,(&str,HashMap<&str,&str>)>,

named!(category_and_keys<&[u8],(&str,HashMap<&str,&str>)>,
do_parse!(
category: category >>
opt!(multispace) >>
Expand All @@ -54,7 +51,7 @@ named!(category_and_keys<CompleteByteSlice,(&str,HashMap<&str,&str>)>,
)
);

named!(categories<CompleteByteSlice, HashMap<&str, HashMap<&str,&str> > >,
named!(categories<&[u8], HashMap<&str, HashMap<&str,&str> > >,
map!(
many0!(
separated_pair!(
Expand All @@ -70,143 +67,130 @@ named!(categories<CompleteByteSlice, HashMap<&str, HashMap<&str,&str> > >,
)
);

/*
#[test]
fn parse_category_test() {
let ini_file = CompleteByteSlice(
b"[category]
let ini_file = &b"[category]
parameter=value
key = value2",
);
key = value2"[..];
let ini_without_category = CompleteByteSlice(
b"\n\nparameter=value
key = value2",
);
let ini_without_category = &b"parameter=value
key = value2"[..];
let res = category(ini_file);
println!("{:?}", res);
match res {
Ok((i, o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i.0), o),
_ => println!("error"),
IResult::Done(i, o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
assert_eq!(res, Ok((ini_without_category, "category")));
assert_eq!(res, IResult::Done(ini_without_category, "category"));
}
#[test]
fn parse_key_value_test() {
let ini_file = CompleteByteSlice(
b"parameter=value
key = value2",
);
let ini_file = &b"parameter=value
key = value2"[..];
let ini_without_key_value = CompleteByteSlice(b"\nkey = value2");
let ini_without_key_value = &b"key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i.0), o1, o2),
_ => println!("error"),
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_key_value_with_space_test() {
let ini_file = CompleteByteSlice(
b"parameter = value
key = value2",
);
let ini_file = &b"parameter = value
key = value2"[..];
let ini_without_key_value = CompleteByteSlice(b"\nkey = value2");
let ini_without_key_value = &b"key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i.0), o1, o2),
_ => println!("error"),
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_key_value_with_comment_test() {
let ini_file = CompleteByteSlice(
b"parameter=value;abc
key = value2",
);
let ini_file = &b"parameter=value;abc
key = value2"[..];
let ini_without_key_value = CompleteByteSlice(b"\nkey = value2");
let ini_without_key_value = &b"key = value2"[..];
let res = key_value(ini_file);
println!("{:?}", res);
match res {
Ok((i, (o1, o2))) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i.0), o1, o2),
_ => println!("error"),
IResult::Done(i, (o1, o2)) => println!("i: {:?} | o: ({:?},{:?})", str::from_utf8(i), o1, o2),
_ => println!("error")
}
assert_eq!(res, Ok((ini_without_key_value, ("parameter", "value"))));
assert_eq!(res, IResult::Done(ini_without_key_value, ("parameter", "value")));
}
#[test]
fn parse_multiple_keys_and_values_test() {
let ini_file = CompleteByteSlice(
b"parameter=value;abc
let ini_file = &b"parameter=value;abc
key = value2
[category]",
);
[category]"[..];
let ini_without_key_value = CompleteByteSlice(b"[category]");
let ini_without_key_value = &b"[category]"[..];
let res = keys_and_values(ini_file);
println!("{:?}", res);
match res {
Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i.0), o),
_ => println!("error"),
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected: HashMap<&str, &str> = HashMap::new();
expected.insert("parameter", "value");
expected.insert("key", "value2");
assert_eq!(res, Ok((ini_without_key_value, expected)));
assert_eq!(res, IResult::Done(ini_without_key_value, expected));
}
#[test]
fn parse_category_then_multiple_keys_and_values_test() {
//FIXME: there can be an empty line or a comment line after a category
let ini_file = CompleteByteSlice(
b"[abcd]
let ini_file = &b"[abcd]
parameter=value;abc
key = value2
[category]",
);
[category]"[..];
let ini_after_parser = CompleteByteSlice(b"[category]");
let ini_after_parser = &b"[category]"[..];
let res = category_and_keys(ini_file);
println!("{:?}", res);
match res {
Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i.0), o),
_ => println!("error"),
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected_h: HashMap<&str, &str> = HashMap::new();
expected_h.insert("parameter", "value");
expected_h.insert("key", "value2");
assert_eq!(res, Ok((ini_after_parser, ("abcd", expected_h))));
assert_eq!(res, IResult::Done(ini_after_parser, ("abcd", expected_h)));
}
#[test]
fn parse_multiple_categories_test() {
let ini_file = CompleteByteSlice(
b"[abcd]
let ini_file = &b"[abcd]
parameter=value;abc
Expand All @@ -215,16 +199,15 @@ key = value2
[category]
parameter3=value3
key4 = value4
",
);
\0"[..];
let ini_after_parser = CompleteByteSlice(b"");
let ini_after_parser = &b"\0"[..];
let res = categories(ini_file);
//println!("{:?}", res);
match res {
Ok((i, ref o)) => println!("i: {:?} | o: {:?}", str::from_utf8(i.0), o),
_ => println!("error"),
IResult::Done(i, ref o) => println!("i: {:?} | o: {:?}", str::from_utf8(i), o),
_ => println!("error")
}
let mut expected_1: HashMap<&str, &str> = HashMap::new();
Expand All @@ -234,11 +217,11 @@ key4 = value4
expected_2.insert("parameter3", "value3");
expected_2.insert("key4", "value4");
let mut expected_h: HashMap<&str, HashMap<&str, &str>> = HashMap::new();
expected_h.insert("abcd", expected_1);
expected_h.insert("abcd", expected_1);
expected_h.insert("category", expected_2);
assert_eq!(res, Ok((ini_after_parser, expected_h)));
assert_eq!(res, IResult::Done(ini_after_parser, expected_h));
}

*/

#[bench]
fn bench_ini(b: &mut test::Bencher) {
Expand All @@ -250,9 +233,9 @@ organization=Acme Widgets Inc.
server=192.0.2.62
port=143
file=payroll.dat
";
\0";

b.iter(|| categories(CompleteByteSlice(str.as_bytes())).unwrap());
b.iter(|| categories(str.as_bytes()).unwrap());
b.bytes = str.len() as u64;
}

Expand All @@ -261,19 +244,18 @@ fn bench_ini_keys_and_values(b: &mut test::Bencher) {
let str = "server=192.0.2.62
port=143
file=payroll.dat
";
\0";

named!(acc<CompleteByteSlice, Vec<(&str,&str)> >, many0!(key_value));
named!(acc< Vec<(&str,&str)> >, many0!(key_value));

b.iter(|| acc(CompleteByteSlice(str.as_bytes())).unwrap());
b.iter(|| acc(str.as_bytes()).unwrap());
b.bytes = str.len() as u64;
}

#[bench]
fn bench_ini_key_value(b: &mut test::Bencher) {
let str = "server=192.0.2.62\n";

b.iter(|| key_value(CompleteByteSlice(str.as_bytes())).unwrap());
b.iter(|| key_value(str.as_bytes()).unwrap());
b.bytes = str.len() as u64;
}

Loading

0 comments on commit 48ab899

Please sign in to comment.