Skip to content

Commit

Permalink
even more precise load calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
mirabilos committed Sep 27, 2023
1 parent 6b3f5a0 commit 3ce3b8d
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions acquire/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,35 @@ BEGIN
SELECT ts, len FROM p
WHERE NOT p.isdrop
),
counted AS (
SELECT ts, len AS pktsizebytes,
sum(len) OVER (
ORDER BY ts
-- 0.1 PRECEDING ⇒ 100ms sliding window
RANGE BETWEEN 0.1 PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW
) AS lensum, min(ts) OVER (
ORDER BY ts
-- same sliding window as above!
RANGE BETWEEN 0.1 PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW
) AS tsmin
FROM prefiltered
),
merged AS (
SELECT ts, pktsizebytes, lensum, tsmin, bwlim,
calculated AS (
SELECT ts, len AS pktsizebytes, bwlim,
-- https://dba.stackexchange.com/a/105828/65843
count(bwlim) OVER (ORDER BY ts) AS ct
FROM counted FULL OUTER JOIN q USING (ts)
count(bwlim) OVER wts AS ct,
-- https://stackoverflow.com/a/77051480/2171120
CASE WHEN (COUNT(*) OVER wtim) > 20
THEN 8 * (sum(len) OVER wnum) / NULLIF(ts - min(ts) OVER wnum, 0)
ELSE 8 * (sum(len) OVER wtim) / NULLIF(ts - min(ts) OVER wtim, 0)
END AS bps
FROM prefiltered FULL OUTER JOIN q USING (ts)
WINDOW
wnum AS (ORDER BY ts
ROWS BETWEEN 20 PRECEDING AND CURRENT ROW
EXCLUDE CURRENT ROW),
wtim AS (ORDER BY ts
RANGE BETWEEN 0.1 PRECEDING AND CURRENT ROW
EXCLUDE CURRENT ROW),
wts AS (ORDER BY ts)
),
divided AS (
SELECT ts, pktsizebytes,
8 * lensum / NULLIF(ts - tsmin, 0) AS bps,
filled AS (
SELECT ts, pktsizebytes, bps,
min(bwlim) OVER (PARTITION BY ct) AS bw
FROM merged
FROM calculated
)
SELECT ts - d AS dts,
(bps::NUMERIC(10,0) / 1000000)::NUMERIC(10,6) AS load,
(bw::NUMERIC(10,0) / 1000000)::NUMERIC(10,6) AS capacity,
pktsizebytes
FROM divided, o ORDER BY ts;
FROM filled, o ORDER BY ts;
RETURN sid;
END;
$$ LANGUAGE 'plpgsql';
Expand Down

0 comments on commit 3ce3b8d

Please sign in to comment.