From be91aae0425ecd3452cde1a56545bab4fc8b61b5 Mon Sep 17 00:00:00 2001 From: HypherionMC Date: Mon, 23 Oct 2023 21:01:55 +0200 Subject: [PATCH] [CHORE] Implement code profiler to find bottlenecks in Verification System --- gradle.properties | 2 +- .../core/accounts/MinecraftAccount.java | 20 +++++--- .../sdlink/core/util/Profiler.java | 47 +++++++++++++++++++ 3 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/hypherionmc/sdlink/core/util/Profiler.java diff --git a/gradle.properties b/gradle.properties index a3ab4af..6aa675f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ version_major=0 version_minor=0 -version_patch=26 +version_patch=27 shade_group=com.hypherionmc.sdlink.shaded. diff --git a/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java b/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java index e4f2283..83aa639 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java +++ b/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java @@ -6,15 +6,14 @@ package com.hypherionmc.sdlink.core.accounts; import com.hypherionmc.sdlink.core.config.SDLinkConfig; import com.hypherionmc.sdlink.core.database.SDLinkAccount; -import com.hypherionmc.sdlink.core.discord.BotController; import com.hypherionmc.sdlink.core.managers.CacheManager; import com.hypherionmc.sdlink.core.managers.RoleManager; import com.hypherionmc.sdlink.core.messaging.Result; +import com.hypherionmc.sdlink.core.util.Profiler; import com.hypherionmc.sdlink.core.util.SDLinkUtils; import com.mojang.authlib.GameProfile; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.UserSnowflake; import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.NotNull; @@ -117,9 +116,12 @@ public class MinecraftAccount { } public SDLinkAccount getStoredAccount() { + Profiler profiler = Profiler.getProfiler("getStoredAccount"); + profiler.start("Load Stored Account"); sdlinkDatabase.reloadCollection("verifiedaccounts"); SDLinkAccount account = sdlinkDatabase.findById(this.uuid.toString(), SDLinkAccount.class); + profiler.stop(); return account == null ? newDBEntry() : account; } @@ -151,16 +153,17 @@ public class MinecraftAccount { @Nullable public DiscordUser getDiscordUser() { + Profiler profiler = Profiler.getProfiler("getDiscordUser"); + profiler.start("Loading Discord User"); SDLinkAccount storedAccount = getStoredAccount(); if (storedAccount == null || SDLinkUtils.isNullOrEmpty(storedAccount.getDiscordID())) return null; - if (CacheManager.getDiscordMembers().isEmpty()) { - User user = BotController.INSTANCE.getJDA().getUserById(storedAccount.getDiscordID()); - return user == null ? null : DiscordUser.of(user.getEffectiveName(), user.getEffectiveAvatarUrl(), user.getIdLong(), user.getAsMention()); - } + if (CacheManager.getDiscordMembers().isEmpty()) + return null; Optional member = CacheManager.getDiscordMembers().stream().filter(m -> m.getId().equalsIgnoreCase(storedAccount.getDiscordID())).findFirst(); + profiler.stop(); return member.map(value -> DiscordUser.of(value.getEffectiveName(), value.getEffectiveAvatarUrl(), value.getIdLong(), value.getAsMention())).orElse(null); } @@ -244,7 +247,10 @@ public class MinecraftAccount { return Result.error("memberNotFound"); } + if (!SDLinkConfig.INSTANCE.accessControl.requiredRoles.isEmpty() && !RoleManager.getVerificationRoles().isEmpty()) { + Profiler profiler = Profiler.getProfiler("checkRequiredRoles"); + profiler.start("Checking Required Roles"); AtomicBoolean anyFound = new AtomicBoolean(false); Optional member = CacheManager.getDiscordMembers().stream().filter(m -> m.getId().equals(account.getDiscordID())).findFirst(); @@ -255,7 +261,7 @@ public class MinecraftAccount { } } })); - + profiler.stop(); if (!anyFound.get()) return Result.error("rolesNotFound"); diff --git a/src/main/java/com/hypherionmc/sdlink/core/util/Profiler.java b/src/main/java/com/hypherionmc/sdlink/core/util/Profiler.java new file mode 100644 index 0000000..4518d7c --- /dev/null +++ b/src/main/java/com/hypherionmc/sdlink/core/util/Profiler.java @@ -0,0 +1,47 @@ +package com.hypherionmc.sdlink.core.util; + +import com.hypherionmc.sdlink.core.config.SDLinkConfig; +import com.hypherionmc.sdlink.core.discord.BotController; + +import java.util.concurrent.TimeUnit; + +public class Profiler { + + private long startTime; + private final String profilerName; + private boolean hasStarted = false; + private String message = ""; + + private Profiler(String profilerName) { + this.profilerName = profilerName; + this.hasStarted = false; + } + + public static Profiler getProfiler(String name) { + return new Profiler(name); + } + + public void start(String message) { + if (!SDLinkConfig.INSTANCE.generalConfig.debugging) + return; + + this.message = message; + this.startTime = System.nanoTime(); + this.hasStarted = true; + } + + public void stop() { + if (!SDLinkConfig.INSTANCE.generalConfig.debugging) + return; + + if (!hasStarted) { + BotController.INSTANCE.getLogger().error("[Profiler (" + this.profilerName + ")] was not started"); + return; + } + + long stopTime = System.nanoTime(); + BotController.INSTANCE.getLogger().info("[Profiler (" + this.profilerName + ")] " + message + " took " + TimeUnit.SECONDS.toSeconds(stopTime - startTime) + " seconds"); + hasStarted = false; + } + +}