chore(plugin): Add a benchmark (#4397)

This commit is contained in:
Donny/강동윤 2022-04-22 14:52:55 +09:00 committed by GitHub
parent 11a5ad5be5
commit 60efb7e9c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 133 additions and 38 deletions

2
Cargo.lock generated
View File

@ -3914,6 +3914,7 @@ name = "swc_plugin_runner"
version = "0.53.1"
dependencies = [
"anyhow",
"criterion",
"once_cell",
"parking_lot",
"serde",
@ -3924,6 +3925,7 @@ dependencies = [
"swc_ecma_loader",
"swc_ecma_parser",
"swc_ecma_visit",
"swc_node_base",
"swc_plugin_proxy",
"testing",
"tracing",

View File

@ -36,55 +36,55 @@ anyhow = "1"
base64 = "0.13.0"
dashmap = "5.1.0"
either = "1"
indexmap = { version = "1", features = ["serde"] }
indexmap = {version = "1", features = ["serde"]}
json_comments = "0.2.0"
lru = "0.7.1"
once_cell = "1.10.0"
parking_lot = "0.12.0"
pathdiff = "0.2.0"
regex = "1"
serde = { version = "1", features = ["derive"] }
serde = {version = "1", features = ["derive"]}
serde_json = "1"
sourcemap = "6"
swc_atoms = { version = "0.2", path = "../swc_atoms" }
swc_cached = { version = "0.1.0", path = "../swc_cached" }
swc_common = { version = "0.17.23", path = "../swc_common", features = [
swc_atoms = {version = "0.2", path = "../swc_atoms"}
swc_cached = {version = "0.1.0", path = "../swc_cached"}
swc_common = {version = "0.17.23", path = "../swc_common", features = [
"sourcemap",
"concurrent",
"parking_lot",
] }
swc_ecma_ast = { version = "0.76.0", path = "../swc_ecma_ast" }
swc_ecma_codegen = { version = "0.105.0", path = "../swc_ecma_codegen" }
swc_ecma_ext_transforms = { version = "0.68.0", path = "../swc_ecma_ext_transforms" }
swc_ecma_lints = { version = "0.35.0", path = "../swc_ecma_lints" }
swc_ecma_loader = { version = "0.29.0", path = "../swc_ecma_loader", features = [
]}
swc_ecma_ast = {version = "0.76.0", path = "../swc_ecma_ast"}
swc_ecma_codegen = {version = "0.105.0", path = "../swc_ecma_codegen"}
swc_ecma_ext_transforms = {version = "0.68.0", path = "../swc_ecma_ext_transforms"}
swc_ecma_lints = {version = "0.35.0", path = "../swc_ecma_lints"}
swc_ecma_loader = {version = "0.29.0", path = "../swc_ecma_loader", features = [
"cache",
"node",
"tsc",
] }
swc_ecma_minifier = { version = "0.105.0", path = "../swc_ecma_minifier" }
swc_ecma_parser = { version = "0.102.0", path = "../swc_ecma_parser" }
swc_ecma_preset_env = { version = "0.121.0", path = "../swc_ecma_preset_env" }
swc_ecma_transforms = { version = "0.146.0", path = "../swc_ecma_transforms", features = [
]}
swc_ecma_minifier = {version = "0.105.0", path = "../swc_ecma_minifier"}
swc_ecma_parser = {version = "0.102.0", path = "../swc_ecma_parser"}
swc_ecma_preset_env = {version = "0.121.0", path = "../swc_ecma_preset_env"}
swc_ecma_transforms = {version = "0.146.0", path = "../swc_ecma_transforms", features = [
"compat",
"module",
"optimization",
"proposal",
"react",
"typescript",
] }
swc_ecma_transforms_base = { version = "0.79.0", path = "../swc_ecma_transforms_base" }
swc_ecma_transforms_compat = { version = "0.93.0", path = "../swc_ecma_transforms_compat" }
swc_ecma_transforms_optimization = { version = "0.116.0", path = "../swc_ecma_transforms_optimization" }
swc_ecma_utils = { version = "0.82.0", path = "../swc_ecma_utils" }
swc_ecma_visit = { version = "0.62.0", path = "../swc_ecma_visit" }
swc_ecmascript = { version = "0.148.0", path = "../swc_ecmascript" }
swc_error_reporters = { version = "0.1.0", path = "../swc_error_reporters" }
swc_node_comments = { version = "0.4.0", path = "../swc_node_comments" }
swc_plugin_proxy = { version = "0.2.1", path = "../swc_plugin_proxy", optional = true }
swc_plugin_runner = { version = "0.53.1", path = "../swc_plugin_runner", optional = true, default-features = false }
swc_timer = { version = "0.5.0", path = "../swc_timer" }
swc_visit = { version = "0.3.0", path = "../swc_visit" }
]}
swc_ecma_transforms_base = {version = "0.79.0", path = "../swc_ecma_transforms_base"}
swc_ecma_transforms_compat = {version = "0.93.0", path = "../swc_ecma_transforms_compat"}
swc_ecma_transforms_optimization = {version = "0.116.0", path = "../swc_ecma_transforms_optimization"}
swc_ecma_utils = {version = "0.82.0", path = "../swc_ecma_utils"}
swc_ecma_visit = {version = "0.62.0", path = "../swc_ecma_visit"}
swc_ecmascript = {version = "0.148.0", path = "../swc_ecmascript"}
swc_error_reporters = {version = "0.1.0", path = "../swc_error_reporters"}
swc_node_comments = {version = "0.4.0", path = "../swc_node_comments"}
swc_plugin_proxy = {version = "0.2.1", path = "../swc_plugin_proxy", optional = true}
swc_plugin_runner = {version = "0.53.1", path = "../swc_plugin_runner", optional = true, default-features = false}
swc_timer = {version = "0.5.0", path = "../swc_timer"}
swc_visit = {version = "0.3.0", path = "../swc_visit"}
tracing = "0.1.32"
[dependencies.napi-derive]
@ -103,11 +103,11 @@ version = "2.0.0"
ansi_term = "0.12"
criterion = "0.3"
rayon = "1.5.1"
swc_ecma_lints = { version = "0.35.0", path = "../swc_ecma_lints", features = [
swc_ecma_lints = {version = "0.35.0", path = "../swc_ecma_lints", features = [
"non_critical_lints",
] }
swc_node_base = { version = "0.5.0", path = "../swc_node_base" }
testing = { version = "0.19.0", path = "../testing" }
]}
swc_node_base = {version = "0.5.0", path = "../swc_node_base"}
testing = {version = "0.19.0", path = "../testing"}
walkdir = "2"
[[example]]

View File

@ -44,11 +44,17 @@ wasmer-wasi = { version = "2.2.1", default-features = false }
wasmer-cache = { version = "2.2.1", optional = true }
[dev-dependencies]
criterion = "0.3"
swc_atoms = { version = "0.2.7", path = '../swc_atoms' }
swc_ecma_loader = { version = "0.29.0", path = "../swc_ecma_loader" }
swc_ecma_parser = { version = "0.102.0", path = "../swc_ecma_parser" }
swc_ecma_visit = { version = "0.62.0", path = "../swc_ecma_visit" }
swc_node_base = { path = "../swc_node_base" }
testing = { version = "0.19.0", path = "../testing" }
# This allows we can run non-wasm32 target build command while some pkg select features for wasmer/js omits its transitive deps
wasmer = "2.2.1"
wasmer-wasi = "2.2.1"
[[bench]]
harness = false
name = "invoke"

View File

@ -0,0 +1,5 @@
class Foo {
}

View File

@ -0,0 +1,82 @@
extern crate swc_node_base;
use std::{
env,
path::{Path, PathBuf},
process::Command,
sync::Arc,
};
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use once_cell::sync::Lazy;
use swc_common::{plugin::Serialized, FileName, FilePathMapping, SourceMap};
use swc_ecma_ast::EsVersion;
use swc_ecma_parser::parse_file_as_program;
use swc_plugin_runner::cache::PluginModuleCache;
static SOURCE: &str = include_str!("./assets/input.js");
fn plugin_group(c: &mut Criterion) {
let plugin_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.join("..")
.join("..")
.join("tests")
.join("rust-plugins")
.join("swc_internal_plugin");
{
let mut cmd = Command::new("cargo");
cmd.current_dir(&plugin_dir);
cmd.arg("build")
.arg("--release")
.arg("--target=wasm32-unknown-unknown");
let status = cmd.status().unwrap();
assert!(status.success());
}
c.bench_function("es/plugin/invoke/1", |b| bench_transform(b, &plugin_dir));
}
fn bench_transform(b: &mut Bencher, plugin_dir: &Path) {
let cache: Lazy<PluginModuleCache> = Lazy::new(PluginModuleCache::new);
b.iter(|| {
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
let fm = cm.new_source_file(FileName::Real("src/test.ts".into()), SOURCE.to_string());
let program = parse_file_as_program(
&fm,
Default::default(),
EsVersion::latest(),
None,
&mut vec![],
)
.unwrap();
let program_ser = Serialized::serialize(&program).unwrap();
let res = swc_plugin_runner::apply_transform_plugin(
"test",
&plugin_dir
.join("target")
.join("wasm32-unknown-unknown")
.join("release")
.join("swc_internal_plugin.wasm"),
&cache,
program_ser,
Serialized::serialize(&String::from("{}")).unwrap(),
Serialized::serialize(&String::from("{}")).unwrap(),
true,
&cm,
)
.unwrap();
let _ = black_box(res);
})
}
criterion_group!(benches, plugin_group);
criterion_main!(benches);

View File

@ -628,7 +628,7 @@ dependencies = [
[[package]]
name = "swc_common"
version = "0.17.21"
version = "0.17.24"
dependencies = [
"ahash",
"anyhow",
@ -668,7 +668,7 @@ dependencies = [
[[package]]
name = "swc_ecma_utils"
version = "0.81.0"
version = "0.82.0"
dependencies = [
"indexmap",
"once_cell",
@ -721,7 +721,7 @@ dependencies = [
[[package]]
name = "swc_plugin"
version = "0.47.0"
version = "0.49.0"
dependencies = [
"swc_atoms",
"swc_common",
@ -734,7 +734,7 @@ dependencies = [
[[package]]
name = "swc_plugin_macro"
version = "0.4.0"
version = "0.4.1"
dependencies = [
"proc-macro2",
"quote",
@ -743,7 +743,7 @@ dependencies = [
[[package]]
name = "swc_plugin_proxy"
version = "0.1.2"
version = "0.2.1"
dependencies = [
"better_scoped_tls",
"rkyv",