Report turn event dispatch timing

This commit is contained in:
bromano
2026-06-05 12:25:42 -07:00
parent 8c11e7d060
commit 348a54d86c
8 changed files with 79 additions and 2 deletions
@@ -410,6 +410,9 @@ fn sample_turn_profile() -> TurnProfile {
sampling_retry_delay_ms: 50,
request_user_input_count: 1,
request_user_input_wait_ms: 250,
event_dispatch_count: 3,
event_dispatch_duration_ms: 75,
final_rollout_flush_duration_ms: 25,
}
}
@@ -3339,6 +3342,9 @@ fn turn_event_serializes_expected_shape() {
sampling_retry_delay_ms: 50,
request_user_input_count: 1,
request_user_input_wait_ms: 250,
event_dispatch_count: 3,
event_dispatch_duration_ms: 75,
final_rollout_flush_duration_ms: 25,
duration_ms: Some(1234),
started_at: Some(455),
completed_at: Some(456),
@@ -3415,6 +3421,9 @@ fn turn_event_serializes_expected_shape() {
"sampling_retry_delay_ms": 50,
"request_user_input_count": 1,
"request_user_input_wait_ms": 250,
"event_dispatch_count": 3,
"event_dispatch_duration_ms": 75,
"final_rollout_flush_duration_ms": 25,
"duration_ms": 1234,
"started_at": 455,
"completed_at": 456
@@ -3732,6 +3741,15 @@ async fn turn_lifecycle_emits_turn_event() {
assert_eq!(payload["event_params"]["started_at"], json!(455));
assert_eq!(payload["event_params"]["completed_at"], json!(456));
assert_eq!(payload["event_params"]["duration_ms"], json!(1234));
assert_eq!(payload["event_params"]["event_dispatch_count"], json!(3));
assert_eq!(
payload["event_params"]["event_dispatch_duration_ms"],
json!(75)
);
assert_eq!(
payload["event_params"]["final_rollout_flush_duration_ms"],
json!(25)
);
assert_eq!(payload["event_params"]["input_tokens"], json!(123));
assert_eq!(payload["event_params"]["cached_input_tokens"], json!(45));
assert_eq!(payload["event_params"]["output_tokens"], json!(140));
+3
View File
@@ -827,6 +827,9 @@ pub(crate) struct CodexTurnEventParams {
pub(crate) sampling_retry_delay_ms: u64,
pub(crate) request_user_input_count: u32,
pub(crate) request_user_input_wait_ms: u64,
pub(crate) event_dispatch_count: u32,
pub(crate) event_dispatch_duration_ms: u64,
pub(crate) final_rollout_flush_duration_ms: u64,
pub(crate) duration_ms: Option<u64>,
pub(crate) started_at: Option<u64>,
pub(crate) completed_at: Option<u64>,
+3
View File
@@ -116,6 +116,9 @@ pub struct TurnProfile {
pub sampling_retry_delay_ms: u64,
pub request_user_input_count: u32,
pub request_user_input_wait_ms: u64,
pub event_dispatch_count: u32,
pub event_dispatch_duration_ms: u64,
pub final_rollout_flush_duration_ms: u64,
}
#[derive(Clone)]
+6
View File
@@ -2436,6 +2436,9 @@ fn codex_turn_event_params(
sampling_retry_delay_ms,
request_user_input_count,
request_user_input_wait_ms,
event_dispatch_count,
event_dispatch_duration_ms,
final_rollout_flush_duration_ms,
} = profile;
let token_usage = turn_state.token_usage.clone();
let codex_error = turn_state.codex_error.as_ref();
@@ -2509,6 +2512,9 @@ fn codex_turn_event_params(
sampling_retry_delay_ms,
request_user_input_count,
request_user_input_wait_ms,
event_dispatch_count,
event_dispatch_duration_ms,
final_rollout_flush_duration_ms,
duration_ms: completed.duration_ms,
started_at,
completed_at: Some(completed.completed_at),
+4
View File
@@ -1658,6 +1658,7 @@ impl Session {
/// Persist the event to rollout and send it to clients.
pub(crate) async fn send_event(&self, turn_context: &TurnContext, msg: EventMsg) {
let dispatch_started_at = Instant::now();
let legacy_source = msg.clone();
self.services
.rollout_thread_trace
@@ -1685,6 +1686,9 @@ impl Session {
};
self.send_event_raw(legacy_event).await;
}
turn_context
.turn_timing_state
.record_event_dispatch(dispatch_started_at.elapsed());
}
/// Forwards terminal turn events from spawned MultiAgentV2 children to their direct parent.
+12 -2
View File
@@ -407,7 +407,12 @@ impl Session {
)
.await;
let sess = session_ctx.clone_session();
if let Err(err) = sess.flush_rollout().await {
let flush_started_at = Instant::now();
let flush_result = sess.flush_rollout().await;
ctx_for_finish
.turn_timing_state
.record_final_rollout_flush(flush_started_at.elapsed());
if let Err(err) = flush_result {
warn!("failed to flush rollout before completing turn: {err}");
sess.send_event(
ctx_for_finish.as_ref(),
@@ -865,7 +870,12 @@ impl Session {
.await;
// Ensure the marker is durably visible before emitting TurnAborted: some clients
// synchronously re-read the rollout on receipt of the abort event.
if let Err(err) = self.flush_rollout().await {
let flush_started_at = Instant::now();
let flush_result = self.flush_rollout().await;
task.turn_context
.turn_timing_state
.record_final_rollout_flush(flush_started_at.elapsed());
if let Err(err) = flush_result {
warn!("failed to flush interrupted-turn marker before emitting TurnAborted: {err}");
}
}
+27
View File
@@ -69,6 +69,9 @@ struct TurnProfileState {
sampling_retry_delay: Duration,
request_user_input_count: u32,
request_user_input_wait: Duration,
event_dispatch_count: u32,
event_dispatch_duration: Duration,
final_rollout_flush_duration: Duration,
completed_profile: Option<TurnProfile>,
}
@@ -139,6 +142,14 @@ impl TurnTimingState {
.record_request_user_input_wait(duration);
}
pub(crate) fn record_event_dispatch(&self, duration: Duration) {
self.profile_state().record_event_dispatch(duration);
}
pub(crate) fn record_final_rollout_flush(&self, duration: Duration) {
self.profile_state().record_final_rollout_flush(duration);
}
pub(crate) fn begin_tool_blocking(self: &Arc<Self>) -> TurnProfileTimingGuard {
let active = self.profile_state().begin_tool_blocking(Instant::now());
TurnProfileTimingGuard {
@@ -239,6 +250,19 @@ impl TurnProfileState {
}
}
fn record_event_dispatch(&mut self, duration: Duration) {
if self.completed_profile.is_none() && self.started_at.is_some() {
self.event_dispatch_count = self.event_dispatch_count.saturating_add(1);
self.event_dispatch_duration = self.event_dispatch_duration.saturating_add(duration);
}
}
fn record_final_rollout_flush(&mut self, duration: Duration) {
if self.completed_profile.is_none() && self.started_at.is_some() {
self.final_rollout_flush_duration = duration;
}
}
fn begin_tool_blocking(&mut self, now: Instant) -> bool {
if self.completed_profile.is_some()
|| self.started_at.is_none()
@@ -296,6 +320,9 @@ impl TurnProfileState {
sampling_retry_delay_ms: duration_to_u64_ms(self.sampling_retry_delay),
request_user_input_count: self.request_user_input_count,
request_user_input_wait_ms: duration_to_u64_ms(self.request_user_input_wait),
event_dispatch_count: self.event_dispatch_count,
event_dispatch_duration_ms: duration_to_u64_ms(self.event_dispatch_duration),
final_rollout_flush_duration_ms: duration_to_u64_ms(self.final_rollout_flush_duration),
};
let total_ms = self
.started_at
+6
View File
@@ -169,6 +169,9 @@ fn turn_profile_breaks_down_sampling_blocking_and_retry_overhead() {
);
state.record_sampling_retry(Duration::from_millis(50));
state.record_request_user_input_wait(Duration::from_millis(250));
state.record_event_dispatch(Duration::from_millis(75));
state.record_event_dispatch(Duration::from_millis(25));
state.record_final_rollout_flush(Duration::from_millis(40));
let _ = state.begin_sampling(started_at + Duration::from_millis(1_000));
state.end_phase(
started_at + Duration::from_millis(1_200),
@@ -188,6 +191,9 @@ fn turn_profile_breaks_down_sampling_blocking_and_retry_overhead() {
sampling_retry_delay_ms: 50,
request_user_input_count: 1,
request_user_input_wait_ms: 250,
event_dispatch_count: 2,
event_dispatch_duration_ms: 100,
final_rollout_flush_duration_ms: 40,
}
);
}