Skip to content

Commit 25baff9

Browse files
committed
Timings, with multithreading support now
1 parent 08344b6 commit 25baff9

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

src/main/java/ch/njol/skript/timings/Timing.java

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,86 @@
2121

2222
package ch.njol.skript.timings;
2323

24+
import java.lang.ref.WeakReference;
2425
import java.util.ArrayList;
26+
import java.util.HashMap;
2527
import java.util.List;
28+
import java.util.Map;
29+
30+
import ch.njol.skript.Skript;
2631

2732
import com.google.common.collect.Lists;
2833

2934
public class Timing {
3035

3136
public class Capture {
3237

33-
Timing parent;
38+
final String thread;
39+
40+
long start;
41+
long end;
42+
43+
long paused; // Time while paused
44+
long pauseBegin;
3445

35-
protected Capture(Timing parent) {
36-
this.parent = parent;
46+
protected Capture(final Thread thread) {
47+
String name = thread.getName();
48+
assert name != null;
49+
this.thread = name;
3750
}
3851

3952
public void start() {
53+
start = System.nanoTime();
54+
}
55+
56+
public void stop() {
57+
end = System.nanoTime();
58+
}
59+
60+
public void pause() {
61+
pauseBegin = System.nanoTime();
62+
}
63+
64+
public void unpause() {
65+
long pauseTime = System.nanoTime() - pauseBegin;
66+
paused += pauseTime;
67+
}
68+
69+
public long result() {
70+
if (end == 0L)
71+
end = System.nanoTime();
4072

73+
return end - start - paused;
74+
}
75+
76+
public boolean isOf(Thread t) {
77+
return (thread.equals(t.getName()));
4178
}
4279
}
4380

4481
private List<Capture> captures = new ArrayList<Capture>();
82+
private Map<Thread,Capture> inProgress;
4583

4684
/**
47-
* Creates a new timing. Only used for {@link Timings}
85+
* Creates a new timing. Only used by {@link Timings}
4886
*/
4987
protected Timing() {
5088
captures = new ArrayList<Capture>();
89+
inProgress = new HashMap<Thread,Capture>();
5190
}
5291

53-
/**
54-
* Creates a capture for timing.
55-
* @return
56-
*/
57-
public Capture capture() {
58-
return new Capture(this);
92+
public Timing start() {
93+
Thread current = Thread.currentThread();
94+
if (inProgress.containsKey(current)) {
95+
inProgress.get(current).stop();
96+
inProgress.remove(current);
97+
}
98+
99+
Capture c = new Capture(current);
100+
c.start();
101+
captures.add(c);
102+
inProgress.put(current, c);
103+
104+
return this;
59105
}
60106
}

0 commit comments

Comments
 (0)