benchmark and improve method parsing

This commit is contained in:
nora 2022-02-20 16:24:11 +01:00
parent 186f744715
commit 45196c722c
10 changed files with 593 additions and 210 deletions

View file

@ -1,4 +1,5 @@
use crate::error::{ConException, TransError};
use rand::Rng;
use std::collections::HashMap;
mod generated;
@ -56,3 +57,70 @@ pub fn parse_method(payload: &[u8]) -> Result<generated::Method, TransError> {
Err(nom::Err::Failure(err) | nom::Err::Error(err)) => Err(err),
}
}
/// Allows the creation of a random instance of that type
pub trait RandomMethod<R: Rng> {
fn random(rng: &mut R) -> Self;
}
impl<R: Rng> RandomMethod<R> for String {
fn random(rng: &mut R) -> Self {
let n = rng.gen_range(0_u16..9999);
format!("string{n}")
}
}
impl<R: Rng, T: RandomMethod<R>> RandomMethod<R> for Vec<T> {
fn random(rng: &mut R) -> Self {
let len = rng.gen_range(1_usize..10);
let mut vec = Vec::with_capacity(len);
(0..len).for_each(|_| vec.push(RandomMethod::random(rng)));
vec
}
}
macro_rules! rand_random_method {
($($ty:ty),+) => {
$(
impl<R: Rng> RandomMethod<R> for $ty {
fn random(rng: &mut R) -> Self {
rng.gen()
}
})+
};
}
rand_random_method!(bool, u8, i8, u16, i16, u32, i32, u64, i64, f32, f64);
impl<R: Rng> RandomMethod<R> for HashMap<String, FieldValue> {
fn random(rng: &mut R) -> Self {
let len = rng.gen_range(0..3);
HashMap::from_iter((0..len).map(|_| (String::random(rng), FieldValue::random(rng))))
}
}
impl<R: Rng> RandomMethod<R> for FieldValue {
fn random(rng: &mut R) -> Self {
let index = rng.gen_range(0_u32..17);
match index {
0 => FieldValue::Boolean(RandomMethod::random(rng)),
1 => FieldValue::ShortShortInt(RandomMethod::random(rng)),
2 => FieldValue::ShortShortUInt(RandomMethod::random(rng)),
3 => FieldValue::ShortInt(RandomMethod::random(rng)),
4 => FieldValue::ShortUInt(RandomMethod::random(rng)),
5 => FieldValue::LongInt(RandomMethod::random(rng)),
6 => FieldValue::LongUInt(RandomMethod::random(rng)),
7 => FieldValue::LongLongInt(RandomMethod::random(rng)),
8 => FieldValue::LongLongUInt(RandomMethod::random(rng)),
9 => FieldValue::Float(RandomMethod::random(rng)),
10 => FieldValue::Double(RandomMethod::random(rng)),
11 => FieldValue::ShortString(RandomMethod::random(rng)),
12 => FieldValue::LongString(RandomMethod::random(rng)),
13 => FieldValue::FieldArray(RandomMethod::random(rng)),
14 => FieldValue::Timestamp(RandomMethod::random(rng)),
15 => FieldValue::FieldTable(RandomMethod::random(rng)),
16 => FieldValue::Void,
_ => unreachable!(),
}
}
}