forked from wesleyegberto/java-new-features
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJFREventStreamTest.java
79 lines (66 loc) · 2.02 KB
/
JFREventStreamTest.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.time.Duration;
import java.util.stream.IntStream;
import java.util.ArrayList;
import jdk.jfr.consumer.RecordingStream;
/***
* The profiles configurations is stored in: `$JAVA_HOME/lib/jrf/`
*/
public class JFREventStreamTest {
public static void main(String[] args) throws InterruptedException {
var task = new FibonacciCalculator(Thread.currentThread());
var thread = new Thread(task, "t-fib");
thread.start();
try (var rs = new RecordingStream()) {
rs.enable("jdk.CPULoad").withPeriod(Duration.ofSeconds(1));
rs.enable("jdk.GarbageCollection").withPeriod(Duration.ofSeconds(1));
rs.enable("jdk.GCHeapSummary").withPeriod(Duration.ofSeconds(1));
rs.onEvent("jdk.CPULoad", event -> {
System.out.printf("\tCPU load: %f%% - %f%%%n",
(event.getFloat("jvmUser") * 100),
(event.getFloat("machineTotal") * 100)
);
});
rs.onEvent("jdk.GCHeapSummary", event -> {
System.out.printf("\tGC - Summary - %s %f MB%n",
event.getString("when"),
(event.getFloat("heapUsed") / 1_000_000)
);
});
rs.onEvent("jdk.GarbageCollection", event -> {
System.out.printf("\tGC - Cause %s - Durantion: %.04fms%n", event.getString("cause"), (event.getFloat("duration") / 1_000_000));
});
rs.startAsync();
thread.join();
}
}
}
class FibonacciCalculator implements Runnable {
private Thread thread;
FibonacciCalculator(Thread thread) {
this.thread = thread;
}
public void run() {
System.out.println("Starting calculation");
int number = 40;
while (true) {
System.out.println("Calculating for " + number);
var result = calculate(number);
System.out.println("Result for " + number + " is " + result);
if (!thread.isAlive()) {
System.out.println("Ending calculation");
break;
}
number += 5;
}
}
/**
* Bad implementation to take much time.
*/
public long calculate(int n) {
if (n <= 1)
return 1;
var list = new ArrayList<>(n);
IntStream.range(0, n).forEach(__ -> list.add(new Object()));
return calculate(n - 1) + calculate(n - 2);
}
}