-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdemo-events.php
More file actions
129 lines (101 loc) · 3.83 KB
/
Copy pathdemo-events.php
File metadata and controls
129 lines (101 loc) · 3.83 KB
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
<?php
declare(strict_types=1);
/**
* Matrix v4.0 Event System & Metrics Demo
*
* This script demonstrates the new event-driven observability features
* introduced in Matrix v4.0.
*/
require_once __DIR__ . '/vendor/autoload.php';
use function Matrix\Support\async;
use function Matrix\Support\await;
use function Matrix\Support\getMetrics;
use function Matrix\Support\listen;
use function Matrix\Support\timeout;
echo "🚀 Matrix v4.0 Event System & Metrics Demo\n";
echo '=' . str_repeat('=', 45) . "\n\n";
// Set up event listeners for demonstration
listen('promise.created', function ($event) {
echo "📦 Promise created: {$event->getPromiseId()} (type: {$event->getType()})\n";
});
listen('promise.resolved', function ($event) {
$duration = round($event->getDuration() * 1000, 2); // Convert to milliseconds
echo "✅ Promise resolved: {$event->getPromiseId()} in {$duration}ms\n";
});
listen('promise.rejected', function ($event) {
$duration = round($event->getDuration() * 1000, 2);
echo "❌ Promise rejected: {$event->getPromiseId()} in {$duration}ms - {$event->getErrorMessage()}\n";
});
listen('promise.timeout', function ($event) {
echo "⏱️ Promise timeout: {$event->getPromiseId()} after {$event->getTimeoutDuration()}s\n";
});
echo "1. Running successful async operations...\n";
// Execute some successful operations
$results = [];
for ($i = 1; $i <= 3; $i++) {
$results[] = await(async(function () use ($i) {
// Simulate different work durations
usleep(rand(10000, 50000)); // 10-50ms
return "Task {$i} completed";
}));
}
echo "\nResults: " . implode(', ', $results) . "\n\n";
echo "2. Running operations with failures...\n";
// Execute operations with some failures
for ($i = 1; $i <= 2; $i++) {
try {
await(async(function () use ($i) {
if ($i === 2) {
throw new Exception("Simulated error in task {$i}");
}
usleep(20000); // 20ms
return "Task {$i} completed";
}));
} catch (Exception $e) {
// Expected for task 2
}
}
echo "\n3. Demonstrating timeout events...\n";
// Demonstrate timeout
try {
await(timeout(
async(function () {
usleep(200000); // 200ms - will timeout
return 'This should timeout';
}),
0.1, // 100ms timeout
'Custom timeout message'
));
} catch (Exception $e) {
echo "Caught timeout exception: {$e->getMessage()}\n";
}
echo "\n4. Current Metrics Summary:\n";
echo str_repeat('-', 30) . "\n";
$metrics = getMetrics();
echo "📊 Performance Metrics:\n";
echo " Active promises: {$metrics['active_promises']}\n";
echo " Completed promises: {$metrics['completed_promises']}\n";
echo ' Success rate: ' . round($metrics['success_rate'], 2) . "%\n";
echo ' Average resolution time: ' . round($metrics['average_resolution_time'] * 1000, 2) . "ms\n\n";
echo "📈 Operation Counters:\n";
foreach ($metrics['counters'] as $key => $count) {
echo " {$key}: {$count}\n";
}
if (! empty($metrics['timings'])) {
echo "\n⏱️ Timing Statistics:\n";
foreach ($metrics['timings'] as $operation => $stats) {
echo " {$operation}:\n";
echo " Count: {$stats['count']}\n";
echo ' Average: ' . round($stats['avg'] * 1000, 2) . "ms\n";
echo ' Min: ' . round($stats['min'] * 1000, 2) . "ms\n";
echo ' Max: ' . round($stats['max'] * 1000, 2) . "ms\n";
echo ' P95: ' . round($stats['p95'] * 1000, 2) . "ms\n";
}
}
echo "\n🎉 Demo completed! Matrix v4.0 provides lifecycle events and bounded metrics.\n";
echo "\nKey benefits:\n";
echo " ✓ Real-time event monitoring\n";
echo " ✓ Performance metrics collection\n";
echo " ✓ Error tracking and classification\n";
echo " ✓ Debugging and profiling capabilities\n";
echo " ✓ Zero-configuration setup\n";