-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive_stats.py
88 lines (68 loc) · 2.09 KB
/
archive_stats.py
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
"""
Author(s): Kamil Vojanec <[email protected]>
Copyright: (C) 2024 CESNET, z.s.p.o.
Module for archiving appliacation statistics.
"""
import logging
from pathlib import Path
from typing import Optional
from lbr_testsuite.dpdk_application.stats_interface import StatsInterface
global_logger = logging.getLogger(__name__)
def _save_dict_to_file(data, path):
with open(path, "w") as f:
for k, v in data.items():
f.write(f"{k}: {v}\n")
def archive_port_stats(
application: StatsInterface,
out_dir: Path,
) -> Optional[Path]:
"""Archive port statistics across all ports of given
application. This function is designed to work with any
object implementing StatsInterface.
Parameters
----------
application : StatsInterface
Object implementing StatsInterface.
out_dir : Path
Output directory.
Returns
-------
Path
Filepath to the stored port statistics or None if statistics
could not be read.
"""
try:
stats = application.get_stats()
except Exception as e:
global_logger.warning(f"Could not obtain port statistics: {e}")
return None
stats_path = out_dir / "dev_stats"
_save_dict_to_file(stats, stats_path)
return stats_path
def archive_port_xstats(
application: StatsInterface,
out_dir: Path,
) -> Optional[Path]:
"""Archive extended port statistics across all ports of
given application. This function is designed to work with
any object implementing StatsInterface.
Parameters
----------
application : StatsInterface
Object implementing StatsInterface
out_dir : Path
Output directory.
Returns
-------
Path
Filepath to the stored extended port statistics or
None if statistics could not be read.
"""
try:
xstats = application.get_xstats()
except Exception as e:
global_logger.warning(f"Could not obtain port extended statistics: {e}")
return None
xstats_path = out_dir / "dev_xstats"
_save_dict_to_file(xstats, xstats_path)
return xstats_path