quantum-pulse 0.1.13

A lightweight, customizable profiling library for Rust with support for custom categories and percentile statistics
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
638
639
640
641
642
643
644
645
646
647
648
649
650
//! Example demonstrating async profiling capabilities with enum-based operations using Operation trait

use quantum_pulse::{profile_async, Category, Operation, ProfileCollector};
use std::time::Duration;
use tokio::time::sleep;
use tokio_stream::{self as stream, StreamExt};

/// Async service operations that implement Operation trait
#[derive(Debug)]
enum AsyncOperation {
    // Authentication operations
    AuthenticateUser,
    ValidateSession,

    // Database operations
    FetchUserProfile,
    FetchUserPreferences,
    FetchUserHistory,
    UpdateUserData,
    BatchQuery,

    // HTTP operations
    EnrichWithExternalData,
    CallPaymentApi,
    FetchWeatherData,

    // Message queue operations
    PublishEvent,
    ConsumeMessage,
    ProcessNotification,

    // Data processing operations
    ProcessUserData,
    ComplexAnalysis,
    StreamProcessing,
    BatchProcessing,

    // Coordination operations
    ServiceCoordination,
    WorkflowOrchestration,
}

/// Database category for async database operations
#[derive(Debug)]
struct AsyncDatabaseCategory;

impl Category for AsyncDatabaseCategory {
    fn get_name(&self) -> &str {
        "AsyncDatabase"
    }

    fn get_description(&self) -> &str {
        "Asynchronous database queries and transactions"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#FF6B6B")
    }

    fn priority(&self) -> i32 {
        1
    }
}

/// HTTP category for external API calls
#[derive(Debug)]
struct AsyncHttpCategory;

impl Category for AsyncHttpCategory {
    fn get_name(&self) -> &str {
        "AsyncHTTP"
    }

    fn get_description(&self) -> &str {
        "Asynchronous HTTP requests and external API calls"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#4ECDC4")
    }

    fn priority(&self) -> i32 {
        2
    }
}

/// Message queue category for async messaging
#[derive(Debug)]
struct AsyncMessageCategory;

impl Category for AsyncMessageCategory {
    fn get_name(&self) -> &str {
        "AsyncMessage"
    }

    fn get_description(&self) -> &str {
        "Asynchronous message queue operations"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#45B7D1")
    }

    fn priority(&self) -> i32 {
        3
    }
}

/// Processing category for data processing operations
#[derive(Debug)]
struct AsyncProcessingCategory;

impl Category for AsyncProcessingCategory {
    fn get_name(&self) -> &str {
        "AsyncProcessing"
    }

    fn get_description(&self) -> &str {
        "Asynchronous data processing and analysis"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#96CEB4")
    }

    fn priority(&self) -> i32 {
        4
    }
}

/// Authentication category for auth operations
#[derive(Debug)]
struct AsyncAuthCategory;

impl Category for AsyncAuthCategory {
    fn get_name(&self) -> &str {
        "AsyncAuth"
    }

    fn get_description(&self) -> &str {
        "Asynchronous authentication and authorization"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#DDA0DD")
    }

    fn priority(&self) -> i32 {
        5
    }
}

/// Coordination category for orchestration
#[derive(Debug)]
struct AsyncCoordinationCategory;

impl Category for AsyncCoordinationCategory {
    fn get_name(&self) -> &str {
        "AsyncCoordination"
    }

    fn get_description(&self) -> &str {
        "Asynchronous service coordination and orchestration"
    }

    fn color_hint(&self) -> Option<&str> {
        Some("#F4A460")
    }

    fn priority(&self) -> i32 {
        6
    }
}

impl Operation for AsyncOperation {
    fn get_category(&self) -> &dyn Category {
        match self {
            AsyncOperation::AuthenticateUser | AsyncOperation::ValidateSession => {
                &AsyncAuthCategory
            }

            AsyncOperation::FetchUserProfile
            | AsyncOperation::FetchUserPreferences
            | AsyncOperation::FetchUserHistory
            | AsyncOperation::UpdateUserData
            | AsyncOperation::BatchQuery => &AsyncDatabaseCategory,

            AsyncOperation::EnrichWithExternalData
            | AsyncOperation::CallPaymentApi
            | AsyncOperation::FetchWeatherData => &AsyncHttpCategory,

            AsyncOperation::PublishEvent
            | AsyncOperation::ConsumeMessage
            | AsyncOperation::ProcessNotification => &AsyncMessageCategory,

            AsyncOperation::ProcessUserData
            | AsyncOperation::ComplexAnalysis
            | AsyncOperation::StreamProcessing
            | AsyncOperation::BatchProcessing => &AsyncProcessingCategory,

            AsyncOperation::ServiceCoordination | AsyncOperation::WorkflowOrchestration => {
                &AsyncCoordinationCategory
            }
        }
    }

