今天打瓦被虐成狗了,我把原因归到了网络波动上(就是不承认菜,不过网络质量确实不好,根本就是不能玩的程度),然后写了下面这个脚本简单测试下网络质量,大家看谁网络质量比较好测试一下。
下面是一组比较夸张的数据,但是游戏中也会出现。
21:32:18 样本数量: 60 | 平均: 111.24ms | 抖动: 55.75ms | 抖动率: 50.12% 21:32:20 样本数量: 60 | 平均: 112.78ms | 抖动: 55.60ms | 抖动率: 49.30% 21:32:22 样本数量: 60 | 平均: 115.65ms | 抖动: 55.04ms | 抖动率: 47.59% 21:32:23 样本数量: 60 | 平均: 117.59ms | 抖动: 60.08ms | 抖动率: 51.09% 21:32:25 样本数量: 60 | 平均: 115.69ms | 抖动: 56.60ms | 抖动率: 48.93% 21:32:26 样本数量: 60 | 平均: 119.04ms | 抖动: 57.91ms | 抖动率: 48.65% 21:32:27 样本数量: 60 | 平均: 118.50ms | 抖动: 58.77ms | 抖动率: 49.60% 21:32:28 样本数量: 60 | 平均: 115.43ms | 抖动: 58.23ms | 抖动率: 50.44% 21:32:29 样本数量: 60 | 平均: 111.57ms | 抖动: 54.64ms | 抖动率: 48.97% import configparser import subprocess import time import re from collections import deque import numpy as np import os CONFIG_FILE = 'config.ini' def create_default_config(path): cOnfig= configparser.ConfigParser() config['monitor'] = { 'host': 'baidu.com', 'window_size': '60', 'ping_interval': '1', 'trim_percent': '0.05' } config['output'] = { 'show_timestamp': 'true', 'log_to_file': 'false', 'log_file': 'jitter_log.txt' } with open(path, 'w') as configfile: config.write(configfile) print(f"[首次运行] 配置文件 '{path}' 已生成,使用默认配置。") def load_config(file): if not os.path.exists(file): create_default_config(file) cOnfig= configparser.ConfigParser() config.read(file) return { 'host': config.get('monitor', 'host'), 'window_size': config.getint('monitor', 'window_size'), 'ping_interval': config.getfloat('monitor', 'ping_interval'), 'trim_percent': config.getfloat('monitor', 'trim_percent'), 'show_timestamp': config.getboolean('output', 'show_timestamp'), 'log_to_file': config.getboolean('output', 'log_to_file'), 'log_file': config.get('output', 'log_file') } def ping_once(host): try: output = subprocess.check_output( ["ping", host, "-n", "1"], stderr=subprocess.STDOUT, universal_newlines=True ) match = re.search(r'(?:时间|time)[=<]?\s*(\d+)\s*ms', output, re.IGNORECASE) if match: return int(match.group(1)) except subprocess.CalledProcessError: pass return None def calculate_jitter_rate(delays, trim_percent=0.05): if len(delays) < 4: return None arr = np.array(delays) lower = np.percentile(arr, trim_percent * 100) upper = np.percentile(arr, (1 - trim_percent) * 100) trimmed = arr[(arr >= lower) & (arr <= upper)] if len(trimmed) < 2: return None avg_delay = np.mean(trimmed) diffs = np.abs(np.diff(trimmed)) avg_jitter = np.mean(diffs) jitter_rate = (avg_jitter / avg_delay) * 100 if avg_delay != 0 else 0 return avg_delay, avg_jitter, jitter_rate def live_ping_jitter(config): delays = deque(maxlen=config['window_size']) while True: delay = ping_once(config['host']) timestamp = time.strftime('%H:%M:%S') if config['show_timestamp'] else '' if delay is not None: delays.append(delay) result = calculate_jitter_rate(list(delays), config['trim_percent']) if result: avg_delay, avg_jitter, jitter_rate = result line = f"{timestamp} 样本数量: {len(delays):2d} | 平均: {avg_delay:.2f}ms | 抖动: {avg_jitter:.2f}ms | 抖动率: {jitter_rate:.2f}%" else: line = f"{timestamp} 收集中... 样本数量: {len(delays)}" else: line = f"{timestamp} 丢包" print(line) if config['log_to_file']: with open(config['log_file'], 'a', encoding='utf-8') as f: f.write(line + '\n') time.sleep(config['ping_interval']) if __name__ == "__main__": cOnfig= load_config('config.ini') live_ping_jitter(config) 