pulzr 0.3.2

A http load testing tool for performance testing.
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
use super::load_balancer::{LoadDistribution, LoadDistributionConfig, LoadDistributionEngine};
use crate::integrations::websocket::{
    CoordinationSettings, CoordinatorTestStatus, DistributedTestConfig, SyncState, TestCommandType,
    WebSocketMessage, WorkerInfo, WorkerLoad, WorkerStatus,
};
use crate::metrics::{DistributedStatsCollector, StatsCollector};
use crate::utils::port_utils::find_available_port;
use anyhow::Result;
use chrono::Utc;
use futures::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{broadcast, RwLock};
use tokio::time::{interval, sleep, Duration, Instant};
use tokio_tungstenite::accept_async;
use uuid::Uuid;

#[derive(Debug, Clone)]
pub struct ConnectedWorker {
    pub worker_id: String,
    pub worker_info: WorkerInfo,
    pub status: WorkerStatus,
    pub last_heartbeat: Instant,
    pub current_load: WorkerLoad,
    pub connection_time: Instant,
}

#[derive(Debug, Clone)]
pub struct SyncTracker {
    pub test_id: String,
    pub sync_state: SyncState,
    pub target_workers: Vec<String>,
    pub ready_workers: HashMap<String, Instant>,
    pub coordination_settings: CoordinationSettings,
    pub sync_start_time: Instant,
    pub sync_timeout: Duration,
}

struct WorkerConnectionContext {
    workers: Arc<RwLock<HashMap<String, ConnectedWorker>>>,
    message_sender: broadcast::Sender<WebSocketMessage>,
    coordinator_id: String,
    max_workers: usize,
    distributed_stats: Arc<DistributedStatsCollector>,
    sync_tracker: Arc<RwLock<Option<SyncTracker>>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoordinatorConfig {
    pub coordinator_id: String,
    pub port: u16,
    pub max_workers: usize,
    pub heartbeat_timeout_secs: u64,
    pub heartbeat_check_interval_secs: u64,
    pub auto_balance_load: bool,
}

impl Default for CoordinatorConfig {
    fn default() -> Self {
        Self {
            coordinator_id: format!("coordinator-{}", Uuid::new_v4()),
            port: 9630,
            max_workers: 100,
            heartbeat_timeout_secs: 30,
            heartbeat_check_interval_secs: 10,
            auto_balance_load: true,
        }
    }
}

pub struct DistributedCoordinator {
    config: CoordinatorConfig,
    actual_port: Option<u16>,
    workers: Arc<RwLock<HashMap<String, ConnectedWorker>>>,
    test_status: Arc<RwLock<CoordinatorTestStatus>>,
    current_test_id: Arc<RwLock<Option<String>>>,
    sync_tracker: Arc<RwLock<Option<SyncTracker>>>,
    distributed_stats: Arc<DistributedStatsCollector>,
    load_balancer: Arc<RwLock<LoadDistributionEngine>>,
    current_distribution: Arc<RwLock<Option<LoadDistribution>>>,
    message_sender: broadcast::Sender<WebSocketMessage>,
}

impl DistributedCoordinator {
    pub fn new(config: CoordinatorConfig, _stats_collector: Arc<StatsCollector>) -> Self {
        let (message_sender, _) = broadcast::channel(1000);
        let distributed_stats = Arc::new(
            DistributedStatsCollector::new().with_websocket_sender(message_sender.clone()),
        );

        // Initialize load balancer with default configuration
        let load_balancer_config = LoadDistributionConfig::default();
        let load_balancer = Arc::new(RwLock::new(LoadDistributionEngine::new(
            load_balancer_config,
        )));

        Self {
            config,
            actual_port: None,
            workers: Arc::new(RwLock::new(HashMap::new())),
            test_status: Arc::new(RwLock::new(CoordinatorTestStatus::Idle)),
            current_test_id: Arc::new(RwLock::new(None)),
            sync_tracker: Arc::new(RwLock::new(None)),
            distributed_stats,
            load_balancer,
            current_distribution: Arc::new(RwLock::new(None)),
            message_sender,
        }
    }

