add measuring of the rewrite() function

This commit is contained in:
Toshit Chawda 2024-10-21 23:10:49 -07:00
parent 66929af43c
commit cd8495b7ae
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
4 changed files with 76 additions and 38 deletions

13
rewriter/Cargo.lock generated
View file

@ -513,6 +513,18 @@ dependencies = [
"hashbrown 0.15.0",
]
[[package]]
name = "instant"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222"
dependencies = [
"cfg-if",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "intrusive-collections"
version = "0.9.7"
@ -1041,6 +1053,7 @@ dependencies = [
"boa_engine",
"console_error_panic_hook",
"getrandom",
"instant",
"js-sys",
"obfstr",
"oxc_allocator",

View file

@ -25,6 +25,7 @@ panic = "abort"
[dependencies]
console_error_panic_hook = "0.1.7"
getrandom = { version = "0.2.15", features = ["js"] }
instant = { version = "0.1.13", features = ["wasm-bindgen"] }
js-sys = "0.3.69"
obfstr = "0.4.3"
oxc_allocator = "0.32.0"

View file

@ -1,9 +1,10 @@
pub mod error;
pub mod rewrite;
use std::{panic, str::FromStr, sync::Arc};
use std::{panic, str::FromStr, sync::Arc, time::Duration};
use error::{Result, RewriterError};
use instant::Instant;
use js_sys::{Function, Object, Reflect};
use oxc_diagnostics::{NamedSource, OxcDiagnostic};
use rewrite::{rewrite, Config, EncodeFn};
@ -12,7 +13,7 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen(typescript_custom_section)]
const REWRITER_OUTPUT: &'static str = r#"
type RewriterOutput = { js: Uint8Array, errors: string[] };
type RewriterOutput = { js: Uint8Array, errors: string[], duration: bigint };
"#;
#[wasm_bindgen]
@ -110,10 +111,15 @@ fn drmcheck() -> bool {
return vec![obfstr!("http://localhost:1337")].contains(&true_origin.as_str());
}
fn duration_to_millis_f64(duration: Duration) -> f64 {
(duration.as_secs() as f64) * 1_000f64 + (duration.subsec_nanos() as f64) / 1_000_000f64
}
fn create_rewriter_output(
out: (Vec<u8>, Vec<OxcDiagnostic>),
url: String,
src: String,
duration: Duration,
) -> Result<RewriterOutput> {
let src = Arc::new(NamedSource::new(url, src).with_language("javascript"));
let errs: Vec<_> = out
@ -123,8 +129,9 @@ fn create_rewriter_output(
.collect();
let obj = Object::new();
set_obj(&obj, "js", &JsValue::from(out.0))?;
set_obj(&obj, "errors", &JsValue::from(errs))?;
set_obj(&obj, "js", &out.0.into())?;
set_obj(&obj, "errors", &errs.into())?;
set_obj(&obj, "duration", &duration_to_millis_f64(duration).into())?;
Ok(RewriterOutput::from(JsValue::from(obj)))
}
@ -141,11 +148,11 @@ pub fn rewrite_js(
return Vec::new();
}
create_rewriter_output(
rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?,
script_url,
js.to_string(),
)
let before = Instant::now();
let out = rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?;
let after = Instant::now();
create_rewriter_output(out, script_url, js.to_string(), after - before)
}
#[wasm_bindgen]
@ -163,9 +170,9 @@ pub fn rewrite_js_from_arraybuffer(
// we know that this is a valid utf-8 string
let js = unsafe { std::str::from_utf8_unchecked(js) };
create_rewriter_output(
rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?,
script_url,
js.to_string(),
)
let before = Instant::now();
let out = rewrite(js, Url::from_str(url)?, get_config(scramjet, url)?)?;
let after = Instant::now();
create_rewriter_output(out, script_url, js.to_string(), after - before)
}

View file

@ -6,6 +6,7 @@ import {
initSync,
rewrite_js,
rewrite_js_from_arraybuffer,
RewriterOutput,
} from "../../../rewriter/out/rewriter.js";
import { $scramjet, flagEnabled } from "../../scramjet";
@ -19,13 +20,51 @@ init();
Error.stackTraceLimit = 50;
function print_errors(errors: string[]) {
const decoder = new TextDecoder();
function rewriteJsWrapper(
input: string | ArrayBuffer,
meta: URLMeta
): string | ArrayBuffer {
let out: RewriterOutput;
if (typeof input === "string") {
out = rewrite_js(
input,
meta.base.href,
"PERCS_PLEASE_FILL_THIS_IN.js",
$scramjet
);
} else {
out = rewrite_js_from_arraybuffer(
new Uint8Array(input),
meta.base.href,
"PERCS_PLEASE_FILL_THIS_IN.js",
$scramjet
);
}
const { js, errors, duration } = out;
// TODO: maybe make this a scram flag?
if (true) {
for (const error of errors) {
console.error("oxc parse error", error);
}
}
// TODO: maybe make this a scram flag?
if (true) {
let timespan: string;
if (duration < 1n) {
timespan = "BLAZINGLY FAST";
} else if (duration < 500n) {
timespan = "decent speed";
} else {
timespan = "really slow";
}
console.log(`oxc rewrite was ${timespan} (${duration}ms)`);
}
return typeof input === "string" ? decoder.decode(js) : js;
}
export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) {
@ -37,29 +76,7 @@ export function rewriteJs(js: string | ArrayBuffer, meta: URLMeta) {
return rewriteJsNaiive(text);
}
// const before = performance.now();
if (typeof js === "string") {
let { js: js_out, errors } = rewrite_js(
js,
meta.base.href,
"PERCS_PLEASE_FILL_THIS_IN.js",
$scramjet
);
js = new TextDecoder().decode(js_out);
print_errors(errors);
} else {
let { js: js_out, errors } = rewrite_js_from_arraybuffer(
new Uint8Array(js),
meta.base.href,
"PERCS_PLEASE_FILL_THIS_IN.js",
$scramjet
);
js = js_out;
print_errors(errors);
}
// const after = performance.now();
// dbg.debug("Rewrite took", Math.floor((after - before) * 10) / 10, "ms");
js = rewriteJsWrapper(js, meta);
return js;
}