Skip to content

Commit

Permalink
Adds correctness test to the benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
zslayton committed Nov 2, 2023
1 parent f9a0073 commit 04aff35
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
50 changes: 37 additions & 13 deletions benches/read_many_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use ion_rs::lazy::decoder::LazyDecoder;
use ion_rs::lazy::encoding::TextEncoding_1_1;
use ion_rs::lazy::r#struct::LazyStruct;
use ion_rs::lazy::reader::{LazyApplicationReader, LazyTextReader_1_0, LazyTextReader_1_1};
use ion_rs::lazy::reader::{LazyApplicationReader, LazyTextReader_1_1};
use ion_rs::lazy::value::LazyValue;
use ion_rs::lazy::value_ref::ValueRef;
use ion_rs::IonResult;
use nom::AsBytes;
use ion_rs::ElementReader;
use ion_rs::IonEq;
use ion_rs::{Element, Format, IonReader, IonResult, TextKind};

fn rewrite_as_compact_text(pretty_ion: &str) -> IonResult<String> {
let values = Element::read_all(pretty_ion).unwrap();
let mut buffer = Vec::new();
Element::write_all_as(&values, Format::Text(TextKind::Compact), &mut buffer)?;
Ok(String::from_utf8(buffer).unwrap())
}

pub fn criterion_benchmark(c: &mut Criterion) {
const NUM_VALUES: usize = 10_000;
let data_1_0 = concat!("{",
"'timestamp': 1670446800245,",
"'threadId': 418,",
r#"'threadName': "scheduler-thread-6","#,
r#"'loggerName': "com.example.organization.product.component.ClassName","#,
"'logLevel': INFO,",
r#"'format': "Request status: {} Client ID: {} Client Host: {} Client Region: {} Timestamp: {}","#,
r#"'parameters': ["SUCCESS","example-client-1","aws-us-east-5f-18b4fa","region 4","2022-12-07T20:59:59.744000Z",],"#,
"}"
).repeat(NUM_VALUES);
let pretty_data_1_0 = r#"{
'timestamp': 1670446800245,
'threadId': 418,
'threadName': "scheduler-thread-6",
'loggerName': "com.example.organization.product.component.ClassName",
'logLevel': INFO,
'format': "Request status: {} Client ID: {} Client Host: {} Client Region: {} Timestamp: {}",
'parameters': ["SUCCESS","example-client-1","aws-us-east-5f-18b4fa","region 4","2022-12-07T20:59:59.744000Z",],
}"#.repeat(NUM_VALUES);
let data_1_0 = rewrite_as_compact_text(&pretty_data_1_0).unwrap();
let template_text = r#"
(macro event (timestamp thread_id thread_name client_num host_id parameters)
{
Expand All @@ -44,6 +52,20 @@ pub fn criterion_benchmark(c: &mut Criterion) {
println!("Ion 1.0 data size: {} bytes", data_1_0.len());
println!("Ion 1.1 data size: {} bytes", data_1_1.len());

// As a sanity check, materialize the data from both the Ion 1.0 and 1.1 streams and make sure
// that they are equivalent before we start measuring the time needed to read them.
let seq_1_0 = LazyTextReader_1_1::new(data_1_0.as_bytes())
.unwrap()
.read_all_elements()
.unwrap();
let mut reader_1_1 = LazyTextReader_1_1::new(data_1_1.as_bytes()).unwrap();
reader_1_1.register_template(template_text).unwrap();
let seq_1_1 = reader_1_1.read_all_elements().unwrap();
assert!(
seq_1_0.ion_eq(&seq_1_1),
"Ion 1.0 sequence was not equal to the Ion 1.1 sequence"
);

fn count_value_and_children<D: LazyDecoder>(lazy_value: &LazyValue<'_, D>) -> IonResult<usize> {
use ValueRef::*;
let child_count = match lazy_value.read()? {
Expand Down Expand Up @@ -93,6 +115,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
while let Some(item) = reader.next().unwrap() {
num_values += count_value_and_children(&item).unwrap();
}
let _ = black_box(num_values);
})
});
c.bench_function("text 1.1: scan all", |b| {
Expand All @@ -112,6 +135,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
while let Some(item) = reader.next().unwrap() {
num_values += count_value_and_children(&item).unwrap();
}
let _ = black_box(num_values);
})
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/ion_data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod ion_eq;
pub(crate) mod ion_eq;
mod ion_ord;

use std::cmp::Ordering;
Expand Down
1 change: 0 additions & 1 deletion src/lazy/expanded/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ impl TemplateCompiler {
signature,
body: compiled_body,
};
println!("{template_macro:?}");
Ok(template_macro)
}

Expand Down
7 changes: 2 additions & 5 deletions src/lazy/expanded/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ impl Debug for TemplateMacro {

impl TemplateMacro {
pub fn name(&self) -> &str {
self.name
.as_ref()
.map(String::as_str)
.unwrap_or("<anonymous>")
self.name.as_deref().unwrap_or("<anonymous>")
}
pub fn signature(&self) -> &MacroSignature {
&self.signature
Expand Down Expand Up @@ -555,7 +552,7 @@ impl TemplateBodyValueExpr {
let value = &expressions[expr_index + 1];
writeln!(f, "{indentation}'{name}':")?;
indentation.push_str(" ");
expr_index += 1 + Self::fmt_expr(f, indentation, host_template, &value)?;
expr_index += 1 + Self::fmt_expr(f, indentation, host_template, value)?;
indentation.truncate(indentation.len() - 4);
}
indentation.truncate(indentation.len() - 4);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ pub use element::{
reader::ElementReader, writer::ElementWriter, Annotations, Element, IntoAnnotatedElement,
IntoAnnotations, Sequence, Value,
};
pub use ion_data::ion_eq::IonEq;
pub use ion_data::IonData;
pub use symbol_ref::SymbolRef;
#[doc(inline)]
Expand Down

0 comments on commit 04aff35

Please sign in to comment.