reinhardt-core 0.1.1

Core components for Reinhardt framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Signal profiler for performance analysis of signal systems
//!
//! Provides detailed performance metrics, bottleneck detection,
//! and optimization recommendations for signal-based systems.
//!
//! # Examples
//!
//! ```
//! use reinhardt_core::signals::{Signal, SignalName};
//! use reinhardt_core::signals::profiler::SignalProfiler;
//!
//! # tokio_test::block_on(async {
//! let signal = Signal::<String>::new(SignalName::custom("user_created"));
//! let profiler = SignalProfiler::new();
//!
//! // Attach profiler to signal
//! signal.add_middleware(profiler.clone());
//!
//! // Connect receivers
//! signal.connect(|_| async {
//!     Ok(())
//! });
//!
//! // Send signal
//! signal.send("user123".to_string()).await.unwrap();
//!
//! // Get performance report
//! let report = profiler.generate_report();
//! assert!(report.contains("Performance Profile"));
//! # });
//! ```

use super::error::SignalError;
use super::middleware::SignalMiddleware;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};

/// Performance profile for a single receiver
#[derive(Debug, Clone)]
pub struct ReceiverProfile {
	/// Receiver dispatch UID
	pub dispatch_uid: String,
	/// Number of times this receiver was called
	pub call_count: usize,
	/// Total execution time
	pub total_duration: Duration,
	/// Minimum execution time
	pub min_duration: Duration,
	/// Maximum execution time
	pub max_duration: Duration,
	/// Average execution time
	pub avg_duration: Duration,
	/// Number of failed executions
	pub failure_count: usize,
	/// Last execution timestamp
	pub last_execution: Option<SystemTime>,
}

impl ReceiverProfile {
	fn new(dispatch_uid: String) -> Self {
		Self {
			dispatch_uid,
			call_count: 0,
			total_duration: Duration::ZERO,
			min_duration: Duration::MAX,
			max_duration: Duration::ZERO,
			avg_duration: Duration::ZERO,
			failure_count: 0,
			last_execution: None,
		}
	}

	fn record_execution(&mut self, duration: Duration, success: bool) {
		self.call_count += 1;
		self.total_duration += duration;
		self.min_duration = self.min_duration.min(duration);
		self.max_duration = self.max_duration.max(duration);
		self.avg_duration =
			Duration::from_nanos((self.total_duration.as_nanos() / self.call_count as u128) as u64);
		self.last_execution = Some(SystemTime::now());

		if !success {
			self.failure_count += 1;
		}
	}

	/// Get success rate as a percentage (0.0 to 100.0)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::{Signal, SignalName};
	/// use reinhardt_core::signals::profiler::SignalProfiler;
	///
	/// # tokio_test::block_on(async {
	/// let profiler = SignalProfiler::<String>::new();
	/// let signal = Signal::<String>::new(SignalName::custom("test"));
	/// signal.add_middleware(profiler.clone());
	///
	/// signal.connect_with_options(
	///     |_| async { Ok(()) },
	///     None,
	///     Some("test_receiver".to_string()),
	///     0
	/// );
	/// signal.send("test".to_string()).await.unwrap();
	///
	/// let profile = profiler.get_receiver_profile("test_receiver").unwrap();
	/// assert_eq!(profile.success_rate(), 100.0);
	/// # });
	/// ```
	pub fn success_rate(&self) -> f64 {
		if self.call_count == 0 {
			return 100.0;
		}
		let successful = self.call_count - self.failure_count;
		(successful as f64 / self.call_count as f64) * 100.0
	}
}

/// Performance statistics for signal emissions
#[derive(Debug, Clone)]
pub struct SignalPerformanceStats {
	/// Total number of signal sends
	pub total_sends: usize,
	/// Total execution time for all signals
	pub total_duration: Duration,
	/// Average time per signal send
	pub avg_send_duration: Duration,
	/// Slowest signal send duration
	pub slowest_send: Duration,
	/// Fastest signal send duration
	pub fastest_send: Duration,
}

impl Default for SignalPerformanceStats {
	fn default() -> Self {
		Self {
			total_sends: 0,
			total_duration: Duration::ZERO,
			avg_send_duration: Duration::ZERO,
			slowest_send: Duration::ZERO,
			fastest_send: Duration::MAX,
		}
	}
}

/// Signal profiler for performance analysis
///
/// This middleware tracks execution times, call counts, and failure rates
/// for each receiver to help identify performance bottlenecks.
pub struct SignalProfiler<T: Send + Sync + 'static> {
	receiver_profiles: Arc<RwLock<HashMap<String, ReceiverProfile>>>,
	performance_stats: Arc<RwLock<SignalPerformanceStats>>,
	current_send_start: Arc<RwLock<Option<Instant>>>,
	current_receiver_start: Arc<RwLock<HashMap<String, Instant>>>,
	_phantom: std::marker::PhantomData<T>,
}

