aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/test/PerformanceTest.java
blob: f9cba46dd92e62daf5751979be5fcbc2e7f57edd (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cz.crcs.ectester.reader.test;

import cz.crcs.ectester.applet.ECTesterApplet;
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.CardMngr;
import cz.crcs.ectester.reader.command.Command;
import cz.crcs.ectester.reader.response.Response;

import javax.smartcardio.CardException;
import java.util.Arrays;

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

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

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

    public static PerformanceTest repeat(CardMngr cardManager, String desc, Command cmd, int count) {
        return new PerformanceTest(cardManager, 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() {
        long baseTime;
        try {
            new Command.SetDryRunMode(cardManager, ECTesterApplet.MODE_DRY_RUN).send();
            testable.run();
            baseTime = testable.getResponse().getDuration();
            testable.reset();
            testable.run();
            baseTime += testable.getResponse().getDuration();
            testable.reset();
            baseTime /= 2;
            new Command.SetDryRunMode(cardManager, ECTesterApplet.MODE_NORMAL).send();
        } catch (CardException ce) {
            throw new TestException(ce);
        }

        times = new long[count];
        reducedTimes = 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();
            reducedTimes[i] = times[i] - baseTime;
            testable.reset();
        }

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

        long[] sorted = reducedTimes.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[] getReducedTimes() {
        return reducedTimes;
    }

    public long getMean() {
        return mean;
    }

    public long getMedian() {
        return median;
    }

    public long getMode() {
        return mode;
    }
}