-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_metrics_snapshot.py
More file actions
1286 lines (1072 loc) · 59.2 KB
/
Copy pathplot_metrics_snapshot.py
File metadata and controls
1286 lines (1072 loc) · 59.2 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
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
#!/usr/bin/env python3
"""
Standalone Metrics Snapshot Visualizer
======================================
Generate visualizations from saved metrics snapshot JSON files.
"""
import json
import os
import sys
import argparse
from datetime import datetime
from typing import Dict, Any
# Core libraries
import pandas as pd
import numpy as np
# Set matplotlib backend before importing pyplot to avoid GUI issues
import matplotlib
matplotlib.use('Agg') # Use non-interactive backend
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
class SnapshotVisualizer:
"""Create visualizations from metrics snapshot JSON files"""
def __init__(self, json_file: str):
self.json_file = json_file
with open(json_file, 'r') as f:
self.data = json.load(f)
# Convert timestamps
self.timestamps = [datetime.fromisoformat(ts) for ts in self.data['timestamps']]
# Ensure we're using non-interactive backend
matplotlib.use('Agg')
plt.ioff()
logger.info(f"Loaded metrics snapshot with {len(self.timestamps)} data points")
def create_comprehensive_dashboard(self, output_dir: str = None):
"""Create enhanced dashboard visualization with console errors"""
if output_dir is None:
output_dir = os.path.dirname(self.json_file)
os.makedirs(output_dir, exist_ok=True)
try:
fig, axes = plt.subplots(3, 3, figsize=(20, 15))
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
fig.suptitle(f'KubeBrowse Comprehensive Performance Dashboard - {timestamp}', fontsize=16, fontweight='bold')
# Node CPU Usage
self._plot_node_cpu_usage(axes[0, 0])
# Node Memory Usage
self._plot_node_memory_usage(axes[0, 1])
# Running Pods
self._plot_running_pods(axes[0, 2])
# API Active Sessions
self._plot_api_sessions(axes[1, 0])
# Session Summary
self._plot_session_summary(axes[1, 1])
# Response Times
self._plot_response_times(axes[1, 2])
# HPA Replica Counts
self._plot_hpa_replicas(axes[2, 0])
# API vs Browser Sessions Correlation
self._plot_sessions_correlation(axes[2, 1])
# Success Rate Over Time
self._plot_success_rate(axes[2, 2])
plt.tight_layout()
# Save the dashboard
dashboard_file = f"{output_dir}/comprehensive_dashboard.png"
plt.savefig(dashboard_file, dpi=300, bbox_inches='tight', facecolor='white', edgecolor='none')
logger.info(f"Dashboard saved to {dashboard_file}")
# Properly close the figure to free memory
plt.close(fig)
plt.clf()
except Exception as plot_error:
logger.error(f"Error creating dashboard plot: {plot_error}")
try:
plt.close('all')
plt.clf()
except:
pass
def _plot_node_cpu_usage(self, ax):
"""Plot node CPU usage"""
ax.set_title('Node CPU Usage (%)')
ax.set_xlabel('Time Points')
ax.set_ylabel('CPU %')
ax.grid(True, alpha=0.3)
if self.data['node_metrics']:
total_timestamps = len(self.timestamps)
for node_name, metrics in self.data['node_metrics'].items():
if metrics['cpu_percent']:
# Ensure data length matches timestamps
cpu_data = metrics['cpu_percent']
data_length = min(len(cpu_data), total_timestamps)
time_points = list(range(data_length))
ax.plot(time_points, cpu_data[:data_length],
label=f'{node_name}', marker='o', markersize=3)
ax.legend()
def _plot_node_memory_usage(self, ax):
"""Plot node memory usage"""
ax.set_title('Node Memory Usage (%)')
ax.set_xlabel('Time Points')
ax.set_ylabel('Memory %')
ax.grid(True, alpha=0.3)
if self.data['node_metrics']:
total_timestamps = len(self.timestamps)
for node_name, metrics in self.data['node_metrics'].items():
if metrics['memory_percent']:
# Ensure data length matches timestamps
memory_data = metrics['memory_percent']
data_length = min(len(memory_data), total_timestamps)
time_points = list(range(data_length))
ax.plot(time_points, memory_data[:data_length],
label=f'{node_name}', marker='s', markersize=3)
ax.legend()
def _plot_running_pods(self, ax):
"""Plot running pods"""
ax.set_title('Running Pods')
ax.set_xlabel('Time Points')
ax.set_ylabel('Pod Count')
ax.grid(True, alpha=0.3)
if self.data['pod_counts']:
# Ensure pod_counts length matches timestamps
total_timestamps = len(self.timestamps)
pod_data = self.data['pod_counts']
data_length = min(len(pod_data), total_timestamps)
time_points = list(range(data_length))
ax.plot(time_points, pod_data[:data_length],
color='blue', marker='o', markersize=4)
ax.fill_between(time_points, pod_data[:data_length], alpha=0.3)
def _plot_api_sessions(self, ax):
"""Plot API active sessions"""
ax.set_title('API Active Sessions')
ax.set_xlabel('Time Points')
ax.set_ylabel('Active Sessions')
ax.grid(True, alpha=0.3)
if self.data['api_sessions']:
total_timestamps = len(self.timestamps)
api_data = self.data['api_sessions']
data_length = min(len(api_data), total_timestamps)
time_points = list(range(data_length))
active_sessions = [api_data[i].get('active_sessions', 0) for i in range(data_length)]
total_connections = [api_data[i].get('total_connections', 0) for i in range(data_length)]
ax.plot(time_points, active_sessions,
color='green', marker='o', markersize=4, label='Active Sessions')
ax.plot(time_points, total_connections,
color='orange', marker='s', markersize=4, label='Total Connections')
ax.legend()
ax.fill_between(time_points, active_sessions, alpha=0.3, color='green')
def _plot_session_summary(self, ax):
"""Plot session summary"""
ax.set_title('Session Summary')
ax.set_xlabel('Status')
ax.set_ylabel('Count')
ax.grid(True, alpha=0.3)
if self.data['session_metrics']:
total_sessions = len(self.data['session_metrics'])
successful_sessions = sum(1 for s in self.data['session_metrics']
if s.get('failed_api_calls', 0) == 0)
failed_sessions = total_sessions - successful_sessions
ax.bar(['Successful', 'Failed'],
[successful_sessions, failed_sessions],
color=['green', 'red'], alpha=0.7)
def _plot_response_times(self, ax):
"""Plot response times"""
ax.set_title('Recent Response Times')
ax.set_xlabel('Recent Sessions')
ax.set_ylabel('Response Time (s)')
ax.grid(True, alpha=0.3)
if self.data['session_metrics']:
recent_sessions = self.data['session_metrics'][-20:]
response_times = [s.get('first_click_response_time', 0)
for s in recent_sessions
if s.get('first_click_response_time') is not None]
if response_times:
ax.plot(range(len(response_times)), response_times,
'go-', markersize=4)
ax.axhline(y=np.mean(response_times),
color='red', linestyle='--',
label=f'Avg: {np.mean(response_times):.2f}s')
ax.legend()
def _plot_hpa_replicas(self, ax):
"""Plot HPA replica counts"""
ax.set_title('HPA Replica Counts')
ax.set_xlabel('Time Points')
ax.set_ylabel('Replicas')
ax.grid(True, alpha=0.3)
if self.data['hpa_metrics']:
total_timestamps = len(self.timestamps)
for hpa_name, metrics in self.data['hpa_metrics'].items():
if metrics['current_replicas']:
# Ensure data length matches timestamps
current_data = metrics['current_replicas']
desired_data = metrics['desired_replicas']
data_length = min(len(current_data), len(desired_data), total_timestamps)
time_points = list(range(data_length))
ax.plot(time_points, current_data[:data_length],
label=f'{hpa_name} Current', marker='o', markersize=3)
ax.plot(time_points, desired_data[:data_length],
label=f'{hpa_name} Desired', marker='s', markersize=3, linestyle='--')
ax.legend()
def _plot_sessions_correlation(self, ax):
"""Plot API vs Browser sessions correlation"""
ax.set_title('API vs Browser Sessions')
ax.set_xlabel('Time Points')
ax.set_ylabel('Session Count')
ax.grid(True, alpha=0.3)
if self.data['api_sessions'] and self.data['session_metrics']:
total_timestamps = len(self.timestamps)
api_data = self.data['api_sessions']
data_length = min(len(api_data), total_timestamps)
time_points = list(range(data_length))
api_sessions = [api_data[i].get('active_sessions', 0) for i in range(data_length)]
# Calculate browser sessions over time (cumulative)
browser_sessions_count = []
for i in range(data_length):
if i < len(self.timestamps):
current_time = self.timestamps[i]
active_browser_sessions = sum(1 for s in self.data['session_metrics']
if datetime.fromisoformat(s['start_time']) <= current_time and
(s.get('end_time') is None or datetime.fromisoformat(s['end_time']) >= current_time))
browser_sessions_count.append(active_browser_sessions)
else:
browser_sessions_count.append(0)
ax.plot(time_points, api_sessions,
color='green', marker='o', markersize=4, label='API Active Sessions')
ax.plot(time_points, browser_sessions_count,
color='blue', marker='s', markersize=4, label='Browser Sessions')
ax.legend()
def _plot_success_rate(self, ax):
"""Plot success rate over time"""
ax.set_title('Success Rate Over Time')
ax.set_xlabel('Time Buckets')
ax.set_ylabel('Success Rate (%)')
ax.grid(True, alpha=0.3)
ax.set_ylim(0, 105)
if self.data['session_metrics'] and len(self.data['session_metrics']) > 5:
bucket_size = max(1, len(self.data['session_metrics']) // 10)
success_rates = []
for i in range(0, len(self.data['session_metrics']), bucket_size):
bucket = self.data['session_metrics'][i:i+bucket_size]
total_calls = sum(s.get('total_api_calls', 0) for s in bucket)
failed_calls = sum(s.get('failed_api_calls', 0) for s in bucket)
if total_calls > 0:
success_rate = ((total_calls - failed_calls) / total_calls) * 100
success_rates.append(success_rate)
if success_rates:
ax.plot(range(len(success_rates)), success_rates,
'b-o', markersize=4)
ax.axhline(y=np.mean(success_rates),
color='green', linestyle='--',
label=f'Avg: {np.mean(success_rates):.1f}%')
ax.legend()
def create_individual_plots(self, output_dir: str = None):
"""Create individual detailed plots"""
if output_dir is None:
output_dir = os.path.dirname(self.json_file)
os.makedirs(output_dir, exist_ok=True)
# Node metrics plot
self._create_node_metrics_plot(output_dir)
# Pod scaling plot
self._create_pod_scaling_plot(output_dir)
# HPA metrics plot
self._create_hpa_metrics_plot(output_dir)
# Performance metrics plot
self._create_performance_metrics_plot(output_dir)
# Session analysis plot
self._create_session_analysis_plot(output_dir)
def _create_node_metrics_plot(self, output_dir: str):
"""Create detailed node metrics plot as two separate plots"""
if not self.data['node_metrics']:
return
total_timestamps = len(self.timestamps)
# First plot: CPU Usage
plt.figure(figsize=(12, 8))
plt.suptitle('Kubernetes Node CPU Usage', fontsize=16, fontweight='bold')
for node_name, metrics in self.data['node_metrics'].items():
# Ensure all metric arrays are aligned with timestamps
cpu_usage_data = metrics['cpu_usage'][:total_timestamps]
# Use the minimum length to avoid index errors
data_length = min(len(cpu_usage_data), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
# CPU Usage (Cores)
plt.plot(aligned_timestamps, cpu_usage_data[:data_length],
label=f'{node_name}', linewidth=2, marker='o', markersize=3)
# Add statistic
if cpu_usage_data:
avg_cpu_usage = np.mean(cpu_usage_data)
plt.axhline(y=avg_cpu_usage, color='green', linestyle='--',
label=f'Avg: {avg_cpu_usage:.1f} Cores')
max_cpu_usage = np.max(cpu_usage_data)
plt.text(0.02, 0.98, f'Max CPU Usage: {max_cpu_usage:.1f} Cores\nAvg CPU Usage: {avg_cpu_usage:.1f} Cores',
transform=plt.gca().transAxes, fontsize=11, verticalalignment='top',
bbox=dict(boxstyle='round', facecolor='white', alpha=0.8))
plt.title('CPU Usage (Cores)', fontweight='bold')
plt.ylabel('CPU Cores')
plt.xlabel('Time')
plt.legend()
plt.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(f'{output_dir}/node_cpu_usage.png', dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"Node CPU usage plot saved to {output_dir}/node_cpu_usage.png")
# Second plot: Memory Usage
plt.figure(figsize=(12, 8))
plt.suptitle('Kubernetes Node Memory Usage', fontsize=16, fontweight='bold')
for node_name, metrics in self.data['node_metrics'].items():
# Ensure all metric arrays are aligned with timestamps
memory_usage_data = metrics['memory_usage'][:total_timestamps]
# Use the minimum length to avoid index errors
data_length = min(len(memory_usage_data), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
# Memory Usage (GB)
plt.plot(aligned_timestamps, memory_usage_data[:data_length],
label=f'{node_name}', linewidth=2, marker='s', markersize=3)
# Add statistic
if memory_usage_data:
avg_memory_usage = np.mean(memory_usage_data)
plt.axhline(y=avg_memory_usage, color='green', linestyle='--',
label=f'Avg: {avg_memory_usage:.1f} GB')
max_memory_usage = np.max(memory_usage_data)
plt.text(0.02, 0.98, f'Max Memory Usage: {max_memory_usage:.1f} GB\nAvg Memory Usage: {avg_memory_usage:.1f} GB',
transform=plt.gca().transAxes, fontsize=11, verticalalignment='top',
bbox=dict(boxstyle='round', facecolor='white', alpha=0.8))
plt.title('Memory Usage (GB)', fontweight='bold')
plt.ylabel('Memory (GB)')
plt.xlabel('Time')
plt.legend()
plt.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(f'{output_dir}/node_memory_usage.png', dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"Node Memory usage plot saved to {output_dir}/node_memory_usage.png")
# def _create_node_metrics_plot(self, output_dir: str):
# """Create detailed node metrics plot"""
# if not self.data['node_metrics']:
# return
# fig, axes = plt.subplots(2, 2, figsize=(16, 12))
# fig.suptitle('Kubernetes Node Resource Usage', fontsize=16, fontweight='bold')
# total_timestamps = len(self.timestamps)
# for node_name, metrics in self.data['node_metrics'].items():
# # Ensure all metric arrays are aligned with timestamps
# cpu_usage_data = metrics['cpu_usage'][:total_timestamps]
# memory_usage_data = metrics['memory_usage'][:total_timestamps]
# cpu_percent_data = metrics['cpu_percent'][:total_timestamps]
# memory_percent_data = metrics['memory_percent'][:total_timestamps]
# # Use the minimum length to avoid index errors
# data_length = min(len(cpu_usage_data), len(memory_usage_data),
# len(cpu_percent_data), len(memory_percent_data), total_timestamps)
# aligned_timestamps = self.timestamps[:data_length]
# # CPU Usage (Cores)
# axes[0, 0].plot(aligned_timestamps, cpu_usage_data[:data_length],
# label=f'{node_name}', linewidth=2, marker='o', markersize=3)
# # Memory Usage (GB)
# axes[0, 1].plot(aligned_timestamps, memory_usage_data[:data_length],
# label=f'{node_name}', linewidth=2, marker='s', markersize=3)
# # CPU Usage (%)
# axes[1, 0].plot(aligned_timestamps, cpu_percent_data[:data_length],
# label=f'{node_name}', linewidth=2, marker='^', markersize=3)
# # Memory Usage (%)
# axes[1, 1].plot(aligned_timestamps, memory_percent_data[:data_length],
# label=f'{node_name}', linewidth=2, marker='d', markersize=3)
# # Customize subplots
# axes[0, 0].set_title('CPU Usage (Cores)', fontweight='bold')
# axes[0, 0].set_ylabel('CPU Cores')
# axes[0, 0].legend()
# axes[0, 0].grid(True, alpha=0.3)
# axes[0, 1].set_title('Memory Usage (GB)', fontweight='bold')
# axes[0, 1].set_ylabel('Memory (GB)')
# axes[0, 1].legend()
# axes[0, 1].grid(True, alpha=0.3)
# axes[1, 0].set_title('CPU Usage (%)', fontweight='bold')
# axes[1, 0].set_ylabel('CPU Utilization (%)')
# axes[1, 0].set_xlabel('Time')
# axes[1, 0].legend()
# axes[1, 0].grid(True, alpha=0.3)
# axes[1, 1].set_title('Memory Usage (%)', fontweight='bold')
# axes[1, 1].set_ylabel('Memory Utilization (%)')
# axes[1, 1].set_xlabel('Time')
# axes[1, 1].legend()
# axes[1, 1].grid(True, alpha=0.3)
# # Format x-axis
# for ax in axes.flat:
# ax.tick_params(axis='x', rotation=45)
# plt.tight_layout()
# plt.savefig(f'{output_dir}/node_metrics_detailed.png', dpi=300, bbox_inches='tight')
# plt.close()
# logger.info(f"Node metrics plot saved to {output_dir}/node_metrics_detailed.png")
def _create_pod_scaling_plot(self, output_dir: str):
"""Create detailed pod scaling plot"""
fig, ax = plt.subplots(figsize=(14, 8))
# Ensure pod counts are aligned with timestamps
total_timestamps = len(self.timestamps)
pod_data = self.data['pod_counts']
data_length = min(len(pod_data), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
aligned_pod_counts = pod_data[:data_length]
# Plot browser sandbox pods
ax.plot(aligned_timestamps, aligned_pod_counts,
linewidth=3, marker='o', markersize=6, color='#2E86AB',
label='Browser Sandbox Pods')
# Add fill area
ax.fill_between(aligned_timestamps, aligned_pod_counts,
alpha=0.3, color='#2E86AB')
ax.set_title('Browser Sandbox Pod Scaling Over Time',
fontsize=16, fontweight='bold', pad=20)
ax.set_xlabel('Time', fontsize=12)
ax.set_ylabel('Number of Running Pods', fontsize=12)
ax.legend(fontsize=12)
ax.grid(True, alpha=0.3)
# Add statistics
max_pods = max(aligned_pod_counts)
avg_pods = np.mean(aligned_pod_counts)
ax.axhline(y=avg_pods, color='red', linestyle='--', alpha=0.7,
label=f'Average: {avg_pods:.1f}')
ax.text(0.02, 0.98, f'Max Pods: {max_pods}\nAvg Pods: {avg_pods:.1f}',
transform=ax.transAxes, fontsize=11, verticalalignment='top',
bbox=dict(boxstyle='round', facecolor='white', alpha=0.8))
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(f'{output_dir}/pod_scaling_detailed.png', dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"Pod scaling plot saved to {output_dir}/pod_scaling_detailed.png")
# TODO: need to make changes here
# def _create_hpa_metrics_plot(self, output_dir: str):
# """Create detailed HPA metrics plot"""
# if not self.data['hpa_metrics']:
# logger.warning("No HPA metrics available")
# return
# n_hpas = len(self.data['hpa_metrics'])
# fig, axes = plt.subplots(n_hpas, 2, figsize=(16, 6 * n_hpas))
# if n_hpas == 1:
# axes = axes.reshape(1, -1)
# fig.suptitle('HPA Scaling Metrics', fontsize=16, fontweight='bold')
# total_timestamps = len(self.timestamps)
# for i, (hpa_name, metrics) in enumerate(self.data['hpa_metrics'].items()):
# # Ensure all HPA metric arrays are aligned with timestamps
# current_replicas = metrics['current_replicas']
# desired_replicas = metrics['desired_replicas']
# cpu_utilization = metrics['cpu_utilization']
# memory_utilization = metrics['memory_utilization']
# data_length = min(len(current_replicas), len(desired_replicas),
# len(cpu_utilization), len(memory_utilization), total_timestamps)
# aligned_timestamps = self.timestamps[:data_length]
# # Replica counts
# axes[i, 0].plot(aligned_timestamps, current_replicas[:data_length],
# label='Current Replicas', linewidth=2, marker='o', color='#1f77b4')
# axes[i, 0].plot(aligned_timestamps, desired_replicas[:data_length],
# label='Desired Replicas', linewidth=2, marker='s', color='#ff7f0e')
# axes[i, 0].set_title(f'{hpa_name} - Replica Scaling', fontweight='bold')
# axes[i, 0].set_ylabel('Replicas')
# axes[i, 0].legend()
# axes[i, 0].grid(True, alpha=0.3)
# # Resource utilization
# axes[i, 1].plot(aligned_timestamps, cpu_utilization[:data_length],
# label='CPU Utilization %', linewidth=2, marker='^', color='#2ca02c')
# axes[i, 1].plot(aligned_timestamps, memory_utilization[:data_length],
# label='Memory Utilization %', linewidth=2, marker='d', color='#d62728')
# axes[i, 1].axhline(y=50, color='red', linestyle='--', alpha=0.5, label='Target (50%)')
# axes[i, 1].set_title(f'{hpa_name} - Resource Utilization', fontweight='bold')
# axes[i, 1].set_ylabel('Utilization (%)')
# axes[i, 1].legend()
# axes[i, 1].grid(True, alpha=0.3)
# # Format x-axis
# axes[i, 0].tick_params(axis='x', rotation=45)
# axes[i, 1].tick_params(axis='x', rotation=45)
# plt.tight_layout()
# plt.savefig(f'{output_dir}/hpa_metrics_detailed.png', dpi=300, bbox_inches='tight')
# plt.close()
# logger.info(f"HPA metrics plot saved to {output_dir}/hpa_metrics_detailed.png")
def _create_hpa_metrics_plot(self, output_dir: str):
"""Create detailed HPA metrics plot as separate plots"""
if not self.data['hpa_metrics']:
logger.warning("No HPA metrics available")
return
total_timestamps = len(self.timestamps)
for hpa_name, metrics in self.data['hpa_metrics'].items():
# Ensure all HPA metric arrays are aligned with timestamps
current_replicas = metrics['current_replicas']
desired_replicas = metrics['desired_replicas']
cpu_utilization = metrics['cpu_utilization']
memory_utilization = metrics['memory_utilization']
data_length = min(len(current_replicas), len(desired_replicas),
len(cpu_utilization), len(memory_utilization), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
# First plot: Replica Scaling
plt.figure(figsize=(12, 8))
plt.suptitle(f'HPA Replica Scaling - {hpa_name}', fontsize=16, fontweight='bold')
plt.plot(aligned_timestamps, current_replicas[:data_length],
label='Current Replicas', linewidth=2, marker='o', color='#1f77b4')
plt.plot(aligned_timestamps, desired_replicas[:data_length],
label='Desired Replicas', linewidth=2, marker='s', color='#ff7f0e')
plt.title(f'{hpa_name} - Replica Scaling', fontweight='bold')
plt.ylabel('Replicas')
plt.xlabel('Time')
plt.legend()
plt.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
# Save replica scaling plot
safe_hpa_name = hpa_name.replace('/', '_').replace(':', '_')
plt.savefig(f'{output_dir}/hpa_replicas_{safe_hpa_name}.png', dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"HPA replica scaling plot saved to {output_dir}/hpa_replicas_{safe_hpa_name}.png")
# Second plot: Resource Utilization
plt.figure(figsize=(12, 8))
plt.suptitle(f'HPA Resource Utilization - {hpa_name}', fontsize=16, fontweight='bold')
plt.plot(aligned_timestamps, cpu_utilization[:data_length],
label='CPU Utilization %', linewidth=2, marker='^', color='#2ca02c')
plt.plot(aligned_timestamps, memory_utilization[:data_length],
label='Memory Utilization %', linewidth=2, marker='d', color='#d62728')
plt.axhline(y=50, color='red', linestyle='--', alpha=0.5, label='Target (50%)')
plt.title(f'{hpa_name} - Resource Utilization', fontweight='bold')
plt.ylabel('Utilization (%)')
plt.xlabel('Time')
plt.legend()
plt.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
# Save resource utilization plot
plt.savefig(f'{output_dir}/hpa_utilization_{safe_hpa_name}.png', dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"HPA resource utilization plot saved to {output_dir}/hpa_utilization_{safe_hpa_name}.png")
# Create a combined overview plot for all HPAs (optional)
if len(self.data['hpa_metrics']) > 1:
# Combined Replica Overview
plt.figure(figsize=(14, 8))
plt.suptitle('All HPAs - Replica Scaling Overview', fontsize=16, fontweight='bold')
for hpa_name, metrics in self.data['hpa_metrics'].items():
current_replicas = metrics['current_replicas']
data_length = min(len(current_replicas), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
plt.plot(aligned_timestamps, current_replicas[:data_length],
label=f'{hpa_name} - Current', linewidth=2, marker='o')
plt.title('Current Replicas - All HPAs', fontweight='bold')
plt.ylabel('Replicas')
plt.xlabel('Time')
plt.legend()
plt.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(f'{output_dir}/hpa_overview_replicas.png', dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"HPA overview plot saved to {output_dir}/hpa_overview_replicas.png")
# Combined Utilization Overview
plt.figure(figsize=(14, 8))
plt.suptitle('All HPAs - CPU Utilization Overview', fontsize=16, fontweight='bold')
for hpa_name, metrics in self.data['hpa_metrics'].items():
cpu_utilization = metrics['cpu_utilization']
data_length = min(len(cpu_utilization), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
plt.plot(aligned_timestamps, cpu_utilization[:data_length],
label=f'{hpa_name}', linewidth=2, marker='^')
plt.axhline(y=50, color='red', linestyle='--', alpha=0.5, label='Target (50%)')
plt.title('CPU Utilization - All HPAs', fontweight='bold')
plt.ylabel('CPU Utilization (%)')
plt.xlabel('Time')
plt.legend()
plt.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(f'{output_dir}/hpa_overview_cpu.png', dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"HPA CPU overview plot saved to {output_dir}/hpa_overview_cpu.png")
def _create_performance_metrics_plot(self, output_dir: str):
"""Create detailed performance metrics plot"""
if not self.data['session_metrics']:
logger.warning("No session metrics available")
return
sessions_df = pd.DataFrame(self.data['session_metrics'])
# Convert timestamps
sessions_df['start_time'] = pd.to_datetime(sessions_df['start_time'])
sessions_df['end_time'] = pd.to_datetime(sessions_df['end_time'])
sessions_df['duration'] = (sessions_df['end_time'] - sessions_df['start_time']).dt.total_seconds()
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
fig.suptitle('Performance Metrics Analysis', fontsize=16, fontweight='bold')
# API Response Time Distribution
valid_response_times = sessions_df[sessions_df['first_click_response_time'].notna()]
if not valid_response_times.empty:
axes[0, 0].hist(valid_response_times['first_click_response_time'],
bins=30, alpha=0.7, color='skyblue', edgecolor='black')
axes[0, 0].axvline(valid_response_times['first_click_response_time'].mean(),
color='red', linestyle='--', linewidth=2,
label=f'Mean: {valid_response_times["first_click_response_time"].mean():.2f}s')
axes[0, 0].set_title('API Response Time Distribution', fontweight='bold')
axes[0, 0].set_xlabel('Response Time (seconds)')
axes[0, 0].set_ylabel('Frequency')
axes[0, 0].legend()
axes[0, 0].grid(True, alpha=0.3)
# Session Duration Distribution
axes[0, 1].hist(sessions_df['duration'], bins=30, alpha=0.7,
color='lightgreen', edgecolor='black')
axes[0, 1].axvline(sessions_df['duration'].mean(), color='red',
linestyle='--', linewidth=2,
label=f'Mean: {sessions_df["duration"].mean():.1f}s')
axes[0, 1].set_title('Session Duration Distribution', fontweight='bold')
axes[0, 1].set_xlabel('Duration (seconds)')
axes[0, 1].set_ylabel('Frequency')
axes[0, 1].legend()
axes[0, 1].grid(True, alpha=0.3)
# Success Rate Over Time
sessions_df['success_rate'] = (sessions_df['total_api_calls'] - sessions_df['failed_api_calls']) / sessions_df['total_api_calls']
sessions_df['time_bucket'] = pd.cut(sessions_df['start_time'], bins=20)
success_by_time = sessions_df.groupby('time_bucket')['success_rate'].mean()
axes[1, 0].plot(range(len(success_by_time)), success_by_time.values * 100,
marker='o', linewidth=2, markersize=6, color='orange')
axes[1, 0].set_title('Success Rate Over Time', fontweight='bold')
axes[1, 0].set_xlabel('Time Bucket')
axes[1, 0].set_ylabel('Success Rate (%)')
axes[1, 0].grid(True, alpha=0.3)
axes[1, 0].set_ylim(0, 105)
# Error Analysis
error_counts = {}
for session in self.data['session_metrics']:
for error in session.get('errors', []):
error_type = error.split(':')[0] # Get error type
error_counts[error_type] = error_counts.get(error_type, 0) + 1
if error_counts:
axes[1, 1].bar(list(error_counts.keys()), list(error_counts.values()),
color='salmon', alpha=0.7)
axes[1, 1].set_title('Error Distribution', fontweight='bold')
axes[1, 1].set_xlabel('Error Type')
axes[1, 1].set_ylabel('Count')
axes[1, 1].tick_params(axis='x', rotation=45)
axes[1, 1].grid(True, alpha=0.3)
else:
axes[1, 1].text(0.5, 0.5, 'No Errors Recorded',
ha='center', va='center', transform=axes[1, 1].transAxes,
fontsize=14, color='green', fontweight='bold')
axes[1, 1].set_title('Error Distribution', fontweight='bold')
plt.tight_layout()
plt.savefig(f'{output_dir}/performance_metrics_detailed.png', dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"Performance metrics plot saved to {output_dir}/performance_metrics_detailed.png")
def _create_session_analysis_plot(self, output_dir: str):
"""Create detailed session analysis plot"""
if not self.data['session_metrics']:
return
sessions_df = pd.DataFrame(self.data['session_metrics'])
sessions_df['start_time'] = pd.to_datetime(sessions_df['start_time'])
sessions_df['end_time'] = pd.to_datetime(sessions_df['end_time'])
fig, axes = plt.subplots(2, 2, figsize=(16, 12))
fig.suptitle('Session Analysis Deep Dive', fontsize=16, fontweight='bold')
# Concurrent Sessions Over Time
time_range = pd.date_range(start=sessions_df['start_time'].min(),
end=sessions_df['end_time'].max(), freq='10S')
concurrent_sessions = []
for timestamp in time_range:
active = sessions_df[
(sessions_df['start_time'] <= timestamp) &
(sessions_df['end_time'] >= timestamp)
].shape[0]
concurrent_sessions.append(active)
axes[0, 0].plot(time_range, concurrent_sessions, linewidth=2, color='purple')
axes[0, 0].fill_between(time_range, concurrent_sessions, alpha=0.3, color='purple')
axes[0, 0].set_title('Concurrent Sessions Over Time', fontweight='bold')
axes[0, 0].set_xlabel('Time')
axes[0, 0].set_ylabel('Concurrent Sessions')
axes[0, 0].grid(True, alpha=0.3)
axes[0, 0].tick_params(axis='x', rotation=45)
# API Calls vs Response Time
valid_sessions = sessions_df[sessions_df['first_click_response_time'].notna()]
if not valid_sessions.empty:
scatter = axes[0, 1].scatter(valid_sessions['total_api_calls'],
valid_sessions['first_click_response_time'],
c=valid_sessions['failed_api_calls'],
cmap='Reds', alpha=0.6, s=50)
axes[0, 1].set_title('API Calls vs Response Time', fontweight='bold')
axes[0, 1].set_xlabel('Total API Calls')
axes[0, 1].set_ylabel('First Click Response Time (s)')
axes[0, 1].grid(True, alpha=0.3)
plt.colorbar(scatter, ax=axes[0, 1], label='Failed API Calls')
# Session Start Rate
sessions_df['hour'] = sessions_df['start_time'].dt.floor('5min')
session_rate = sessions_df.groupby('hour').size()
axes[1, 0].bar(range(len(session_rate)), session_rate.values,
color='teal', alpha=0.7)
axes[1, 0].set_title('Session Start Rate (5-min buckets)', fontweight='bold')
axes[1, 0].set_xlabel('Time Bucket')
axes[1, 0].set_ylabel('Sessions Started')
axes[1, 0].grid(True, alpha=0.3)
# Performance Percentiles
if not valid_sessions.empty:
percentiles = [50, 75, 90, 95, 99]
response_time_percentiles = [valid_sessions['first_click_response_time'].quantile(p/100)
for p in percentiles]
axes[1, 1].bar([f'P{p}' for p in percentiles], response_time_percentiles,
color='gold', alpha=0.7)
axes[1, 1].set_title('Response Time Percentiles', fontweight='bold')
axes[1, 1].set_xlabel('Percentile')
axes[1, 1].set_ylabel('Response Time (s)')
axes[1, 1].grid(True, alpha=0.3)
# Add values on bars
for i, v in enumerate(response_time_percentiles):
axes[1, 1].text(i, v + 0.01, f'{v:.2f}s', ha='center', va='bottom')
plt.tight_layout()
plt.savefig(f'{output_dir}/session_analysis_detailed.png', dpi=300, bbox_inches='tight')
plt.close()
logger.info(f"Session analysis plot saved to {output_dir}/session_analysis_detailed.png")
def create_interactive_dashboard(self, output_dir: str = None):
"""Create interactive Plotly dashboard with all metrics"""
if output_dir is None:
output_dir = os.path.dirname(self.json_file)
# Calculate number of subplots needed
total_timestamps = len(self.timestamps)
has_hpa = bool(self.data['hpa_metrics'])
has_sessions = bool(self.data['session_metrics'])
# Create subplot layout - 4 rows x 2 columns
fig = make_subplots(
rows=4, cols=2,
subplot_titles=('Node CPU Usage (Cores)', 'Node Memory Usage (GB)',
'Pod Scaling', 'HPA Replica Scaling' if has_hpa else 'API Active Sessions',
'Response Time Distribution' if has_sessions else 'Empty',
'Performance Percentiles' if has_sessions else 'Empty',
'Concurrent Sessions' if has_sessions else 'Empty',
'Success Rate Over Time' if has_sessions else 'Empty'),
specs=[[{"secondary_y": False}, {"secondary_y": False}],
[{"secondary_y": False}, {"secondary_y": False}],
[{"secondary_y": False}, {"secondary_y": False}],
[{"secondary_y": False}, {"secondary_y": False}]]
)
# Node CPU Usage (Cores)
if self.data['node_metrics']:
for node_name, metrics in self.data['node_metrics'].items():
cpu_usage_data = metrics['cpu_usage'][:total_timestamps]
data_length = min(len(cpu_usage_data), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
fig.add_trace(
go.Scatter(x=aligned_timestamps, y=cpu_usage_data[:data_length],
name=f'{node_name} CPU', mode='lines+markers',
hovertemplate='<b>%{fullData.name}</b><br>Time: %{x}<br>CPU: %{y:.2f} cores<extra></extra>'),
row=1, col=1
)
# Node Memory Usage (GB)
if self.data['node_metrics']:
for node_name, metrics in self.data['node_metrics'].items():
memory_usage_data = metrics['memory_usage'][:total_timestamps]
data_length = min(len(memory_usage_data), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
fig.add_trace(
go.Scatter(x=aligned_timestamps, y=memory_usage_data[:data_length],
name=f'{node_name} Memory', mode='lines+markers',
hovertemplate='<b>%{fullData.name}</b><br>Time: %{x}<br>Memory: %{y:.2f} GB<extra></extra>'),
row=1, col=2
)
# Pod Scaling
if self.data['pod_counts']:
pod_data = self.data['pod_counts']
data_length = min(len(pod_data), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
fig.add_trace(
go.Scatter(x=aligned_timestamps, y=pod_data[:data_length],
name='Browser Pods', mode='lines+markers',
fill='tozeroy', fillcolor='rgba(46, 134, 171, 0.3)',
hovertemplate='<b>Browser Pods</b><br>Time: %{x}<br>Pods: %{y}<extra></extra>'),
row=2, col=1
)
# HPA Replica Scaling or API Sessions
if has_hpa:
for hpa_name, metrics in self.data['hpa_metrics'].items():
current_replicas = metrics['current_replicas']
desired_replicas = metrics['desired_replicas']
data_length = min(len(current_replicas), len(desired_replicas), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
fig.add_trace(
go.Scatter(x=aligned_timestamps, y=current_replicas[:data_length],
name=f'{hpa_name} Current', mode='lines+markers',
hovertemplate='<b>%{fullData.name}</b><br>Time: %{x}<br>Replicas: %{y}<extra></extra>'),
row=2, col=2
)
fig.add_trace(
go.Scatter(x=aligned_timestamps, y=desired_replicas[:data_length],
name=f'{hpa_name} Desired', mode='lines+markers',
line=dict(dash='dash'),
hovertemplate='<b>%{fullData.name}</b><br>Time: %{x}<br>Replicas: %{y}<extra></extra>'),
row=2, col=2
)
break # Only show first HPA for clarity
elif self.data['api_sessions']:
api_data = self.data['api_sessions']
data_length = min(len(api_data), total_timestamps)
aligned_timestamps = self.timestamps[:data_length]
active_sessions = [api_data[i].get('active_sessions', 0) for i in range(data_length)]
total_connections = [api_data[i].get('total_connections', 0) for i in range(data_length)]
fig.add_trace(
go.Scatter(x=aligned_timestamps, y=active_sessions,
name='Active Sessions', mode='lines+markers',
hovertemplate='<b>Active Sessions</b><br>Time: %{x}<br>Sessions: %{y}<extra></extra>'),
row=2, col=2
)
fig.add_trace(
go.Scatter(x=aligned_timestamps, y=total_connections,
name='Total Connections', mode='lines+markers',
hovertemplate='<b>Total Connections</b><br>Time: %{x}<br>Connections: %{y}<extra></extra>'),
row=2, col=2
)
# Response Time Distribution and Performance Analysis
if has_sessions:
sessions_df = pd.DataFrame(self.data['session_metrics'])
sessions_df['start_time'] = pd.to_datetime(sessions_df['start_time'])
sessions_df['end_time'] = pd.to_datetime(sessions_df['end_time'])
# Response Time Distribution
valid_response_times = sessions_df[sessions_df['first_click_response_time'].notna()]
if not valid_response_times.empty:
fig.add_trace(
go.Histogram(x=valid_response_times['first_click_response_time'],
name='Response Time', nbinsx=30,
hovertemplate='<b>Response Time Distribution</b><br>Time Range: %{x}<br>Count: %{y}<extra></extra>'),
row=3, col=1
)
# Performance Percentiles
percentiles = [50, 75, 90, 95, 99]
response_time_percentiles = [valid_response_times['first_click_response_time'].quantile(p/100)
for p in percentiles]
fig.add_trace(
go.Bar(x=[f'P{p}' for p in percentiles], y=response_time_percentiles,
name='Response Time Percentiles',
hovertemplate='<b>%{x}</b><br>Response Time: %{y:.3f}s<extra></extra>'),
row=3, col=2
)
# Concurrent Sessions Over Time
time_range = pd.date_range(start=sessions_df['start_time'].min(),
end=sessions_df['end_time'].max(), freq='10S')
concurrent_sessions = []
for timestamp in time_range:
active = sessions_df[
(sessions_df['start_time'] <= timestamp) &
(sessions_df['end_time'] >= timestamp)