    pub async fn start(&mut self) -> Result<u16> {
        let actual_port = find_available_port(self.config.port, 50).ok_or_else(|| {
            anyhow::anyhow!(
                "Could not find available port starting from {}",
                self.config.port
            )
        })?;

        self.actual_port = Some(actual_port);

        let addr = SocketAddr::from(([0, 0, 0, 0], actual_port)); // Listen on all interfaces
        let listener = TcpListener::bind(&addr).await?;

        println!(
            "Distributed Load Testing Coordinator listening on: ws://{}",
            addr
        );
        println!("Coordinator ID: {}", self.config.coordinator_id);

        // Start heartbeat monitoring task
        self.start_heartbeat_monitor().await;

        // Start failure recovery and load rebalancing task
        self.start_failure_recovery().await;

        // Start status broadcasting task
        self.start_status_broadcaster().await;

        // Accept worker connections
        let workers = Arc::clone(&self.workers);
        let message_sender = self.message_sender.clone();
        let coordinator_id = self.config.coordinator_id.clone();
        let max_workers = self.config.max_workers;
        let distributed_stats = Arc::clone(&self.distributed_stats);
        let sync_tracker = Arc::clone(&self.sync_tracker);

        tokio::spawn(async move {
            while let Ok((stream, addr)) = listener.accept().await {
                println!("New connection from: {}", addr);

                let connection_context = WorkerConnectionContext {
                    workers: Arc::clone(&workers),
                    message_sender: message_sender.clone(),
                    coordinator_id: coordinator_id.clone(),
                    max_workers,
                    distributed_stats: Arc::clone(&distributed_stats),
                    sync_tracker: Arc::clone(&sync_tracker),
                };

                tokio::spawn(handle_worker_connection(stream, addr, connection_context));
            }
        });

        Ok(actual_port)
    }

    async fn start_heartbeat_monitor(&self) {
        let workers = Arc::clone(&self.workers);
        let timeout_duration = Duration::from_secs(self.config.heartbeat_timeout_secs);
        let heartbeat_check_interval = self.config.heartbeat_check_interval_secs;
        let message_sender = self.message_sender.clone();
        let distributed_stats = Arc::clone(&self.distributed_stats);

        tokio::spawn(async move {
            let mut interval = interval(Duration::from_secs(heartbeat_check_interval)); // Use configurable interval

            loop {
                interval.tick().await;
                let now = Instant::now();
                let mut workers_guard = workers.write().await;
                let mut disconnected_workers = Vec::new();

                for (worker_id, worker) in workers_guard.iter() {
                    if now.duration_since(worker.last_heartbeat) > timeout_duration {
                        disconnected_workers.push(worker_id.clone());
                    }
                }

                for worker_id in disconnected_workers {
                    if let Some(worker) = workers_guard.remove(&worker_id) {
                        println!(
                            "⚠️  Worker {} failed: Heartbeat timeout after {}s (was {:?} for {:?})",
                            worker_id,
                            timeout_duration.as_secs(),
                            worker.status,
                            now.duration_since(worker.connection_time)
                        );

                        // Remove from distributed stats
                        distributed_stats.remove_worker(&worker_id).await;

                        // Create detailed failure event
                        let failure_msg = WebSocketMessage::WorkerFailure {
                            timestamp: Utc::now(),
                            worker_id: worker_id.clone(),
                            reason: "Heartbeat timeout".to_string(),
                            last_seen: worker.last_heartbeat.elapsed().as_secs(),
                            worker_info: worker.worker_info.clone(),
                        };
                        let _ = message_sender.send(failure_msg);

                        // Also send disconnect message for compatibility
                        let disconnect_msg = WebSocketMessage::WorkerDisconnect {
                            timestamp: Utc::now(),
                            worker_id: worker_id.clone(),
                            reason: "Heartbeat timeout".to_string(),
                        };
                        let _ = message_sender.send(disconnect_msg);
                    }
                }
            }
        });
    }

    async fn start_failure_recovery(&self) {
        let workers = Arc::clone(&self.workers);
        let test_status = Arc::clone(&self.test_status);
        let current_distribution = Arc::clone(&self.current_distribution);
        let load_balancer = Arc::clone(&self.load_balancer);
        let message_sender = self.message_sender.clone();

        tokio::spawn(async move {
            let mut interval = interval(Duration::from_secs(15)); // Check every 15 seconds

            loop {
                interval.tick().await;

                let status = {
                    let status_guard = test_status.read().await;
                    status_guard.clone()
                };

                // Only perform recovery during active testing
                if matches!(status, CoordinatorTestStatus::Running) {
                    let workers_guard = workers.read().await;
                    let active_workers: Vec<String> = workers_guard.keys().cloned().collect();
                    let worker_count = active_workers.len();
                    drop(workers_guard);

                    // Check if we need to rebalance load due to worker changes
                    let should_rebalance = {
                        let distribution_guard = current_distribution.read().await;
                        if let Some(ref distribution) = *distribution_guard {
                            // Check if current worker set differs from distribution
                            let distribution_workers: std::collections::HashSet<String> =
                                distribution.worker_allocations.keys().cloned().collect();
                            let current_workers: std::collections::HashSet<String> =
                                active_workers.iter().cloned().collect();

                            distribution_workers != current_workers
                        } else {
                            false
                        }
                    };

                    if should_rebalance && worker_count > 0 {
                        println!(
                            "🔄 Rebalancing load after worker changes ({} workers active)",
                            worker_count
                        );

                        // Get load balancer configuration
                        let (_strategy, total_target_load) = {
                            let balancer_guard = load_balancer.read().await;
                            (balancer_guard.get_config().strategy.clone(), 1000)
                            // Example target load
                        };

                        // Create new distribution for remaining workers - using simplified approach for recovery
                        let balancer_guard = load_balancer.read().await;
                        let worker_metrics: Vec<_> = active_workers
                            .iter()
                            .map(|worker_id| {
                                use crate::integrations::websocket::WorkerLoad;
                                use crate::metrics::stats::LiveMetrics;

                                crate::metrics::distributed_stats::WorkerMetrics {
                                    worker_id: worker_id.clone(),
                                    status: "Idle".to_string(),
                                    metrics: LiveMetrics {
                                        requests_sent: 0,
                                        requests_completed: 0,
                                        requests_failed: 0,
                                        current_rps: 0.0,
                                        avg_response_time: 0.0,
                                        min_response_time: 0,
                                        max_response_time: 0,
                                        p50_response_time: 0,
                                        p90_response_time: 0,
                                        p95_response_time: 0,
                                        p99_response_time: 0,
                                        active_connections: 0,
                                        queue_size: 0,
                                        bytes_received: 0,
                                        status_codes: std::collections::HashMap::new(),
                                        errors: std::collections::HashMap::new(),
                                        latency_histogram:
                                            crate::metrics::stats::LatencyHistogram::new(),
                                        active_alerts: Vec::new(),
                                    },
                                    load: WorkerLoad {
                                        current_rps: 0.0,
                                        active_connections: 0,
                                        memory_usage_mb: 0,
                                        cpu_usage_percent: 0.0,
                                        total_requests_sent: 0,
                                        errors_count: 0,
                                    },
                                    last_update: chrono::Utc::now(),
                                    connection_time: chrono::Utc::now(),
                                }
                            })
                            .collect();

                        let new_distribution = balancer_guard.calculate_distribution(
                            &worker_metrics,
                            total_target_load,
                            None,
                        );
                        let mut distribution_guard = current_distribution.write().await;
                        *distribution_guard = Some(new_distribution.clone());
                        drop(distribution_guard);
                        drop(balancer_guard);

                        // Broadcast rebalancing event
                        let rebalance_msg = WebSocketMessage::LoadRebalanced {
                            timestamp: chrono::Utc::now(),
                            active_workers: active_workers.clone(),
                            new_distribution,
                            reason: "Worker failure recovery".to_string(),
                        };
                        let _ = message_sender.send(rebalance_msg);

                        println!("✅ Load rebalanced across {} workers", worker_count);
                    }

                    // Check for worker performance issues and potential preemptive failures
                    let workers_guard = workers.read().await;
                    for (worker_id, worker) in workers_guard.iter() {
                        let last_heartbeat_age = worker.last_heartbeat.elapsed();

                        // Warn about workers that are approaching timeout
                        if last_heartbeat_age > Duration::from_secs(20)
                            && last_heartbeat_age < Duration::from_secs(25)
                        {
                            println!(
                                "⚠️  Worker {} heartbeat delayed: {}s (timeout at 30s)",
                                worker_id,
                                last_heartbeat_age.as_secs()
                            );

                            let warning_msg = WebSocketMessage::WorkerWarning {
                                timestamp: chrono::Utc::now(),
                                worker_id: worker_id.clone(),
                                warning_type: "heartbeat_delay".to_string(),
                                message: format!(
                                    "Heartbeat delayed {}s",
                                    last_heartbeat_age.as_secs()
                                ),
                            };
                            let _ = message_sender.send(warning_msg);
                        }
                    }
                }

                // Small delay to prevent tight loop
                sleep(Duration::from_millis(100)).await;
            }
        });
    }

    async fn start_status_broadcaster(&self) {
        let workers = Arc::clone(&self.workers);
        let test_status = Arc::clone(&self.test_status);
        let message_sender = self.message_sender.clone();
        let coordinator_id = self.config.coordinator_id.clone();

        tokio::spawn(async move {
            let mut interval = interval(Duration::from_secs(5)); // Broadcast every 5 seconds

            loop {
                interval.tick().await;

                let workers_guard = workers.read().await;
                let connected_worker_ids: Vec<String> = workers_guard.keys().cloned().collect();
                let status = test_status.read().await.clone();
                drop(workers_guard);

                let status_msg = WebSocketMessage::CoordinatorStatus {
                    timestamp: Utc::now(),
                    coordinator_id: coordinator_id.clone(),
                    connected_workers: connected_worker_ids,
                    test_status: status,
                };

                let _ = message_sender.send(status_msg);
            }
        });
    }

    pub async fn start_distributed_test(&self, test_config: DistributedTestConfig) -> Result<()> {
        let mut status_guard = self.test_status.write().await;
        if !matches!(*status_guard, CoordinatorTestStatus::Idle) {
            return Err(anyhow::anyhow!("Another test is already running"));
        }

        *status_guard = CoordinatorTestStatus::Preparing;
        drop(status_guard);

        let mut test_id_guard = self.current_test_id.write().await;
        *test_id_guard = Some(test_config.test_id.clone());
        drop(test_id_guard);

        // Get available workers
        let workers_guard = self.workers.read().await;
        let available_workers: Vec<String> = workers_guard
            .iter()
            .filter(|(_, worker)| matches!(worker.status, WorkerStatus::Idle))
            .map(|(id, _)| id.clone())
            .collect();
        drop(workers_guard);

        if available_workers.is_empty() {
            let mut status_guard = self.test_status.write().await;
            *status_guard = CoordinatorTestStatus::Failed;
            return Err(anyhow::anyhow!("No available workers for testing"));
        }

        // Check if synchronized start is enabled
        if test_config.coordination_settings.synchronized_start {
            self.start_synchronized_test(test_config, available_workers)
                .await
        } else {
            self.start_unsynchronized_test(test_config, available_workers)
                .await
        }
    }

    async fn start_unsynchronized_test(
        &self,
        test_config: DistributedTestConfig,
        available_workers: Vec<String>,
    ) -> Result<()> {
        let command_id = Uuid::new_v4().to_string();
        let test_command = WebSocketMessage::TestCommand {
            timestamp: Utc::now(),
            command_id,
            command_type: TestCommandType::Start,
            test_config,
            target_workers: available_workers,
        };

        self.message_sender.send(test_command)?;

        let mut status_guard = self.test_status.write().await;
        *status_guard = CoordinatorTestStatus::Running;

        println!("Distributed test started successfully (unsynchronized)");
        Ok(())
    }

    async fn start_synchronized_test(
        &self,
        test_config: DistributedTestConfig,
        available_workers: Vec<String>,
    ) -> Result<()> {
        println!(
            "Starting synchronized test with {} workers",
            available_workers.len()
        );

        // Initialize sync tracker
        let sync_timeout = Duration::from_secs(test_config.coordination_settings.sync_timeout_secs);
        let mut sync_tracker_guard = self.sync_tracker.write().await;
        *sync_tracker_guard = Some(SyncTracker {
            test_id: test_config.test_id.clone(),
            sync_state: SyncState::Preparing,
            target_workers: available_workers.clone(),
            ready_workers: HashMap::new(),
            coordination_settings: test_config.coordination_settings.clone(),
            sync_start_time: Instant::now(),
            sync_timeout,
        });
        drop(sync_tracker_guard);

        // Send prepare message to all target workers
        let prepare_message = WebSocketMessage::SyncPrepare {
            timestamp: Utc::now(),
            test_id: test_config.test_id.clone(),
            coordinator_id: self.config.coordinator_id.clone(),
            target_workers: available_workers.clone(),
            sync_timeout_secs: test_config.coordination_settings.sync_timeout_secs,
        };

        self.message_sender.send(prepare_message)?;

        // Send test command to workers (they will wait for sync start signal)
        let command_id = Uuid::new_v4().to_string();
        let test_command = WebSocketMessage::TestCommand {
            timestamp: Utc::now(),
            command_id,
            command_type: TestCommandType::Start,
            test_config,
            target_workers: available_workers,
        };

        self.message_sender.send(test_command)?;

        // Start sync monitoring task
        self.start_sync_monitor().await;

        println!("Synchronized test preparation initiated");
        Ok(())
    }

    async fn start_sync_monitor(&self) {
        let sync_tracker = Arc::clone(&self.sync_tracker);
        let message_sender = self.message_sender.clone();
        let test_status = Arc::clone(&self.test_status);
        let coordinator_id = self.config.coordinator_id.clone();

        tokio::spawn(async move {
            loop {
                sleep(Duration::from_millis(500)).await;

                let sync_guard = sync_tracker.read().await;
                if let Some(ref tracker) = *sync_guard {
                    let elapsed = tracker.sync_start_time.elapsed();

                    // Check for timeout
                    if elapsed > tracker.sync_timeout {
                        let failed_workers: Vec<String> = tracker
                            .target_workers
                            .iter()
                            .filter(|w| !tracker.ready_workers.contains_key(*w))
                            .cloned()
                            .collect();

                        let timeout_message = WebSocketMessage::SyncTimeout {
                            timestamp: Utc::now(),
                            test_id: tracker.test_id.clone(),
                            coordinator_id: coordinator_id.clone(),
                            timeout_reason: format!("Sync timeout after {}s", elapsed.as_secs()),
                            failed_workers,
                        };

                        let _ = message_sender.send(timeout_message);

                        // Update test status to failed
                        let mut status_guard = test_status.write().await;
                        *status_guard = CoordinatorTestStatus::Failed;

                        println!("❌ Synchronization timeout after {}s", elapsed.as_secs());
                        break;
                    }

                    // Check if all workers are ready
                    if tracker.ready_workers.len() == tracker.target_workers.len()
                        && matches!(tracker.sync_state, SyncState::Preparing)
                    {
                        let start_timestamp = Utc::now();
                        let start_message = WebSocketMessage::SyncStart {
                            timestamp: start_timestamp,
                            test_id: tracker.test_id.clone(),
                            coordinator_id: coordinator_id.clone(),
                            target_workers: tracker.target_workers.clone(),
                            start_timestamp,
                        };

                        if message_sender.send(start_message).is_ok() {
                            // Update test status to running
                            let mut status_guard = test_status.write().await;
                            *status_guard = CoordinatorTestStatus::Running;

                            println!("✅ All workers ready - synchronous test started!");
                            break;
                        }
                    }
                } else {
                    // No sync tracker, exit
                    break;
                }
            }
        });
    }

    pub async fn stop_distributed_test(&self) -> Result<()> {
        let mut status_guard = self.test_status.write().await;
        if !matches!(*status_guard, CoordinatorTestStatus::Running) {
            return Err(anyhow::anyhow!("No test is currently running"));
        }

        *status_guard = CoordinatorTestStatus::Stopping;
        drop(status_guard);

        // Check if we should stop synchronously
        let sync_guard = self.sync_tracker.read().await;
        let should_sync_stop = if let Some(ref tracker) = *sync_guard {
            tracker.coordination_settings.synchronized_stop
        } else {
            false
        };
        drop(sync_guard);

        if should_sync_stop {
            self.stop_synchronized_test().await
        } else {
            self.stop_unsynchronized_test().await
        }
    }

    async fn stop_unsynchronized_test(&self) -> Result<()> {
        let command_id = Uuid::new_v4().to_string();
        let stop_command = WebSocketMessage::TestCommand {
            timestamp: Utc::now(),
            command_id,
            command_type: TestCommandType::Stop,
            test_config: DistributedTestConfig {
                test_id: "stop".to_string(),
                base_config: crate::integrations::websocket::TestConfig {
                    url: "".to_string(),
                    concurrent_requests: 0,
                    rps: None,
                    duration_secs: 0,
                    total_requests: None,
                    method: "GET".to_string(),
                    user_agent_mode: "default".to_string(),
                },
                worker_assignments: Vec::new(),
                coordination_settings: CoordinationSettings {
                    synchronized_start: false,
                    synchronized_stop: false,
                    sync_timeout_secs: 30,
                    max_sync_wait_secs: 60,
                    heartbeat_interval_secs: 10,
                    metrics_reporting_interval_secs: 5,
                    timeout_secs: 300,
                },
            },
            target_workers: Vec::new(), // All workers
        };

        self.message_sender.send(stop_command)?;

        let mut status_guard = self.test_status.write().await;
        *status_guard = CoordinatorTestStatus::Completed;

        println!("Stop command sent to all workers (unsynchronized)");
        Ok(())
    }

    async fn stop_synchronized_test(&self) -> Result<()> {
        let sync_guard = self.sync_tracker.read().await;
        let (test_id, target_workers) = if let Some(ref tracker) = *sync_guard {
            (tracker.test_id.clone(), tracker.target_workers.clone())
        } else {
            return Err(anyhow::anyhow!("No active synchronized test to stop"));
        };
        drop(sync_guard);

        println!(
            "Stopping synchronized test with {} workers",
            target_workers.len()
        );

        // Send synchronized stop message
        let stop_timestamp = Utc::now();
        let sync_stop_message = WebSocketMessage::SyncStop {
            timestamp: stop_timestamp,
            test_id: test_id.clone(),
            coordinator_id: self.config.coordinator_id.clone(),
            target_workers: target_workers.clone(),
            stop_timestamp,
        };

        self.message_sender.send(sync_stop_message)?;

        // Also send regular stop command
        let command_id = Uuid::new_v4().to_string();
        let stop_command = WebSocketMessage::TestCommand {
            timestamp: Utc::now(),
            command_id,
            command_type: TestCommandType::Stop,
            test_config: DistributedTestConfig {
                test_id: test_id.clone(),
                base_config: crate::integrations::websocket::TestConfig {
                    url: "".to_string(),
                    concurrent_requests: 0,
                    rps: None,
                    duration_secs: 0,
                    total_requests: None,
                    method: "GET".to_string(),
                    user_agent_mode: "default".to_string(),
                },
                worker_assignments: Vec::new(),
                coordination_settings: CoordinationSettings {
                    synchronized_start: false,
                    synchronized_stop: true,
                    sync_timeout_secs: 30,
                    max_sync_wait_secs: 60,
                    heartbeat_interval_secs: 10,
                    metrics_reporting_interval_secs: 5,
                    timeout_secs: 300,
                },
            },
            target_workers,
        };

        self.message_sender.send(stop_command)?;

        // Clean up sync tracker
        let mut sync_guard = self.sync_tracker.write().await;
        *sync_guard = None;

        let mut status_guard = self.test_status.write().await;
        *status_guard = CoordinatorTestStatus::Completed;

        println!("✅ Synchronized stop initiated for all workers");
        Ok(())
    }

    pub async fn get_connected_workers(&self) -> Vec<ConnectedWorker> {
        let workers_guard = self.workers.read().await;
        workers_guard.values().cloned().collect()
    }

    pub async fn get_test_status(&self) -> CoordinatorTestStatus {
        self.test_status.read().await.clone()
    }

    pub fn get_message_sender(&self) -> broadcast::Sender<WebSocketMessage> {
        self.message_sender.clone()
    }

    pub fn get_actual_port(&self) -> Option<u16> {
        self.actual_port
    }

    /// Get the distributed stats collector for external access
    pub fn get_distributed_stats(&self) -> Arc<DistributedStatsCollector> {
        Arc::clone(&self.distributed_stats)
    }

    /// Calculate optimal load distribution based on current worker state
    pub async fn calculate_load_distribution(
        &self,
        total_concurrent: usize,
        total_rps: Option<u64>,
    ) -> Result<LoadDistribution> {
        let workers = self.distributed_stats.get_all_workers().await;
        let load_balancer = self.load_balancer.read().await;
        let distribution =
            load_balancer.calculate_distribution(&workers, total_concurrent, total_rps);

        // Store the current distribution
        {
            let mut current_dist = self.current_distribution.write().await;
            *current_dist = Some(distribution.clone());
        }

        Ok(distribution)
    }

    /// Apply load distribution by sending worker assignments
    pub async fn apply_load_distribution(&self, distribution: &LoadDistribution) -> Result<()> {
        let mut successful_assignments = 0;
        let total_assignments = distribution.worker_allocations.len();

        for (worker_id, allocation) in &distribution.worker_allocations {
            // Create a distributed test config for this worker's allocation
            let worker_test_config = DistributedTestConfig {
                test_id: format!("load-balance-{}", chrono::Utc::now().timestamp()),
                base_config: crate::integrations::websocket::TestConfig {
                    url: "load-balanced".to_string(),
                    concurrent_requests: allocation.concurrent_requests,
                    rps: allocation.rps_limit,
                    duration_secs: 0,
                    total_requests: None,
                    method: "GET".to_string(),
                    user_agent_mode: "default".to_string(),
                },
                worker_assignments: vec![crate::integrations::websocket::WorkerAssignment {
                    worker_id: worker_id.clone(),
                    concurrent_requests: allocation.concurrent_requests,
                    rps: allocation.rps_limit,
                    duration_secs: None,
                    start_delay_secs: 0.0,
                }],
                coordination_settings: CoordinationSettings {
                    synchronized_start: false,
                    synchronized_stop: false,
                    sync_timeout_secs: 30,
                    max_sync_wait_secs: 60,
                    heartbeat_interval_secs: 10,
                    metrics_reporting_interval_secs: 5,
                    timeout_secs: 300,
                },
            };

            // Send load balancing command to specific worker
            let command_id = uuid::Uuid::new_v4().to_string();
            let load_balance_command = WebSocketMessage::TestCommand {
                timestamp: Utc::now(),
                command_id,
                command_type: TestCommandType::Start,
                test_config: worker_test_config,
                target_workers: vec![worker_id.clone()],
            };

            match self.message_sender.send(load_balance_command) {
                Ok(_) => {
                    successful_assignments += 1;
                    println!(
                        "Applied load allocation to {}: {} concurrent, {:?} RPS",
                        worker_id, allocation.concurrent_requests, allocation.rps_limit
                    );
                }
                Err(e) => {
                    eprintln!("Failed to send load allocation to {}: {}", worker_id, e);
                }
            }
        }

        if successful_assignments == total_assignments {
            println!(
                "✅ Load distribution applied successfully to all {} workers",
                total_assignments
            );
            Ok(())
        } else {
            Err(anyhow::anyhow!(
                "Only {}/{} worker allocations were applied successfully",
                successful_assignments,
                total_assignments
            ))
        }
    }

    /// Check if rebalancing is needed and perform it if necessary
    pub async fn check_and_rebalance(&self) -> Result<bool> {
        let current_distribution = self.current_distribution.read().await;
        let workers = self.distributed_stats.get_all_workers().await;
        let load_balancer = self.load_balancer.read().await;

        if let Some(ref distribution) = *current_distribution {
            if load_balancer.should_rebalance(distribution, &workers) {
                drop(current_distribution);
                drop(load_balancer);

                println!("🔄 Rebalancing load distribution...");

                // Recalculate distribution with current parameters
                // In a real implementation, these would come from the active test configuration
                let new_distribution = self.calculate_load_distribution(100, Some(200)).await?;
                self.apply_load_distribution(&new_distribution).await?;

                println!("✅ Load rebalancing completed");
                return Ok(true);
            }
        }

        Ok(false)
    }

    /// Get current load distribution
    pub async fn get_current_distribution(&self) -> Option<LoadDistribution> {
        self.current_distribution.read().await.clone()
    }

    /// Update load balancer configuration
    pub async fn update_load_balancer_config(&self, config: LoadDistributionConfig) {
        let mut load_balancer = self.load_balancer.write().await;
        load_balancer.update_config(config);
        println!("Updated load balancer configuration");
    }

    /// Get load balancer configuration
    pub async fn get_load_balancer_config(&self) -> LoadDistributionConfig {
        let load_balancer = self.load_balancer.read().await;
        load_balancer.get_config().clone()
    }

    pub async fn get_load_balancer(&self) -> Arc<RwLock<LoadDistributionEngine>> {
        Arc::clone(&self.load_balancer)
    }
}

async fn handle_worker_connection(
    stream: TcpStream,
    addr: SocketAddr,
    context: WorkerConnectionContext,
) {
    let ws_stream = match accept_async(stream).await {
        Ok(ws_stream) => ws_stream,
        Err(e) => {
            eprintln!("WebSocket connection error from {}: {}", addr, e);
            return;
        }
    };

    println!("WebSocket connection established with {}", addr);

    let (mut ws_sender, mut ws_receiver) = ws_stream.split();
    let mut message_receiver = context.message_sender.subscribe();

    // Handle incoming messages from worker
    let workers_clone = Arc::clone(&context.workers);
    let message_sender_clone = context.message_sender.clone();
    let coordinator_id_clone = context.coordinator_id.clone();
    let distributed_stats_clone = Arc::clone(&context.distributed_stats);
    let sync_tracker_clone = Arc::clone(&context.sync_tracker);
    let max_workers = context.max_workers;

    let receive_task = tokio::spawn(async move {
        while let Some(msg) = ws_receiver.next().await {
            match msg {
                Ok(tokio_tungstenite::tungstenite::protocol::Message::Text(text)) => {
                    if let Ok(message) = serde_json::from_str::<WebSocketMessage>(&text) {
                        handle_worker_message(
                            message,
                            &workers_clone,
                            &message_sender_clone,
                            &coordinator_id_clone,
                            max_workers,
                            &distributed_stats_clone,
                            &sync_tracker_clone,
                        )
                        .await;
                    }
                }
                Ok(tokio_tungstenite::tungstenite::protocol::Message::Close(_)) => {
                    println!("Worker connection closed: {}", addr);
                    break;
                }
                Err(e) => {
                    eprintln!("WebSocket error from {}: {}", addr, e);
                    break;
                }
                _ => {}
            }
        }
    });

    // Handle outgoing messages to worker
    let send_task = tokio::spawn(async move {
        while let Ok(message) = message_receiver.recv().await {
            let json = match serde_json::to_string(&message) {
                Ok(json) => json,
                Err(e) => {
                    eprintln!("Failed to serialize message: {}", e);
                    continue;
                }
            };

            if let Err(e) = ws_sender
                .send(tokio_tungstenite::tungstenite::protocol::Message::Text(
                    json.into(),
                ))
                .await
            {
                eprintln!("Failed to send message to worker: {}", e);
                break;
            }
        }
    });

    // Wait for either task to complete
    tokio::select! {
        _ = receive_task => {},
        _ = send_task => {},
    }

    println!("Worker connection handler finished for {}", addr);
}

async fn handle_worker_message(
    message: WebSocketMessage,
    workers: &Arc<RwLock<HashMap<String, ConnectedWorker>>>,
    message_sender: &broadcast::Sender<WebSocketMessage>,
    coordinator_id: &str,
    max_workers: usize,
    distributed_stats: &Arc<DistributedStatsCollector>,
    sync_tracker: &Arc<RwLock<Option<SyncTracker>>>,
) {
    match message {
        WebSocketMessage::WorkerJoinRequest {
            worker_id,
            worker_info,
            ..
        } => {
            let mut workers_guard = workers.write().await;

            let (accepted, message_text) = if workers_guard.len() >= max_workers {
                (false, "Maximum number of workers reached".to_string())
            } else if workers_guard.contains_key(&worker_id) {
                (false, "Worker ID already exists".to_string())
            } else {
                let connection_time = Instant::now();
                workers_guard.insert(
                    worker_id.clone(),
                    ConnectedWorker {
                        worker_id: worker_id.clone(),
                        worker_info,
                        status: WorkerStatus::Idle,
                        last_heartbeat: connection_time,
                        current_load: WorkerLoad {
                            current_rps: 0.0,
                            active_connections: 0,
                            memory_usage_mb: 0,
                            cpu_usage_percent: 0.0,
                            total_requests_sent: 0,
                            errors_count: 0,
                        },
                        connection_time,
                    },
                );

                // Add worker to distributed stats tracking
                distributed_stats
                    .add_worker(worker_id.clone(), Utc::now())
                    .await;

                (true, "Worker successfully joined".to_string())
            };

            let response = WebSocketMessage::WorkerJoinResponse {
                timestamp: Utc::now(),
                worker_id,
                accepted,
                coordinator_id: coordinator_id.to_string(),
                message: message_text,
            };

            let _ = message_sender.send(response);
        }
        WebSocketMessage::WorkerHeartbeat {
            worker_id,
            status,
            current_load,
            ..
        } => {
            let mut workers_guard = workers.write().await;
            if let Some(worker) = workers_guard.get_mut(&worker_id) {
                worker.last_heartbeat = Instant::now();
                worker.status = status;
                worker.current_load = current_load;
            }
        }
        WebSocketMessage::TestCommandResponse { .. } => {
            // Forward the response to all listeners
            let _ = message_sender.send(message);
        }
        WebSocketMessage::WorkerMetrics {
            ref worker_id,
            ref metrics,
            ref worker_load,
            ..
        } => {
            // Update distributed stats with worker metrics
            distributed_stats
                .update_worker_metrics(
                    worker_id.clone(),
                    metrics.clone(),
                    worker_load.clone(),
                    "Running".to_string(),
                )
                .await;

            // Forward the metrics to all listeners
            let _ = message_sender.send(message);
        }
        WebSocketMessage::SyncReady {
            test_id,
            worker_id,
            ready_for_start,
            preparation_time_ms,
            ..
        } => {
            let mut sync_guard = sync_tracker.write().await;
            if let Some(ref mut tracker) = *sync_guard {
                if tracker.test_id == test_id && tracker.target_workers.contains(&worker_id) {
                    if ready_for_start {
                        tracker
                            .ready_workers
                            .insert(worker_id.clone(), Instant::now());
                        println!(
                            "✅ Worker {} ready for sync start ({}ms preparation time)",
                            worker_id, preparation_time_ms
                        );

                        // Check if all workers are now ready
                        if tracker.ready_workers.len() == tracker.target_workers.len() {
                            println!(
                                "🚀 All {} workers ready for synchronized start!",
                                tracker.target_workers.len()
                            );
                        }
                    } else {
                        // Worker reported not ready
                        println!("❌ Worker {} not ready for sync start", worker_id);

                        let status_message = WebSocketMessage::SyncStatus {
                            timestamp: Utc::now(),
                            test_id: test_id.clone(),
                            worker_id: worker_id.clone(),
                            sync_state: SyncState::Failed,
                            message: "Worker preparation failed".to_string(),
                        };
                        let _ = message_sender.send(status_message);
                    }
                }
            }
        }
        WebSocketMessage::SyncStatus {
            ref test_id,
            ref worker_id,
            ref sync_state,
            message: ref status_message,
            ..
        } => {
            println!(
                "📊 Sync status from {}: {:?} - {}",
                worker_id, sync_state, status_message
            );

            // Update sync tracker if applicable
            let mut sync_guard = sync_tracker.write().await;
            if let Some(ref mut tracker) = *sync_guard {
                if tracker.test_id == *test_id && tracker.target_workers.contains(worker_id) {
                    match sync_state {
                        SyncState::Ready => {
                            // Worker is ready for synchronized start
                            tracker
                                .ready_workers
                                .insert(worker_id.clone(), Instant::now());
                            println!("✅ Worker {} ready for synchronized start", worker_id);

                            // Check if all workers are now ready
                            if tracker.ready_workers.len() == tracker.target_workers.len() {
                                println!(
                                    "🚀 All {} workers ready for synchronized start!",
                                    tracker.target_workers.len()
                                );
                            }
                        }
                        SyncState::Running => {
                            println!("✅ Worker {} has started synchronized test", worker_id);
                        }
                        SyncState::Idle => {
                            println!("✅ Worker {} has stopped synchronized test", worker_id);
                        }
                        SyncState::Failed => {
                            println!("❌ Worker {} reported synchronization failure", worker_id);
                        }
                        _ => {
                            println!("📋 Worker {} sync state: {:?}", worker_id, sync_state);
                        }
                    }
                }
            }

            // Forward the sync status to all listeners
            let _ = message_sender.send(message);
        }
        _ => {
            // Handle other message types as needed
        }
    }
}