@@ -64,9 +64,10 @@ class StatsAggregatorSubsystem(Subsystem):
64
64
)
65
65
66
66
67
- def _log_or_write_to_file (output_file : Optional [str ], text : str ) -> None :
67
+ def _log_or_write_to_file (output_file : Optional [str ], lines : list [ str ] ) -> None :
68
68
"""Send text to the stdout or write to the output file."""
69
- if text :
69
+ if lines :
70
+ text = "\n " .join (lines )
70
71
if output_file :
71
72
with safe_open (output_file , "w" ) as fh :
72
73
fh .write (text )
@@ -101,7 +102,7 @@ def __call__(
101
102
if not finished :
102
103
return
103
104
104
- output_contents = ""
105
+ output_lines = []
105
106
106
107
if self .log :
107
108
# Capture global counters.
@@ -116,7 +117,7 @@ def __call__(
116
117
counter_lines = "\n " .join (
117
118
f" { name } : { count } " for name , count in sorted (counters .items ())
118
119
)
119
- output_contents += f"Counters:\n { counter_lines } "
120
+ output_lines . append ( f"Counters:\n { counter_lines } " )
120
121
121
122
if self .memory :
122
123
ids : set [int ] = set ()
@@ -138,23 +139,23 @@ def __call__(
138
139
memory_lines = "\n " .join (
139
140
f" { size } \t \t { count } \t \t { name } " for size , count , name in sorted (entries )
140
141
)
141
- output_contents += (
142
- f"\n Memory summary (total size in bytes, count, name):\n { memory_lines } "
142
+ output_lines . append (
143
+ f"Memory summary (total size in bytes, count, name):\n { memory_lines } "
143
144
)
144
145
145
146
if not (self .log and self .has_histogram_module ):
146
- _log_or_write_to_file (self .output_file , output_contents )
147
+ _log_or_write_to_file (self .output_file , output_lines )
147
148
return
148
149
149
150
from hdrh .histogram import HdrHistogram # pants: no-infer-dep
150
151
151
152
histograms = context .get_observation_histograms ()["histograms" ]
152
153
if not histograms :
153
- output_contents += " \n No observation histogram were recorded."
154
- _log_or_write_to_file (self .output_file , output_contents )
154
+ output_lines . append ( "No observation histogram were recorded.")
155
+ _log_or_write_to_file (self .output_file , output_lines )
155
156
return
156
157
157
- output_contents += " \n Observation histogram summaries:"
158
+ output_lines . append ( "Observation histogram summaries:")
158
159
for name , encoded_histogram in histograms .items ():
159
160
# Note: The Python library for HDR Histogram will only decode compressed histograms
160
161
# that are further encoded with base64. See
@@ -166,8 +167,8 @@ def __call__(
166
167
[25 , 50 , 75 , 90 , 95 , 99 ]
167
168
).items ()
168
169
)
169
- output_contents += (
170
- f"\n Summary of `{ name } ` observation histogram:\n "
170
+ output_lines . append (
171
+ f"Summary of `{ name } ` observation histogram:\n "
171
172
f" min: { histogram .get_min_value ()} \n "
172
173
f" max: { histogram .get_max_value ()} \n "
173
174
f" mean: { histogram .get_mean_value ():.3f} \n "
@@ -176,7 +177,7 @@ def __call__(
176
177
f" sum: { int (histogram .get_mean_value () * histogram .total_count )} \n "
177
178
f"{ percentile_to_vals } "
178
179
)
179
- _log_or_write_to_file (self .output_file , output_contents )
180
+ _log_or_write_to_file (self .output_file , output_lines )
180
181
181
182
182
183
@dataclass (frozen = True )
0 commit comments