From 350f97a8d05c4a6ae8f9c2f5a5248a6cabb2e9ec Mon Sep 17 00:00:00 2001 From: Boshen Date: Fri, 1 Aug 2025 10:41:12 +0800 Subject: [PATCH] chore: add more benchmarks --- benches/strip.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/benches/strip.rs b/benches/strip.rs index e13c6f1..9efed73 100644 --- a/benches/strip.rs +++ b/benches/strip.rs @@ -1,17 +1,78 @@ -use criterion::{Criterion, criterion_group, criterion_main}; +use criterion::{BatchSize, Criterion, criterion_group, criterion_main}; pub fn bench(c: &mut Criterion) { c.bench_function("tsconfig", |b| { - b.iter(|| { - let mut data = String::from(TSCONFIG); - json_strip_comments::strip(&mut data).unwrap(); - }); + b.iter_batched( + || String::from(TSCONFIG), + |mut data| json_strip_comments::strip(&mut data).unwrap(), + BatchSize::SmallInput, + ); + }); + + // Benchmark with no comments (fast path test) + c.bench_function("no_comments", |b| { + b.iter_batched( + || String::from(NO_COMMENTS_JSON), + |mut data| json_strip_comments::strip(&mut data).unwrap(), + criterion::BatchSize::SmallInput, + ); + }); + + // Benchmark with minimal comments + c.bench_function("minimal_comments", |b| { + b.iter_batched( + || String::from(MINIMAL_COMMENTS), + |mut data| json_strip_comments::strip(&mut data).unwrap(), + criterion::BatchSize::SmallInput, + ); + }); + + // Benchmark with large input with many comments + c.bench_function("large_with_comments", |b| { + b.iter_batched( + || MINIMAL_COMMENTS.repeat(100), + |mut data| json_strip_comments::strip(&mut data).unwrap(), + criterion::BatchSize::SmallInput, + ); }); } criterion_group!(strip, bench); criterion_main!(strip); +const NO_COMMENTS_JSON: &str = r#" +{ + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "node", + "resolveJsonModule": true, + "allowJs": true, + "sourceMap": true, + "outDir": "./built", + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true + }, + "exclude": [ + "node_modules", + ".npm" + ], + "include": ["./src"] +} +"#; + +const MINIMAL_COMMENTS: &str = r#" +{ + // Simple line comment + "name": "test", /* block comment */ + "value": 42, + # hash comment + "trailing": true, +} +"#; + const TSCONFIG: &str = r#" { "compilerOptions": {