[CHORE] Implement code profiler to find bottlenecks in Verification System

This commit is contained in:
2023-10-23 21:01:55 +02:00
parent e68a70b3b2
commit be91aae042
3 changed files with 61 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
version_major=0 version_major=0
version_minor=0 version_minor=0
version_patch=26 version_patch=27
shade_group=com.hypherionmc.sdlink.shaded. shade_group=com.hypherionmc.sdlink.shaded.

View File

@@ -6,15 +6,14 @@ package com.hypherionmc.sdlink.core.accounts;
import com.hypherionmc.sdlink.core.config.SDLinkConfig; import com.hypherionmc.sdlink.core.config.SDLinkConfig;
import com.hypherionmc.sdlink.core.database.SDLinkAccount; 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.CacheManager;
import com.hypherionmc.sdlink.core.managers.RoleManager; import com.hypherionmc.sdlink.core.managers.RoleManager;
import com.hypherionmc.sdlink.core.messaging.Result; import com.hypherionmc.sdlink.core.messaging.Result;
import com.hypherionmc.sdlink.core.util.Profiler;
import com.hypherionmc.sdlink.core.util.SDLinkUtils; import com.hypherionmc.sdlink.core.util.SDLinkUtils;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.UserSnowflake; import net.dv8tion.jda.api.entities.UserSnowflake;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -117,9 +116,12 @@ public class MinecraftAccount {
} }
public SDLinkAccount getStoredAccount() { public SDLinkAccount getStoredAccount() {
Profiler profiler = Profiler.getProfiler("getStoredAccount");
profiler.start("Load Stored Account");
sdlinkDatabase.reloadCollection("verifiedaccounts"); sdlinkDatabase.reloadCollection("verifiedaccounts");
SDLinkAccount account = sdlinkDatabase.findById(this.uuid.toString(), SDLinkAccount.class); SDLinkAccount account = sdlinkDatabase.findById(this.uuid.toString(), SDLinkAccount.class);
profiler.stop();
return account == null ? newDBEntry() : account; return account == null ? newDBEntry() : account;
} }
@@ -151,16 +153,17 @@ public class MinecraftAccount {
@Nullable @Nullable
public DiscordUser getDiscordUser() { public DiscordUser getDiscordUser() {
Profiler profiler = Profiler.getProfiler("getDiscordUser");
profiler.start("Loading Discord User");
SDLinkAccount storedAccount = getStoredAccount(); SDLinkAccount storedAccount = getStoredAccount();
if (storedAccount == null || SDLinkUtils.isNullOrEmpty(storedAccount.getDiscordID())) if (storedAccount == null || SDLinkUtils.isNullOrEmpty(storedAccount.getDiscordID()))
return null; return null;
if (CacheManager.getDiscordMembers().isEmpty()) { if (CacheManager.getDiscordMembers().isEmpty())
User user = BotController.INSTANCE.getJDA().getUserById(storedAccount.getDiscordID()); return null;
return user == null ? null : DiscordUser.of(user.getEffectiveName(), user.getEffectiveAvatarUrl(), user.getIdLong(), user.getAsMention());
}
Optional<Member> member = CacheManager.getDiscordMembers().stream().filter(m -> m.getId().equalsIgnoreCase(storedAccount.getDiscordID())).findFirst(); Optional<Member> 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); 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"); return Result.error("memberNotFound");
} }
if (!SDLinkConfig.INSTANCE.accessControl.requiredRoles.isEmpty() && !RoleManager.getVerificationRoles().isEmpty()) { if (!SDLinkConfig.INSTANCE.accessControl.requiredRoles.isEmpty() && !RoleManager.getVerificationRoles().isEmpty()) {
Profiler profiler = Profiler.getProfiler("checkRequiredRoles");
profiler.start("Checking Required Roles");
AtomicBoolean anyFound = new AtomicBoolean(false); AtomicBoolean anyFound = new AtomicBoolean(false);
Optional<Member> member = CacheManager.getDiscordMembers().stream().filter(m -> m.getId().equals(account.getDiscordID())).findFirst(); Optional<Member> member = CacheManager.getDiscordMembers().stream().filter(m -> m.getId().equals(account.getDiscordID())).findFirst();
@@ -255,7 +261,7 @@ public class MinecraftAccount {
} }
} }
})); }));
profiler.stop();
if (!anyFound.get()) if (!anyFound.get())
return Result.error("rolesNotFound"); return Result.error("rolesNotFound");

View File

@@ -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;
}
}