chore(es/minifier): Add full benchmark for .minify() (#4341)

This commit is contained in:
Donny/강동윤 2022-04-16 07:31:15 +09:00 committed by GitHub
parent 8191762dd5
commit ba5f7436c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 135 additions and 12 deletions

View File

@ -116,6 +116,10 @@ name = "transform"
harness = false harness = false
name = "bugs" name = "bugs"
[[bench]]
harness = false
name = "minify"
[[bench]] [[bench]]
harness = false harness = false
name = "typescript" name = "typescript"

View 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);

View File

@ -245,18 +245,23 @@ impl Vars {
N: for<'aa> VisitMutWith<MultiReplacer<'aa>>, N: for<'aa> VisitMutWith<MultiReplacer<'aa>>,
{ {
let mut changed = false; let mut changed = false;
n.visit_mut_with(&mut MultiReplacer::new( if !self.simple_functions.is_empty() {
&mut self.simple_functions, n.visit_mut_with(&mut MultiReplacer::new(
true, &mut self.simple_functions,
MultiReplacerMode::OnlyCallee, true,
&mut changed, MultiReplacerMode::OnlyCallee,
)); &mut changed,
n.visit_mut_with(&mut MultiReplacer::new( ));
&mut self.vars_for_inlining, }
false,
MultiReplacerMode::Normal, if !self.vars_for_inlining.is_empty() {
&mut changed, n.visit_mut_with(&mut MultiReplacer::new(
)); &mut self.vars_for_inlining,
false,
MultiReplacerMode::Normal,
&mut changed,
));
}
changed changed
} }

View File

@ -0,0 +1 @@
import '../../index.js';

View 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,
})

View File

@ -3,6 +3,10 @@
/* auto-generated by NAPI-RS */ /* 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 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 minify(code: Buffer, opts: Buffer, signal?: AbortSignal | undefined | null): Promise<TransformOutput>
export function minifySync(code: Buffer, opts: Buffer): TransformOutput export function minifySync(code: Buffer, opts: Buffer): TransformOutput

14
scripts/bench-node.sh Executable file
View 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"