PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
5.15k stars 927 forks source link

Optimization needed #3187

Open andreasdc opened 1 year ago

andreasdc commented 1 year ago

Hello, I'm trying to spawn some bots, but I notice that there is high CPU and RAM usage, I wanted to disable listening for some packets, but I didn't succeed. Do you know how can I do that? Thanks for help.

extremeheat commented 1 year ago

Can you define what the "high CPU and RAM usage" is with numbers? Without this information it's not possible to understand if anything is abnormal. Deserialization should be cheap process of basic memory allocations, so it should not be a bottleneck. Unless you have like 500MB of RAM on your system and your OS has to reach into the page file.

andreasdc commented 1 year ago

I have 2GB and 1 thread available, I'm trying to connect like 5 bots and it's hard.

extremeheat commented 1 year ago

You'd need to provide more information with absolute numbers. Run some benchmarks with one bot, measure the MBs of RAM and CPU usage %, then try progressively increasing the bots to find out if resource usage is not increasing linearly as expected. If the growth is greater than O(n) then yes there may be a problem.

andreasdc commented 1 year ago

6 bot and gets to 100% cpu. Is there a way to limit chunks, disable entities spawn, entities movmement etc.? I notice that when nickname is longer than 16 characters there are problems.

extremeheat commented 1 year ago

For the view distance, you can set viewDistance in createBot options to tiny to ask the server to send less chunks. mineflayer will read any chunk the server provides, so it's up to the server to act on the view distance. You can disable physics if you don't need movement per the bot options. Without chunks, there would be no physics and the bot would be unable to move or fall through the world. Mineflayer does not do client-side computation for any entity beyond the current player, so there is nothing to cut there.

Since you have not provided any data, there is nothing to indicate there is a issue in mineflayer wrt scaling bots.

andreasdc commented 1 year ago

How to disable listening for certain packets, like metadata, slots, entities spawn, movement? If it's not possible with the api, how to do that in the source code?

extremeheat commented 1 year ago

Please take the time to read and understand my comments. Not reading a packet won't offer any performance change, since the expensive part is decompression and decryption, but you can't know what packet you are reading without entering the deserialization step. If you would like to do a more thorough analysis instead of a benchmark, you can investigate with a debugger.

andreasdc commented 1 year ago

I will see, but I noticed that bot.on('login' is called multiple times on server switching, but bot.once('spawn', is not called after joining, you need to switch the server to call it. Also processing the packet is also heavy, not only reading it.

extremeheat commented 1 year ago

mineflayer doesn't support server switching, that seems like an issue with your code

rom1504 commented 1 year ago

Try running bots with worker threads instead of same process. See mineflayer pathfinder multiple example

Won't change performance but probably would solve your problem

Btw what problem are you trying to solve?

On Thu, Sep 7, 2023, 03:33 extremeheat @.***> wrote:

mineflayer doesn't support server switching, that seems like an issue with your code

— Reply to this email directly, view it on GitHub https://github.com/PrismarineJS/mineflayer/issues/3187#issuecomment-1709297359, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAR437SDUIZXRHH765EBW2TXZEI55ANCNFSM6AAAAAA4OBR5UI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

andreasdc commented 1 year ago

I was trying to ignore some incoming packets to save some cpu, bots wouldn't have to calculate entites, chat messages etc. I managed to get it to work, will try worker threads in the future probably. BTW how to make bots to have default movement behaviour, but no walking?

andreasdc commented 1 year ago

Try running bots with worker threads instead of same process. See mineflayer pathfinder multiple example Won't change performance but probably would solve your problem Btw what problem are you trying to solve? On Thu, Sep 7, 2023, 03:33 extremeheat @.> wrote: mineflayer doesn't support server switching, that seems like an issue with your code — Reply to this email directly, view it on GitHub <#3187 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAR437SDUIZXRHH765EBW2TXZEI55ANCNFSM6AAAAAA4OBR5UI . You are receiving this because you are subscribed to this thread.Message ID: @.>

If I have 1 thread it won't help much, right? I just tested it instant 100% cpu.

andreasdc commented 1 year ago

I have enough ram and cpu, but still they have unstable ping and packets.

rom1504 commented 1 year ago

How many bots are you running? What are your bots doing?

On Tue, Sep 19, 2023, 18:54 andreasdc @.***> wrote:

I have enough ram and cpu, but still they have unstable ping and packets.

— Reply to this email directly, view it on GitHub https://github.com/PrismarineJS/mineflayer/issues/3187#issuecomment-1726080747, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAR437Q4A4IWWB4B3MXK6ATX3HE5XANCNFSM6AAAAAA4OBR5UI . You are receiving this because you commented.Message ID: @.***>

andreasdc commented 1 year ago

Less than 20 bots and it happens when they are just standing.

andreasdc commented 1 year ago

How to optimize it? If there is some action like players running near them, they go crazy and they lag.

rom1504 commented 1 year ago

Are you using worker threads ?

andreasdc commented 1 year ago

No, they didn't change anything and I have only 1 thread there.