Skip to content

Commit

Permalink
Bench: Parser & ByteSource Mocks
Browse files Browse the repository at this point in the history
Add mocks for parser and byte source traits to be used in the benchmarks
  • Loading branch information
AmmarAbouZor authored and marcmo committed Sep 18, 2024
1 parent 489ca61 commit a996912
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
83 changes: 83 additions & 0 deletions application/apps/indexer/sources/benches/mocks/mock_parser.rs
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 application/apps/indexer/sources/benches/mocks/mock_source.rs
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))
}
}
2 changes: 2 additions & 0 deletions application/apps/indexer/sources/benches/mocks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod mock_parser;
pub mod mock_source;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ async fn produce(val: usize, val_2: usize) {
assert_eq!(val, val_2);
}

mod mocks;

fn producer_benchmark(c: &mut Criterion) {
let val = 1024;

Expand Down

0 comments on commit a996912

Please sign in to comment.