loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Performance issues with lb4 services

Open u3ih opened this issue 1 year ago • 3 comments

Describe the bug

Using @service or @inject to inject services can lead to performance issues when calling api (although not using them).

i used autocannon for benchmarking Full command: autocannon -t12 -c1000 -d30 http://localhost:3000/ping

I tried to inject services into the controller (ping.controller). The more services I import and the larger the project, the more req in seconds decreases

Logs

No response

Additional information

I ran the project on 8 CPUs and tried it multiple times. These are the average results I got.

- Without Injection

I ran the autocannon test and got around 680k requests in 30 seconds. image

- With `@service` (around 37 `@service` with the same service file)

I got around 420k requests in 30 seconds. image

- With `@inject` (around 37 `@inject` with the same service file)

I got around 600k requests in 30 seconds. image

Reproduction

https://github.com/u3ih/loopback-benchmarking

u3ih avatar Jul 24 '24 09:07 u3ih

For me, this is to be expected.... Using DI indeed runs more code than not using it. So it is normal that it can slow down a bit. What I find very interesting tough is the fact that @inject seems way faster than @service which is simply a sugar coated inject...

mathieuBP avatar Aug 02 '24 07:08 mathieuBP

I dont think its just 'a bit' with this example its around 30%. And with a big project, it slows down even more 😢

u3ih avatar Aug 19 '24 04:08 u3ih

Pre initializing services during application startup and apply correct scope to services could reduce the api response delays as each api request will try to initialize all services that are injected which is not of global scope

gopiuma avatar Nov 26 '24 04:11 gopiuma