Commit | Line | Data |
---|---|---|
e06c9955 | 1 | /******************************************************************************* |
658401c8 | 2 | * Copyright (c) 2016 Ericsson |
e06c9955 MK |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | *******************************************************************************/ | |
9 | ||
658401c8 | 10 | package org.eclipse.tracecompass.analysis.timing.core.tests.segmentstore.statistics; |
e06c9955 MK |
11 | |
12 | import static org.junit.Assert.assertEquals; | |
13 | ||
14 | import java.util.ArrayList; | |
e06c9955 MK |
15 | import java.util.List; |
16 | import java.util.Random; | |
17 | ||
18 | import org.eclipse.jdt.annotation.NonNull; | |
5b901f94 | 19 | import org.eclipse.tracecompass.analysis.timing.core.segmentstore.statistics.SegmentStoreStatistics; |
660d4ed9 | 20 | import org.eclipse.tracecompass.segmentstore.core.BasicSegment; |
e06c9955 MK |
21 | import org.eclipse.tracecompass.segmentstore.core.ISegment; |
22 | import org.junit.Test; | |
23 | ||
24 | /** | |
25 | * Test the staticsmodule. This is done with two tests. | |
26 | * <ol> | |
27 | * <li>test the values vs some sample points calculated by hand (sanity test) | |
28 | * </li> | |
29 | * <li>2- test exhaustively vs a reference implementation.</li> | |
30 | * </ol> | |
31 | * | |
32 | * @author Matthew Khouzam | |
33 | * | |
34 | */ | |
35 | public class SegmentStoreStatisticsTest { | |
36 | ||
37 | private static final int MEDIUM_AMOUNT_OF_SEGMENTS = 100; | |
38 | private static final int LARGE_AMOUNT_OF_SEGMENTS = 1000000; | |
39 | ||
40 | private static final double NO_ERROR = 0.0; | |
41 | private static final double ERROR = 0.000001; | |
42 | ||
660d4ed9 | 43 | private static void testOnlineVsOffline(List<@NonNull BasicSegment> fixture) { |
e06c9955 MK |
44 | SegmentStoreStatistics sss = getSegStoreStat(fixture); |
45 | OfflineStatisticsCalculator osc = new OfflineStatisticsCalculator(fixture); | |
46 | assertEquals("Average", osc.getAvg(), sss.getAverage(), ERROR); | |
47 | assertEquals("Standard Deviation", osc.getStdDev(), sss.getStdDev(), ERROR); | |
48 | assertEquals("Min", osc.getMin(), sss.getMin()); | |
49 | assertEquals("Max", osc.getMax(), sss.getMax()); | |
a1e4b7e8 BH |
50 | assertEquals("Min Segment", osc.getMin(), sss.getMinSegment().getLength()); |
51 | assertEquals("Max Segment", osc.getMax(), sss.getMaxSegment().getLength()); | |
e06c9955 MK |
52 | } |
53 | ||
54 | /** | |
55 | * Test incrementing | |
56 | */ | |
57 | @Test | |
58 | public void climbTest() { | |
660d4ed9 | 59 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); |
e06c9955 | 60 | for (int i = 0; i < MEDIUM_AMOUNT_OF_SEGMENTS; i++) { |
660d4ed9 | 61 | fixture.add(createDummySegment(i, i * 2)); |
e06c9955 MK |
62 | } |
63 | SegmentStoreStatistics sss = getSegStoreStat(fixture); | |
64 | assertEquals("Average", 49.5, sss.getAverage(), ERROR); | |
65 | assertEquals("Min", 0, sss.getMin()); | |
66 | assertEquals("Max", 99, sss.getMax()); | |
67 | assertEquals("Standard Deviation", 29.0, sss.getStdDev(), 0.02); | |
a1e4b7e8 BH |
68 | assertEquals("Min Segment", 0, sss.getMinSegment().getLength()); |
69 | assertEquals("Max Segment", 99, sss.getMaxSegment().getLength()); | |
e06c9955 MK |
70 | testOnlineVsOffline(fixture); |
71 | } | |
72 | ||
660d4ed9 | 73 | private static SegmentStoreStatistics getSegStoreStat(List<@NonNull BasicSegment> fixture) { |
e06c9955 MK |
74 | SegmentStoreStatistics sss = new SegmentStoreStatistics(); |
75 | for (ISegment seg : fixture) { | |
76 | sss.update(seg); | |
77 | } | |
78 | return sss; | |
79 | } | |
80 | ||
81 | /** | |
82 | * Test decrementing | |
83 | */ | |
84 | @Test | |
85 | public void decrementingTest() { | |
660d4ed9 | 86 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); |
e06c9955 | 87 | for (int i = MEDIUM_AMOUNT_OF_SEGMENTS; i >= 0; i--) { |
660d4ed9 | 88 | fixture.add(createDummySegment(i, i * 2)); |
e06c9955 MK |
89 | } |
90 | SegmentStoreStatistics sss = getSegStoreStat(fixture); | |
91 | assertEquals("Average", 50, sss.getAverage(), NO_ERROR); | |
92 | assertEquals("Min", 0, sss.getMin()); | |
93 | assertEquals("Max", 100, sss.getMax()); | |
94 | assertEquals("Standard Deviation", 29.3, sss.getStdDev(), 0.01); | |
a1e4b7e8 BH |
95 | assertEquals("Min Segment", 0, sss.getMinSegment().getLength()); |
96 | assertEquals("Max Segment", 100, sss.getMaxSegment().getLength()); | |
e06c9955 MK |
97 | testOnlineVsOffline(fixture); |
98 | } | |
99 | ||
100 | /** | |
101 | * Test small | |
102 | */ | |
103 | @Test | |
104 | public void smallTest() { | |
660d4ed9 | 105 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); |
e06c9955 | 106 | for (int i = 1; i >= 0; i--) { |
660d4ed9 | 107 | fixture.add(createDummySegment(i, i * 2)); |
e06c9955 MK |
108 | } |
109 | testOnlineVsOffline(fixture); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Test large | |
114 | */ | |
115 | @Test | |
116 | public void largeTest() { | |
660d4ed9 | 117 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); |
e06c9955 | 118 | for (int i = 1; i <= LARGE_AMOUNT_OF_SEGMENTS; i++) { |
660d4ed9 | 119 | fixture.add(createDummySegment(i, i * 2)); |
e06c9955 MK |
120 | } |
121 | testOnlineVsOffline(fixture); | |
122 | } | |
123 | ||
124 | /** | |
125 | * Test noise | |
126 | */ | |
127 | @Test | |
128 | public void noiseTest() { | |
129 | Random rnd = new Random(); | |
130 | rnd.setSeed(1234); | |
660d4ed9 | 131 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); |
e06c9955 MK |
132 | for (int i = 1; i <= LARGE_AMOUNT_OF_SEGMENTS; i++) { |
133 | int start = Math.abs(rnd.nextInt(100000000)); | |
134 | int end = start + Math.abs(rnd.nextInt(1000000)); | |
660d4ed9 | 135 | fixture.add(createDummySegment(start, end)); |
e06c9955 MK |
136 | } |
137 | testOnlineVsOffline(fixture); | |
138 | } | |
139 | ||
140 | /** | |
141 | * Test gaussian noise | |
142 | */ | |
143 | @Test | |
144 | public void gaussianNoiseTest() { | |
145 | Random rnd = new Random(); | |
146 | rnd.setSeed(1234); | |
660d4ed9 | 147 | List<@NonNull BasicSegment> fixture = new ArrayList<>(); |
e06c9955 MK |
148 | for (int i = 1; i <= LARGE_AMOUNT_OF_SEGMENTS; i++) { |
149 | int start = Math.abs(rnd.nextInt(100000000)); | |
150 | final int delta = Math.abs(rnd.nextInt(1000)); | |
151 | int end = start + delta * delta; | |
660d4ed9 | 152 | fixture.add(createDummySegment(start, end)); |
e06c9955 MK |
153 | } |
154 | testOnlineVsOffline(fixture); | |
155 | } | |
156 | ||
660d4ed9 MK |
157 | private static @NonNull BasicSegment createDummySegment(int start, int end) { |
158 | return new BasicSegment(start, end); | |
e06c9955 MK |
159 | } |
160 | } |