1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.wifi; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 22 import org.junit.Before; 23 import org.junit.Test; 24 25 import java.util.Random; 26 27 /** 28 * Unit tests for {@link com.android.server.wifi.WifiLinkLayerStats}. 29 */ 30 public class WifiLinkLayerStatsTest { 31 32 ExtendedWifiInfo mWifiInfo; 33 WifiLinkLayerStats mWifiLinkLayerStats; 34 Random mRandom = new Random(); 35 36 /** 37 * Sets up for unit test 38 */ 39 @Before 40 public void setUp() throws Exception { 41 mWifiInfo = new ExtendedWifiInfo(); 42 mWifiLinkLayerStats = new WifiLinkLayerStats(); 43 } 44 45 /** 46 * Increments the counters 47 * 48 * The values are carved up among the 4 classes (be, bk, vi, vo) so the totals come out right. 49 */ 50 private void bumpCounters(WifiLinkLayerStats s, int txg, int txr, int txb, int rxg) { 51 int a = mRandom.nextInt(31); 52 int b = mRandom.nextInt(31); 53 int m0 = a & b; 54 int m1 = a & ~b; 55 int m2 = ~a & b; 56 int m3 = ~a & ~b; 57 assertEquals(-1, m0 + m1 + m2 + m3); 58 59 s.rxmpdu_be += rxg & m0; 60 s.txmpdu_be += txg & m0; 61 s.lostmpdu_be += txb & m0; 62 s.retries_be += txr & m0; 63 64 s.rxmpdu_bk += rxg & m1; 65 s.txmpdu_bk += txg & m1; 66 s.lostmpdu_bk += txb & m1; 67 s.retries_bk += txr & m1; 68 69 s.rxmpdu_vi += rxg & m2; 70 s.txmpdu_vi += txg & m2; 71 s.lostmpdu_vi += txb & m2; 72 s.retries_vi += txr & m2; 73 74 s.rxmpdu_vo += rxg & m3; 75 s.txmpdu_vo += txg & m3; 76 s.lostmpdu_vo += txb & m3; 77 s.retries_vo += txr & m3; 78 } 79 80 /** 81 * 82 * Check that average rates converge to the right values 83 * 84 * Check that the total packet counts are correct 85 * 86 */ 87 @Test 88 public void checkThatAverageRatesConvergeToTheRightValuesAndTotalsAreRight() throws Exception { 89 int txg = mRandom.nextInt(1000); 90 int txr = mRandom.nextInt(100); 91 int txb = mRandom.nextInt(100); 92 int rxg = mRandom.nextInt(1000); 93 int n = 3 * 5; // Time constant is 3 seconds, 5 times time constant should get 99% there 94 for (int i = 0; i < n; i++) { 95 bumpCounters(mWifiLinkLayerStats, txg, txr, txb, rxg); 96 mWifiLinkLayerStats.timeStampInMs += 1000; 97 mWifiInfo.updatePacketRates(mWifiLinkLayerStats, mWifiLinkLayerStats.timeStampInMs); 98 } 99 // assertEquals(double, double, double) takes a tolerance as the third argument 100 assertEquals((double) txg, mWifiInfo.txSuccessRate, txg * 0.02); 101 assertEquals((double) txr, mWifiInfo.txRetriesRate, txr * 0.02); 102 assertEquals((double) txb, mWifiInfo.txBadRate, txb * 0.02); 103 assertEquals((double) rxg, mWifiInfo.rxSuccessRate, rxg * 0.02); 104 105 assertEquals(mWifiInfo.txSuccess, n * txg); 106 assertEquals(mWifiInfo.txRetries, n * txr); 107 assertEquals(mWifiInfo.txBad, n * txb); 108 assertEquals(mWifiInfo.rxSuccess, n * rxg); 109 } 110 111 /** 112 * A single packet in a short period of time should have small effect 113 */ 114 @Test 115 public void aSinglePacketInAShortPeriodOfTimeShouldHaveSmallEffect() throws Exception { 116 bumpCounters(mWifiLinkLayerStats, 999999999, 999999999, 999999999, 99999999); 117 mWifiLinkLayerStats.timeStampInMs = 999999999; 118 mWifiInfo.updatePacketRates(mWifiLinkLayerStats, mWifiLinkLayerStats.timeStampInMs); 119 assertEquals(0.0, mWifiInfo.txSuccessRate, 0.0001); 120 bumpCounters(mWifiLinkLayerStats, 1, 1, 1, 1); 121 mWifiLinkLayerStats.timeStampInMs += 1; 122 mWifiInfo.updatePacketRates(mWifiLinkLayerStats, mWifiLinkLayerStats.timeStampInMs); 123 assertEquals(0.33, mWifiInfo.txSuccessRate, 0.01); 124 } 125 126 /** 127 * Check for bad interactions with the alternative updatePacketRates method 128 */ 129 @Test 130 public void afterSourceSwitchTheRatesShouldGetReset() throws Exception { 131 // Do some updates using link layer stats 132 bumpCounters(mWifiLinkLayerStats, 999, 999, 999, 999); 133 mWifiLinkLayerStats.timeStampInMs = 999999999; 134 mWifiInfo.updatePacketRates(mWifiLinkLayerStats, mWifiLinkLayerStats.timeStampInMs); 135 assertEquals(0.0, mWifiInfo.txSuccessRate, 0.0001); 136 assertEquals(0.0, mWifiInfo.rxSuccessRate, 0.0001); 137 bumpCounters(mWifiLinkLayerStats, 1_000_000_000, 777000, 66600, 1_000_100_000); 138 mWifiLinkLayerStats.timeStampInMs += 10_000; 139 mWifiInfo.updatePacketRates(mWifiLinkLayerStats, mWifiLinkLayerStats.timeStampInMs); 140 assertTrue("" + mWifiInfo + " " + mWifiLinkLayerStats, mWifiInfo.txSuccessRate > 0.95e+8); 141 assertTrue("" + mWifiInfo + " " + mWifiLinkLayerStats, mWifiInfo.rxSuccessRate > 0.95e+8); 142 // Now update with traffic counters 143 mWifiLinkLayerStats.timeStampInMs += 10_000; 144 mWifiInfo.updatePacketRates(2_000_000_000L, 2_000_000_000L, 145 mWifiLinkLayerStats.timeStampInMs); 146 // Despite the increase, the rates should be zero after the change in source 147 assertEquals(0.0, mWifiInfo.txSuccessRate, 0.0001); 148 assertEquals(0.0, mWifiInfo.rxSuccessRate, 0.0001); 149 assertEquals(0, mWifiInfo.txBad); 150 assertEquals(0, mWifiInfo.txRetries); 151 // Make sure that updates from this source work, too 152 mWifiLinkLayerStats.timeStampInMs += 10_000; 153 mWifiInfo.updatePacketRates(3_000_000_000L, 3_000_000_000L, 154 mWifiLinkLayerStats.timeStampInMs); 155 assertTrue(mWifiInfo.txSuccessRate > 0.95e+8); 156 assertTrue(mWifiInfo.rxSuccessRate > 0.95e+8); 157 // Switch back to using link layer stats 158 mWifiLinkLayerStats.timeStampInMs += 10_000; 159 bumpCounters(mWifiLinkLayerStats, 1_000_000_000, 777000, 66600, 1_000_100_000); 160 mWifiInfo.updatePacketRates(mWifiLinkLayerStats, mWifiLinkLayerStats.timeStampInMs); 161 assertEquals(0.0, mWifiInfo.txSuccessRate, 0.0001); 162 assertEquals(0.0, mWifiInfo.rxSuccessRate, 0.0001); 163 } 164 } 165