mirror of
https://github.com/swc-project/swc.git
synced 2024-11-22 06:46:41 +03:00
chore(es/minifier): Add full benchmark for .minify()
(#4341)
This commit is contained in:
parent
8191762dd5
commit
ba5f7436c1
@ -116,6 +116,10 @@ name = "transform"
|
||||
harness = false
|
||||
name = "bugs"
|
||||
|
||||
[[bench]]
|
||||
harness = false
|
||||
name = "minify"
|
||||
|
||||
[[bench]]
|
||||
harness = false
|
||||
name = "typescript"
|
||||
|
84
crates/swc/benches/minify.rs
Normal file
84
crates/swc/benches/minify.rs
Normal file
@ -0,0 +1,84 @@
|
||||
/// Explicit extern crate to use allocator.
|
||||
extern crate swc_node_base;
|
||||
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
|
||||
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
|
||||
use swc::{config::JsMinifyOptions, try_with_handler};
|
||||
use swc_common::{FilePathMapping, SourceMap};
|
||||
|
||||
fn mk() -> swc::Compiler {
|
||||
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
|
||||
|
||||
swc::Compiler::new(cm)
|
||||
}
|
||||
|
||||
fn bench_minify(b: &mut Bencher, filename: &str) {
|
||||
let c = mk();
|
||||
|
||||
c.run(|| {
|
||||
b.iter(|| {
|
||||
let fm = black_box(
|
||||
c.cm.load_file(
|
||||
&PathBuf::from("..")
|
||||
.join("swc_ecma_minifier")
|
||||
.join("benches")
|
||||
.join("full")
|
||||
.join(filename),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
let res = try_with_handler(c.cm.clone(), Default::default(), |handler| {
|
||||
c.minify(
|
||||
fm,
|
||||
handler,
|
||||
&JsMinifyOptions {
|
||||
compress: true.into(),
|
||||
mangle: true.into(),
|
||||
format: Default::default(),
|
||||
ecma: Default::default(),
|
||||
keep_classnames: Default::default(),
|
||||
keep_fnames: Default::default(),
|
||||
module: Default::default(),
|
||||
safari10: Default::default(),
|
||||
toplevel: Default::default(),
|
||||
source_map: Default::default(),
|
||||
output_path: Default::default(),
|
||||
inline_sources_content: Default::default(),
|
||||
},
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
black_box(res);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
fn files_group(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("libraries");
|
||||
group.sample_size(10);
|
||||
|
||||
let mut bench_file = |name: &str| {
|
||||
group.bench_function(name, |b| {
|
||||
bench_minify(b, &format!("{}.js", name));
|
||||
});
|
||||
};
|
||||
|
||||
bench_file("antd");
|
||||
bench_file("d3");
|
||||
bench_file("echarts");
|
||||
bench_file("jquery");
|
||||
bench_file("lodash");
|
||||
bench_file("moment");
|
||||
bench_file("react");
|
||||
bench_file("terser");
|
||||
bench_file("three");
|
||||
bench_file("typescript");
|
||||
bench_file("victory");
|
||||
bench_file("vue");
|
||||
}
|
||||
|
||||
criterion_group!(benches, files_group);
|
||||
criterion_main!(benches);
|
@ -245,18 +245,23 @@ impl Vars {
|
||||
N: for<'aa> VisitMutWith<MultiReplacer<'aa>>,
|
||||
{
|
||||
let mut changed = false;
|
||||
n.visit_mut_with(&mut MultiReplacer::new(
|
||||
&mut self.simple_functions,
|
||||
true,
|
||||
MultiReplacerMode::OnlyCallee,
|
||||
&mut changed,
|
||||
));
|
||||
n.visit_mut_with(&mut MultiReplacer::new(
|
||||
&mut self.vars_for_inlining,
|
||||
false,
|
||||
MultiReplacerMode::Normal,
|
||||
&mut changed,
|
||||
));
|
||||
if !self.simple_functions.is_empty() {
|
||||
n.visit_mut_with(&mut MultiReplacer::new(
|
||||
&mut self.simple_functions,
|
||||
true,
|
||||
MultiReplacerMode::OnlyCallee,
|
||||
&mut changed,
|
||||
));
|
||||
}
|
||||
|
||||
if !self.vars_for_inlining.is_empty() {
|
||||
n.visit_mut_with(&mut MultiReplacer::new(
|
||||
&mut self.vars_for_inlining,
|
||||
false,
|
||||
MultiReplacerMode::Normal,
|
||||
&mut changed,
|
||||
));
|
||||
}
|
||||
|
||||
changed
|
||||
}
|
||||
|
1
node-swc/benches/load.mjs
Normal file
1
node-swc/benches/load.mjs
Normal file
@ -0,0 +1 @@
|
||||
import '../../index.js';
|
11
node-swc/benches/minify.mjs
Normal file
11
node-swc/benches/minify.mjs
Normal file
@ -0,0 +1,11 @@
|
||||
import { minify } from '../../index.js';
|
||||
import { promises as fs } from 'fs';
|
||||
|
||||
const files = process.argv.slice(2);
|
||||
const inputCode = await fs.readFile(files[0], 'utf8');
|
||||
|
||||
await minify(inputCode, {
|
||||
compress: true,
|
||||
mangle: true,
|
||||
sourceMap: false,
|
||||
})
|
4
node-swc/src/binding.d.ts
vendored
4
node-swc/src/binding.d.ts
vendored
@ -3,6 +3,10 @@
|
||||
|
||||
/* auto-generated by NAPI-RS */
|
||||
|
||||
export interface TransformOutput {
|
||||
code: string
|
||||
map?: string | undefined | null
|
||||
}
|
||||
export function bundle(confItems: Buffer, signal?: AbortSignal | undefined | null): Promise<{ [index: string]: { code: string, map?: string } }>
|
||||
export function minify(code: Buffer, opts: Buffer, signal?: AbortSignal | undefined | null): Promise<TransformOutput>
|
||||
export function minifySync(code: Buffer, opts: Buffer): TransformOutput
|
||||
|
14
scripts/bench-node.sh
Executable file
14
scripts/bench-node.sh
Executable file
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -eu
|
||||
|
||||
|
||||
echo "Binary load time"
|
||||
hyperfine --warmup 10 --runs 5 'node ./node-swc/benches/load.mjs'
|
||||
|
||||
echo "Load + minify antd, all core"
|
||||
hyperfine --warmup 5 --runs 5 "node ./node-swc/benches/minify.mjs $PWD/crates/swc_ecma_minifier/benches/full/antd.js"
|
||||
|
||||
echo "Load + minify antd, 2 core"
|
||||
hyperfine --warmup 5 --runs 5 "RAYON_NUM_THREADS=2 node ./node-swc/benches/minify.mjs $PWD/crates/swc_ecma_minifier/benches/full/antd.js"
|
||||
|
Loading…
Reference in New Issue
Block a user