nitro
nitro copied to clipboard
fix(app): reuse computed runtime config across requests
๐ Linked issue
https://github.com/unjs/nitro/issues/2569
โ Type of change
- [ ] ๐ Documentation (updates to the documentation, readme, or JSdoc annotations)
- [ ] ๐ Bug fix (a non-breaking change that fixes an issue)
- [x] ๐ Enhancement (improving an existing functionality like performance)
- [ ] โจ New feature (a non-breaking change that adds functionality)
- [ ] ๐งน Chore (updates to the build process or auxiliary tools and libraries)
- [ ] โ ๏ธ Breaking change (fix or feature that would cause existing functionality to change)
๐ Description
In our Nuxt application, we noticed that as the size of the runtime config the performance decreases significantly. Loading the runtime config occurs in every request and was making up a significant portion of the request time. Since there is not a need to recompute the config every request, we created a plugin to load the config at startup instead of for each request.
export default defineNitroPlugin((nitroApp: NitroApp) => {
const runtimeConfig = useRuntimeConfig() as RuntimeConfig
nitroApp.hooks.hook('request', (event) => {
event.context.nitro = event.context.nitro ?? {}
event.context.nitro.runtimeConfig = runtimeConfig
})
})
But I think this performance improvement would also be beneficial for other Nitro users and would make sense in core. Unless my assumptions here are wrong. Perhaps in that case, it could be configured?
I've done some very rough tests with autocannon against a cached page to show the impact we're seeing. It's nearly twice as fast in the average case.
Before Change
โโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโ
โ Stat โ 2.5% โ 50% โ 97.5% โ 99% โ Avg โ Stdev โ Max โ
โโโโโโโโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโค
โ Latency โ 23 ms โ 28 ms โ 36 ms โ 39 ms โ 31.3 ms โ 53.74 ms โ 1370 ms โ
โโโโโโโโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโ
โโโโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโ
โ Stat โ 1% โ 2.5% โ 50% โ 97.5% โ Avg โ Stdev โ Min โ
โโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโค
โ Req/Sec โ 45 โ 45 โ 344 โ 355 โ 314.4 โ 90.13 โ 45 โ
โโโโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโโค
โ Bytes/Sec โ 12.3 MB โ 12.3 MB โ 94.3 MB โ 97.3 MB โ 86.2 MB โ 24.7 MB โ 12.3 MB โ
โโโโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโ
โโโโโโโโฌโโโโโโโโ
โ Code โ Count โ
โโโโโโโโผโโโโโโโโค
โ 200 โ 3144 โ
โโโโโโโโดโโโโโโโโ
After Change
โโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโ
โ Stat โ 2.5% โ 50% โ 97.5% โ 99% โ Avg โ Stdev โ Max โ
โโโโโโโโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโค
โ Latency โ 13 ms โ 15 ms โ 21 ms โ 25 ms โ 17.75 ms โ 48.12 ms โ 1142 ms โ
โโโโโโโโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโ
โโโโโโโโโโโโโฌโโโโโโฌโโโโโโโฌโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโ
โ Stat โ 1% โ 2.5% โ 50% โ 97.5% โ Avg โ Stdev โ Min โ
โโโโโโโโโโโโโผโโโโโโผโโโโโโโผโโโโโโโโโผโโโโโโโโโผโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโค
โ Req/Sec โ 0 โ 0 โ 616 โ 642 โ 547.6 โ 188.61 โ 472 โ
โโโโโโโโโโโโโผโโโโโโผโโโโโโโผโโโโโโโโโผโโโโโโโโโผโโโโโโโโโผโโโโโโโโโโผโโโโโโโโโค
โ Bytes/Sec โ 0 B โ 0 B โ 169 MB โ 176 MB โ 150 MB โ 51.7 MB โ 129 MB โ
โโโโโโโโโโโโโดโโโโโโดโโโโโโโดโโโโโโโโโดโโโโโโโโโดโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโ
โโโโโโโโฌโโโโโโโโ
โ Code โ Count โ
โโโโโโโโผโโโโโโโโค
โ 200 โ 5476 โ
โโโโโโโโดโโโโโโโโ
๐ Checklist
- [x] I have linked an issue or discussion.
- [ ] I have updated the documentation accordingly.