Files
oxc-project__oxc/tasks/benchmark/benches/codegen.rs
2026-09-04 14:48:49 +01:00

63 lines
2.4 KiB
Rust

use std::path::{Path, PathBuf};
use oxc_allocator::Allocator;
use oxc_benchmark::{BenchmarkId, Criterion, criterion_group, criterion_main};
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_tasks_common::TestFiles;
use oxc_transformer::{TransformOptions, Transformer};
fn bench_codegen(criterion: &mut Criterion) {
for file in TestFiles::minimal().files() {
let id = BenchmarkId::from_parameter(&file.file_name);
let source_text = &file.source_text;
let source_type = file.source_type;
let allocator = Allocator::default();
let parser_ret = Parser::new(&allocator, source_text, source_type).parse();
assert!(parser_ret.diagnostics.is_empty());
let mut program = parser_ret.program;
let semantic_ret = SemanticBuilder::new()
.with_enum_eval(true)
.with_comment_attachments(true)
.build(&program);
let attachments = semantic_ret.comment_attachments.unwrap();
let scoping = semantic_ret.semantic.into_scoping();
let transform_options = TransformOptions::enable_all();
let transformer_ret =
Transformer::new(&allocator, Path::new(&file.file_name), &transform_options)
.build_with_scoping(scoping, &mut program);
assert!(transformer_ret.diagnostics.is_empty());
let mut group = criterion.benchmark_group("codegen");
group.bench_function(id, |b| {
b.iter_with_large_drop(|| {
Codegen::new()
.with_options(CodegenOptions {
source_map_path: Some(PathBuf::from(&file.file_name)),
..CodegenOptions::default()
})
.build(&program)
});
});
let comments_id = BenchmarkId::new("comments", &file.file_name);
group.bench_function(comments_id, |b| {
b.iter_with_large_drop(|| {
Codegen::new()
.with_options(CodegenOptions {
source_map_path: Some(PathBuf::from(&file.file_name)),
..CodegenOptions::default()
})
.build_with_comment_attachments(&program, &attachments)
});
});
group.finish();
}
}
criterion_group!(codegen, bench_codegen);
criterion_main!(codegen);