    fn to_str(&self) -> String {
        match self {
            AsyncOperation::AuthenticateUser => "authenticate_user".to_string(),
            AsyncOperation::ValidateSession => "validate_session".to_string(),
            AsyncOperation::FetchUserProfile => "fetch_user_profile".to_string(),
            AsyncOperation::FetchUserPreferences => "fetch_user_preferences".to_string(),
            AsyncOperation::FetchUserHistory => "fetch_user_history".to_string(),
            AsyncOperation::UpdateUserData => "update_user_data".to_string(),
            AsyncOperation::BatchQuery => "batch_query".to_string(),
            AsyncOperation::EnrichWithExternalData => "enrich_with_external_data".to_string(),
            AsyncOperation::CallPaymentApi => "call_payment_api".to_string(),
            AsyncOperation::FetchWeatherData => "fetch_weather_data".to_string(),
            AsyncOperation::PublishEvent => "publish_event".to_string(),
            AsyncOperation::ConsumeMessage => "consume_message".to_string(),
            AsyncOperation::ProcessNotification => "process_notification".to_string(),
            AsyncOperation::ProcessUserData => "process_user_data".to_string(),
            AsyncOperation::ComplexAnalysis => "complex_analysis".to_string(),
            AsyncOperation::StreamProcessing => "stream_processing".to_string(),
            AsyncOperation::BatchProcessing => "batch_processing".to_string(),
            AsyncOperation::ServiceCoordination => "service_coordination".to_string(),
            AsyncOperation::WorkflowOrchestration => "workflow_orchestration".to_string(),
        }
    }
}

#[tokio::main]
async fn main() {
    println!("=== Async Profiling Example ===\n");

    // Clear any existing data
    ProfileCollector::clear_all();

    // Example 1: Simple async operation profiling
    println!("1. Simple async operation profiling:");
    let result = profile_async!(AsyncOperation::AuthenticateUser, async {
        simulate_async_work(50).await;
        "User authenticated"
    })
    .await;
    println!("   Result: {}", result);

    // Example 2: Database operations
    println!("\n2. Database operations:");
    let result = profile_async!(AsyncOperation::FetchUserProfile, async {
        simulate_async_work(100).await;
        "User profile data"
    })
    .await;
    println!("   Result: {}", result);
    println!("   User data: {}", result);

    // Example 3: Concurrent async operations
    println!("\n3. Running concurrent async operations:");
    run_concurrent_operations().await;

    // Example 4: Complex async workflow
    println!("\n4. Complex async workflow:");
    process_user_request(123).await;

    // Example 5: Stream processing
    println!("\n5. Stream processing:");
    process_data_stream().await;

    // Example 6: Message queue operations
    println!("\n6. Message queue operations:");
    handle_message_queue().await;

    // Example 7: Coordinated workflow
    println!("\n7. Coordinated workflow:");
    // Simulate orchestrating a multi-service workflow
    orchestrate_services().await;

    // Use unused variants to avoid warnings
    if false {
        profile_async!(AsyncOperation::CallPaymentApi, async {
            println!("Processing payment");
        })
        .await;

        profile_async!(AsyncOperation::BatchProcessing, async {
            println!("Batch processing");
        })
        .await;
    }

    // Generate and display report
    show_async_profiling_results();
}

async fn run_concurrent_operations() {
    println!("   Starting 3 concurrent operations...");

    let (result1, result2, result3) = tokio::join!(
        profile_async!(AsyncOperation::BatchQuery, async {
            simulate_async_work(40).await;
            "Batch query complete"
        }),
        profile_async!(AsyncOperation::FetchWeatherData, async {
            simulate_async_work(60).await;
            "Weather data fetched"
        }),
        profile_async!(AsyncOperation::PublishEvent, async {
            simulate_async_work(25).await;
            "Event published"
        })
    );

    println!("   Results: {}, {}, {}", result1, result2, result3);
}

