-
-
Notifications
You must be signed in to change notification settings - Fork 400
Add timings back with a native event #7994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev/feature
Are you sure you want to change the base?
Changes from 2 commits
82338c9
1fffbaa
b97a905
75f97fa
3455349
d005af4
607eb2c
e0530c4
87885e4
c203803
a480d77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ch.njol.skript.events; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.events.bukkit.ProfileCompletedEvent; | ||
import ch.njol.skript.lang.Literal; | ||
import ch.njol.skript.lang.SkriptEvent; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
import ch.njol.skript.registrations.EventValues; | ||
import ch.njol.skript.util.Getter; | ||
|
||
public class EvtProfileEvent extends SkriptEvent { | ||
static { | ||
Skript.registerEvent("Profiler Event Available", EvtProfileEvent.class, ProfileCompletedEvent.class, | ||
"(profile|profiler) [event] (complete|completed|available)" | ||
).description( | ||
"Called after a new profiler event is available" | ||
).since("2.11"); | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
EventValues.registerEventValue( | ||
ProfileCompletedEvent.class, | ||
String.class, | ||
ProfileCompletedEvent::getName | ||
); | ||
|
||
EventValues.registerEventValue( | ||
ProfileCompletedEvent.class, | ||
Number.class, | ||
ProfileCompletedEvent::getDurationMs | ||
); | ||
} | ||
|
||
@Override | ||
public boolean init(final Literal<?>[] args, final int matchedPattern, final ParseResult parser) { | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { return "profile event complete"; } | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public boolean isEventPrioritySupported() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean check(Event event) { | ||
return event instanceof ProfileCompletedEvent; | ||
} | ||
} | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ch.njol.skript.events.bukkit; | ||
|
||
import org.bukkit.event.Event; | ||
import org.bukkit.event.HandlerList; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class ProfileCompletedEvent extends Event { | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static final HandlerList handlers = new HandlerList(); | ||
private final String sectionName; | ||
private final double durationMs; | ||
|
||
public ProfileCompletedEvent(String sectionName, double durationMs) { | ||
this.sectionName = sectionName; | ||
this.durationMs = durationMs; | ||
} | ||
|
||
public String getName() { | ||
return sectionName; | ||
} | ||
|
||
public double getDurationMs() { | ||
return durationMs; | ||
} | ||
|
||
@Override | ||
public @NotNull HandlerList getHandlers() { | ||
return handlers; | ||
} | ||
|
||
public static HandlerList getHandlerList() { | ||
return handlers; | ||
} | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.lang.script.Script; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
|
@@ -137,7 +138,11 @@ protected final Trigger loadCode(SectionNode sectionNode, String name, @Nullable | |
// return the parser to its original state | ||
parser.restoreBackup(parserBackup); | ||
|
||
return new Trigger(parser.getCurrentScript(), name, skriptEvent, triggerItems); | ||
Script script = parser.getCurrentScript(); | ||
Trigger trigger = new Trigger(script, name, skriptEvent, triggerItems); | ||
trigger.setDebugLabel(script.nameAndPath() + ".sk: line " + sectionNode.getLine() + " as part of section"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we be more specific than 'part of section'? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the event name as well here. |
||
|
||
return trigger; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ch.njol.skript.timings; | ||
|
||
import ch.njol.skript.Skript; | ||
|
||
/** | ||
* Profiler instance for the ProfilerAPI | ||
* Equivalent to a Timing | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public class Profiler { | ||
private final String name; | ||
private long startTime = 0; | ||
private long totalTime = 0; | ||
private boolean running = false; | ||
|
||
public Profiler(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void start() { | ||
if (running) return; // prevent double-starts | ||
startTime = System.nanoTime(); | ||
running = true; | ||
} | ||
|
||
public void stop() { | ||
if (!running) return; // prevent stop without start | ||
long endTime = System.nanoTime(); | ||
long duration = endTime - startTime; | ||
totalTime += duration; | ||
running = false; | ||
|
||
Skript.debug("[Profiler] " + name + " took " + (duration / 1_000_000.0) + " ms"); | ||
} | ||
|
||
public long getTimeNanos() { | ||
return totalTime; | ||
} | ||
|
||
public double getTime() { | ||
return totalTime / 1_000_000.0; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package ch.njol.skript.timings; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.events.bukkit.ProfileCompletedEvent; | ||
import ch.njol.skript.timings.Profiler; | ||
import org.bukkit.Bukkit; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Timings alternative for post-timings removal | ||
*/ | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class ProfilerAPI { | ||
|
||
private static volatile boolean enabled = true; | ||
public static final ThreadLocal<Boolean> isFiringProfilerEvent = ThreadLocal.withInitial(() -> false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm not so sure about this design for checking if a trigger is in a profiler event. If the thread changes at all (see skript-reflect or skbee async sections), won't it be unreliable? |
||
|
||
@Nullable | ||
public static Profiler start(String name) { | ||
if (!enabled()) // Timings disabled :( | ||
return null; | ||
Profiler profiler = new Profiler(name); | ||
profiler.start(); | ||
return profiler; | ||
} | ||
|
||
public static void stop(@Nullable Profiler profiler) { | ||
if (profiler == null) // Timings disabled... | ||
return; | ||
profiler.stop(); | ||
|
||
if (isFiringProfilerEvent.get()) return; | ||
isFiringProfilerEvent.set(true); | ||
try { | ||
Bukkit.getPluginManager().callEvent(new ProfileCompletedEvent(profiler.getName(), profiler.getTime())); | ||
} finally { | ||
isFiringProfilerEvent.set(false); | ||
} | ||
} | ||
|
||
public static boolean enabled() { | ||
return enabled; | ||
} | ||
|
||
public static void setEnabled(boolean flag) { | ||
enabled = flag; | ||
} | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.