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