forked from esrlabs/chipmunk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mocks for parser and byte source traits to be used in the benchmarks
- Loading branch information
1 parent
489ca61
commit a996912
Showing
4 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
application/apps/indexer/sources/benches/mocks/mock_parser.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use std::fmt::Display; | ||
|
||
use parsers::{LogMessage, Parser}; | ||
use serde::Serialize; | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
pub struct MockParser { | ||
/// Sets how much bytes each call of [`Parser::parse()`] should consume. | ||
consume: usize, | ||
/// Sets how many times the method [`Parser::parse()`] will be called before it'll return None. | ||
max_count: usize, | ||
/// Internal counter to keep track how many times [`Parser::parse()`] has been called. | ||
counter: usize, | ||
} | ||
|
||
impl MockParser { | ||
/// Creates new instance of the mock parser with the given settings. | ||
/// | ||
/// * `consume`: Sets how much bytes each call of [`Parser::parse()`] should consume. | ||
/// * `max_count`: Sets how many times the method [`Parser::parse()`] will be called before it'll return None. | ||
pub fn new(consume: usize, max_count: usize) -> Self { | ||
Self { | ||
consume, | ||
max_count, | ||
counter: 0, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Serialize)] | ||
/// Return type of [`Parser::parse()`] method for [`MockParser`] | ||
pub struct MockMessage { | ||
content: String, | ||
} | ||
|
||
impl Display for MockMessage { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.content) | ||
} | ||
} | ||
|
||
impl LogMessage for MockMessage { | ||
fn to_writer<W: std::io::prelude::Write>( | ||
&self, | ||
writer: &mut W, | ||
) -> Result<usize, std::io::Error> { | ||
let len = self.content.len(); | ||
writer.write_all(self.content.as_bytes())?; | ||
Ok(len) | ||
} | ||
} | ||
|
||
impl MockMessage { | ||
pub fn new(msg: String) -> Self { | ||
Self { content: msg } | ||
} | ||
} | ||
|
||
impl From<String> for MockMessage { | ||
fn from(value: String) -> Self { | ||
Self::new(value) | ||
} | ||
} | ||
|
||
impl Parser<MockMessage> for MockParser { | ||
fn parse<'a>( | ||
&mut self, | ||
input: &'a [u8], | ||
_timestamp: Option<u64>, | ||
) -> Result<(&'a [u8], Option<parsers::ParseYield<MockMessage>>), parsers::Error> { | ||
if self.counter >= self.max_count { | ||
return Err(parsers::Error::Eof); | ||
} | ||
self.counter += 1; | ||
|
||
let msg = String::from_utf8_lossy(&input[..self.consume]).to_string(); | ||
|
||
Ok(( | ||
&input[..self.consume], | ||
Some(parsers::ParseYield::Message(msg.into())), | ||
)) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
application/apps/indexer/sources/benches/mocks/mock_source.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::iter; | ||
|
||
use async_trait::async_trait; | ||
use sources::{ByteSource, ReloadInfo}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct MockByteSource { | ||
/// Represent the bytes that will be repeated to fill the internal buffer | ||
data_sample: u8, | ||
/// Sets how many bytes will be loaded into the internal buffer on each | ||
/// [`ByteSource::reload()`] call. | ||
load_amount: usize, | ||
/// The internal buffer | ||
buffer: Vec<u8>, | ||
} | ||
|
||
impl MockByteSource { | ||
/// Creates a new instant of [`MockByteSource`] | ||
/// | ||
/// * `data_sample`: Represent the bytes that will be repeated to fill the internal buffer | ||
/// * `load_amount`: Sets how many bytes will be loaded into the internal buffer on | ||
/// each [`ByteSource::reload()`] call. | ||
pub fn new(data_sample: u8, load_amount: usize) -> Self { | ||
Self { | ||
data_sample, | ||
load_amount, | ||
buffer: Vec::new(), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ByteSource for MockByteSource { | ||
fn consume(&mut self, offset: usize) { | ||
self.buffer | ||
.truncate(self.buffer.len().checked_sub(offset).unwrap()) | ||
} | ||
|
||
fn current_slice(&self) -> &[u8] { | ||
&self.buffer | ||
} | ||
|
||
fn len(&self) -> usize { | ||
self.buffer.len() | ||
} | ||
|
||
async fn reload( | ||
&mut self, | ||
_filter: Option<&sources::SourceFilter>, | ||
) -> Result<Option<sources::ReloadInfo>, sources::Error> { | ||
self.buffer | ||
.extend(iter::repeat(self.data_sample).take(self.load_amount)); | ||
|
||
let info = ReloadInfo::new(self.load_amount, self.len(), 0, None); | ||
|
||
Ok(Some(info)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod mock_parser; | ||
pub mod mock_source; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters