Less work in a listener

Discussion in 'Plugin Development' started by javoris767, Apr 21, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    javoris767

    I made a thread about this earlier, but I don't think I explained it too well. So basically my plugin is taking a lot of cpu during the VotifierEvent even when it is not being called. I got a report saying it happens right when the server starts up. Someone suggested using schedules or something, but I could never understand them how they work :/

    Code: link
     
  2. Offline

    Nitnelave

    Are you sure it is your plugin that makes the server slow down, and not the votifier part? Are you sure the Votifier event is not called more often than you think (print a string every time it is called, even when it is cancelled, to have an idea)?
     
  3. Offline

    javoris767

    He said he contacted the authored that that It was my plugin.
     
  4. Offline

    Nitnelave

    Have you checked when the votifier event is called as I suggested?
     
  5. Offline

    javoris767

    Every method is being called correctly.

    Bump ._.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  6. Offline

    javoris767

  7. Offline

    Nitnelave

    No, sorry, can't think of anything.
     
  8. Offline

    javoris767

    Please anyone? Its taking up about 34.xxxms.

    After each vote the CPU usage goes up and just says up. I don't really see what's wrong:/ I'd really appreciate some help.

    Code:
    package me.javoris767.votesql.listeners;
    import java.util.ArrayList;
    import java.util.List;
    import me.javoris767.votesql.VoteSQL;
    import me.javoris767.votesql.utils.Functions;
    import me.javoris767.votesql.utils.VoteSQLAPI;
    import me.javoris767.votesql.utils.VoteSQLChat;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import com.vexsoftware.votifier.model.Vote;
    import com.vexsoftware.votifier.model.VotifierEvent;
    public class VotingListener implements Listener
    {
    private VoteSQL _plugin;
    public VotingListener(VoteSQL plugin)
    {
    _plugin = plugin;
    Bukkit.getServer().getPluginManager().registerEvents(this, _plugin);
    }
    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled=true)
    public void onVote(VotifierEvent event)
    {
    Vote vote = event.getVote();
    String siteVotedOn = vote.getServiceName();
    String username = vote.getUsername();
    Player player = Bukkit.getPlayer(username);
    int money = _plugin.getConfig().getInt("VoteSQL.currency.Amount");
    // Broadcast Vote
    if (_plugin.getConfig().getBoolean("VoteSQL.onVote.messageEnabled") == true)
    {
    VoteSQLChat.broadcastVoteMessage(username, siteVotedOn);
    }
    // Currency
    if (Bukkit.getPluginManager().getPlugin("Vault") == null) {
    VoteSQLChat.logSevere("Vault not found!");
    }else
    if(_plugin.getConfig().getBoolean("VoteSQL.currency.Enabled") == true)
    {
    VoteSQLChat.sendCurrencyReveivedMessage(player, username, money);
    Functions.addMoney(player, _plugin.getConfig().getInt("VoteSQL.currency.Amount"));
    }
    // Add to SQL
    if (_plugin.getConfig().getBoolean("VoteSQL.MySQL.Enabled") == true)
    {
    if(username == "" || username == null) {
    VoteSQLChat.logInfo("Empty vote string");
    }else{
    Functions.addData(username);
    }
    }
    // Custom Commands
    List<String> commands = new ArrayList<String>();
    if(_plugin.getConfig().getBoolean("VoteSQL.onVote.commandsEnabled") == true)
    {
    commands = _plugin.getConfig().getStringList("VoteSQL.onVote.Commands");
    for (String command : commands) {
    if(command.contains("%p")) {
    command = command.replace("%p", player.getName());
    }
    command = Functions.formatMessage(command, vote);
    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
    }
    // Add to FlatFile
    if (_plugin.getConfig().getBoolean("VoteSQL.FlatFile.Enabled") == true)
    {
    Integer numberOfVotes = VoteSQLAPI.voteMap.get(username
    .toLowerCase());
    if(username == "" || username == null) {
    }else{
    numberOfVotes++;
    VoteSQLAPI.voteMap.put(username.toLowerCase(), numberOfVotes);
    VoteSQLAPI.saveDataFile();
    }
    }
    }
    }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  9. Offline

    javoris767

    How do I initialize a class in a runnable o-o?
     
  10. Offline

    Nitnelave

    What do you mean? You can have any class implement Runnable, if you need.
     
  11. Offline

    javoris767

    I don't really get what I'm supposed to do...
     
  12. Offline

    lycano

    javoris767 A runnable would not fix your issue. If your event is not called then it cant bring the CPU time up...

    To see whats happening we need the whole plugin. If you want to do it on your own try finding some stuff about "how to debug java" Then you can see what really happens.
     
  13. Offline

    javoris767

    I've been testing it quite alot and here's the timings :/
    [​IMG]
     
  14. Offline

    lycano

    javoris767 do you have a github repo? Could check the code then.
     
  15. Offline

    javoris767

  16. Offline

    lycano

    javoris767 the github repo does not contain the full source. Some listeners are missing.
     
  17. Offline

    javoris767

    The only listener is VotingListener.
     
  18. Offline

    lycano

    Well the code tells me a different story...

    In VoteSQLAPI.java, which is the logic implementation i believe, i found this

    import me.javoris767.votesql.listeners.PlayerListener;
    import me.javoris767.votesql.listeners.ZAMListener;

    Used in method registerListeners() in L240

    Both files are not in the github repo.
     
  19. Offline

    javoris767

    Oh, that was something I was testing out. I deleted those a while ago.
    Edit: Nevermind, these are just some stuff I was testing out.
     
  20. Offline

    javoris767

    Any ideas?
     
  21. Offline

    lycano

    javoris767 If you deleted it then this code will not compile. You said that your problem is that the performance is not very good but in this condition it wont even compile thus i cant debug it.
     
  22. Offline

    javoris767

    I thought I deleted them, but I didn't delete them. All the files should be updated on my repository.
     
  23. Offline

    lycano

    javoris767 I do believe its a problem of the votifier plugin not yours. I dont see where your actions would result in such delays except that you do establish a connection to the sql server each time you add data (that can slow down the process but should not be that huge when using a local server. You should make use of Pooled Connections (google it) instead).

    Do you get such delays when running in file or mysql mode?

    Also i looked over the code of that votifier plugin .. he executes the VotifierEvent in a scheduled thread in a while loop ... from a thread .... i dont think this is a good way to do cause he will run into trouble when using threads this way ... but well can't change that.

    Also can you tell me what you want to do? From what i can tell i see that you try to log all votes to sql or flatfile ... in that case you should contact the author and request that change as he can implement that way easier than you could do by using the API.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  24. Offline

    javoris767

    lycano Turns out that sending the data to the SQL is the problem. No delays, but it still raises the timings. I'll try the Pooled Connections you suggested :p
     
  25. Offline

    Wingzzz

    If it is creating a new connection everytime you're setting/getting data, that's bad. Establish one connection on startup, then use it. If you try to use it ensure you null check it first, this way at that point you can then say something like (if null, establish) so that it only creates the connection if one doesn't exist(ie: it dropped connection).
     
  26. Offline

    javoris767

    Wingzzz Ohhh, I understand now. If its already connected, on vote check for that :D
     
  27. Offline

    lycano

  28. Offline

    javoris767

  29. Offline

    lycano

    Well, i dont think so. Pooling Connections is really advanced stuff .. something you normally learn after feeling safe in java...

    Anyways to use the miniconnectionpoolmanager you need a datasource. As DataSource is an interface you need to implement it in a class called MysqlDataSource for example.

    Checkout the AuthMe Reloaded Source for more details on that ... for example: https://github.com/Xephi/AuthMeRelo...ami/authme/datasource/MySQLDataSource.java#82

    Checkout how they did it and try to do it on your own. I would not just copy paste it as its not the best coding style and structure but for understanding its enough ^^
     
  30. Offline

    javoris767

    lycano Thanks, I manage to get alot working, but still no performance increase.
     
Thread Status:
Not open for further replies.

Share This Page