Skip to content

Commit

Permalink
add more metrics to find slowdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
jagrosh committed Sep 8, 2022
1 parent 09598cd commit feedf8a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
38 changes: 30 additions & 8 deletions src/main/java/com/jagrosh/giveawaybot/GiveawayListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.jagrosh.interactions.responses.InteractionResponse;
import com.jagrosh.interactions.responses.MessageCallback;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -42,6 +44,7 @@
public class GiveawayListener implements InteractionsListener
{
private final Logger log = LoggerFactory.getLogger(GiveawayListener.class);
private final Map<String,Long> metrics = new HashMap<>();
private final GiveawayBot bot;

public GiveawayListener(GiveawayBot bot)
Expand Down Expand Up @@ -106,8 +109,10 @@ private InteractionResponse onButton(Interaction interaction)
String customId = interaction.getComponentData().getCustomId();
if(customId.equalsIgnoreCase(GiveawayManager.ENTER_BUTTON_ID))
{
long timeStart = System.nanoTime();
long id = interaction.getMessage().getIdLong();
Giveaway g = bot.getDatabase().getGiveaway(id);
long timeGive = System.nanoTime();
if(g == null)
return GBCommand.respondError(LocalizedMessage.ERROR_GIVEAWAY_ENDED.getLocalizedMessage(interaction.getEffectiveLocale()));

Expand All @@ -116,16 +121,22 @@ private InteractionResponse onButton(Interaction interaction)
log.debug(String.format("Giveaway guild/channel ids don't match for giveaway %d! Giveaway: %d/%d Interaction: %d/%d", g.getMessageId(), g.getGuildId(), g.getChannelId(), interaction.getGuildId(), interaction.getChannelId()));

int entered = bot.getDatabase().addEntry(id, interaction.getUser());
if(entered >= 0)
return new MessageCallback(bot.getGiveawayManager().renderGiveaway(g, entered), true);
else
return new MessageCallback(new SentMessage.Builder()
long timeEnter = System.nanoTime();
MessageCallback msg = entered >= 0
? new MessageCallback(bot.getGiveawayManager().renderGiveaway(g, entered), true)
: new MessageCallback(new SentMessage.Builder()
.setReferenceMessage(id)
.setContent(LocalizedMessage.ERROR_GIVEAWAY_ALREADY_ENTERED.getLocalizedMessage(interaction.getEffectiveLocale()))
.addComponent(new ActionRowComponent(new ButtonComponent(ButtonComponent.Style.DANGER,
LocalizedMessage.GIVEAWAY_LEAVE.getLocalizedMessage(interaction.getEffectiveLocale()),
GiveawayManager.LEAVE_BUTTON_ID + ":" + id)))
.setEphemeral(true).build());
long timeRender = System.nanoTime();
increaseMetricValue("ButtonTotalTime", timeRender - timeStart);
increaseMetricValue("ButtonRenderTime", timeRender - timeEnter);
increaseMetricValue("ButtonEnterTime", timeEnter - timeGive);
increaseMetricValue("ButtonRetrieveTime", timeGive - timeStart);
return msg;
}
else if(customId.toLowerCase().startsWith(GiveawayManager.LEAVE_BUTTON_ID.toLowerCase()))
{
Expand All @@ -148,13 +159,16 @@ else if(interaction.getChannelId() == bot.getControlChannel())
case "view-statistics":
long total = Runtime.getRuntime().totalMemory() / 1024 / 1024;
long used = total - (Runtime.getRuntime().freeMemory() / 1024 / 1024);
long uptime = ManagementFactory.getRuntimeMXBean().getUptime() / 1000;
return new MessageCallback(new SentMessage.Builder()
.setContent("```css"
+ "\nUptime: " + FormatUtil.secondsToTime(ManagementFactory.getRuntimeMXBean().getUptime() / 1000).replace("*", "")
+ "\nMemory: " + used + "mb / " + total + "mb"
+ "\nUptime : " + FormatUtil.secondsToTime(uptime).replace("*", "")
+ "\nMemory : " + used + "mb / " + total + "mb"
+ "\nGiveaways: " + bot.getDatabase().countAllGiveaways()
+ "\nAvg Req: " + (interaction.getClient().getMetrics().getOrDefault("TotalTime", 0L) / interaction.getClient().getMetrics().getOrDefault("TotalRequests", 1L) * 1e-9)
+ "\nMetrics: " + interaction.getClient().getMetrics()
+ "\nAvg Req : " + (interaction.getClient().getMetrics().getOrDefault("TotalTime", 0L) / interaction.getClient().getMetrics().getOrDefault("TotalRequests", 1L) * 1e-9)
+ "\nReq/Sec : " + (interaction.getClient().getMetrics().getOrDefault("TotalRequests", 0L) / uptime)
+ "\nMetrics : " + interaction.getClient().getMetrics()
+ "\nMetrics2 : " + metrics
+ "\n```")
.setEphemeral(true).build());
case "shutdown":
Expand All @@ -169,4 +183,12 @@ else if(interaction.getChannelId() == bot.getControlChannel())
}
return GBCommand.respondError(LocalizedMessage.ERROR_GENERIC.getLocalizedMessage(interaction.getEffectiveLocale()));
}

private synchronized long increaseMetricValue(String key, long count)
{
long val = metrics.getOrDefault(key, 0L);
val += count;
metrics.put(key, val);
return val;
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/jagrosh/giveawaybot/data/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class Database
private final EntityManagerFactory emf;
private final EntityManager em;
private final Map<Long, GiveawayEntries> cachedEntries = new HashMap<>();
//private final Map<Long, Giveaway> cachedGiveaways = new HashMap<>();
private final ScheduledExecutorService cacheCombiner = Executors.newSingleThreadScheduledExecutor();

public Database(String host, String user, String pass)
Expand All @@ -53,7 +54,7 @@ public Database(String host, String user, String pass)
em.getMetamodel().managedType(Giveaway.class);
em.getMetamodel().managedType(GiveawayEntries.class);
em.getMetamodel().managedType(GuildSettings.class);
cacheCombiner.scheduleWithFixedDelay(() -> syncEntries(), 30, 30, TimeUnit.SECONDS);
cacheCombiner.scheduleWithFixedDelay(() -> syncEntries(), 60, 60, TimeUnit.SECONDS);
}

public void shutdown()
Expand Down Expand Up @@ -135,6 +136,11 @@ public synchronized void setGuildEmoji(long guildId, String emoji)
// giveaways
public Giveaway getGiveaway(long id)
{
//if(cachedGiveaways.containsKey(id))
// return cachedGiveaways.get(id);
//Giveaway g = em.find(Giveaway.class, id);
//cachedGiveaways.put(id, g);
//return g;
return em.find(Giveaway.class, id);
}

Expand Down

0 comments on commit feedf8a

Please sign in to comment.