agar.io-clone icon indicating copy to clipboard operation
agar.io-clone copied to clipboard

CPU heavy load

Open abstream opened this issue 9 years ago • 15 comments

My cpu(2 core digital ocean, 2GB instance) goes to 15% with just 2 players, I wonder what is going to be with 50 or 100? Do you guys have some real statistics with your hosting? What are the hosting requirements? What kind of a server do I need? How many instances of the server do you run, basically what is your approach in regards of the heavy cpu load?

abstream avatar Nov 25 '15 23:11 abstream

Performance has been a big issue for a long time now. Nobody's actually tested this clone on a large scale, so there aren't really any numbers or recommended specifications for hosting.

abalabahaha avatar Nov 25 '15 23:11 abalabahaha

Thanks for your response. Could you point me to the most cpu consuming part of this project, I would like to have a look....Would you say that the Ogra open source server with a custom client(or maybe agar.io client) is doing any better?

abstream avatar Nov 25 '15 23:11 abstream

agar.io custom clients are not allowed by their ToS I believe. Ogar server, though, has better performance IIRC.

EDIT: Sorry, didn't make it clear. Yeah scratch is fine.

abalabahaha avatar Nov 27 '15 01:11 abalabahaha

I created a new branch for multi server supporting recently here https://github.com/huytd/agar.io-clone/tree/multi-server hopefully it will help increase the performance and reduce CPU load

huytd avatar Nov 27 '15 06:11 huytd

@abalabahaha custom clients are fine as long as they are written from scratch, the client in this repository is fine and i saw the agar.io dev to post comments on that.

@huytd thanks about the multi server repo - could you explain really fast how it works and how it helps the cpu load issue?

abstream avatar Nov 27 '15 11:11 abstream

@huytd I Have some doubt than @abstream Quoting him: "@huytd thanks about the multi server repo - could you explain really fast how it works and how it helps the cpu load issue?" Ty so much!

mbenatti avatar Jan 12 '16 06:01 mbenatti

I put some brief explaination in this wiki file, please check it https://github.com/huytd/agar.io-clone/wiki/Multi-server-Experimental

I'm here for any question :D

huytd avatar Jan 12 '16 09:01 huytd

I'm gonna look into methods for this as well. I'm sure we can optimize the weaknesses of the game to bring down the cpu usage.

underGhost avatar Jan 19 '16 16:01 underGhost

I was able to remove some of the canvas renderings (since I'm working on the logic for z-index). Viruses are image sprites and the players will be too. The grid was moved to a background image instead of canvas rendering it. People will also be able to customize their backgrounds if they don't want a grid :)

underGhost avatar Feb 19 '16 01:02 underGhost

By the way I tried converting from socket.io to ws on my own fork. I did not get as far as using binary protocol, but it did not significantly improve things it seems.

In my environment the problem is that the rendering on the client simply takes too long to fit into the 16ms budget required for smooth (60fps) animation. I think it is likely that the changes made by @underGhost should improve that but I haven't tried it yet.

So I am inclined not PR the socket changes because it's quite disruptive and doesn't really seem to help.

julianjelfs avatar Feb 22 '16 13:02 julianjelfs

Yeah, the fact is... we have too many for/nested for loops just to render a single object (a piece of food, or a player,...), and this bunch of loops will be called every 1000 / 60 milliseconds...

I think we should thinking how to improve the rendering performance, mostly is, reduce the unnecessary for loops, for example:

  • Using an image instead of drawing shapes for foods (the most popular object on the screen)
  • Use multiple canvas layers (http://www.ibm.com/developerworks/library/wa-canvashtml5layering/)
  • ...

huytd avatar Feb 22 '16 23:02 huytd

The binary protocol merely makes packets smaller, but doesn't help with CPU that much, so it isn't the biggest issue at hand.

abalabahaha avatar Feb 23 '16 01:02 abalabahaha

And it also would be faster if we migrate from socket.io to ws

ghost avatar May 05 '16 14:05 ghost

socket.io to ws doesn't make a huge amount of difference by the way. I tried it. Rendering is more of a problem. network might end up being the bottle neck in the end so it could still be worth it though.

julianjelfs avatar May 05 '16 15:05 julianjelfs

I was reading up on socket.io vs WebSocket and it seems that socket ends up starting with fallback Ajax before upgrading, which causes a lot of waste. In combination with WebSocket, I think several of the fixes you all have mentioned would really clear the engine up. As for binary: It will be more strain on the CPU to parse the data into binary, you might consider storing the data in binary to cut the chase. I know y'all haven't even been here for a year, but I'm interested in taking a poke into the code, and might do the mentioned changes on my own fork.

RepComm avatar Sep 03 '17 15:09 RepComm