2022-04-22 08:52:55 +03:00
|
|
|
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;
|
2022-07-07 10:46:49 +03:00
|
|
|
use swc_common::{
|
|
|
|
plugin::{PluginSerializedBytes, VersionedSerializable},
|
|
|
|
FileName, FilePathMapping, SourceMap,
|
|
|
|
};
|
2022-04-22 08:52:55 +03:00
|
|
|
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();
|
|
|
|
|
2022-07-07 10:46:49 +03:00
|
|
|
let program = VersionedSerializable::new(program);
|
2022-07-06 07:42:00 +03:00
|
|
|
let program_ser = PluginSerializedBytes::try_serialize(&program).unwrap();
|
2022-04-22 08:52:55 +03:00
|
|
|
|
2022-07-08 09:02:42 +03:00
|
|
|
let mut transform_plugin_executor = swc_plugin_runner::create_plugin_transform_executor(
|
2022-04-22 08:52:55 +03:00
|
|
|
&plugin_dir
|
|
|
|
.join("target")
|
|
|
|
.join("wasm32-unknown-unknown")
|
|
|
|
.join("release")
|
|
|
|
.join("swc_internal_plugin.wasm"),
|
2022-07-08 09:02:42 +03:00
|
|
|
&swc_plugin_runner::cache::PLUGIN_MODULE_CACHE,
|
2022-04-22 08:52:55 +03:00
|
|
|
&cm,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-07-08 09:02:42 +03:00
|
|
|
let res = transform_plugin_executor
|
|
|
|
.transform(
|
|
|
|
&program_ser,
|
|
|
|
&PluginSerializedBytes::try_serialize(&VersionedSerializable::new(String::from(
|
|
|
|
"{}",
|
|
|
|
)))
|
|
|
|
.unwrap(),
|
|
|
|
&PluginSerializedBytes::try_serialize(&VersionedSerializable::new(String::from(
|
|
|
|
"{}",
|
|
|
|
)))
|
|
|
|
.unwrap(),
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2022-04-22 08:52:55 +03:00
|
|
|
let _ = black_box(res);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, plugin_group);
|
|
|
|
criterion_main!(benches);
|