==> tests/rate.c <==1/* Rate windows, slow start and lease times, step by step as jobs.rs computes them. */
2#include "mesh/jobs/rate.h"
3#include "mesh/check.h"
4
5int main(void) {
6 mj_rate r = {0};
7 mj_rate_count(&r, 10, 100); /* the window opens at 100 */
8 CHECK(r.ips == 0.0 && r.k == 10 && r.t0 == 100);
9 mj_rate_count(&r, 30, 500); /* 40 items in 400 ms: 100/s */
10 CHECK(r.ips == 100.0 && r.k == 0 && r.t0 == 500);
11 mj_rate_count(&r, 120, 900); /* 300/s: the EWMA gives 200 */
12 CHECK(r.ips == 200.0);
13 /* Slow start: a full-size lease done fast doubles the target; a slow one halves it. */
14 mj_rate s = {.target = 8};
15 mj_rate_lease_done(&s, 8, 1000, 1100, false);
16 CHECK(s.ips == 80.0 && s.target == 16);
17 mj_rate_lease_done(&s, 4, 2000, 2050, false); /* short of half the target: no change */
18 CHECK_U(s.target, 16);
19 mj_rate_lease_done(&s, 30, 3000, 3800, true);
20 CHECK(s.target == 15 && s.idle_at == 3800);
21 mj_rate_busy(&s, 3840); /* within IDLE_MS: the window stays */
22 CHECK(s.idle_at == 0);
23 s.idle_at = 4000;
24 s.k = 7;
25 mj_rate_busy(&s, 4100);
26 CHECK(s.t0 == 4100 && s.k == 0);
27 mj_rate big = {.target = MESH_JOBS_LEASE_MAX};
28 mj_rate_lease_done(&big, MESH_JOBS_LEASE_MAX, 0, 10, false);
29 CHECK_U(big.target, MESH_JOBS_LEASE_MAX);
30 CHECK_U(mj_lease_time(100, 0.0), 10000);
31 CHECK_U(mj_lease_time(100, 400.0), 251);
32 CHECK_U(mj_lease_time(3, 7.0), 429);
33 return mesh_check_done();
34}