1 /*******************************************************************************
2 * Copyright (c) 2015 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
.os
.linux
.core
.tests
.latency
;
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
.internal
.analysis
.os
.linux
.core
.latency
.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());
58 public void climbTest() {
59 List
<@NonNull SystemCall
> fixture
= new ArrayList
<>();
60 for (int i
= 0; i
< MEDIUM_AMOUNT_OF_SEGMENTS
; i
++) {
61 fixture
.add(createAnonSyscall(i
, i
* 2));
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);
68 testOnlineVsOffline(fixture
);
71 private static SegmentStoreStatistics
getSegStoreStat(List
<@NonNull SystemCall
> fixture
) {
72 SegmentStoreStatistics sss
= new SegmentStoreStatistics();
73 for (ISegment seg
: fixture
) {
83 public void decrementingTest() {
84 List
<@NonNull SystemCall
> fixture
= new ArrayList
<>();
85 for (int i
= MEDIUM_AMOUNT_OF_SEGMENTS
; i
>= 0; i
--) {
86 fixture
.add(createAnonSyscall(i
, i
* 2));
88 SegmentStoreStatistics sss
= getSegStoreStat(fixture
);
89 assertEquals("Average", 50, sss
.getAverage(), NO_ERROR
);
90 assertEquals("Min", 0, sss
.getMin());
91 assertEquals("Max", 100, sss
.getMax());
92 assertEquals("Standard Deviation", 29.3, sss
.getStdDev(), 0.01);
93 testOnlineVsOffline(fixture
);
100 public void smallTest() {
101 List
<@NonNull SystemCall
> fixture
= new ArrayList
<>();
102 for (int i
= 1; i
>= 0; i
--) {
103 fixture
.add(createAnonSyscall(i
, i
* 2));
105 testOnlineVsOffline(fixture
);
112 public void largeTest() {
113 List
<@NonNull SystemCall
> fixture
= new ArrayList
<>();
114 for (int i
= 1; i
<= LARGE_AMOUNT_OF_SEGMENTS
; i
++) {
115 fixture
.add(createAnonSyscall(i
, i
* 2));
117 testOnlineVsOffline(fixture
);
124 public void noiseTest() {
125 Random rnd
= new Random();
127 List
<@NonNull SystemCall
> fixture
= new ArrayList
<>();
128 for (int i
= 1; i
<= LARGE_AMOUNT_OF_SEGMENTS
; i
++) {
129 int start
= Math
.abs(rnd
.nextInt(100000000));
130 int end
= start
+ Math
.abs(rnd
.nextInt(1000000));
131 fixture
.add(createAnonSyscall(start
, end
));
133 testOnlineVsOffline(fixture
);
137 * Test gaussian noise
140 public void gaussianNoiseTest() {
141 Random rnd
= new Random();
143 List
<@NonNull SystemCall
> fixture
= new ArrayList
<>();
144 for (int i
= 1; i
<= LARGE_AMOUNT_OF_SEGMENTS
; i
++) {
145 int start
= Math
.abs(rnd
.nextInt(100000000));
146 final int delta
= Math
.abs(rnd
.nextInt(1000));
147 int end
= start
+ delta
* delta
;
148 fixture
.add(createAnonSyscall(start
, end
));
150 testOnlineVsOffline(fixture
);
153 private static @NonNull SystemCall
createAnonSyscall(int start
, int end
) {
154 return new SystemCall(new InitialInfo(start
, "", Collections
.EMPTY_MAP
), end
, 0);