|
21 | 21 |
|
22 | 22 | package ch.njol.skript.timings;
|
23 | 23 |
|
| 24 | +import java.lang.ref.WeakReference; |
24 | 25 | import java.util.ArrayList;
|
| 26 | +import java.util.HashMap; |
25 | 27 | import java.util.List;
|
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import ch.njol.skript.Skript; |
26 | 31 |
|
27 | 32 | import com.google.common.collect.Lists;
|
28 | 33 |
|
29 | 34 | public class Timing {
|
30 | 35 |
|
31 | 36 | public class Capture {
|
32 | 37 |
|
33 |
| - Timing parent; |
| 38 | + final String thread; |
| 39 | + |
| 40 | + long start; |
| 41 | + long end; |
| 42 | + |
| 43 | + long paused; // Time while paused |
| 44 | + long pauseBegin; |
34 | 45 |
|
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; |
37 | 50 | }
|
38 | 51 |
|
39 | 52 | 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(); |
40 | 72 |
|
| 73 | + return end - start - paused; |
| 74 | + } |
| 75 | + |
| 76 | + public boolean isOf(Thread t) { |
| 77 | + return (thread.equals(t.getName())); |
41 | 78 | }
|
42 | 79 | }
|
43 | 80 |
|
44 | 81 | private List<Capture> captures = new ArrayList<Capture>();
|
| 82 | + private Map<Thread,Capture> inProgress; |
45 | 83 |
|
46 | 84 | /**
|
47 |
| - * Creates a new timing. Only used for {@link Timings} |
| 85 | + * Creates a new timing. Only used by {@link Timings} |
48 | 86 | */
|
49 | 87 | protected Timing() {
|
50 | 88 | captures = new ArrayList<Capture>();
|
| 89 | + inProgress = new HashMap<Thread,Capture>(); |
51 | 90 | }
|
52 | 91 |
|
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; |
59 | 105 | }
|
60 | 106 | }
|
0 commit comments