2022-09-10 06:50:54 +03:00
|
|
|
#![cfg_attr(not(feature = "__rkyv"), allow(warnings))]
|
|
|
|
|
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};
|
2022-09-09 11:05:51 +03:00
|
|
|
#[cfg(feature = "__rkyv")]
|
|
|
|
use swc_common::plugin::serialized::PluginSerializedBytes;
|
2022-07-07 10:46:49 +03:00
|
|
|
use swc_common::{
|
2022-09-09 11:05:51 +03:00
|
|
|
collections::AHashMap, plugin::metadata::TransformPluginMetadataContext, FileName,
|
2022-09-18 12:54:06 +03:00
|
|
|
FilePathMapping, Globals, Mark, SourceMap, GLOBALS,
|
2022-07-07 10:46:49 +03:00
|
|
|
};
|
2022-04-22 08:52:55 +03:00
|
|
|
use swc_ecma_ast::EsVersion;
|
|
|
|
use swc_ecma_parser::parse_file_as_program;
|
2022-09-18 12:54:06 +03:00
|
|
|
use swc_plugin_runner::cache::init_plugin_module_cache_once;
|
2022-04-22 08:52:55 +03:00
|
|
|
|
|
|
|
static SOURCE: &str = include_str!("./assets/input.js");
|
|
|
|
|
|
|
|
fn plugin_group(c: &mut Criterion) {
|
2022-09-18 12:54:06 +03:00
|
|
|
init_plugin_module_cache_once(&None);
|
|
|
|
|
2022-04-22 08:52:55 +03:00
|
|
|
let plugin_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
|
|
|
|
.join("tests")
|
2022-09-18 12:54:06 +03:00
|
|
|
.join("fixture")
|
|
|
|
.join("swc_noop_plugin");
|
2022-04-22 08:52:55 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut cmd = Command::new("cargo");
|
|
|
|
|
|
|
|
cmd.current_dir(&plugin_dir);
|
|
|
|
cmd.arg("build")
|
|
|
|
.arg("--release")
|
2022-09-18 12:54:06 +03:00
|
|
|
.arg("--target=wasm32-wasi");
|
2022-04-22 08:52:55 +03:00
|
|
|
|
|
|
|
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) {
|
2022-09-09 11:05:51 +03:00
|
|
|
#[cfg(feature = "__rkyv")]
|
2022-04-22 08:52:55 +03:00
|
|
|
b.iter(|| {
|
2022-09-18 12:54:06 +03:00
|
|
|
GLOBALS.set(&Globals::new(), || {
|
|
|
|
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
|
2022-04-22 08:52:55 +03:00
|
|
|
|
2022-09-18 12:54:06 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Real("src/test.ts".into()), SOURCE.to_string());
|
2022-07-21 08:34:48 +03:00
|
|
|
|
2022-09-18 12:54:06 +03:00
|
|
|
let program = parse_file_as_program(
|
|
|
|
&fm,
|
|
|
|
Default::default(),
|
|
|
|
EsVersion::latest(),
|
|
|
|
None,
|
|
|
|
&mut vec![],
|
|
|
|
)
|
2022-07-08 09:02:42 +03:00
|
|
|
.unwrap();
|
|
|
|
|
2022-09-18 12:54:06 +03:00
|
|
|
let program_ser = PluginSerializedBytes::try_serialize(&program).unwrap();
|
|
|
|
|
|
|
|
let mut transform_plugin_executor =
|
|
|
|
swc_plugin_runner::create_plugin_transform_executor(
|
|
|
|
&plugin_dir
|
|
|
|
.join("target")
|
|
|
|
.join("wasm32-wasi")
|
|
|
|
.join("release")
|
|
|
|
.join("swc_noop_plugin.wasm"),
|
|
|
|
&swc_plugin_runner::cache::PLUGIN_MODULE_CACHE,
|
|
|
|
&cm,
|
|
|
|
&Arc::new(TransformPluginMetadataContext::new(
|
|
|
|
None,
|
|
|
|
"development".to_string(),
|
|
|
|
None,
|
|
|
|
)),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let experimental_metadata: AHashMap<String, String> = AHashMap::default();
|
|
|
|
let _experimental_metadata =
|
|
|
|
PluginSerializedBytes::try_serialize(&experimental_metadata)
|
|
|
|
.expect("Should be a hashmap");
|
|
|
|
|
|
|
|
let res = transform_plugin_executor
|
|
|
|
.transform(&program_ser, Mark::new(), true)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let _ = black_box(res);
|
|
|
|
});
|
2022-04-22 08:52:55 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(benches, plugin_group);
|
|
|
|
criterion_main!(benches);
|