diff options
author | Matt Mastracci <matthew@mastracci.com> | 2023-11-05 14:27:36 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-05 14:27:36 -0700 |
commit | 485fade0b6910e29557c6627d37985b47735bf8e (patch) | |
tree | 0f6c97c135c8a92226caecb363effe7eabf47936 /cli | |
parent | 4530cd5f0d5e6acdbbb11a60eac711d47f274d3f (diff) |
chore: migrate to new deno_core and metrics (#21057)
- Uses the new OpMetrics system for sync and async calls
- Partial revert of #21048 as we moved Array.fromAsync upstream to
deno_core
Diffstat (limited to 'cli')
-rw-r--r-- | cli/args/flags.rs | 1 | ||||
-rw-r--r-- | cli/args/mod.rs | 8 | ||||
-rw-r--r-- | cli/factory.rs | 1 | ||||
-rw-r--r-- | cli/main.rs | 3 | ||||
-rw-r--r-- | cli/ops/testing.rs | 94 | ||||
-rw-r--r-- | cli/standalone/mod.rs | 1 | ||||
-rw-r--r-- | cli/tests/integration/js_unit_tests.rs | 1 | ||||
-rw-r--r-- | cli/tests/integration/run_tests.rs | 5 | ||||
-rw-r--r-- | cli/tests/testdata/run/future_check.ts | 1 | ||||
-rw-r--r-- | cli/tests/testdata/run/future_check2.out | 1 | ||||
-rw-r--r-- | cli/tests/unit/metrics_test.ts | 93 | ||||
-rw-r--r-- | cli/tests/unit/timers_test.ts | 13 | ||||
-rw-r--r-- | cli/tsc/mod.rs | 2 | ||||
-rw-r--r-- | cli/worker.rs | 3 |
14 files changed, 57 insertions, 170 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 82a536e47..9b0f840e9 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -398,6 +398,7 @@ pub struct Flags { pub config_flag: ConfigFlag, pub node_modules_dir: Option<bool>, pub vendor: Option<bool>, + pub enable_op_summary_metrics: bool, pub enable_testing_features: bool, pub ext: Option<String>, pub ignore: Vec<PathBuf>, diff --git a/cli/args/mod.rs b/cli/args/mod.rs index fa8d94de5..4acbb1763 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -1137,6 +1137,14 @@ impl CliOptions { } } + pub fn enable_op_summary_metrics(&self) -> bool { + self.flags.enable_op_summary_metrics + || matches!( + self.flags.subcommand, + DenoSubcommand::Test(_) | DenoSubcommand::Repl(_) + ) + } + pub fn enable_testing_features(&self) -> bool { self.flags.enable_testing_features } diff --git a/cli/factory.rs b/cli/factory.rs index 037ba4e2f..e69631305 100644 --- a/cli/factory.rs +++ b/cli/factory.rs @@ -670,6 +670,7 @@ impl CliFactory { argv: self.options.argv().clone(), log_level: self.options.log_level().unwrap_or(log::Level::Info).into(), coverage_dir: self.options.coverage_dir(), + enable_op_summary_metrics: self.options.enable_op_summary_metrics(), enable_testing_features: self.options.enable_testing_features(), has_node_modules_dir: self.options.has_node_modules_dir(), hmr: self.options.has_hmr(), diff --git a/cli/main.rs b/cli/main.rs index 80fe59005..18b0fe827 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -367,8 +367,7 @@ pub fn main() { // Using same default as VSCode: // https://github.com/microsoft/vscode/blob/48d4ba271686e8072fc6674137415bc80d936bc7/extensions/typescript-language-features/src/configuration/configuration.ts#L213-L214 DenoSubcommand::Lsp => vec!["--max-old-space-size=3072".to_string()], - // TODO(bartlomieju): upstream this to `deno_core` crate - _ => vec!["--harmony-array-from-async".to_string()], + _ => vec![], }; init_v8_flags(&default_v8_flags, &flags.v8_flags, get_v8_flags_from_env()); deno_core::JsRuntime::init_platform(None); diff --git a/cli/ops/testing.rs b/cli/ops/testing.rs index 7454670c6..6984bc5b1 100644 --- a/cli/ops/testing.rs +++ b/cli/ops/testing.rs @@ -14,16 +14,17 @@ use deno_core::error::AnyError; use deno_core::op2; use deno_core::v8; use deno_core::ModuleSpecifier; -use deno_core::OpMetrics; +use deno_core::OpMetricsSummary; +use deno_core::OpMetricsSummaryTracker; use deno_core::OpState; use deno_runtime::deno_fetch::reqwest; use deno_runtime::permissions::create_child_permissions; use deno_runtime::permissions::ChildPermissionsArg; use deno_runtime::permissions::PermissionsContainer; use serde::Serialize; -use std::cell::Ref; use std::collections::hash_map::Entry; use std::collections::HashMap; +use std::rc::Rc; use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; use uuid::Uuid; @@ -243,38 +244,28 @@ fn op_test_event_step_result_failed( struct TestOpSanitizers(HashMap<u32, TestOpSanitizerState>); enum TestOpSanitizerState { - Collecting { metrics: Vec<OpMetrics> }, + Collecting { metrics: Vec<OpMetricsSummary> }, Finished { report: Vec<TestOpSanitizerReport> }, } fn try_collect_metrics( - state: &OpState, + metrics: &OpMetricsSummaryTracker, force: bool, op_id_host_recv_msg: usize, op_id_host_recv_ctrl: usize, -) -> Result<Ref<Vec<OpMetrics>>, bool> { - let metrics = state.tracker.per_op(); - for op_metric in &*metrics { - let has_pending_ops = op_metric.ops_dispatched_async - + op_metric.ops_dispatched_async_unref - > op_metric.ops_completed_async + op_metric.ops_completed_async_unref; - if has_pending_ops && !force { - let host_recv_msg = metrics - .get(op_id_host_recv_msg) - .map(|op_metric| { - op_metric.ops_dispatched_async + op_metric.ops_dispatched_async_unref - > op_metric.ops_completed_async - + op_metric.ops_completed_async_unref - }) - .unwrap_or(false); - let host_recv_ctrl = metrics - .get(op_id_host_recv_ctrl) - .map(|op_metric| { - op_metric.ops_dispatched_async + op_metric.ops_dispatched_async_unref - > op_metric.ops_completed_async - + op_metric.ops_completed_async_unref - }) - .unwrap_or(false); +) -> Result<std::cell::Ref<Vec<OpMetricsSummary>>, bool> { + let metrics = metrics.per_op(); + let host_recv_msg = metrics + .get(op_id_host_recv_msg) + .map(OpMetricsSummary::has_outstanding_ops) + .unwrap_or(false); + let host_recv_ctrl = metrics + .get(op_id_host_recv_ctrl) + .map(OpMetricsSummary::has_outstanding_ops) + .unwrap_or(false); + + for op_metric in metrics.iter() { + if op_metric.has_outstanding_ops() && !force { return Err(host_recv_msg || host_recv_ctrl); } } @@ -294,23 +285,23 @@ fn op_test_op_sanitizer_collect( #[smi] op_id_host_recv_msg: usize, #[smi] op_id_host_recv_ctrl: usize, ) -> Result<u8, AnyError> { - let metrics = { - let metrics = match try_collect_metrics( - state, - force, - op_id_host_recv_msg, - op_id_host_recv_ctrl, - ) { - Ok(metrics) => metrics, - Err(false) => { - return Ok(1); - } - Err(true) => { - return Ok(2); - } - }; - metrics.clone() - }; + let metrics = state.borrow::<Rc<OpMetricsSummaryTracker>>(); + let metrics = match try_collect_metrics( + metrics, + force, + op_id_host_recv_msg, + op_id_host_recv_ctrl, + ) { + Ok(metrics) => metrics, + Err(false) => { + return Ok(1); + } + Err(true) => { + return Ok(2); + } + } + .clone(); + let op_sanitizers = state.borrow_mut::<TestOpSanitizers>(); match op_sanitizers.0.entry(id) { Entry::Vacant(entry) => { @@ -348,11 +339,12 @@ fn op_test_op_sanitizer_finish( ) -> Result<u8, AnyError> { // Drop `fetch` connection pool at the end of a test state.try_take::<reqwest::Client>(); + let metrics = state.borrow::<Rc<OpMetricsSummaryTracker>>(); // Generate a report of pending ops let report = { let after_metrics = match try_collect_metrics( - state, + metrics, force, op_id_host_recv_msg, op_id_host_recv_ctrl, @@ -380,14 +372,10 @@ fn op_test_op_sanitizer_finish( for (id, (before, after)) in before_metrics.iter().zip(after_metrics.iter()).enumerate() { - let async_pending_before = before.ops_dispatched_async - + before.ops_dispatched_async_unref - - before.ops_completed_async - - before.ops_completed_async_unref; - let async_pending_after = after.ops_dispatched_async - + after.ops_dispatched_async_unref - - after.ops_completed_async - - after.ops_completed_async_unref; + let async_pending_before = + before.ops_dispatched_async - before.ops_completed_async; + let async_pending_after = + after.ops_dispatched_async - after.ops_completed_async; let diff = async_pending_after as i64 - async_pending_before as i64; if diff != 0 { report.push(TestOpSanitizerReport { id, diff }); diff --git a/cli/standalone/mod.rs b/cli/standalone/mod.rs index 803655b9a..4c5430834 100644 --- a/cli/standalone/mod.rs +++ b/cli/standalone/mod.rs @@ -453,6 +453,7 @@ pub async fn run( argv: metadata.argv, log_level: WorkerLogLevel::Info, coverage_dir: None, + enable_op_summary_metrics: false, enable_testing_features: false, has_node_modules_dir, hmr: false, diff --git a/cli/tests/integration/js_unit_tests.rs b/cli/tests/integration/js_unit_tests.rs index 863776aa2..33d79236c 100644 --- a/cli/tests/integration/js_unit_tests.rs +++ b/cli/tests/integration/js_unit_tests.rs @@ -53,7 +53,6 @@ util::unit_test_factory!( link_test, make_temp_test, message_channel_test, - metrics_test, mkdir_test, navigator_test, net_test, diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs index 61749f751..153b35dc5 100644 --- a/cli/tests/integration/run_tests.rs +++ b/cli/tests/integration/run_tests.rs @@ -3425,11 +3425,6 @@ itest!(unstable_ffi_19 { exit_code: 70, }); -itest!(future_check2 { - args: "run --check run/future_check.ts", - output: "run/future_check2.out", -}); - itest!(event_listener_error { args: "run --quiet run/event_listener_error.ts", output: "run/event_listener_error.ts.out", diff --git a/cli/tests/testdata/run/future_check.ts b/cli/tests/testdata/run/future_check.ts deleted file mode 100644 index 4d41fe06a..000000000 --- a/cli/tests/testdata/run/future_check.ts +++ /dev/null @@ -1 +0,0 @@ -Deno.metrics(); diff --git a/cli/tests/testdata/run/future_check2.out b/cli/tests/testdata/run/future_check2.out deleted file mode 100644 index c626a5485..000000000 --- a/cli/tests/testdata/run/future_check2.out +++ /dev/null @@ -1 +0,0 @@ -Check [WILDCARD]/future_check.ts diff --git a/cli/tests/unit/metrics_test.ts b/cli/tests/unit/metrics_test.ts deleted file mode 100644 index 6f8c3b46d..000000000 --- a/cli/tests/unit/metrics_test.ts +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals } from "./test_util.ts"; - -Deno.test(async function metrics() { - // Write to stdout to ensure a "data" message gets sent instead of just - // control messages. - const dataMsg = new Uint8Array([13, 13, 13]); // "\r\r\r", - await Deno.stdout.write(dataMsg); - - // WARNING: bytesReceived & bytesSentControl are now always zero - // following https://github.com/denoland/deno/pull/9843 - - const m1 = Deno.metrics(); - assert(m1.opsDispatched > 0); - assert(m1.opsCompleted > 0); - assert(m1.bytesSentControl === 0); - assert(m1.bytesSentData === 0); - assert(m1.bytesReceived === 0); - const m1OpWrite = m1.ops["op_write"]; - assert(m1OpWrite.opsDispatchedAsync > 0); - assert(m1OpWrite.opsCompletedAsync > 0); - assert(m1OpWrite.bytesSentControl === 0); - assert(m1OpWrite.bytesSentData >= 0); - assert(m1OpWrite.bytesReceived === 0); - - await Deno.stdout.write(dataMsg); - - const m2 = Deno.metrics(); - assert(m2.opsDispatchedAsync > m1.opsDispatchedAsync); - assert(m2.opsCompletedAsync > m1.opsCompletedAsync); - assert(m2.bytesSentControl === m1.bytesSentControl); - assert(m2.bytesSentData === 0); - assert(m2.bytesReceived === m1.bytesReceived); - const m2OpWrite = m2.ops["op_write"]; - assert(m2OpWrite.opsDispatchedAsync > m1OpWrite.opsDispatchedAsync); - assert(m2OpWrite.opsCompletedAsync > m1OpWrite.opsCompletedAsync); - assert(m2OpWrite.bytesSentControl === m1OpWrite.bytesSentControl); - assert(m2OpWrite.bytesSentData === 0); - assert(m2OpWrite.bytesReceived === m1OpWrite.bytesReceived); -}); - -Deno.test( - { permissions: { write: true } }, - function metricsUpdatedIfNoResponseSync() { - const filename = Deno.makeTempDirSync() + "/test.txt"; - - const data = new Uint8Array([41, 42, 43]); - Deno.writeFileSync(filename, data, { mode: 0o666 }); - - const metrics = Deno.metrics(); - assert(metrics.opsDispatched === metrics.opsCompleted); - assert(metrics.opsDispatchedSync === metrics.opsCompletedSync); - }, -); - -Deno.test( - { permissions: { write: true } }, - async function metricsUpdatedIfNoResponseAsync() { - const filename = Deno.makeTempDirSync() + "/test.txt"; - - const data = new Uint8Array([41, 42, 43]); - await Deno.writeFile(filename, data, { mode: 0o666 }); - - const metrics = Deno.metrics(); - assert(metrics.opsDispatched === metrics.opsCompleted); - assert(metrics.opsDispatchedSync === metrics.opsCompletedSync); - assert(metrics.opsDispatchedAsync === metrics.opsCompletedAsync); - }, -); - -// Test that ops from extensions have metrics (via OpMiddleware) -Deno.test.ignore(function metricsForOpCrates() { - const _ = new URL("https://deno.land"); - - const m1 = Deno.metrics().ops["op_url_parse"]; - assert(m1.opsDispatched > 0); - assert(m1.opsCompleted > 0); -}); - -// Test that op_names == Objects.keys(Deno[Deno.internal].core.ops) -// since building the per-op metrics depends on op_names being complete -Deno.test(function opNamesMatch() { - // @ts-ignore: Deno[Deno.internal].core allowed - const ops = Object.keys(Deno[Deno.internal].core.ops); - // @ts-ignore: Deno[Deno.internal].core allowed - ops.concat(Object.keys(Deno[Deno.internal].core.asyncOps)); - - assertEquals( - // @ts-ignore: Deno[Deno.internal].core allowed - Deno[Deno.internal].core.opNames().sort(), - ops.sort().filter((name) => name !== "asyncOpsInfo"), - ); -}); diff --git a/cli/tests/unit/timers_test.ts b/cli/tests/unit/timers_test.ts index 6dedb04ed..f9beddabd 100644 --- a/cli/tests/unit/timers_test.ts +++ b/cli/tests/unit/timers_test.ts @@ -412,19 +412,6 @@ Deno.test(function clearTimeoutAndClearIntervalNotBeEquals() { assertNotEquals(clearTimeout, clearInterval); }); -Deno.test(async function timerMaxCpuBug() { - // There was a bug where clearing a timeout would cause Deno to use 100% CPU. - clearTimeout(setTimeout(() => {}, 1000)); - // We can check this by counting how many ops have triggered in the interim. - // Certainly less than 10 ops should have been dispatched in next 100 ms. - const { ops: pre } = Deno.metrics(); - await delay(100); - const { ops: post } = Deno.metrics(); - const before = pre.op_sleep.opsDispatched; - const after = post.op_sleep.opsDispatched; - assert(after - before < 10); -}); - Deno.test(async function timerOrdering() { const array: number[] = []; const donePromise = deferred(); diff --git a/cli/tsc/mod.rs b/cli/tsc/mod.rs index 524719755..22c29a228 100644 --- a/cli/tsc/mod.rs +++ b/cli/tsc/mod.rs @@ -948,7 +948,7 @@ mod tests { .context("Unable to get CWD") .unwrap(), ); - let mut op_state = OpState::new(1, None); + let mut op_state = OpState::new(None); op_state.put(state); op_state } diff --git a/cli/worker.rs b/cli/worker.rs index 5022b39f1..173d944c0 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -86,6 +86,7 @@ pub struct CliMainWorkerOptions { pub argv: Vec<String>, pub log_level: WorkerLogLevel, pub coverage_dir: Option<String>, + pub enable_op_summary_metrics: bool, pub enable_testing_features: bool, pub has_node_modules_dir: bool, pub hmr: bool, @@ -546,6 +547,7 @@ impl CliMainWorkerFactory { .map(|p| p.get()) .unwrap_or(1), log_level: shared.options.log_level, + enable_op_summary_metrics: shared.options.enable_op_summary_metrics, enable_testing_features: shared.options.enable_testing_features, locale: deno_core::v8::icu::get_language_tag(), location: shared.options.location.clone(), @@ -732,6 +734,7 @@ fn create_web_worker_callback( .map(|p| p.get()) .unwrap_or(1), log_level: shared.options.log_level, + enable_op_summary_metrics: shared.options.enable_op_summary_metrics, enable_testing_features: shared.options.enable_testing_features, locale: deno_core::v8::icu::get_language_tag(), location: Some(args.main_module.clone()), |