Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read/write records #1117

Merged
merged 16 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix
  • Loading branch information
raviqqe committed Apr 28, 2024
commit be782b8229a43dc989476e453d34684ded1c3e8c
2 changes: 2 additions & 0 deletions profiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod flamegraph;
mod read;
mod record;
mod stack_profiler;
mod write;

pub use collapse::collapse_stacks;
pub use duration::calculate_durations;
Expand All @@ -19,6 +20,7 @@ pub use record::{
DurationRecord, ProcedureOperation, ProcedureRecord, Record, Stack, StackedRecord,
};
pub use stack_profiler::StackProfiler;
pub use write::write_records;

const COLUMN_SEPARATOR: char = '\t';
const FRAME_SEPARATOR: char = ';';
Expand Down
59 changes: 59 additions & 0 deletions profiler/src/write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::Record;
use std::io::{self, Write};

pub fn write_records(
records: impl IntoIterator<Item = impl Record>,
mut writer: impl Write,
) -> Result<(), io::Error> {
for record in records {
writeln!(writer, "{}", record)?;
}

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{ProcedureOperation, ProcedureRecord, Stack};
use indoc::indoc;
use pretty_assertions::assert_eq;

#[test]
fn write() {
let mut buffer = vec![];

write_records(
[
ProcedureRecord::new(
ProcedureOperation::Call,
Stack::new(vec![Some("baz".into())]),
0,
),
ProcedureRecord::new(
ProcedureOperation::Return,
Stack::new(vec![Some("foo".into()), Some("bar".into())]),
42,
),
ProcedureRecord::new(
ProcedureOperation::ReturnCall,
Stack::new(vec![None; 3]),
2045,
),
],
&mut buffer,
)
.unwrap();

assert_eq!(
buffer,
indoc!(
b"
call\tbaz\t0
return\tfoo;bar\t42
return_call\t;;\t2045
"
)
);
}
}
Loading