Collect simple stats about engine usage (#5823)

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-03-18 16:19:24 +13:00
committed by GitHub
parent 0688ce7fe9
commit 988a068d6d
8 changed files with 67 additions and 7 deletions

View File

@ -18,7 +18,7 @@ use tokio::sync::{mpsc, oneshot, RwLock};
use tokio_tungstenite::tungstenite::Message as WsMsg; use tokio_tungstenite::tungstenite::Message as WsMsg;
use uuid::Uuid; use uuid::Uuid;
use super::ExecutionKind; use super::{EngineStats, ExecutionKind};
use crate::{ use crate::{
engine::EngineManager, engine::EngineManager,
errors::{KclError, KclErrorDetails}, errors::{KclError, KclErrorDetails},
@ -52,6 +52,7 @@ pub struct EngineConnection {
session_data: Arc<RwLock<Option<ModelingSessionData>>>, session_data: Arc<RwLock<Option<ModelingSessionData>>>,
execution_kind: Arc<RwLock<ExecutionKind>>, execution_kind: Arc<RwLock<ExecutionKind>>,
stats: EngineStats,
} }
pub struct TcpRead { pub struct TcpRead {
@ -344,6 +345,7 @@ impl EngineConnection {
default_planes: Default::default(), default_planes: Default::default(),
session_data, session_data,
execution_kind: Default::default(), execution_kind: Default::default(),
stats: Default::default(),
}) })
} }
} }
@ -378,6 +380,10 @@ impl EngineManager for EngineConnection {
original original
} }
fn stats(&self) -> &EngineStats {
&self.stats
}
fn get_default_planes(&self) -> Arc<RwLock<Option<DefaultPlanes>>> { fn get_default_planes(&self) -> Arc<RwLock<Option<DefaultPlanes>>> {
self.default_planes.clone() self.default_planes.clone()
} }

View File

@ -16,7 +16,7 @@ use kittycad_modeling_cmds::{self as kcmc};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use uuid::Uuid; use uuid::Uuid;
use super::ExecutionKind; use super::{EngineStats, ExecutionKind};
use crate::{ use crate::{
errors::KclError, errors::KclError,
exec::DefaultPlanes, exec::DefaultPlanes,
@ -32,6 +32,7 @@ pub struct EngineConnection {
execution_kind: Arc<RwLock<ExecutionKind>>, execution_kind: Arc<RwLock<ExecutionKind>>,
/// The default planes for the scene. /// The default planes for the scene.
default_planes: Arc<RwLock<Option<DefaultPlanes>>>, default_planes: Arc<RwLock<Option<DefaultPlanes>>>,
stats: EngineStats,
} }
impl EngineConnection { impl EngineConnection {
@ -42,6 +43,7 @@ impl EngineConnection {
artifact_commands: Arc::new(RwLock::new(Vec::new())), artifact_commands: Arc::new(RwLock::new(Vec::new())),
execution_kind: Default::default(), execution_kind: Default::default(),
default_planes: Default::default(), default_planes: Default::default(),
stats: Default::default(),
}) })
} }
} }
@ -60,6 +62,10 @@ impl crate::engine::EngineManager for EngineConnection {
Arc::new(RwLock::new(IndexMap::new())) Arc::new(RwLock::new(IndexMap::new()))
} }
fn stats(&self) -> &EngineStats {
&self.stats
}
fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> { fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> {
self.artifact_commands.clone() self.artifact_commands.clone()
} }

View File

