Skip to content
This repository has been archived by the owner on Jun 22, 2024. It is now read-only.

Commit

Permalink
Added new flags for timings (Slimefun#3246)
Browse files Browse the repository at this point in the history
Co-authored-by: JustAHuman-xD <[email protected]>
Co-authored-by: qwertyuioplkjhgfd <[email protected]>
Co-authored-by: TheBusyBiscuit <[email protected]>
Co-authored-by: J3fftw <[email protected]>
Co-authored-by: Jeffrey <[email protected]>
  • Loading branch information
6 people authored Aug 5, 2023
1 parent 16313ef commit 8bcf073
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,56 @@
import io.github.thebusybiscuit.slimefun4.core.commands.SlimefunCommand;
import io.github.thebusybiscuit.slimefun4.core.commands.SubCommand;
import io.github.thebusybiscuit.slimefun4.core.services.profiler.PerformanceInspector;
import io.github.thebusybiscuit.slimefun4.core.services.profiler.SummaryOrderType;
import io.github.thebusybiscuit.slimefun4.core.services.profiler.inspectors.ConsolePerformanceInspector;
import io.github.thebusybiscuit.slimefun4.core.services.profiler.inspectors.PlayerPerformanceInspector;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;

class TimingsCommand extends SubCommand {

private static final String FLAG_PREFIX = "--";
private final Set<String> flags = new HashSet<>(Arrays.asList("verbose"));
private final Set<String> flags = new HashSet<>(Arrays.asList("verbose", "avg", "low"));

@ParametersAreNonnullByDefault
TimingsCommand(Slimefun plugin, SlimefunCommand cmd) {
super(plugin, cmd, "timings", false);
}

@Override
protected String getDescription() {
protected @Nonnull String getDescription() {
return "commands.timings.description";
}

@Override
public void onExecute(CommandSender sender, String[] args) {
if (sender.hasPermission("slimefun.command.timings") || sender instanceof ConsoleCommandSender) {
if (hasInvalidFlags(sender, args)) {
return;
}
public void onExecute(CommandSender sender, @Nonnull String[] args) {
if (!sender.hasPermission("slimefun.command.timings") && !(sender instanceof ConsoleCommandSender)) {
Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true);
return;
}

boolean verbose = hasFlag(args, "verbose");
if (hasInvalidFlags(sender, args)) {
return;
}

if (verbose && sender instanceof Player) {
Slimefun.getLocalization().sendMessage(sender, "commands.timings.verbose-player", true);
return;
}
boolean verbose = hasFlag(args, "verbose");

Slimefun.getLocalization().sendMessage(sender, "commands.timings.please-wait", true);
if (verbose && sender instanceof Player) {
Slimefun.getLocalization().sendMessage(sender, "commands.timings.verbose-player", true);
return;
}

PerformanceInspector inspector = inspectorOf(sender, verbose);
Slimefun.getProfiler().requestSummary(inspector);
} else {
Slimefun.getLocalization().sendMessage(sender, "messages.no-permission", true);
SummaryOrderType orderType = SummaryOrderType.HIGHEST;

if (hasFlag(args, "avg")) {
orderType = SummaryOrderType.AVERAGE;
} else if (hasFlag(args, "low")) {
orderType = SummaryOrderType.LOWEST;
}

Slimefun.getLocalization().sendMessage(sender, "commands.timings.please-wait", true);

PerformanceInspector inspector = inspectorOf(sender, verbose, orderType);
Slimefun.getProfiler().requestSummary(inspector);
}

@ParametersAreNonnullByDefault
Expand Down Expand Up @@ -87,12 +97,12 @@ private boolean hasFlag(String[] args, String flag) {
}

@Nonnull
private PerformanceInspector inspectorOf(@Nonnull CommandSender sender, boolean verbose) {
@ParametersAreNonnullByDefault
private PerformanceInspector inspectorOf(CommandSender sender, boolean verbose, SummaryOrderType orderType) {
if (sender instanceof Player player) {
return new PlayerPerformanceInspector(player);
return new PlayerPerformanceInspector(player, orderType);
} else {
return new ConsolePerformanceInspector(sender, verbose);
return new ConsolePerformanceInspector(sender, verbose, orderType);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ public interface PerformanceInspector {
*/
boolean isVerbose();

/**
* The order type for the summary of timings.
*
* @return The order type for the summary of timings.
*/
@Nonnull SummaryOrderType getOrderType();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package io.github.thebusybiscuit.slimefun4.core.services.profiler;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
Expand Down Expand Up @@ -64,13 +62,18 @@ public void send(@Nonnull PerformanceInspector sender) {
summarizeTimings(totalTickedBlocks, "block", sender, items, entry -> {
int count = profiler.getBlocksOfId(entry.getKey());
String time = NumberUtils.getAsMillis(entry.getValue());
String message = entry.getKey() + " - " + count + "x (%s)";

if (count > 1) {
String average = NumberUtils.getAsMillis(entry.getValue() / count);
if (count <= 1) {
return String.format(message, time);
}

String average = NumberUtils.getAsMillis(entry.getValue() / count);

return entry.getKey() + " - " + count + "x (" + time + " | avg: " + average + ')';
if (sender.getOrderType() == SummaryOrderType.AVERAGE) {
return String.format(message, average + " | total: " + time);
} else {
return entry.getKey() + " - " + count + "x (" + time + ')';
return String.format(message, time + " | avg: " + average);
}
});

Expand All @@ -91,8 +94,8 @@ public void send(@Nonnull PerformanceInspector sender) {

@ParametersAreNonnullByDefault
private void summarizeTimings(int count, String name, PerformanceInspector inspector, Map<String, Long> map, Function<Map.Entry<String, Long>, String> formatter) {
Stream<Map.Entry<String, Long>> stream = map.entrySet().stream();
List<Entry<String, Long>> results = stream.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).collect(Collectors.toList());
Set<Entry<String, Long>> entrySet = map.entrySet();
List<Entry<String, Long>> results = inspector.getOrderType().sort(profiler, entrySet);
String prefix = count + " " + name + (count != 1 ? 's' : "");

if (inspector instanceof PlayerPerformanceInspector playerPerformanceInspector) {
Expand Down Expand Up @@ -181,22 +184,15 @@ private String getPerformanceRating() {
rest--;
}

builder.append(ChatColor.DARK_GRAY);

for (int i = 0; i < rest; i++) {
builder.append(':');
}

builder.append(" - ");

builder.append(rating.getColor() + ChatUtils.humanize(rating.name()));

builder.append(ChatColor.GRAY);
builder.append(" (");
builder.append(NumberUtils.roundDecimalNumber(percentage));
builder.append("%)");
builder.append(ChatColor.DARK_GRAY)
.append(":".repeat(Math.max(0, rest)))
.append(" - ")
.append(rating.getColor()).append(ChatUtils.humanize(rating.name()))
.append(ChatColor.GRAY)
.append(" (")
.append(NumberUtils.roundDecimalNumber(percentage))
.append("%)");

return builder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.thebusybiscuit.slimefun4.core.services.profiler;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Holds the different types of ordering for summaries.
*
* @author Walshy
*/
public enum SummaryOrderType {

/**
* Sort by highest to the lowest total timings
*/
HIGHEST,
/**
* Sort by lowest to the highest total timings
*/
LOWEST,
/**
* Sort by average timings (highest to lowest)
*/
AVERAGE;

@ParametersAreNonnullByDefault
List<Map.Entry<String, Long>> sort(SlimefunProfiler profiler, Set<Map.Entry<String, Long>> entrySet) {
switch(this) {
case HIGHEST:
return entrySet.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toList());
case LOWEST:
return entrySet.stream()
.sorted(Comparator.comparingLong(Map.Entry::getValue))
.collect(Collectors.toList());
default:
final Map<String, Long> map = new HashMap<>();
for (Map.Entry<String, Long> entry : entrySet) {
int count = profiler.getBlocksOfId(entry.getKey());
long avg = count > 0 ? entry.getValue() / count : entry.getValue();

map.put(entry.getKey(), avg);
}
return map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.thebusybiscuit.slimefun4.core.services.profiler.inspectors;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import io.github.thebusybiscuit.slimefun4.core.services.profiler.SummaryOrderType;
import org.apache.commons.lang.Validate;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
Expand All @@ -28,35 +30,61 @@ public class ConsolePerformanceInspector implements PerformanceInspector {
*/
private final boolean verbose;

/**
* The order type of the timings.
*/
private final SummaryOrderType orderType;

/**
* This creates a new {@link ConsolePerformanceInspector} for the given {@link CommandSender}.
*
* @param console
* The {@link CommandSender}, preferabbly a {@link ConsoleCommandSender}
* The {@link CommandSender}, preferably a {@link ConsoleCommandSender}
* @param verbose
* Whether the summary will be verbose or not
* @param orderType
* The {@link SummaryOrderType} of the timings
*/
public ConsolePerformanceInspector(@Nonnull CommandSender console, boolean verbose) {
@ParametersAreNonnullByDefault
public ConsolePerformanceInspector(CommandSender console, boolean verbose, SummaryOrderType orderType) {
Validate.notNull(console, "CommandSender cannot be null");
Validate.notNull(orderType, "SummaryOrderType cannot be null");

this.console = console;
this.verbose = verbose;
this.orderType = orderType;
}

/**
* {@inheritDoc}
*/
@Override
public boolean isValid() {
// The console is always "online".
return true;
}

/**
* {@inheritDoc}
*/
@Override
public boolean isVerbose() {
return verbose;
}

/**
* {@inheritDoc}
*/
@Override
public @Nonnull SummaryOrderType getOrderType() {
return orderType;
}

/**
* {@inheritDoc}
*/
@Override
public void sendMessage(@Nonnull String msg) {
console.sendMessage(msg);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import io.github.thebusybiscuit.slimefun4.core.services.profiler.SummaryOrderType;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
Expand All @@ -27,34 +28,62 @@ public class PlayerPerformanceInspector implements PerformanceInspector {
*/
private final UUID uuid;

/**
* The order type of the timings.
*/
private final SummaryOrderType orderType;

/**
* This creates a new {@link PlayerPerformanceInspector} for the given {@link Player}.
*
* @param player
* The {@link Player}
* @param orderType
* The {@link SummaryOrderType} of the timings
*/
public PlayerPerformanceInspector(@Nonnull Player player) {
public PlayerPerformanceInspector(@Nonnull Player player, @Nonnull SummaryOrderType orderType) {
Validate.notNull(player, "Player cannot be null");
Validate.notNull(orderType, "SummaryOrderType cannot be null");

this.uuid = player.getUniqueId();
this.orderType = orderType;
}

@Nullable
private Player getPlayer() {
/**
* {@inheritDoc}
*/
private @Nullable Player getPlayer() {
return Bukkit.getPlayer(uuid);
}

/**
* {@inheritDoc}
*/
@Override
public boolean isValid() {
Player player = getPlayer();
return player != null && player.isOnline();
}

/**
* {@inheritDoc}
*/
@Override
public boolean isVerbose() {
return false;
}

/**
* {@inheritDoc}
*/
@Override
public @Nonnull SummaryOrderType getOrderType() {
return orderType;
}

/**
* {@inheritDoc}
*/
@Override
public void sendMessage(@Nonnull String msg) {
Player player = getPlayer();
Expand Down

0 comments on commit 8bcf073

Please sign in to comment.