Performance

Discussion in 'Spigot Plugin Development' started by MisterFantasy, Jun 7, 2017.

  1. Hello,

    I was requested to code a plugin, where I would have to save all players play-time on a server.
    Where I would also have two commands which are /playtime (player) and a command to see the top 5 of most active players (players with the most amount of playtime).

    Now, because there is /playtime (player) and I want the top command to be real-time, I need some advice about what the best way would be to go about this.

    I've thought of a few things myself:
    - 1. When a player joins the server, they get put into a hashmap where it would put the current time / current date, and when they leave it would grab the time / date of when they left and compare that to the one in the hashmap, then it would add that time thats calculated (in seconds) to a file where it saves all player time for each player.
    Now if I were to grab the playertime for a player I would just grab the time in the file (where it saves all playertime) and calculate the difference between the hashmap and the current time etcetera, to get their playtime. Same goes for the beasttop.
    - 2. On enable it would start a runnable, which every second adds 1 'point' or second to the file where playertime for each player is saved for each player. That way I wouldn't need to measure the time between the login time and their current time, and it would be real time aswell. But would this be good performance wise?

    Thanks in advance!
     
  2. Writing the data every second would definitely have a higher performance impact than just calculating their join/leave time, even if a small one.
     
  3. Alright thanks, do you have any other ways I could possibly do this?
     
  4. Why not just have it check every 5 minutes? Get it on player join and then check it 5 minutes after?
     
    • Like Like x 1
  5. Because the person that i'm coding it for wants real-time statistics.
     
  6. Explain more, why would this be more useful?
     
  7. Since I learned to work with mysql it makes everything easier, I recommend using timestamp and check the time every 5 minutes to save the player data.
     
  8. Choco

    Moderator

    What are you talking about... You really think that executing IO operations every single second (multiplied by however many players there are online) is going to have a higher performance than some basic arithmetic operations?
     
  9. @ZNetwork
     
  10. Exactly what he's saying you save the data into a MySQL server, and have that check and compare the difference every 5 minutes (make a command to sync it if he wants it real time)
     
  11. By writing data to lets say a top.yml it would need to save that data each time it's written to the file for each player, correct?
     
  12. Like i've mentioned multiple times in this thread, the person I am coding this for wants this in real-time so I cant be asked to save it every 5 minutes.
     
  13. What do you mean in real time?
     
  14. Choco

    Moderator

    The employer wants real-time data in a file? That's absolutely stupid and no one in their right mind should be asking for that if they want any kind of efficient program. IO is a pricey operation and doing it every second is not an option. If you only have to display that information when a player calls upon the /playtime command, just calculate it there. You shouldn't need to write it to file until your plugin shuts down
     
  15. With real-time I mean that when a player performs /playtime (playername) it would get their playtime at that exact moment, but i've decided to save the System#currentTimeMillis() in a hashmap with the player UUID as the key like this:
    Code (Java):
    @EventHandler
        public void onPlayerLogin(PlayerLoginEvent e)
        {
            Player player = e.getPlayer();
            UUID uuid = player.getUniqueId();
            long current = System.currentTimeMillis();
            plugin.getPlayertime().put(uuid, current);
        }
    and when a player quits (or when the server stops w/e):
    Code (Java):
    @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e)
        {
            Player player = e.getPlayer();
            UUID uuid = player.getUniqueId();
       
            long calculation = (System.currentTimeMillis() / 1000) - (plugin.getPlayertime().get(uuid) / 1000);    
            plugin.getYaml().set(uuid.toString() + ".time", (long) plugin.getYaml().get(uuid.toString() + ".time") + calculation);
            plugin.saveTop();
            plugin.getPlayertime().remove(uuid);
        }
     
    • Winner Winner x 1