aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/me/lucko/spark/profiler/SamplerBuilder.java
blob: 7db05151e219d84262d8bdd4aba05e71b10ee532 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package me.lucko.spark.profiler;

import java.util.Timer;
import java.util.concurrent.TimeUnit;

/**
 * Builds {@link Sampler} instances.
 */
public class SamplerBuilder {

    private int samplingInterval = 4;
    private long timeout = -1;
    private ThreadDumper threadDumper = ThreadDumper.ALL;
    private ThreadGrouper threadGrouper = ThreadGrouper.BY_NAME;

    private int ticksOver = -1;
    private TickCounter tickCounter = null;

    public SamplerBuilder() {
    }

    public SamplerBuilder samplingInterval(int samplingInterval) {
        this.samplingInterval = samplingInterval;
        return this;
    }

    public SamplerBuilder completeAfter(long timeout, TimeUnit unit) {
        if (timeout <= 0) {
            throw new IllegalArgumentException("timeout > 0");
        }
        this.timeout = System.currentTimeMillis() + unit.toMillis(timeout);
        return this;
    }

    public SamplerBuilder threadDumper(ThreadDumper threadDumper) {
        this.threadDumper = threadDumper;
        return this;
    }

    public SamplerBuilder threadGrouper(ThreadGrouper threadGrouper) {
        this.threadGrouper = threadGrouper;
        return this;
    }

    public SamplerBuilder ticksOver(int ticksOver, TickCounter tickCounter) {
        this.ticksOver = ticksOver;
        this.tickCounter = tickCounter;
        return this;
    }

    public Sampler start(Timer samplingThread) {
        Sampler sampler;
        if (this.ticksOver != -1 && this.tickCounter != null) {
            sampler = new Sampler(this.samplingInterval, this.threadDumper, this.threadGrouper, this.timeout, this.tickCounter, this.ticksOver);
        } else {
            sampler = new Sampler(this.samplingInterval, this.threadDumper, this.threadGrouper, this.timeout);
        }

        sampler.start(samplingThread);
        return sampler;
    }

}