impl<T: Send + Sync + 'static> SignalProfiler<T> {
	/// Create a new signal profiler
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::profiler::SignalProfiler;
	///
	/// let profiler = SignalProfiler::<String>::new();
	/// ```
	pub fn new() -> Self {
		Self {
			receiver_profiles: Arc::new(RwLock::new(HashMap::new())),
			performance_stats: Arc::new(RwLock::new(SignalPerformanceStats::default())),
			current_send_start: Arc::new(RwLock::new(None)),
			current_receiver_start: Arc::new(RwLock::new(HashMap::new())),
			_phantom: std::marker::PhantomData,
		}
	}

	/// Get profile for a specific receiver
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::{Signal, SignalName};
	/// use reinhardt_core::signals::profiler::SignalProfiler;
	///
	/// # tokio_test::block_on(async {
	/// let profiler = SignalProfiler::<String>::new();
	/// let signal = Signal::<String>::new(SignalName::custom("test"));
	/// signal.add_middleware(profiler.clone());
	///
	/// signal.connect_with_options(
	///     |_| async { Ok(()) },
	///     None,
	///     Some("my_receiver".to_string()),
	///     0
	/// );
	/// signal.send("test".to_string()).await.unwrap();
	///
	/// let profile = profiler.get_receiver_profile("my_receiver");
	/// assert!(profile.is_some());
	/// # });
	/// ```
	pub fn get_receiver_profile(&self, dispatch_uid: &str) -> Option<ReceiverProfile> {
		self.receiver_profiles.read().get(dispatch_uid).cloned()
	}

	/// Get all receiver profiles
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::profiler::SignalProfiler;
	///
	/// let profiler = SignalProfiler::<String>::new();
	/// let profiles = profiler.all_receiver_profiles();
	/// // Returns all receiver performance profiles
	/// ```
	pub fn all_receiver_profiles(&self) -> Vec<ReceiverProfile> {
		self.receiver_profiles.read().values().cloned().collect()
	}

	/// Get performance statistics
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::{Signal, SignalName};
	/// use reinhardt_core::signals::profiler::SignalProfiler;
	///
	/// # tokio_test::block_on(async {
	/// let profiler = SignalProfiler::<String>::new();
	/// let signal = Signal::<String>::new(SignalName::custom("test"));
	/// signal.add_middleware(profiler.clone());
	///
	/// signal.connect(|_| async { Ok(()) });
	/// signal.send("test".to_string()).await.unwrap();
	///
	/// let stats = profiler.performance_stats();
	/// assert_eq!(stats.total_sends, 1);
	/// # });
	/// ```
	pub fn performance_stats(&self) -> SignalPerformanceStats {
		self.performance_stats.read().clone()
	}

	/// Get slowest receivers (top N by average execution time)
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::profiler::SignalProfiler;
	///
	/// let profiler = SignalProfiler::<String>::new();
	/// let slowest = profiler.slowest_receivers(5);
	/// // Returns up to 5 slowest receivers
	/// ```
	pub fn slowest_receivers(&self, count: usize) -> Vec<ReceiverProfile> {
		let mut profiles = self.all_receiver_profiles();
		profiles.sort_by(|a, b| b.avg_duration.cmp(&a.avg_duration));
		profiles.truncate(count);
		profiles
	}

	/// Get receivers with highest failure rates
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::profiler::SignalProfiler;
	///
	/// let profiler = SignalProfiler::<String>::new();
	/// let unreliable = profiler.most_unreliable_receivers(5);
	/// // Returns up to 5 receivers with highest failure rates
	/// ```
	pub fn most_unreliable_receivers(&self, count: usize) -> Vec<ReceiverProfile> {
		let mut profiles = self.all_receiver_profiles();
		profiles.sort_by(|a, b| {
			let a_rate = a.success_rate();
			let b_rate = b.success_rate();
			a_rate
				.partial_cmp(&b_rate)
				.unwrap_or(std::cmp::Ordering::Equal)
		});
		profiles.truncate(count);
		profiles
	}

	/// Reset all profiling data
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::profiler::SignalProfiler;
	///
	/// let profiler = SignalProfiler::<String>::new();
	/// profiler.reset();
	/// assert_eq!(profiler.all_receiver_profiles().len(), 0);
	/// ```
	pub fn reset(&self) {
		self.receiver_profiles.write().clear();
		*self.performance_stats.write() = SignalPerformanceStats::default();
		*self.current_send_start.write() = None;
		self.current_receiver_start.write().clear();
	}

	/// Generate a human-readable performance report
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_core::signals::{Signal, SignalName};
	/// use reinhardt_core::signals::profiler::SignalProfiler;
	///
	/// # tokio_test::block_on(async {
	/// let profiler = SignalProfiler::<String>::new();
	/// let signal = Signal::<String>::new(SignalName::custom("test"));
	/// signal.add_middleware(profiler.clone());
	///
	/// signal.connect(|_| async { Ok(()) });
	/// signal.send("test".to_string()).await.unwrap();
	///
	/// let report = profiler.generate_report();
	/// assert!(report.contains("Performance Profile"));
	/// # });
	/// ```
	pub fn generate_report(&self) -> String {
		let stats = self.performance_stats.read();
		let profiles = self.receiver_profiles.read();

		let mut report = String::from("=== Signal Performance Profile ===\n\n");

		report.push_str("Overall Statistics:\n");
		report.push_str(&format!("  Total sends: {}\n", stats.total_sends));
		report.push_str(&format!("  Total duration: {:?}\n", stats.total_duration));
		report.push_str(&format!(
			"  Average send duration: {:?}\n",
			stats.avg_send_duration
		));
		report.push_str(&format!("  Slowest send: {:?}\n", stats.slowest_send));
		report.push_str(&format!(
			"  Fastest send: {:?}\n",
			if stats.fastest_send == Duration::MAX {
				Duration::ZERO
			} else {
				stats.fastest_send
			}
		));

		if !profiles.is_empty() {
			report.push_str("\nReceiver Profiles:\n");

			let mut sorted_profiles: Vec<_> = profiles.values().collect();
			sorted_profiles.sort_by(|a, b| b.avg_duration.cmp(&a.avg_duration));

			for profile in sorted_profiles {
				report.push_str(&format!("\n  {}:\n", profile.dispatch_uid));
				report.push_str(&format!("    Calls: {}\n", profile.call_count));
				report.push_str(&format!("    Total time: {:?}\n", profile.total_duration));
				report.push_str(&format!("    Avg time: {:?}\n", profile.avg_duration));
				report.push_str(&format!("    Min time: {:?}\n", profile.min_duration));
				report.push_str(&format!("    Max time: {:?}\n", profile.max_duration));
				report.push_str(&format!(
					"    Success rate: {:.2}%\n",
					profile.success_rate()
				));

				if profile.failure_count > 0 {
					report.push_str(&format!("    Failures: {}\n", profile.failure_count));
				}
			}
		}

		// Recommendations
		report.push_str("\nRecommendations:\n");

		if let Some(slowest) = self.slowest_receivers(1).first()
			&& slowest.avg_duration.as_millis() > 100
		{
			report.push_str(&format!(
				"  âš  Receiver '{}' is slow (avg: {:?}). Consider optimization.\n",
				slowest.dispatch_uid, slowest.avg_duration
			));
		}

		let unreliable = self.most_unreliable_receivers(3);
		for profile in unreliable {
			if profile.success_rate() < 95.0 {
				report.push_str(&format!(
					"  âš  Receiver '{}' has low success rate ({:.2}%). Check error handling.\n",
					profile.dispatch_uid,
					profile.success_rate()
				));
			}
		}

		if stats.total_sends > 0 && stats.avg_send_duration.as_millis() > 500 {
			report.push_str(&format!(
				"  âš  Average send duration is high ({:?}). Consider async execution or batching.\n",
				stats.avg_send_duration
			));
		}

		report
	}
}

