Repository navigation
Expand file tree
/
Copy pathProfiler.java
More file actions
64 lines (55 loc) · 1.87 KB
/
Copy pathProfiler.java
File metadata and controls
64 lines (55 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package org.javacs;
import com.sun.source.util.*;
import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.logging.Logger;
class Profiler implements TaskListener {
static boolean quiet = false;
Set<URI> files = new HashSet<>();
TaskEvent current;
Instant started = Instant.EPOCH;
Map<TaskEvent.Kind, Duration> profile = new EnumMap<>(TaskEvent.Kind.class);
@Override
public void started(TaskEvent e) {
files.add(e.getSourceFile().toUri());
endCurrent();
current = e;
started = Instant.now();
}
@Override
public void finished(TaskEvent e) {
endCurrent();
}
private void endCurrent() {
if (current == null) return;
var soFar = profile.getOrDefault(current.getKind(), Duration.ZERO);
var add = Duration.between(started, Instant.now());
var total = soFar.plus(add);
profile.put(current.getKind(), total);
current = null;
started = Instant.EPOCH;
}
void print() {
if (quiet) return;
var lines = new StringJoiner("; ");
for (var kind : TaskEvent.Kind.values()) {
if (!profile.containsKey(kind)) continue;
var elapsed = profile.get(kind);
var s = elapsed.getSeconds() + elapsed.getNano() / 10e9;
lines.add(String.format("%s: %.3fs", kind, s));
}
// If n is small, print each file name
if (files.size() <= 3) {
var names = new StringJoiner(", ");
for (var f : files) {
names.add(Parser.fileName(f));
}
LOG.info(String.format("...compiled %s: %s", names, lines));
} else {
LOG.info(String.format("...compiled %d files: %s", files.size(), lines));
}
}
private static final Logger LOG = Logger.getLogger("main");
}