1+ use std:: fs;
2+ use std:: path:: Path ;
3+
14use biome_rowan:: NodeCache ;
25use biome_tailwind_parser:: { parse_tailwind, parse_tailwind_with_cache} ;
3- use divan:: { Bencher , counter:: BytesCount } ;
4- use std:: path:: Path ;
6+ use criterion:: { BenchmarkId , Criterion , Throughput , black_box, criterion_group, criterion_main} ;
57
68#[ cfg( target_os = "windows" ) ]
79#[ global_allocator]
@@ -19,57 +21,57 @@ static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
1921#[ global_allocator]
2022static GLOBAL : std:: alloc:: System = std:: alloc:: System ;
2123
22- fn main ( ) {
23- divan:: main ( ) ;
24- }
24+ /// Load fixture files from `benches/fixtures` returning (file_name, content).
25+ fn load_fixtures ( ) -> Vec < ( String , String ) > {
26+ let fixtures_dir = Path :: new ( env ! ( "CARGO_MANIFEST_DIR" ) ) . join ( "benches/fixtures" ) ;
27+ let mut fixtures = Vec :: new ( ) ;
2528
26- fn fixture_names ( ) -> impl Iterator < Item = String > {
27- let fixtures_dir = Path :: new ( "benches/fixtures" ) ;
28- let mut names = Vec :: new ( ) ;
29-
30- if let Ok ( entries) = std:: fs:: read_dir ( fixtures_dir) {
29+ if let Ok ( entries) = fs:: read_dir ( & fixtures_dir) {
3130 for entry in entries. flatten ( ) {
3231 let path = entry. path ( ) ;
3332 if path. is_file ( )
34- && let Some ( file_name) = path. file_name ( ) . and_then ( |n| n. to_str ( ) ) {
35- names. push ( file_name. to_string ( ) ) ;
36- }
33+ && let Some ( file_name) = path. file_name ( ) . and_then ( |n| n. to_str ( ) )
34+ {
35+ let content = fs:: read_to_string ( & path) . expect ( "Failed to read fixture file" ) ;
36+ fixtures. push ( ( file_name. to_string ( ) , content) ) ;
37+ }
3738 }
3839 }
3940
40- names . into_iter ( )
41+ fixtures
4142}
4243
43- #[ divan:: bench( name = "uncached" , args = fixture_names( ) , sample_size=10 ) ]
44- fn bench_uncached ( bencher : Bencher , filename : & str ) {
45- let fixtures_dir = Path :: new ( "benches/fixtures" ) ;
46- let path = fixtures_dir. join ( filename) ;
47- let code = std:: fs:: read_to_string ( & path) . unwrap_or_default ( ) ;
44+ fn bench_tailwind ( c : & mut Criterion ) {
45+ let fixtures = load_fixtures ( ) ;
4846
49- bencher
50- . with_inputs ( || code. clone ( ) )
51- . input_counter ( BytesCount :: of_str)
52- . bench_local_values ( |code| {
53- let result = parse_tailwind ( & code) ;
54- divan:: black_box ( result) ;
55- } ) ;
56- }
47+ let mut group = c. benchmark_group ( "tailwind_parser" ) ;
5748
58- #[ divan:: bench( name = "cached" , args = fixture_names( ) , sample_size=10 ) ]
59- fn bench_cached ( bencher : Bencher , filename : & str ) {
60- let fixtures_dir = Path :: new ( "benches/fixtures" ) ;
61- let path = fixtures_dir. join ( filename) ;
62- let code = std:: fs:: read_to_string ( & path) . unwrap_or_default ( ) ;
49+ for ( name, content) in & fixtures {
50+ let len = content. len ( ) as u64 ;
6351
64- bencher
65- . with_inputs ( || {
52+ group. throughput ( Throughput :: Bytes ( len) ) ;
53+ group. bench_with_input ( BenchmarkId :: new ( "uncached" , name) , content, |b, code| {
54+ b. iter ( || {
55+ let result = parse_tailwind ( black_box ( code) ) ;
56+ black_box ( result) ;
57+ } ) ;
58+ } ) ;
59+
60+ group. throughput ( Throughput :: Bytes ( len) ) ;
61+ group. bench_with_input ( BenchmarkId :: new ( "cached" , name) , content, |b, code| {
6662 let mut cache = NodeCache :: default ( ) ;
67- // Warm-up parse to populate cache.
68- let _ = parse_tailwind_with_cache ( & code, & mut cache) ;
69- ( cache , code . clone ( ) )
70- } )
71- . input_counter ( | ( _cache , code) | BytesCount :: of_str ( code ) )
72- . bench_local_values ( | ( mut cache , code ) | {
73- divan :: black_box ( parse_tailwind_with_cache ( & code , & mut cache ) ) ;
63+ // Warm-up parse to populate cache (excluded from measurement) .
64+ let _ = parse_tailwind_with_cache ( code, & mut cache) ;
65+
66+ b . iter ( || {
67+ let result = parse_tailwind_with_cache ( black_box ( code) , & mut cache ) ;
68+ black_box ( result ) ;
69+ } ) ;
7470 } ) ;
71+ }
72+
73+ group. finish ( ) ;
7574}
75+
76+ criterion_group ! ( tailwind_parser, bench_tailwind) ;
77+ criterion_main ! ( tailwind_parser) ;
0 commit comments