Log alert digest by service and severity
Requirement
From a stream of service logs (data in the code), keep only
WARN and ERROR lines and render an indented
digest: services sorted by alert count, under each service the severity
with its count, under each severity the distinct messages. The
header line carries the overall ERROR/WARN totals. Both versions must
print the digest under Expected output.
Expected output
Alert digest — ERROR 4, WARN 5
api (4)
ERROR x2
- timeout calling billing
WARN x2
- slow query 1.2s
- disk 85% full
auth (3)
ERROR x1
- bad signature
WARN x2
- token near expiry
billing (2)
ERROR x1
- invoice 442 failed
WARN x1
- retrying charge
Side by side
Native Dart
FxDart
Why they differ
An indented digest is a tree flattened to lines, and
flatMap is exactly that flattening: the outer
groupBy + sortBy + flatMap emits
a header plus its children per service, the inner flatMap
does the same per severity, and uniq handles the repeated
messages where they occur. countBy gives the header totals
in one word. The native version is three nested for loops
writing into one shared body list, plus a hand-rolled
counting map and a seen-set for dedupe — the tree shape is
real in both versions, but only one of them lets you read it off the
indentation of the code itself.
Benchmark
N = 100
Time Tie
Peak memory Tie
N = 10,000
Time Tie
Peak memory Tie
N = 1,000,000
Time Tie
Peak memory Native wins
Bars are medians of repeated timed iterations in fresh processes per side (small N is batched for timer resolution). Sides within 5% of each other — or within 0.6 ms, a difference no person can perceive — count as a tie; close relative races are re-measured up to 5 runs. In an app, anything under a few milliseconds is invisible to the user regardless of which bar is shorter. Memory is peak process RSS. The Dart VM and the dataset are identical on both sides, so the difference between the two bars is what the pipeline itself holds onto.