2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 02:51:32 -05:00
|
|
|
|
|
|
|
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;
|
2024-03-24 01:22:37 -04:00
|
|
|
use deno_core::error::type_error;
|
2022-03-11 17:07:02 -05:00
|
|
|
use deno_core::error::AnyError;
|
2023-09-12 07:14:45 -04:00
|
|
|
use deno_core::op2;
|
2023-04-13 13:43:23 -04:00
|
|
|
use deno_core::v8;
|
2022-03-11 17:07:02 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_core::OpState;
|
2024-06-06 23:37:53 -04:00
|
|
|
use deno_runtime::deno_permissions::create_child_permissions;
|
|
|
|
use deno_runtime::deno_permissions::ChildPermissionsArg;
|
|
|
|
use deno_runtime::deno_permissions::PermissionsContainer;
|
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,
|
2024-03-24 01:22:37 -04:00
|
|
|
op_bench_get_origin,
|
2023-03-17 14:22:15 -04:00
|
|
|
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
|
|
|
},
|
|
|
|
);
|
2022-03-11 17:07:02 -05:00
|
|
|
|
2024-03-24 01:22:37 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
|
|
|
fn op_bench_get_origin(state: &mut OpState) -> String {
|
|
|
|
state.borrow::<ModuleSpecifier>().to_string()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-03-11 17:07:02 -05:00
|
|
|
pub fn op_pledge_test_permissions(
|
|
|
|
state: &mut OpState,
|
2023-09-12 07:14:45 -04:00
|
|
|
#[serde] args: ChildPermissionsArg,
|
2022-03-11 17:07:02 -05:00
|
|
|
) -> 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 = {
|
2024-06-06 23:37:53 -04:00
|
|
|
let mut parent_permissions = parent_permissions.0.lock();
|
2023-01-07 11:25:34 -05:00
|
|
|
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
|
2024-03-12 13:42:26 -04:00
|
|
|
state.put(worker_permissions.0.clone());
|
2023-01-07 11:25:34 -05:00
|
|
|
state.put::<PermissionsContainer>(worker_permissions);
|
2022-03-11 17:07:02 -05:00
|
|
|
|
|
|
|
Ok(token)
|
|
|
|
}
|
|
|
|
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2]
|
2022-03-11 17:07:02 -05:00
|
|
|
pub fn op_restore_test_permissions(
|
|
|
|
state: &mut OpState,
|
2023-09-12 07:14:45 -04:00
|
|
|
#[serde] token: Uuid,
|
2022-03-11 17:07:02 -05:00
|
|
|
) -> 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;
|
2024-03-12 13:42:26 -04:00
|
|
|
state.put(permissions.0.clone());
|
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"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-30 13:58:44 -04:00
|
|
|
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
|
2024-03-24 01:22:37 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2]
|
2024-03-24 01:22:37 -04:00
|
|
|
fn op_register_bench(
|
2022-05-30 13:58:44 -04:00
|
|
|
state: &mut OpState,
|
2024-03-24 01:22:37 -04:00
|
|
|
#[global] function: v8::Global<v8::Function>,
|
|
|
|
#[string] name: String,
|
|
|
|
baseline: bool,
|
|
|
|
#[string] group: Option<String>,
|
|
|
|
ignore: bool,
|
|
|
|
only: bool,
|
|
|
|
warmup: bool,
|
|
|
|
#[buffer] ret_buf: &mut [u8],
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if ret_buf.len() != 4 {
|
|
|
|
return Err(type_error(format!(
|
|
|
|
"Invalid ret_buf length: {}",
|
|
|
|
ret_buf.len()
|
|
|
|
)));
|
|
|
|
}
|
2022-05-30 13:58:44 -04:00
|
|
|
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,
|
2024-03-24 01:22:37 -04:00
|
|
|
name,
|
2023-04-13 13:43:23 -04:00
|
|
|
origin: origin.clone(),
|
2024-03-24 01:22:37 -04:00
|
|
|
baseline,
|
|
|
|
group,
|
|
|
|
ignore,
|
|
|
|
only,
|
|
|
|
warmup,
|
2022-05-30 13:58:44 -04:00
|
|
|
};
|
2023-04-13 13:43:23 -04:00
|
|
|
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();
|
2024-03-24 01:22:37 -04:00
|
|
|
ret_buf.copy_from_slice(&(id as u32).to_le_bytes());
|
|
|
|
Ok(())
|
2022-05-30 13:58:44 -04:00
|
|
|
}
|
|
|
|
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2]
|
|
|
|
fn op_dispatch_bench_event(state: &mut OpState, #[serde] 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();
|
|
|
|
}
|
|
|
|
|
2023-09-23 08:04:47 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[number]
|
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)
|
|
|
|
}
|