-
-
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
Open
JIBSIL
wants to merge
11
commits into
SkriptLang:dev/feature
Choose a base branch
from
JIBSIL:master
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
82338c9
Add native event timings
JIBSIL 1fffbaa
Add native event timings
JIBSIL b97a905
Fix spacing issues
JIBSIL 75f97fa
Remove erroneous import
JIBSIL 3455349
Fix version on profiler event
JIBSIL d005af4
Add javadocs to ProfileCompletedEvent
JIBSIL 607eb2c
Remove references to timings in documentation
JIBSIL e0530c4
Remove final from profile event init
JIBSIL 87885e4
Fix method order
JIBSIL c203803
Be more specific with Section debug info
JIBSIL a480d77
Merge branch 'master' of https://github.com/JIBSIL/SkriptWithTimings
JIBSIL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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("INSERT VERSION"); | ||
|
||
EventValues.registerEventValue( | ||
ProfileCompletedEvent.class, | ||
String.class, | ||
ProfileCompletedEvent::getName | ||
); | ||
|
||
EventValues.registerEventValue( | ||
ProfileCompletedEvent.class, | ||
Number.class, | ||
ProfileCompletedEvent::getDurationMs | ||
); | ||
} | ||
|
||
@Override | ||
public boolean init(Literal<?>[] args, int matchedPattern, ParseResult parser) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean check(Event event) { | ||
return event instanceof ProfileCompletedEvent; | ||
} | ||
|
||
@Override | ||
public boolean isEventPrioritySupported() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return "profile event complete"; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/ch/njol/skript/events/bukkit/ProfileCompletedEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ch.njol.skript.events.bukkit; | ||
|
||
import org.bukkit.event.Event; | ||
import org.bukkit.event.HandlerList; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Represents a custom event triggered when a profiling section is completed. | ||
* This event holds information about the name of the profiling section and | ||
* the time it took to complete in milliseconds. | ||
*/ | ||
public class ProfileCompletedEvent extends Event { | ||
private static final HandlerList handlers = new HandlerList(); | ||
private final String sectionName; | ||
private final double durationMs; | ||
|
||
/** | ||
* Constructs a new ProfileCompletedEvent. | ||
* @param sectionName the name of the profiling section | ||
* @param durationMs the duration in milliseconds the section took to execute | ||
*/ | ||
public ProfileCompletedEvent(String sectionName, double durationMs) { | ||
this.sectionName = sectionName; | ||
this.durationMs = durationMs; | ||
} | ||
|
||
/** | ||
* Gets the name of the profiling section that was completed. | ||
* @return the name of the completed profiling section | ||
*/ | ||
public String getName() { | ||
return sectionName; | ||
} | ||
|
||
/** | ||
* Gets the duration in milliseconds that the profiling section took to complete. | ||
* @return the execution time of the section in milliseconds | ||
*/ | ||
public double getDurationMs() { | ||
return durationMs; | ||
} | ||
|
||
/** | ||
* Gets the list of handlers registered for this event. | ||
* @return the list of handlers | ||
*/ | ||
@Override | ||
public @NotNull HandlerList getHandlers() { | ||
return handlers; | ||
} | ||
|
||
/** | ||
* Gets the static handler list for this event class. | ||
* @return the static handler list | ||
*/ | ||
public static HandlerList getHandlerList() { | ||
return handlers; | ||
} | ||
JIBSIL marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package ch.njol.skript.timings; | ||
|
||
import ch.njol.skript.Skript; | ||
|
||
/** | ||
* Profiler instance for the ProfilerAPI | ||
* Each profiler times how long it takes for a given trigger to complete, | ||
* along with the invocator script information | ||
*/ | ||
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
|
||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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; | ||
|
||
/** | ||
* Profiler API for timing how long a given event takes | ||
*/ | ||
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
|
||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.