From edd376645bd43e20ac00e04686a925b0684d1392 Mon Sep 17 00:00:00 2001 From: wangqing Date: Thu, 18 Apr 2024 16:47:52 +0800 Subject: [PATCH] btrfs_stats.py: Precompiled regular expressions improve performance Signed-off-by: wangqing --- btrfs_stats.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/btrfs_stats.py b/btrfs_stats.py index 029e066..f54670f 100755 --- a/btrfs_stats.py +++ b/btrfs_stats.py @@ -12,6 +12,8 @@ from prometheus_client import CollectorRegistry, Gauge, generate_latest +DEVICE_PATTERN = re.compile(r"^\[([^\]]+)\]\.(\S+)\s+(\d+)$", re.MULTILINE) + def get_btrfs_mount_points(): """List all btrfs mount points. @@ -47,7 +49,7 @@ def get_btrfs_errors(mountpoint): continue # Sample line: # [/dev/vdb1].flush_io_errs 0 - m = re.search(r"^\[([^\]]+)\]\.(\S+)\s+(\d+)$", line.decode("utf-8")) + m = DEVICE_PATTERN.match(line.decode("utf-8")) if not m: raise RuntimeError("unexpected output from btrfs: '%s'" % line) yield m.group(1), m.group(2), int(m.group(3))