| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015 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.os.linux.core.tests.latency; |
| 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.internal.analysis.os.linux.core.latency.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 | } |
| 53 | |
| 54 | /** |
| 55 | * Test incrementing |
| 56 | */ |
| 57 | @Test |
| 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)); |
| 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); |
| 68 | testOnlineVsOffline(fixture); |
| 69 | } |
| 70 | |
| 71 | private static SegmentStoreStatistics getSegStoreStat(List<@NonNull SystemCall> fixture) { |
| 72 | SegmentStoreStatistics sss = new SegmentStoreStatistics(); |
| 73 | for (ISegment seg : fixture) { |
| 74 | sss.update(seg); |
| 75 | } |
| 76 | return sss; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Test decrementing |
| 81 | */ |
| 82 | @Test |
| 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)); |
| 87 | } |
| 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); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Test small |
| 98 | */ |
| 99 | @Test |
| 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)); |
| 104 | } |
| 105 | testOnlineVsOffline(fixture); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Test large |
| 110 | */ |
| 111 | @Test |
| 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)); |
| 116 | } |
| 117 | testOnlineVsOffline(fixture); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Test noise |
| 122 | */ |
| 123 | @Test |
| 124 | public void noiseTest() { |
| 125 | Random rnd = new Random(); |
| 126 | rnd.setSeed(1234); |
| 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)); |
| 132 | } |
| 133 | testOnlineVsOffline(fixture); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Test gaussian noise |
| 138 | */ |
| 139 | @Test |
| 140 | public void gaussianNoiseTest() { |
| 141 | Random rnd = new Random(); |
| 142 | rnd.setSeed(1234); |
| 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)); |
| 149 | } |
| 150 | testOnlineVsOffline(fixture); |
| 151 | } |
| 152 | |
| 153 | private static @NonNull SystemCall createAnonSyscall(int start, int end) { |
| 154 | return new SystemCall(new InitialInfo(start, "", Collections.EMPTY_MAP), end, 0); |
| 155 | } |
| 156 | } |