aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/music/util/TimeMeasure.java
blob: cbb04ff660ad884797841300a35effff76c74ce7 (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
package org.onap.music.util;

import org.apache.commons.lang3.tuple.Pair;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;

public interface TimeMeasure {
    void enter(String context);

    void exit();

    /*
        Return a map of measure contexts to a list of 101 percentiles [0,100]
     */
    Map<String, ArrayList<Double>> percentiles();

    /*
        Returns a map of measure contexts to <mean, sme>
     */
    Map<String, Pair<Double, Double>> stats();
}

class TimeMeasureExample
{
    public static void main(String[] args) {
        TimeMeasure tm = new SamplerHistogramTimeMeasure();
        double x = 0;

        tm.enter("A");
        for (int i = 0; i < 100000; i++) {
            tm.enter("B");
            tm.enter("C");
            x += ThreadLocalRandom.current().nextDouble(100);
            tm.exit();
            tm.exit();
        }
        tm.enter("C");
        tm.exit();
        tm.exit();

        System.out.println(x);
        Map<String, ArrayList<Double>> e = tm.percentiles();
        Map<String, Pair<Double, Double>> m = tm.stats();
        DecimalFormat df = new DecimalFormat("000.000000");
        e.forEach((k,v) -> System.out.println("" + k + "\t\t: " + Arrays.toString(v.stream().map(w -> "" + df.format(w)).toArray())));
        m.forEach((k,v) -> System.out.println("" + k + "\t\t: " + df.format(v.getLeft()) + " (" + df.format(v.getRight()) + ")"));
    }
}