summaryrefslogtreecommitdiff
path: root/src/main/java/org/javacs/Profiler.java
blob: 742ee358392fcded4fea187894f904194cbefec9 (plain)
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
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 {
    Set<URI> files = new HashSet<>();
    Map<TaskEvent.Kind, Instant> started = new EnumMap<>(TaskEvent.Kind.class);
    Map<TaskEvent.Kind, Duration> profile = new EnumMap<>(TaskEvent.Kind.class);

    @Override
    public void started(TaskEvent e) {
        started.put(e.getKind(), Instant.now());
        files.add(e.getSourceFile().toUri());
        // TODO log file name when we compile something that wasn't in the batch
    }

    @Override
    public void finished(TaskEvent e) {
        var k = e.getKind();
        var start = started.getOrDefault(k, Instant.now());
        var elapsed = Duration.between(start, Instant.now());
        var soFar = profile.getOrDefault(k, Duration.ZERO);
        var total = soFar.plus(elapsed);
        profile.put(k, total);
    }

    void print() {
        var lines = new StringJoiner("; ");
        for (var k : TaskEvent.Kind.values()) {
            if (!profile.containsKey(k)) continue;
            var elapsed = profile.get(k);
            var s = elapsed.getSeconds() + elapsed.getNano() / 1000.0 / 1000.0 / 1000.0;
            lines.add(String.format("%s: %.3fs", k, s));
        }
        // TODO log names if n is small
        LOG.info(String.format("Compiled %d files: %s", files.size(), lines));
    }

    private static final Logger LOG = Logger.getLogger("main");
}