async fn process_user_request(user_id: u64) {
    println!("   Processing request for user {}...", user_id);

    // Step 1: Authentication
    let _auth_token = profile_async!(AsyncOperation::AuthenticateUser, async {
        simulate_async_work(20).await;
        format!("token_{}", user_id)
    })
    .await;

    // Step 2: Validate session
    profile_async!(AsyncOperation::ValidateSession, async {
        simulate_async_work(10).await;
    })
    .await;

    // Step 3: Fetch user data (parallel queries)
    let (profile_data, preferences, history) = tokio::join!(
        profile_async!(AsyncOperation::FetchUserProfile, async {
            simulate_async_work(35).await;
            "User profile"
        }),
        profile_async!(AsyncOperation::FetchUserPreferences, async {
            simulate_async_work(25).await;
            "User preferences"
        }),
        profile_async!(AsyncOperation::FetchUserHistory, async {
            simulate_async_work(45).await;
            "User history"
        })
    );

    // Step 4: Process data
    let processed = profile_async!(AsyncOperation::ProcessUserData, async {
        simulate_async_work(30).await;
        format!(
            "Processed: {} + {} + {}",
            profile_data, preferences, history
        )
    })
    .await;

    // Step 5: Enrich with external data
    let enriched = profile_async!(AsyncOperation::EnrichWithExternalData, async {
        simulate_async_work(70).await;
        format!("{} [enriched]", processed)
    })
    .await;

    // Step 6: Update user data
    profile_async!(AsyncOperation::UpdateUserData, async {
        simulate_async_work(20).await;
        println!("   Updated user data for user {}", user_id);
    })
    .await;

    // Step 7: Publish result
    profile_async!(AsyncOperation::PublishEvent, async {
        simulate_async_work(15).await;
        println!("   Published result for user {}: {}", user_id, enriched);
    })
    .await;

    println!("   Request processing complete for user {}", user_id);
}

async fn process_data_stream() {
    println!("   Processing stream of 5 items...");

    let mut stream = stream::iter(1..=5);

    while let Some(item) = stream.next().await {
        profile_async!(AsyncOperation::StreamProcessing, async {
            simulate_async_work(15 * item).await;
            println!("     Processed stream item: {}", item);
        })
        .await;
    }

    println!("   Stream processing complete");
}

async fn handle_message_queue() {
    println!("   Handling message queue operations...");

    // Simulate consuming messages
    for i in 1..=3 {
        let message = profile_async!(AsyncOperation::ConsumeMessage, async {
            simulate_async_work(20).await;
            format!("Message_{}", i)
        })
        .await;

        // Process notification
        profile_async!(AsyncOperation::ProcessNotification, async {
            simulate_async_work(10).await;
            println!("     Processed notification: {}", message);
        })
        .await;
    }

    println!("   Message queue handling complete");
}

async fn orchestrate_services() {
    println!("   Orchestrating services...");

    // Service coordination
    let coord_op = AsyncOperation::ServiceCoordination;
    profile_async!(coord_op, async {
        simulate_async_work(50).await;
        println!("     Services coordinated");
    })
    .await;

    // Workflow orchestration
    let workflow_op = AsyncOperation::WorkflowOrchestration;
    profile_async!(workflow_op, async {
        // Simulate complex workflow
        for step in 1..=3 {
            simulate_async_work(30).await;
            println!("       Workflow step {} completed", step);
        }
        println!("     Workflow orchestration complete");
    })
    .await;

    // Complex analysis
    let analysis_op = AsyncOperation::ComplexAnalysis;
    profile_async!(analysis_op, async {
        simulate_async_work(80).await;
        println!("     Complex analysis complete");
    })
    .await;

    println!("   Service orchestration complete");
}

