mirror of
https://github.com/stefanoamorelli/crabrl.git
synced 2026-04-18 07:10:42 +00:00
fix: update benchmarks to use available test fixtures
- Add parse_str method to Parser - Update benchmark to use existing fixtures or minimal inline XBRL - Remove unused crabrl_bench.rs
This commit is contained in:
@@ -1,23 +1,37 @@
|
||||
use crabrl::Parser;
|
||||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||
use std::path::Path;
|
||||
|
||||
fn parse_small_file(c: &mut Criterion) {
|
||||
fn parse_sample_sec_file(c: &mut Criterion) {
|
||||
let parser = Parser::new();
|
||||
let content = include_bytes!("../tests/fixtures/small.xml");
|
||||
let sample_file = Path::new("fixtures/sample-sec.xml");
|
||||
|
||||
c.bench_function("parse_small", |b| {
|
||||
b.iter(|| parser.parse_bytes(black_box(content)));
|
||||
if sample_file.exists() {
|
||||
c.bench_function("parse_sample_sec", |b| {
|
||||
b.iter(|| parser.parse_file(black_box(&sample_file)));
|
||||
});
|
||||
} else {
|
||||
// If no fixtures exist, use a minimal inline XBRL for benchmarking
|
||||
let minimal_xbrl = r#"<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xbrl xmlns="http://www.xbrl.org/2003/instance">
|
||||
<context id="ctx1">
|
||||
<entity>
|
||||
<identifier scheme="http://www.sec.gov/CIK">0000000000</identifier>
|
||||
</entity>
|
||||
<period>
|
||||
<instant>2023-12-31</instant>
|
||||
</period>
|
||||
</context>
|
||||
<unit id="usd">
|
||||
<measure>iso4217:USD</measure>
|
||||
</unit>
|
||||
</xbrl>"#;
|
||||
|
||||
c.bench_function("parse_minimal", |b| {
|
||||
b.iter(|| parser.parse_str(black_box(minimal_xbrl)));
|
||||
});
|
||||
}
|
||||
|
||||
fn parse_medium_file(c: &mut Criterion) {
|
||||
let parser = Parser::new();
|
||||
let content = include_bytes!("../tests/fixtures/medium.xml");
|
||||
|
||||
c.bench_function("parse_medium", |b| {
|
||||
b.iter(|| parser.parse_bytes(black_box(content)));
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(benches, parse_small_file, parse_medium_file);
|
||||
criterion_group!(benches, parse_sample_sec_file);
|
||||
criterion_main!(benches);
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
use crabrl::Parser;
|
||||
use std::env;
|
||||
use std::time::Instant;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() != 2 {
|
||||
eprintln!("Usage: {} <xbrl_file>", args[0]);
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let filepath = &args[1];
|
||||
let parser = Parser::new();
|
||||
|
||||
let start = Instant::now();
|
||||
match parser.parse_file(filepath) {
|
||||
Ok(doc) => {
|
||||
let elapsed = start.elapsed();
|
||||
let ms = elapsed.as_secs_f64() * 1000.0;
|
||||
println!(
|
||||
"crabrl found: {} facts, {} contexts, {} units (in {:.3}ms)",
|
||||
doc.facts.len(),
|
||||
doc.contexts.len(),
|
||||
doc.units.len(),
|
||||
ms
|
||||
);
|
||||
|
||||
// Additional stats
|
||||
println!("Facts: {}", doc.facts.len());
|
||||
println!("Contexts: {}", doc.contexts.len());
|
||||
println!("Units: {}", doc.units.len());
|
||||
println!("Tuples: {}", doc.tuples.len());
|
||||
println!("Footnotes: {}", doc.footnotes.len());
|
||||
println!("Time: {:.3}ms", ms);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error parsing file: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,10 @@ impl Parser {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn parse_str(&self, content: &str) -> Result<Document> {
|
||||
self.parse_bytes(content.as_bytes())
|
||||
}
|
||||
|
||||
pub fn parse_file<P: AsRef<Path>>(&self, path: P) -> Result<Document> {
|
||||
let content = std::fs::read(path)?;
|
||||
self.parse_bytes(&content)
|
||||
|
||||
Reference in New Issue
Block a user