chore(diagnostics): log slow UI handlers
- backend events and conversation rebuilds over 30 ms go to diagnostics.log Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -5,6 +5,7 @@ use std::sync::OnceLock;
5
5
6
6
pub const DIAGNOSTICS_FILE: &str = "diagnostics.log";
7
7
const MAX_BYTES: u64 = 1024 * 1024;
8
const SLOW_UI_HANDLER: std::time::Duration = std::time::Duration::from_millis(30);
8
9
9
10
static PATH: OnceLock<PathBuf> = OnceLock::new();
10
11
@@ -64,6 +65,12 @@ pub fn directory() -> Option<PathBuf> {
64
65
PATH.get().and_then(|path| path.parent()).map(PathBuf::from)
65
66
}
66
67
68
pub fn record_if_slow(label: &str, elapsed: std::time::Duration) {
69
if elapsed >= SLOW_UI_HANDLER {
70
record(format!("slow ui handler: {label} took {} ms", elapsed.as_millis()));
71
}
72
}
73
67
74
pub fn record(message: impl AsRef<str>) {
68
75
let message = message.as_ref();
69
76
eprintln!("{message}");
@@ -5,6 +5,7 @@ use std::sync::OnceLock;5
5
6
pub const DIAGNOSTICS_FILE: &str = "diagnostics.log";6
pub const DIAGNOSTICS_FILE: &str = "diagnostics.log";7
const MAX_BYTES: u64 = 1024 * 1024;7
const MAX_BYTES: u64 = 1024 * 1024;8
const SLOW_UI_HANDLER: std::time::Duration = std::time::Duration::from_millis(30);8
9
9
static PATH: OnceLock<PathBuf> = OnceLock::new();10
static PATH: OnceLock<PathBuf> = OnceLock::new();10
11
@@ -64,6 +65,12 @@ pub fn directory() -> Option<PathBuf> {64
PATH.get().and_then(|path| path.parent()).map(PathBuf::from)65
PATH.get().and_then(|path| path.parent()).map(PathBuf::from)65
}66
}66
67
68
pub fn record_if_slow(label: &str, elapsed: std::time::Duration) {69
if elapsed >= SLOW_UI_HANDLER {70
record(format!("slow ui handler: {label} took {} ms", elapsed.as_millis()));71
}72
}73
67
pub fn record(message: impl AsRef<str>) {74
pub fn record(message: impl AsRef<str>) {68
let message = message.as_ref();75
let message = message.as_ref();69
eprintln!("{message}");76
eprintln!("{message}");@@ -72,6 +72,30 @@ pub enum BackendEvent {
72
72
LiveRetry(Arc<Notify>),
73
73
}
74
74
75
impl BackendEvent {
76
pub fn kind(&self) -> String {
77
match self {
78
BackendEvent::Connection(_) => "Connection".into(),
79
BackendEvent::Engine(_) => "Engine".into(),
80
BackendEvent::Calls(_) => "Calls".into(),
81
BackendEvent::Calendar(_) => "Calendar".into(),
82
BackendEvent::CalendarNudge(_) => "CalendarNudge".into(),
83
BackendEvent::Ring(_) => "Ring".into(),
84
BackendEvent::ThreadChanged(_) => "ThreadChanged".into(),
85
BackendEvent::Core(event) => {
86
let debug = format!("{event:?}");
87
let name = debug.split(['{', '(', ' ']).next().unwrap_or_default();
88
format!("Core::{name}")
89
}
90
BackendEvent::Live(_) => "Live".into(),
91
BackendEvent::Typing(_) => "Typing".into(),
92
BackendEvent::Favorites(_) => "Favorites".into(),
93
BackendEvent::Synced(_) => "Synced".into(),
94
BackendEvent::LiveRetry(_) => "LiveRetry".into(),
95
}
96
}
97
}
98
75
99
type EventSender = mpsc::UnboundedSender<BackendEvent>;
76
100
77
101
pub fn classify_session_error(error: &session::Error) -> ConnectionState {
@@ -72,6 +72,30 @@ pub enum BackendEvent {72
LiveRetry(Arc<Notify>),72
LiveRetry(Arc<Notify>),73
}73
}74
74
75
impl BackendEvent {76
pub fn kind(&self) -> String {77
match self {78
BackendEvent::Connection(_) => "Connection".into(),79
BackendEvent::Engine(_) => "Engine".into(),80
BackendEvent::Calls(_) => "Calls".into(),81
BackendEvent::Calendar(_) => "Calendar".into(),82
BackendEvent::CalendarNudge(_) => "CalendarNudge".into(),83
BackendEvent::Ring(_) => "Ring".into(),84
BackendEvent::ThreadChanged(_) => "ThreadChanged".into(),85
BackendEvent::Core(event) => {86
let debug = format!("{event:?}");87
let name = debug.split(['{', '(', ' ']).next().unwrap_or_default();88
format!("Core::{name}")89
}90
BackendEvent::Live(_) => "Live".into(),91
BackendEvent::Typing(_) => "Typing".into(),92
BackendEvent::Favorites(_) => "Favorites".into(),93
BackendEvent::Synced(_) => "Synced".into(),94
BackendEvent::LiveRetry(_) => "LiveRetry".into(),95
}96
}97
}98
75
type EventSender = mpsc::UnboundedSender<BackendEvent>;99
type EventSender = mpsc::UnboundedSender<BackendEvent>;76
100
77
pub fn classify_session_error(error: &session::Error) -> ConnectionState {101
pub fn classify_session_error(error: &session::Error) -> ConnectionState {@@ -248,7 +248,10 @@ fn main() {
248
248
let state = state.clone();
249
249
cx.spawn(async move |cx| {
250
250
while let Some(event) = receiver.recv().await {
251
let kind = event.kind();
252
let started = std::time::Instant::now();
251
253
state.update(cx, |state, cx| state.apply(event, cx));
254
app_base::diagnostics::record_if_slow(&kind, started.elapsed());
252
255
}
253
256
})
254
257
.detach();
@@ -248,7 +248,10 @@ fn main() {248
let state = state.clone();248
let state = state.clone();249
cx.spawn(async move |cx| {249
cx.spawn(async move |cx| {250
while let Some(event) = receiver.recv().await {250
while let Some(event) = receiver.recv().await {251
let kind = event.kind();252
let started = std::time::Instant::now();251
state.update(cx, |state, cx| state.apply(event, cx));253
state.update(cx, |state, cx| state.apply(event, cx));254
app_base::diagnostics::record_if_slow(&kind, started.elapsed());252
}255
}253
})256
})254
.detach();257
.detach();@@ -1662,6 +1662,12 @@ impl ConversationView {
1662
1662
}
1663
1663
1664
1664
fn rebuild(&mut self, reset: bool, cx: &mut Context<Self>) {
1665
let started = std::time::Instant::now();
1666
self.rebuild_rows(reset, cx);
1667
app_base::diagnostics::record_if_slow("conversation rebuild", started.elapsed());
1668
}
1669
1670
fn rebuild_rows(&mut self, reset: bool, cx: &mut Context<Self>) {
1665
1671
let Some(current) = self.current.as_ref() else {
1666
1672
let mut rows: Vec<Row> = self
1667
1673
.pending
@@ -1662,6 +1662,12 @@ impl ConversationView {1662
}1662
}1663
1663
1664
fn rebuild(&mut self, reset: bool, cx: &mut Context<Self>) {1664
fn rebuild(&mut self, reset: bool, cx: &mut Context<Self>) {1665
let started = std::time::Instant::now();1666
self.rebuild_rows(reset, cx);1667
app_base::diagnostics::record_if_slow("conversation rebuild", started.elapsed());1668
}1669
1670
fn rebuild_rows(&mut self, reset: bool, cx: &mut Context<Self>) {1665
let Some(current) = self.current.as_ref() else {1671
let Some(current) = self.current.as_ref() else {1666
let mut rows: Vec<Row> = self1672
let mut rows: Vec<Row> = self1667
.pending1673
.pending