@ -11,7 +11,7 @@ use uuid::Uuid;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use crate::{ use crate::{
engine::ExecutionKind, engine::{EngineStats, ExecutionKind},
errors::{KclError, KclErrorDetails}, errors::{KclError, KclErrorDetails},
execution::{ArtifactCommand, DefaultPlanes, IdGenerator}, execution::{ArtifactCommand, DefaultPlanes, IdGenerator},
SourceRange, SourceRange,
@ -45,6 +45,7 @@ pub struct EngineConnection {
execution_kind: Arc<RwLock<ExecutionKind>>, execution_kind: Arc<RwLock<ExecutionKind>>,
/// The default planes for the scene. /// The default planes for the scene.
default_planes: Arc<RwLock<Option<DefaultPlanes>>>, default_planes: Arc<RwLock<Option<DefaultPlanes>>>,
stats: EngineStats,
} }
// Safety: WebAssembly will only ever run in a single-threaded context. // Safety: WebAssembly will only ever run in a single-threaded context.
@ -62,6 +63,7 @@ impl EngineConnection {
artifact_commands: Arc::new(RwLock::new(Vec::new())), artifact_commands: Arc::new(RwLock::new(Vec::new())),
execution_kind: Default::default(), execution_kind: Default::default(),
default_planes: Default::default(), default_planes: Default::default(),
stats: Default::default(),
}) })
} }
@ -141,6 +143,10 @@ impl crate::engine::EngineManager for EngineConnection {
self.responses.clone() self.responses.clone()
} }
fn stats(&self) -> &EngineStats {
&self.stats
}
fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> { fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> {
self.artifact_commands.clone() self.artifact_commands.clone()
} }

View File

