Skip to content

Commit 87f6c52

Browse files
Store stats text in lines
1 parent 8f283db commit 87f6c52

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/python/pants/goal/stats_aggregator.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ class StatsAggregatorSubsystem(Subsystem):
6464
)
6565

6666

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:
6868
"""Send text to the stdout or write to the output file."""
69-
if text:
69+
if lines:
70+
text = "\n".join(lines)
7071
if output_file:
7172
with safe_open(output_file, "w") as fh:
7273
fh.write(text)
@@ -101,7 +102,7 @@ def __call__(
101102
if not finished:
102103
return
103104

104-
output_contents = ""
105+
output_lines = []
105106

106107
if self.log:
107108
# Capture global counters.
@@ -116,7 +117,7 @@ def __call__(
116117
counter_lines = "\n".join(
117118
f" {name}: {count}" for name, count in sorted(counters.items())
118119
)
119-
output_contents += f"Counters:\n{counter_lines}"
120+
output_lines.append(f"Counters:\n{counter_lines}")
120121

121122
if self.memory:
122123
ids: set[int] = set()
@@ -138,23 +139,23 @@ def __call__(
138139
memory_lines = "\n".join(
139140
f" {size}\t\t{count}\t\t{name}" for size, count, name in sorted(entries)
140141
)
141-
output_contents += (
142-
f"\nMemory 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}"
143144
)
144145

145146
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)
147148
return
148149

149150
from hdrh.histogram import HdrHistogram # pants: no-infer-dep
150151

151152
histograms = context.get_observation_histograms()["histograms"]
152153
if not histograms:
153-
output_contents += "\nNo 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)
155156
return
156157

157-
output_contents += "\nObservation histogram summaries:"
158+
output_lines.append("Observation histogram summaries:")
158159
for name, encoded_histogram in histograms.items():
159160
# Note: The Python library for HDR Histogram will only decode compressed histograms
160161
# that are further encoded with base64. See
@@ -166,8 +167,8 @@ def __call__(
166167
[25, 50, 75, 90, 95, 99]
167168
).items()
168169
)
169-
output_contents += (
170-
f"\nSummary of `{name}` observation histogram:\n"
170+
output_lines.append(
171+
f"Summary of `{name}` observation histogram:\n"
171172
f" min: {histogram.get_min_value()}\n"
172173
f" max: {histogram.get_max_value()}\n"
173174
f" mean: {histogram.get_mean_value():.3f}\n"
@@ -176,7 +177,7 @@ def __call__(
176177
f" sum: {int(histogram.get_mean_value() * histogram.total_count)}\n"
177178
f"{percentile_to_vals}"
178179
)
179-
_log_or_write_to_file(self.output_file, output_contents)
180+
_log_or_write_to_file(self.output_file, output_lines)
180181

181182

182183
@dataclass(frozen=True)

0 commit comments

Comments
 (0)