-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
70 lines (58 loc) · 2.46 KB
/
stats.py
File metadata and controls
70 lines (58 loc) · 2.46 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
"""Statistics tracking for DDoS testing"""
import threading
import time
from datetime import datetime
class StatsTracker:
"""Thread-safe statistics tracker"""
def __init__(self):
self.lock = threading.Lock()
self.packets_sent = 0
self.bytes_sent = 0
self.start_time = time.time()
self.last_display = time.time()
self.last_packets = 0
self.last_bytes = 0
def increment_packets(self):
"""Increment packet counter"""
with self.lock:
self.packets_sent += 1
def add_bytes(self, bytes_count):
"""Add to bytes counter"""
with self.lock:
self.bytes_sent += bytes_count
def display_stats(self):
"""Display real-time statistics"""
with self.lock:
current_time = time.time()
elapsed = current_time - self.start_time
# Calculate rates
packets_per_sec = self.packets_sent / elapsed if elapsed > 0 else 0
bytes_per_sec = self.bytes_sent / elapsed if elapsed > 0 else 0
# Calculate bandwidth in MB/s
mbps = (bytes_per_sec * 8) / (1024 * 1024)
# Current rates (last second)
time_diff = current_time - self.last_display
if time_diff >= 1:
current_pps = (self.packets_sent - self.last_packets) / time_diff
current_bps = (self.bytes_sent - self.last_bytes) / time_diff
current_mbps = (current_bps * 8) / (1024 * 1024)
self.last_packets = self.packets_sent
self.last_bytes = self.bytes_sent
self.last_display = current_time
else:
current_pps = packets_per_sec
current_mbps = mbps
# Display stats
print(f"\r[{datetime.now().strftime('%H:%M:%S')}] "
f"Packets: {self.packets_sent:,} | "
f"PPS: {current_pps:.1f} | "
f"Bandwidth: {current_mbps:.2f} Mbps | "
f"Elapsed: {elapsed:.1f}s", end="", flush=True)
def get_total_stats(self):
"""Get total statistics"""
with self.lock:
return {
'packets': self.packets_sent,
'bytes': self.bytes_sent,
'duration': time.time() - self.start_time
}