THINK FIRST·CODE LATER

← All labs

Streaming percentiles and alerts with hysteresis

Problem

Input: a line window=W enter=E exit=X k=K, then request latencies in ms (whitespace-separated, in arrival order).

Process tumbling windows of W requests (ignore an incomplete last window). For each window compute mean, p50 and p95 (np.percentile, default interpolation). Count consecutive windows with p95 > E; when not alerting and that count reaches K, turn the alert on ( ALERT ON suffix with two spaces); when alerting and p95 < X, turn it off ( alert cleared). Print window 3: mean 222, p50 120, p95 396 ms ALERT ON (values with 0 decimals), then alerts fired: 1, alert active at end: no and overall mean 142 ms vs overall p95 371 ms.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v13-3-data-1.txt

window=5 enter=200 exit=150 k=2
80 90 85 100 95
90 85 300 95 100
120 110 400 380 100
90 350 360 95 110
100 160 170 90 95
80 85 90 70 95
What your program must print for v13-3-data-1.txt
window 1: mean 90, p50 90, p95 99 ms
window 2: mean 134, p50 95, p95 260 ms
window 3: mean 222, p50 120, p95 396 ms  ALERT ON
window 4: mean 201, p50 110, p95 358 ms
window 5: mean 123, p50 100, p95 168 ms
window 6: mean 84, p50 85, p95 94 ms  alert cleared
alerts fired: 1, alert active at end: no
overall mean 142 ms vs overall p95 371 ms

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v13-3-data-1.txt

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v13-3-data-1.txt | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v13-3-data-1.txt", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.