1 package org.slf4j.profiler;
2
3
4
5 /**
6 *
7 * This demo illustrates usage of SLF4J profilers.
8 *
9 * <p>
10 * We have been given the task of generating a large number, say N,
11 * of random integers. We need to transform that array into a smaller array
12 * containing only prime numbers. The new array has to be sorted.
13 *
14 * <p>
15 * While tackling this problem, we would like to measure the
16 * time spent in each subtask.
17 *
18 * <p>
19 * A typical output for this demo would be:
20 <pre>
21 + Profiler [DEMO]
22 |-- elapsed time [RANDOM] 0.089 seconds.
23 |---+ Profiler [SORT_AND_PRUNE]
24 |-- elapsed time [SORT] 0.221 seconds.
25 |-- elapsed time [PRUNE_COMPOSITES] 11.567 seconds.
26 |-- Subtotal [SORT_AND_PRUNE] 11.788 seconds.
27 |-- elapsed time [SORT_AND_PRUNE] 11.788 seconds.
28 |-- Total [DEMO] 11.877 seconds.
29 </pre>
30 *
31 * @author Ceki Gulcu
32 */
33 public class NestedProfilerDemo {
34
35 public static void main(String[] args) {
36 // create a profiler called "DEMO"
37 Profiler profiler = new Profiler("DEMO");
38
39 // register this profiler in the thread context's profiler registry
40 ProfilerRegistry profilerRegistry = ProfilerRegistry.getThreadContextInstance();
41 profiler.registerWith(profilerRegistry);
42
43 // start a stopwatch called "RANDOM"
44 profiler.start("RANDOM");
45 RandomIntegerArrayGenerator riaGenerator = new RandomIntegerArrayGenerator();
46 int n = 10*1000;
47 int[] randomArray = riaGenerator.generate(n);
48
49 // create and start a nested profiler called "SORT_AND_PRUNE"
50 // By virtue of its parent-child relationship with the "DEMO"
51 // profiler, and the previous registration of the parent profiler,
52 // this nested profiler will be automatically registered
53 // with the thread context's profiler registry
54 profiler.startNested(SortAndPruneComposites.NESTED_PROFILER_NAME);
55
56 SortAndPruneComposites pruner = new SortAndPruneComposites(randomArray);
57 pruner.sortAndPruneComposites();
58
59 // stop and print the "DEMO" printer
60 profiler.stop().print();
61 }
62 }