impl<T: Send + Sync + 'static> Clone for SignalProfiler<T> {
	fn clone(&self) -> Self {
		Self {
			receiver_profiles: Arc::clone(&self.receiver_profiles),
			performance_stats: Arc::clone(&self.performance_stats),
			current_send_start: Arc::clone(&self.current_send_start),
			current_receiver_start: Arc::clone(&self.current_receiver_start),
			_phantom: std::marker::PhantomData,
		}
	}
}

impl<T: Send + Sync + 'static> Default for SignalProfiler<T> {
	fn default() -> Self {
		Self::new()
	}
}

#[async_trait::async_trait]
impl<T: Send + Sync + 'static> SignalMiddleware<T> for SignalProfiler<T> {
	async fn before_send(&self, _instance: &T) -> Result<bool, SignalError> {
		*self.current_send_start.write() = Some(Instant::now());
		Ok(true)
	}

	async fn after_send(
		&self,
		_instance: &T,
		_results: &[Result<(), SignalError>],
	) -> Result<(), SignalError> {
		if let Some(start) = *self.current_send_start.read() {
			let duration = start.elapsed();
			let mut stats = self.performance_stats.write();

			stats.total_sends += 1;
			stats.total_duration += duration;
			let divisor = u32::try_from(stats.total_sends).unwrap_or(u32::MAX);
			stats.avg_send_duration = stats.total_duration / divisor;
			stats.slowest_send = stats.slowest_send.max(duration);
			stats.fastest_send = stats.fastest_send.min(duration);
		}

		*self.current_send_start.write() = None;
		Ok(())
	}

	async fn before_receiver(
		&self,
		_instance: &T,
		dispatch_uid: Option<&str>,
	) -> Result<bool, SignalError> {
		if let Some(uid) = dispatch_uid {
			self.current_receiver_start
				.write()
				.insert(uid.to_string(), Instant::now());
		}
		Ok(true)
	}

	async fn after_receiver(
		&self,
		_instance: &T,
		dispatch_uid: Option<&str>,
		result: &Result<(), SignalError>,
	) -> Result<(), SignalError> {
		if let Some(uid) = dispatch_uid
			&& let Some(start) = self.current_receiver_start.write().remove(uid)
		{
			let duration = start.elapsed();
			let success = result.is_ok();

			let mut profiles = self.receiver_profiles.write();
			let profile = profiles
				.entry(uid.to_string())
				.or_insert_with(|| ReceiverProfile::new(uid.to_string()));

			profile.record_execution(duration, success);
		}

		Ok(())
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::signals::{SignalName, signal::Signal};

	#[derive(Debug, Clone)]
	// Allow dead_code: test-only data struct; fields accessed indirectly via Clone/Debug derives and signal dispatch
	#[allow(dead_code)]
	struct TestData {
		value: String,
	}

	#[tokio::test]
	async fn test_profiler_tracks_execution_time() {
		let profiler = SignalProfiler::new();
		let signal = Signal::<TestData>::new(SignalName::custom("test"));
		signal.add_middleware(profiler.clone());

		signal.connect_with_options(
			|_| async { Ok(()) },
			None,
			Some("slow_receiver".to_string()),
			0,
		);

		signal
			.send(TestData {
				value: "test".to_string(),
			})
			.await
			.unwrap();

		let stats = profiler.performance_stats();
		assert_eq!(stats.total_sends, 1);
		// avg_send_duration should be recorded (non-zero for valid execution)
		assert!(stats.avg_send_duration > Duration::ZERO);
	}

	#[tokio::test]
	async fn test_profiler_tracks_receiver_performance() {
		let profiler = SignalProfiler::new();
		let signal = Signal::<TestData>::new(SignalName::custom("test"));
		signal.add_middleware(profiler.clone());

		signal.connect_with_options(
			|_| async { Ok(()) },
			None,
			Some("test_receiver".to_string()),
			0,
		);

		signal
			.send(TestData {
				value: "test".to_string(),
			})
			.await
			.unwrap();

		let profile = profiler.get_receiver_profile("test_receiver");
		let profile = profile.unwrap();
		assert_eq!(profile.call_count, 1);
		// avg_duration should be recorded (non-zero for valid execution)
		assert!(profile.avg_duration > Duration::ZERO);
	}

	#[tokio::test]
	async fn test_profiler_report() {
		let profiler = SignalProfiler::new();
		let signal = Signal::<TestData>::new(SignalName::custom("test"));
		signal.add_middleware(profiler.clone());

		signal.connect(|_| async { Ok(()) });
		signal
			.send(TestData {
				value: "test".to_string(),
			})
			.await
			.unwrap();

		let report = profiler.generate_report();
		assert!(report.contains("Performance Profile"));
		assert!(report.contains("Total sends: 1"));
	}

	#[tokio::test]
	async fn test_profiler_reset() {
		let profiler = SignalProfiler::new();
		let signal = Signal::<TestData>::new(SignalName::custom("test"));
		signal.add_middleware(profiler.clone());

		signal.connect(|_| async { Ok(()) });
		signal
			.send(TestData {
				value: "test".to_string(),
			})
			.await
			.unwrap();

		assert_eq!(profiler.performance_stats().total_sends, 1);

		profiler.reset();

		assert_eq!(profiler.performance_stats().total_sends, 0);
		assert_eq!(profiler.all_receiver_profiles().len(), 0);
	}

	#[tokio::test]
	async fn test_slowest_receivers() {
		let profiler = SignalProfiler::new();
		let signal = Signal::<TestData>::new(SignalName::custom("test"));
		signal.add_middleware(profiler.clone());

		signal.connect_with_options(
			|_| async {
				tokio::time::sleep(tokio::time::Duration::from_millis(20)).await;
				Ok(())
			},
			None,
			Some("slow".to_string()),
			0,
		);

		signal.connect_with_options(
			|_| async {
				tokio::time::sleep(tokio::time::Duration::from_millis(5)).await;
				Ok(())
			},
			None,
			Some("fast".to_string()),
			0,
		);

		signal
			.send(TestData {
				value: "test".to_string(),
			})
			.await
			.unwrap();

		let slowest = profiler.slowest_receivers(1);
		assert_eq!(slowest.len(), 1);
		assert_eq!(slowest[0].dispatch_uid, "slow");
	}
}