@ -8,7 +8,13 @@ pub mod conn_mock;
#[cfg(feature = "engine")] #[cfg(feature = "engine")]
pub mod conn_wasm; pub mod conn_wasm;
use std::{collections::HashMap, sync::Arc}; use std::{
collections::HashMap,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
use indexmap::IndexMap; use indexmap::IndexMap;
use kcmc::{ use kcmc::{
@ -58,6 +64,21 @@ impl ExecutionKind {
} }
} }
#[derive(Default, Debug)]
pub struct EngineStats {
pub commands_batched: AtomicUsize,
pub batches_sent: AtomicUsize,
}
impl Clone for EngineStats {
fn clone(&self) -> Self {
Self {
commands_batched: AtomicUsize::new(self.commands_batched.load(Ordering::Relaxed)),
batches_sent: AtomicUsize::new(self.batches_sent.load(Ordering::Relaxed)),
}
}
}
#[async_trait::async_trait] #[async_trait::async_trait]
pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static { pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
/// Get the batch of commands to be sent to the engine. /// Get the batch of commands to be sent to the engine.
@ -97,6 +118,8 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
/// Get the default planes. /// Get the default planes.
fn get_default_planes(&self) -> Arc<RwLock<Option<DefaultPlanes>>>; fn get_default_planes(&self) -> Arc<RwLock<Option<DefaultPlanes>>>;
fn stats(&self) -> &EngineStats;
/// Get the default planes, creating them if they don't exist. /// Get the default planes, creating them if they don't exist.
async fn default_planes( async fn default_planes(
&self, &self,
@ -254,6 +277,7 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
// Add cmd to the batch. // Add cmd to the batch.
self.batch().write().await.push((req, source_range)); self.batch().write().await.push((req, source_range));
self.stats().commands_batched.fetch_add(1, Ordering::Relaxed);
Ok(()) Ok(())
} }
@ -277,6 +301,9 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
for cmd in cmds { for cmd in cmds {
extended_cmds.push((WebSocketRequest::ModelingCmdReq(cmd.clone()), source_range)); extended_cmds.push((WebSocketRequest::ModelingCmdReq(cmd.clone()), source_range));
} }
self.stats()
.commands_batched
.fetch_add(extended_cmds.len(), Ordering::Relaxed);
self.batch().write().await.extend(extended_cmds); self.batch().write().await.extend(extended_cmds);
Ok(()) Ok(())
@ -303,6 +330,7 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
// Add cmd to the batch end. // Add cmd to the batch end.
self.batch_end().write().await.insert(id, (req, source_range)); self.batch_end().write().await.insert(id, (req, source_range));
self.stats().commands_batched.fetch_add(1, Ordering::Relaxed);
Ok(()) Ok(())
} }
@ -405,6 +433,7 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static {
if batch_end { if batch_end {
self.batch_end().write().await.clear(); self.batch_end().write().await.clear();
} }
self.stats().batches_sent.fetch_add(1, Ordering::Relaxed);
// We pop off the responses to cleanup our mappings. // We pop off the responses to cleanup our mappings.
match final_req { match final_req {

View File

@ -750,6 +750,7 @@ impl ExecutorContext {
"Post interpretation KCL memory stats: {:#?}", "Post interpretation KCL memory stats: {:#?}",
exec_state.stack().memory.stats exec_state.stack().memory.stats
)); ));
crate::log::log(format!("Engine stats: {:?}", self.engine.stats()));
if !self.is_mock() { if !self.is_mock() {
let mut mem = exec_state.stack().deep_clone(); let mut mem = exec_state.stack().deep_clone();

View File

@ -81,7 +81,7 @@ mod walk;
mod wasm; mod wasm;
pub use coredump::CoreDump; pub use coredump::CoreDump;
pub use engine::{EngineManager, ExecutionKind}; pub use engine::{EngineManager, EngineStats, ExecutionKind};
pub use errors::{ pub use errors::{
CompilationError, ConnectionError, ExecError, KclError, KclErrorWithOutputs, Report, ReportWithOutputs, CompilationError, ConnectionError, ExecError, KclError, KclErrorWithOutputs, Report, ReportWithOutputs,
}; };

View File

@ -11,7 +11,13 @@ use kcl_lib::{ExecState, ExecutorContext, ExecutorSettings, Program};
async fn main() { async fn main() {
let mut args = env::args(); let mut args = env::args();
args.next(); args.next();
let filename = args.next().unwrap_or_else(|| "main.kcl".to_owned()); let mut filename = args.next().unwrap_or_else(|| "main.kcl".to_owned());
if !filename.ends_with(".kcl") {
if !filename.ends_with('/') && !filename.ends_with('\\') {
filename += "/";
}
filename += "main.kcl";
}
let mut f = File::open(&filename).unwrap(); let mut f = File::open(&filename).unwrap();
let mut text = String::new(); let mut text = String::new();

View File

@ -4,7 +4,7 @@ use anyhow::Result;
use indexmap::IndexMap; use indexmap::IndexMap;
use kcl_lib::{ use kcl_lib::{
exec::{ArtifactCommand, DefaultPlanes, IdGenerator}, exec::{ArtifactCommand, DefaultPlanes, IdGenerator},
ExecutionKind, KclError, EngineStats, ExecutionKind, KclError,
}; };
use kittycad_modeling_cmds::{ use kittycad_modeling_cmds::{
self as kcmc, self as kcmc,
@ -26,6 +26,7 @@ pub struct EngineConnection {
execution_kind: Arc<RwLock<ExecutionKind>>, execution_kind: Arc<RwLock<ExecutionKind>>,
/// The default planes for the scene. /// The default planes for the scene.
default_planes: Arc<RwLock<Option<DefaultPlanes>>>, default_planes: Arc<RwLock<Option<DefaultPlanes>>>,
stats: EngineStats,
} }
impl EngineConnection { impl EngineConnection {
@ -38,6 +39,7 @@ impl EngineConnection {
core_test: result, core_test: result,
execution_kind: Default::default(), execution_kind: Default::default(),
default_planes: Default::default(), default_planes: Default::default(),
stats: Default::default(),
}) })
} }
@ -369,6 +371,10 @@ impl kcl_lib::EngineManager for EngineConnection {
Arc::new(RwLock::new(IndexMap::new())) Arc::new(RwLock::new(IndexMap::new()))
} }
fn stats(&self) -> &EngineStats {
&self.stats
}
fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> { fn artifact_commands(&self) -> Arc<RwLock<Vec<ArtifactCommand>>> {
Arc::new(RwLock::new(Vec::new())) Arc::new(RwLock::new(Vec::new()))
} }