fn show_async_profiling_results() {
    println!("\n{}", "=".repeat(70));
    println!("ASYNC PROFILING RESULTS BY CATEGORY");
    println!("{}", "=".repeat(70));

    ProfileCollector::report_stats();

    let summary = ProfileCollector::get_summary();
    println!("\nSUMMARY:");
    println!("- Total async operations: {}", summary.total_operations);
    println!("- Unique async operations: {}", summary.unique_operations);
    println!("- Total async time: {}μs", summary.total_time_micros);

    println!("\n{}", "=".repeat(70));
    println!("DETAILED ANALYSIS BY ASYNC CATEGORY");
    println!("{}", "=".repeat(70));

    let all_stats = ProfileCollector::get_all_stats();

    // Group operations by category
    let mut categories: std::collections::HashMap<
        String,
        Vec<(String, quantum_pulse::OperationStats)>,
    > = std::collections::HashMap::new();

    for (key, stats) in all_stats {
        if let Some((category, operation)) = key.split_once("::") {
            categories
                .entry(category.to_string())
                .or_insert_with(Vec::new)
                .push((operation.to_string(), stats));
        }
    }

    // Display results grouped by category
    let category_order = vec![
        "AsyncAuth",
        "AsyncDatabase",
        "AsyncHTTP",
        "AsyncMessage",
        "AsyncProcessing",
        "AsyncCoordination",
    ];

    for category_name in category_order {
        if let Some(ops) = categories.get(category_name) {
            println!("\n🔄 {} Category:", category_name);
            let mut total_calls = 0;
            let mut total_time = Duration::ZERO;

            for (op_name, stats) in ops {
                println!(
                    "   {} - {} calls, avg: {:?}",
                    op_name,
                    stats.count,
                    stats.mean()
                );
                total_calls += stats.count;
                total_time += stats.total;
            }

            println!(
                "   📊 Category Total: {} calls, {:?} total time",
                total_calls, total_time
            );
        }
    }

    println!("\n{}", "=".repeat(70));
    println!("TOP ASYNC OPERATIONS");
    println!("{}", "=".repeat(70));

    let all_stats = ProfileCollector::get_all_stats();

    // Top by total time
    let mut sorted_by_total: Vec<_> = all_stats.iter().collect();
    sorted_by_total.sort_by(|a, b| b.1.total.cmp(&a.1.total));

    println!("\n⏱️  Most Time Consuming Async Operations (Top 5):");
    for (i, (name, stats)) in sorted_by_total.iter().take(5).enumerate() {
        println!(
            "  {}. {} - {:?} total ({} calls)",
            i + 1,
            name,
            stats.total,
            stats.count
        );
    }

    // Top by call count
    let mut sorted_by_count: Vec<_> = all_stats.iter().collect();
    sorted_by_count.sort_by(|a, b| b.1.count.cmp(&a.1.count));

    println!("\n📈 Most Frequently Called Async Operations (Top 5):");
    for (i, (name, stats)) in sorted_by_count.iter().take(5).enumerate() {
        println!(
            "  {}. {} - {} calls (avg: {:?})",
            i + 1,
            name,
            stats.count,
            stats.mean()
        );
    }

    // Top by average time
    let mut sorted_by_avg: Vec<_> = all_stats.iter().collect();
    sorted_by_avg.sort_by(|a, b| b.1.mean().cmp(&a.1.mean()));

    println!("\n⚡ Highest Average Latency Async Operations (Top 5):");
    for (i, (name, stats)) in sorted_by_avg.iter().take(5).enumerate() {
        println!(
            "  {}. {} - avg: {:?} ({} calls)",
            i + 1,
            name,
            stats.mean(),
            stats.count
        );
    }
}

async fn simulate_async_work(millis: u64) {
    sleep(Duration::from_millis(millis)).await;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_async_operation_categories() {
        let auth_op = AsyncOperation::AuthenticateUser;
        assert_eq!(auth_op.get_category().get_name(), "AsyncAuth");
        assert_eq!(auth_op.to_str(), "authenticate_user");

        let db_op = AsyncOperation::FetchUserProfile;
        assert_eq!(db_op.get_category().get_name(), "AsyncDatabase");
        assert_eq!(db_op.to_str(), "fetch_user_profile");

        let http_op = AsyncOperation::EnrichWithExternalData;
        assert_eq!(http_op.get_category().get_name(), "AsyncHTTP");
        assert_eq!(http_op.to_str(), "enrich_with_external_data");
    }

    #[test]
    fn test_async_category_properties() {
        let auth_cat = AsyncAuthCategory;
        assert_eq!(auth_cat.get_name(), "AsyncAuth");
        assert_eq!(auth_cat.priority(), 5);
        assert!(auth_cat.color_hint().is_some());

        let db_cat = AsyncDatabaseCategory;
        assert_eq!(db_cat.get_name(), "AsyncDatabase");
        assert_eq!(db_cat.priority(), 1);
    }

    #[tokio::test]
    async fn test_async_profiling_basic() {
        ProfileCollector::clear_all();

        let op = AsyncOperation::AuthenticateUser;
        let result = profile_async!(op, async {
            sleep(Duration::from_millis(1)).await;
            "authenticated"
        });

        assert_eq!(result, "authenticated");
        assert!(ProfileCollector::has_data());
        let stats = ProfileCollector::get_stats("AsyncAuth::authenticate_user");
        assert!(stats.is_some());
        assert_eq!(stats.unwrap().count, 1);
    }

    #[tokio::test]
    async fn test_concurrent_async_profiling() {
        ProfileCollector::clear_all();

        let op1 = AsyncOperation::FetchUserProfile;
        let op2 = AsyncOperation::FetchUserPreferences;

        let (result1, result2) = tokio::join!(
            profile_async!(op1, async {
                sleep(Duration::from_millis(1)).await;
                "profile"
            }),
            profile_async!(op2, async {
                sleep(Duration::from_millis(1)).await;
                "preferences"
            })
        );

        assert_eq!(result1, "profile");
        assert_eq!(result2, "preferences");
        assert!(ProfileCollector::has_data());
        assert_eq!(ProfileCollector::total_operations(), 2);
    }
}