fastify / fastify-rate-limit

A low overhead rate limiter for your routes
MIT License
504 stars 71 forks source link

Report the correct TTL within the response context #349

Closed gurgunday closed 11 months ago

gurgunday commented 11 months ago

respCtx would not report the real TTL but the static timeWindow

The reason we can't directly use ttl is that it's not ceiled so impossible to test consistently

I don't think we should be wasting time (however small it might be) on formatting text here

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.05 ms │ 12 ms │
└─────────┴──────┴──────┴───────┴──────┴─────────┴─────────┴───────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬──────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg      │ Stdev    │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼──────────┼─────────┤
│ Req/Sec   │ 47,871  │ 47,871  │ 52,063  │ 52,351  │ 51,501.1 │ 1,243.97 │ 47,852  │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼──────────┼─────────┤
│ Bytes/Sec │ 18.1 MB │ 18.1 MB │ 19.7 MB │ 19.8 MB │ 19.5 MB  │ 474 kB   │ 18.1 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴──────────┴─────────┘

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

100 2xx responses, 566412 non 2xx responses
567k requests in 11.01s, 214 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 │ 11 ms │
└─────────┴──────┴──────┴───────┴──────┴─────────┴─────────┴───────┘
┌───────────┬─────────┬─────────┬─────────┬─────────┬──────────┬─────────┬─────────┐
│ Stat      │ 1%      │ 2.5%    │ 50%     │ 97.5%   │ Avg      │ Stdev   │ Min     │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼─────────┼─────────┤
│ Req/Sec   │ 45,567  │ 45,567  │ 51,007  │ 52,383  │ 50,457.6 │ 2,160.9 │ 45,561  │
├───────────┼─────────┼─────────┼─────────┼─────────┼──────────┼─────────┼─────────┤
│ Bytes/Sec │ 17.1 MB │ 17.1 MB │ 19.1 MB │ 19.6 MB │ 18.9 MB  │ 828 kB  │ 17.1 MB │
└───────────┴─────────┴─────────┴─────────┴─────────┴──────────┴─────────┴─────────┘

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

100 2xx responses, 504561 non 2xx responses
505k requests in 10.01s, 189 MB read

Benchmark:

import fastify from 'fastify';

const app = fastify();

await app.register(import("../index.js"), {
    max: 100,
    timeWindow: '1 minute'
});

app.get('/', (request, reply) => {
    reply.send('Hello, world!');
});

app.listen(3000, (err) => {
    if (err) {
        console.error(err);
        process.exit(1);
    }
    console.log('Server is running on port 3000');
});