scottoffen / grapevine

Fast, unopinionated, embeddable, minimalist web framework for .NET
https://scottoffen.github.io/grapevine/
MIT License
102 stars 16 forks source link

High CPU usage when rest server running and idle (25% CPU) #41

Closed sanchezpablo closed 3 years ago

sanchezpablo commented 3 years ago

Is it normal that when server is running the CPU usage is 25% no matter what? there is no activity and the CPU usage is 25% continously.

image

Can we do anything to make it idle?

scottoffen commented 3 years ago

When the server is idle (no requests) it should be taking up 0% CPU. Can you post some sample code for me to take a look at?

sanchezpablo commented 3 years ago

Hi @scottoffen I tried with the sample provided in the repository, but the same result, so I decided to left a comment, maybe it's something else... I've tried also with other embedded webserver (https://github.com/unosquare/embedio) and the CPU is 0% when the server is idle.

If you need more information, let me know.

scottoffen commented 3 years ago

How are you blocking the thread while the server is running? If you are just benchmarking, use this method:

server.Start();
Console.ReadLine();

image

sanchezpablo commented 3 years ago

I used the server.Run() as in the sample, when I use server.Start() the CPU gets down to 0% when idle. So the server.Run() does something else in the background?

        public static void Run(this IRestServer server)
        {
            server.Start();
            while (server.IsListening) { }
        }

Probably the while loop inside...

Thanks!

scottoffen commented 3 years ago

That while loop takes up a lot. I already changed it to poll the server at a predefined number of seconds, but that isn't in a release yet. I'll be changing it again soon to something more performant, so expect that to change.

Either way, when idle, the server takes up 0% cpu.