-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rust benchmark case for code splitting
- Loading branch information
Showing
9 changed files
with
10,196 additions
and
130 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![feature(trait_upcasting)] | ||
#![allow(clippy::unwrap_used)] | ||
|
||
use basic::basic; | ||
use build_chunk_graph::chunk_graph; | ||
use criterion::criterion_main; | ||
|
||
mod basic; | ||
mod build_chunk_graph; | ||
|
||
criterion_main!(basic, chunk_graph); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
#![allow(clippy::unwrap_used)] | ||
use std::sync::Arc; | ||
|
||
use criterion::criterion_group; | ||
use rspack::builder::{ | ||
Builder as _, BuilderContext, ExperimentsBuilder, OptimizationOptionsBuilder, | ||
}; | ||
use rspack_benchmark::Criterion; | ||
use rspack_core::Compiler; | ||
use rspack_fs::{MemoryFileSystem, WritableFileSystem}; | ||
use tokio::runtime::Builder; | ||
|
||
async fn compile(fs: Arc<MemoryFileSystem>, parallel: bool) { | ||
let mut builder_context = BuilderContext::default(); | ||
let mut compiler = Compiler::builder() | ||
.context("/") | ||
.entry("main", "/src/dynamic-0.js") | ||
.input_filesystem(fs.clone()) | ||
.output_filesystem(fs.clone()) | ||
.optimization( | ||
OptimizationOptionsBuilder::default() | ||
.remove_available_modules(true) | ||
.build(&mut builder_context, true, false, false), | ||
) | ||
.experiments( | ||
ExperimentsBuilder::default() | ||
.parallel_code_splitting(parallel) | ||
.build(&mut builder_context, true, false), | ||
) | ||
.build(); | ||
|
||
compiler.run().await.unwrap(); | ||
|
||
assert!(compiler | ||
.compilation | ||
.get_errors() | ||
.collect::<Vec<_>>() | ||
.is_empty()); | ||
} | ||
|
||
async fn prepare_large_code_splitting_case( | ||
num: usize, | ||
random_table: &Vec<Vec<usize>>, | ||
fs: &MemoryFileSystem, | ||
) { | ||
let mut ctx: Vec<(String, String)> = vec![]; | ||
gen_dynamic_module(num, 0, random_table, &mut ctx); | ||
|
||
fs.create_dir_all("/src".into()).await.unwrap(); | ||
fs.create_dir_all("/src/leaves".into()).await.unwrap(); | ||
|
||
for (name, code) in ctx { | ||
fs.write(name.as_str().into(), code.as_bytes()) | ||
.await | ||
.unwrap(); | ||
} | ||
} | ||
|
||
fn gen_static_leaf_module(index: usize, ctx: &mut Vec<(String, String)>) { | ||
let code = " | ||
function Navbar({ show }) { | ||
return ( | ||
{show} | ||
) | ||
} | ||
export default Navbar"; | ||
|
||
ctx.push(( | ||
format!("/src/leaves/Component-{}.js", index) | ||
.as_str() | ||
.into(), | ||
code.to_string(), | ||
)); | ||
} | ||
|
||
fn gen_dynamic_module( | ||
num: usize, | ||
index: usize, | ||
random_table: &Vec<Vec<usize>>, | ||
ctx: &mut Vec<(String, String)>, | ||
) -> bool { | ||
if index >= num { | ||
return false; | ||
} | ||
|
||
let mut access = vec![]; | ||
let mut static_imports = vec![]; | ||
let mut dynamic_imports = vec![]; | ||
let mut reuse = vec![]; | ||
|
||
for i in index..index + 10 { | ||
static_imports.push(format!( | ||
"import Comp{} from '/src/leaves/Component-{}.js'", | ||
i, i, | ||
)); | ||
gen_static_leaf_module(i, ctx); | ||
access.push(format!("Comp{}", i)); | ||
} | ||
|
||
let depth = index / 10; | ||
for random in random_table[depth].iter() { | ||
reuse.push(format!( | ||
"import Comp{} from '/src/leaves/Component-{}.js'", | ||
random, random, | ||
)); | ||
access.push(format!("Comp{}", random)); | ||
} | ||
|
||
if gen_dynamic_module(num, index + 10, random_table, ctx) { | ||
dynamic_imports.push(format!("import('/src/dynamic-{}.js')", depth + 1)); | ||
} | ||
|
||
let code = format!( | ||
"{}\n{}\n{}\n{};export default {};", | ||
static_imports.join("\n"), | ||
reuse.join("\n"), | ||
access.join("\n"), | ||
dynamic_imports.join("\n"), | ||
depth | ||
); | ||
|
||
ctx.push((format!("/src/dynamic-{}.js", depth).as_str().into(), code)); | ||
true | ||
} | ||
|
||
pub fn build_chunk_graph_benchmark(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("build_chunk_graph"); | ||
group.sample_size(10); | ||
|
||
let rt = Builder::new_multi_thread() | ||
.build() | ||
.expect("should not fail to build tokio runtime"); | ||
|
||
let fs = MemoryFileSystem::default(); | ||
let random_table = | ||
serde_json::from_str::<Vec<Vec<usize>>>(include_str!("build_chunk_graph/random_table.json")) | ||
.expect("should not fail to parse random table json"); | ||
|
||
rt.block_on(async { | ||
fs.create_dir_all("/src".into()) | ||
.await | ||
.expect("should not fail to create dir"); | ||
prepare_large_code_splitting_case(5000, &random_table, &fs).await; | ||
}); | ||
|
||
let fs = Arc::new(fs); | ||
|
||
group.bench_function("build_chunk_graph", |b| { | ||
b.to_async(&rt).iter(|| { | ||
let fs = fs.clone(); | ||
compile(fs, false) | ||
}); | ||
}); | ||
|
||
group.bench_function("build_chunk_graph_parallel", |b| { | ||
b.to_async(&rt).iter(|| { | ||
let fs = fs.clone(); | ||
compile(fs, true) | ||
}); | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(chunk_graph, build_chunk_graph_benchmark); |
Oops, something went wrong.