2023-01-13 02:51:32 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
use std::time;
|
|
|
|
|
2022-03-11 17:07:02 -05:00
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2023-04-13 13:43:23 -04:00
|
|
|
use deno_core::serde_v8;
|
|
|
|
use deno_core::v8;
|
2022-03-11 17:07:02 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_runtime::permissions::create_child_permissions;
|
|
|
|
use deno_runtime::permissions::ChildPermissionsArg;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2022-05-30 13:58:44 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2022-03-11 17:07:02 -05:00
|
|
|
use tokio::sync::mpsc::UnboundedSender;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2023-01-13 02:51:32 -05:00
|
|
|
use crate::tools::bench::BenchDescription;
|
|
|
|
use crate::tools::bench::BenchEvent;
|
2023-04-13 13:43:23 -04:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct BenchContainer(
|
|
|
|
pub Vec<(BenchDescription, v8::Global<v8::Function>)>,
|
|
|
|
);
|
2023-01-13 02:51:32 -05:00
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_core::extension!(deno_bench,
|
|
|
|
ops = [
|
|
|
|
op_pledge_test_permissions,
|
|
|
|
op_restore_test_permissions,
|
|
|
|
op_register_bench,
|
|
|
|
op_dispatch_bench_event,
|
|
|
|
op_bench_now,
|
|
|
|
],
|
2023-03-17 18:15:27 -04:00
|
|
|
options = {
|
2023-03-17 14:22:15 -04:00
|
|
|
sender: UnboundedSender<BenchEvent>,
|
|
|
|
},
|
2023-03-17 18:15:27 -04:00
|
|
|
state = |state, options| {
|
|
|
|
state.put(options.sender);
|
2023-04-13 13:43:23 -04:00
|
|
|
state.put(BenchContainer::default());
|
2023-03-17 14:22:15 -04:00
|
|
|
},
|
2023-03-18 18:30:04 -04:00
|
|
|
customizer = |ext: &mut deno_core::ExtensionBuilder| {
|
|
|
|
ext.force_op_registration();
|
|
|
|
},
|
2023-03-17 14:22:15 -04:00
|
|
|
);
|
2022-03-11 17:07:02 -05:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2023-01-07 11:25:34 -05:00
|
|
|
struct PermissionsHolder(Uuid, PermissionsContainer);
|
2022-03-11 17:07:02 -05:00
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-11 17:07:02 -05:00
|
|
|
pub fn op_pledge_test_permissions(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ChildPermissionsArg,
|
|
|
|
) -> Result<Uuid, AnyError> {
|
|
|
|
let token = Uuid::new_v4();
|
2023-01-07 11:25:34 -05:00
|
|
|
let parent_permissions = state.borrow_mut::<PermissionsContainer>();
|
|
|
|
let worker_permissions = {
|
|
|
|
let mut parent_permissions = parent_permissions.0.lock();
|
|
|
|
let perms = create_child_permissions(&mut parent_permissions, args)?;
|
|
|
|
PermissionsContainer::new(perms)
|
|
|
|
};
|
2022-03-11 17:07:02 -05:00
|
|
|
let parent_permissions = parent_permissions.clone();
|
|
|
|
|
2022-04-17 11:47:24 -04:00
|
|
|
if state.try_take::<PermissionsHolder>().is_some() {
|
|
|
|
panic!("pledge test permissions called before restoring previous pledge");
|
|
|
|
}
|
|
|
|
|
2022-03-11 17:07:02 -05:00
|
|
|
state.put::<PermissionsHolder>(PermissionsHolder(token, parent_permissions));
|
|
|
|
|
|
|
|
// NOTE: This call overrides current permission set for the worker
|
2023-01-07 11:25:34 -05:00
|
|
|
state.put::<PermissionsContainer>(worker_permissions);
|
2022-03-11 17:07:02 -05:00
|
|
|
|
|
|
|
Ok(token)
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-11 17:07:02 -05:00
|
|
|
pub fn op_restore_test_permissions(
|
|
|
|
state: &mut OpState,
|
|
|
|
token: Uuid,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if let Some(permissions_holder) = state.try_take::<PermissionsHolder>() {
|
|
|
|
if token != permissions_holder.0 {
|
|
|
|
panic!("restore test permissions token does not match the stored token");
|
|
|
|
}
|
|
|
|
|
|
|
|
let permissions = permissions_holder.1;
|
2023-01-07 11:25:34 -05:00
|
|
|
state.put::<PermissionsContainer>(permissions);
|
2022-03-11 17:07:02 -05:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(generic_error("no permissions to restore"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 13:43:23 -04:00
|
|
|
#[derive(Deserialize)]
|
2022-05-30 13:58:44 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2023-04-13 13:43:23 -04:00
|
|
|
struct BenchInfo<'s> {
|
|
|
|
#[serde(rename = "fn")]
|
|
|
|
function: serde_v8::Value<'s>,
|
2022-05-30 13:58:44 -04:00
|
|
|
name: String,
|
|
|
|
baseline: bool,
|
|
|
|
group: Option<String>,
|
2023-04-13 13:43:23 -04:00
|
|
|
ignore: bool,
|
|
|
|
only: bool,
|
2022-05-30 13:58:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct BenchRegisterResult {
|
|
|
|
id: usize,
|
2023-04-13 13:43:23 -04:00
|
|
|
origin: String,
|
2022-05-30 13:58:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
|
2023-04-13 13:43:23 -04:00
|
|
|
#[op(v8)]
|
|
|
|
fn op_register_bench<'a>(
|
|
|
|
scope: &mut v8::HandleScope<'a>,
|
2022-05-30 13:58:44 -04:00
|
|
|
state: &mut OpState,
|
2023-04-13 13:43:23 -04:00
|
|
|
info: BenchInfo<'a>,
|
2022-05-30 13:58:44 -04:00
|
|
|
) -> Result<BenchRegisterResult, AnyError> {
|
|
|
|
let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
2023-04-13 13:43:23 -04:00
|
|
|
let origin = state.borrow::<ModuleSpecifier>().to_string();
|
2022-05-30 13:58:44 -04:00
|
|
|
let description = BenchDescription {
|
|
|
|
id,
|
|
|
|
name: info.name,
|
2023-04-13 13:43:23 -04:00
|
|
|
origin: origin.clone(),
|
2022-05-30 13:58:44 -04:00
|
|
|
baseline: info.baseline,
|
|
|
|
group: info.group,
|
2023-04-13 13:43:23 -04:00
|
|
|
ignore: info.ignore,
|
|
|
|
only: info.only,
|
2022-05-30 13:58:44 -04:00
|
|
|
};
|
2023-04-13 13:43:23 -04:00
|
|
|
let function: v8::Local<v8::Function> = info.function.v8_value.try_into()?;
|
|
|
|
let function = v8::Global::new(scope, function);
|
|
|
|
state
|
|
|
|
.borrow_mut::<BenchContainer>()
|
|
|
|
.0
|
|
|
|
.push((description.clone(), function));
|
2022-05-30 13:58:44 -04:00
|
|
|
let sender = state.borrow::<UnboundedSender<BenchEvent>>().clone();
|
|
|
|
sender.send(BenchEvent::Register(description)).ok();
|
2023-04-13 13:43:23 -04:00
|
|
|
Ok(BenchRegisterResult { id, origin })
|
2022-05-30 13:58:44 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-05-13 04:36:31 -04:00
|
|
|
fn op_dispatch_bench_event(state: &mut OpState, event: BenchEvent) {
|
2023-04-13 13:43:23 -04:00
|
|
|
assert!(
|
|
|
|
matches!(event, BenchEvent::Output(_)),
|
|
|
|
"Only output events are expected from JS."
|
|
|
|
);
|
2022-03-11 17:07:02 -05:00
|
|
|
let sender = state.borrow::<UnboundedSender<BenchEvent>>().clone();
|
|
|
|
sender.send(event).ok();
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_bench_now(state: &mut OpState) -> Result<u64, AnyError> {
|
2022-03-11 17:07:02 -05:00
|
|
|
let ns = state.borrow::<time::Instant>().elapsed().as_nanos();
|
|
|
|
let ns_u64 = u64::try_from(ns)?;
|
|
|
|
Ok(ns_u64)
|
|
|
|
}
|