fastify / fastify-rate-limit

A low overhead rate limiter for your routes
MIT License
477 stars 66 forks source link

perf: pregenerate `timeWindow` string when possible and use `noop` as default function #364

Closed gurgunday closed 6 months ago

gurgunday commented 6 months ago

We shouldn't keep recreating the string if it's static, it might become a problem under an attack

Note: This was already the case, but it got removed in https://github.com/fastify/fastify-rate-limit/pull/357

gurgunday commented 6 months ago

Not seeing a real benefit in benchmarks, so will close this one

gurgunday commented 6 months ago

Ok, now it's better on my end:

NEW:

Running 10s test @ http://127.0.0.1:3000/
10 connections

┌─────────┬──────┬──────┬───────┬──────┬─────────┬─────────┬───────┐
│ Stat    │ 2.5% │ 50%  │ 97.5% │ 99%  │ Avg     │ Stdev   │ Max   │
├─────────┼──────┼──────┼───────┼──────┼─────────┼─────────┼───────┤
│ Latency │ 0 ms │ 0 ms │ 0 ms  │ 0 ms │ 0.01 ms │ 0.06 ms │ 14 ms │
└─────────┴──────┴──────┴───────┴──────┴─────────┴─────────┴───────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬──────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg      │ Stdev    │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼──────────┼─────────┤
│ Req/Sec   │ 48,991  │ 48,991  │ 52,671  │ 53,823  │ 52,365.1 │ 1,201.95 │ 48,981  │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼──────────┼─────────┤
│ Bytes/Sec │ 17.2 MB │ 17.2 MB │ 19.7 MB │ 20.2 MB │ 19.5 MB  │ 770 kB   │ 17.2 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴──────────┴─────────┘

Req/Bytes counts sampled once per second.
# of samples: 11

10000 2xx responses, 565963 non 2xx responses
576k requests in 11.01s, 215 MB read

OLD:

Running 10s test @ http://127.0.0.1:3000/
10 connections

┌─────────┬──────┬──────┬───────┬──────┬─────────┬─────────┬───────┐
│ Stat    │ 2.5% │ 50%  │ 97.5% │ 99%  │ Avg     │ Stdev   │ Max   │
├─────────┼──────┼──────┼───────┼──────┼─────────┼─────────┼───────┤
│ Latency │ 0 ms │ 0 ms │ 0 ms  │ 0 ms │ 0.01 ms │ 0.06 ms │ 14 ms │
└─────────┴──────┴──────┴───────┴──────┴─────────┴─────────┴───────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg      │ Stdev  │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼────────┼─────────┤
│ Req/Sec   │ 49,375  │ 49,375  │ 51,391  │ 52,383  │ 51,308.8 │ 978.75 │ 49,373  │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼────────┼─────────┤
│ Bytes/Sec │ 17.5 MB │ 17.5 MB │ 19.3 MB │ 19.6 MB │ 19.1 MB  │ 618 kB │ 17.5 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴────────┴─────────┘

Req/Bytes counts sampled once per second.
# of samples: 10

10000 2xx responses, 503109 non 2xx responses
513k requests in 10.01s, 191 MB read
gurgunday commented 6 months ago

Uzlopak was right, noop is faster than ?.