1515use std:: {
1616 error:: Error ,
1717 sync:: {
18- atomic:: { AtomicU64 , Ordering } ,
19- Arc ,
18+ Arc , atomic:: { AtomicU64 , Ordering }
2019 } ,
2120 thread,
22- time:: Duration ,
21+ time:: { Duration , Instant } ,
2322} ;
2423
2524use dashmap:: { mapref:: one:: MappedRef , DashMap } ;
2625use downcast_rs:: { impl_downcast, Downcast } ;
2726use livekit:: webrtc:: {
2827 native:: apm:: AudioProcessingModule , native:: audio_resampler:: AudioResampler , prelude:: * ,
2928} ;
30- use metrics_logger:: { LogMode , MetricsLogger , metrics} ;
29+ use metrics_logger:: { LogMode , MetricsLogger , metrics:: { self , histogram } } ;
3130use parking_lot:: { deadlock, Mutex } ;
3231use tokio:: { sync:: oneshot, task:: JoinHandle } ;
3332
@@ -173,13 +172,19 @@ impl FfiServer {
173172 }
174173
175174 pub fn send_event ( & self , message : proto:: ffi_event:: Message ) -> FfiResult < ( ) > {
175+
176+ let t0 = Instant :: now ( ) ;
177+
176178 let cb = self
177179 . config
178180 . lock ( )
179181 . as_ref ( )
180182 . map_or_else ( || Err ( FfiError :: NotConfigured ) , |c| Ok ( c. callback_fn . clone ( ) ) ) ?;
181183
182184 cb ( proto:: FfiEvent { message : Some ( message) } ) ;
185+
186+ let delta = t0. elapsed ( ) ;
187+ histogram ! ( "send_event" ) . record ( delta. as_millis ( ) as f64 ) ;
183188 Ok ( ( ) )
184189 }
185190
@@ -197,7 +202,12 @@ impl FfiServer {
197202 where
198203 T : FfiHandle ,
199204 {
205+ let t0 = Instant :: now ( ) ;
206+
200207 self . ffi_handles . insert ( id, Box :: new ( handle) ) ;
208+
209+ let delta = t0. elapsed ( ) ;
210+ histogram ! ( "store_handle" ) . record ( delta. as_millis ( ) as f64 ) ;
201211 }
202212
203213 pub fn retrieve_handle < T > (
@@ -211,6 +221,8 @@ impl FfiServer {
211221 return Err ( FfiError :: InvalidRequest ( "handle is invalid" . into ( ) ) ) ;
212222 }
213223
224+ let t0 = Instant :: now ( ) ;
225+
214226 let handle =
215227 self . ffi_handles . get ( & id) . ok_or ( FfiError :: InvalidRequest ( "handle not found" . into ( ) ) ) ?;
216228
@@ -220,6 +232,9 @@ impl FfiServer {
220232 return Err ( FfiError :: InvalidRequest ( msg. into ( ) ) ) ;
221233 }
222234
235+ let delta = t0. elapsed ( ) ;
236+ histogram ! ( "retrieve_handle" ) . record ( delta. as_millis ( ) as f64 ) ;
237+
223238 let handle = handle. map ( |v| v. downcast_ref :: < T > ( ) . unwrap ( ) ) ;
224239 Ok ( handle)
225240 }
@@ -246,8 +261,15 @@ impl FfiServer {
246261 }
247262
248263 pub fn drop_handle ( & self , id : FfiHandleId ) -> bool {
264+
265+ let t0 = Instant :: now ( ) ;
266+
249267 let existed = self . ffi_handles . remove ( & id) . is_some ( ) ;
250268 self . handle_dropped_txs . remove ( & id) ;
269+
270+ let delta = t0. elapsed ( ) ;
271+ histogram ! ( "drop_handle" ) . record ( delta. as_millis ( ) as f64 ) ;
272+
251273 return existed;
252274 }
253275
0 commit comments