1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.analysis
.timing
.core
.tests
.segmentstore
.statistics
;
12 import static org
.junit
.Assert
.assertEquals
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Collections
;
16 import java
.util
.List
;
17 import java
.util
.Random
;
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
;
27 * Test the staticsmodule. This is done with two tests.
29 * <li>test the values vs some sample points calculated by hand (sanity test)
31 * <li>2- test exhaustively vs a reference implementation.</li>
34 * @author Matthew Khouzam
37 public class SegmentStoreStatisticsTest
{
39 private static final int MEDIUM_AMOUNT_OF_SEGMENTS
= 100;
40 private static final int LARGE_AMOUNT_OF_SEGMENTS
= 1000000;
42 private static final double NO_ERROR
= 0.0;
43 private static final double ERROR
= 0.000001;
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());
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));
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
);
75 private static SegmentStoreStatistics
getSegStoreStat(List
<@NonNull SystemCall
> fixture
) {
76 SegmentStoreStatistics sss
= new SegmentStoreStatistics();
77 for (ISegment seg
: fixture
) {
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));
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
);
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));
111 testOnlineVsOffline(fixture
);
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));
123 testOnlineVsOffline(fixture
);
130 public void noiseTest() {
131 Random rnd
= new Random();
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
));
139 testOnlineVsOffline(fixture
);
143 * Test gaussian noise
146 public void gaussianNoiseTest() {
147 Random rnd
= new Random();
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
));
156 testOnlineVsOffline(fixture
);
159 private static @NonNull SystemCall
createAnonSyscall(int start
, int end
) {
160 return new SystemCall(new InitialInfo(start
, "", Collections
.EMPTY_MAP
), end
, 0);