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
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 120693bd860c9224e2304a7bb12f0a747178ef7f
52 changes: 24 additions & 28 deletions profiler/src/collapse.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
use crate::{Error, StackedRecord};
use std::io::Write;

/// Collapses stacks.
pub fn collapse_stacks(
records: impl IntoIterator<Item = Result<impl StackedRecord, Error>>,
mut writer: impl Write,
) -> Result<(), Error> {
for record in records {
pub fn collapse_stacks<R: StackedRecord>(
records: impl IntoIterator<Item = Result<R, Error>>,
) -> impl Iterator<Item = Result<R, Error>> {
records.into_iter().map(|record| {
let mut record = record?;

record.stack_mut().collapse_frames();

writeln!(writer, "{record}")?;
}

Ok(())
Ok(record)
})
}

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

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

collapse_stacks(
[
assert_eq!(
collapse_stacks([
Ok(DurationRecord::new(
Stack::new(vec![Some("bar".into()), Some("foo".into())]),
123,
Expand All @@ -43,19 +36,22 @@ mod tests {
]),
42,
)),
],
&mut buffer,
)
.unwrap();

assert_eq!(
String::from_utf8(buffer).unwrap(),
indoc!(
"
bar;foo\t123
baz;bar;foo\t42
"
)
])
.collect::<Vec<_>>(),
vec![
Ok(DurationRecord::new(
Stack::new(vec![Some("bar".into()), Some("foo".into())]),
123,
)),
Ok(DurationRecord::new(
Stack::new(vec![
Some("baz".into()),
Some("bar".into()),
Some("foo".into()),
]),
42,
)),
]
);
}
}
Loading