aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/test/PerformanceTest.java
blob: 9abaadcb933d2cc7396e8775a1a006ab4eb5f49d (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package cz.crcs.ectester.reader.test;

import cz.crcs.ectester.common.test.Result;
import cz.crcs.ectester.common.test.SimpleTest;
import cz.crcs.ectester.common.test.TestCallback;
import cz.crcs.ectester.common.test.TestException;
import cz.crcs.ectester.reader.command.Command;
import cz.crcs.ectester.reader.response.Response;

import java.util.Arrays;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class PerformanceTest extends SimpleTest<CommandTestable> {
    private long[] times;
    private Response[] responses;
    private long mean;
    private long median;
    private long mode;
    private int count;
    private String desc;

    private PerformanceTest(CommandTestable testable, int count, String desc) {
        super(testable, new TestCallback<CommandTestable>() {
            @Override
            public Result apply(CommandTestable testable) {
                return new Result(Result.Value.SUCCESS);
            }
        });
        this.count = count;
        this.desc = desc;
    }

    public static PerformanceTest repeat(Command cmd, int count) {
        return new PerformanceTest(new CommandTestable(cmd), count, null);
    }

    public static PerformanceTest repeat(String desc, Command cmd, int count) {
        return new PerformanceTest(new CommandTestable(cmd), count, desc);
    }

    @Override
    public String getDescription() {
        String rest = String.format("Mean = %d ns, Median = %d ns, Mode = %d ns", mean, median, mode);
        return (desc == null ? rest : desc + " (" + rest + ")");
    }

    @Override
    protected void runSelf() {
        times = new long[count];
        responses = new Response[count];
        for (int i = 0; i < count; ++i) {
            testable.run();
            responses[i] = testable.getResponse();
            times[i] = responses[i].getDuration();
            testable.reset();
        }

        mean = Arrays.stream(times).sum() / count;

        long[] sorted = times.clone();
        Arrays.sort(sorted);
        if (count % 2 == 0) {
            median = (sorted[(count / 2) - 1] + sorted[count / 2]) / 2;
        } else {
            median = sorted[count / 2];
        }

        long max_occurences = 0;
        int i = 0;
        while (i < count) {
            long current_value = sorted[i];
            long current_occurences = 0;
            while (i < count && sorted[i] == current_value) {
                i++;
                current_occurences++;
            }
            if (current_occurences > max_occurences) {
                max_occurences = current_occurences;
                mode = current_value;
            }
        }
        result = callback.apply(testable);
    }

    public long getCount() {
        return count;
    }

    public Command getCommand() {
        return testable.getCommand();
    }

    public Response[] getResponses() {
        return responses;
    }

    public long[] getTimes() {
        return times;
    }

    public long getMean() {
        return mean;
    }

    public long getMedian() {
        return median;
    }

    public long getMode() {
        return mode;
    }
}