Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2011 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.framework.tests;
     18 
     19 import com.android.tradefed.log.LogUtil.CLog;
     20 
     21 import java.util.ArrayList;
     22 import java.util.Collection;
     23 import java.util.List;
     24 
     25 /**
     26  * Simple container class used to store network Stats.
     27  */
     28 public class BandwidthStats {
     29     private long mRxBytes = 0;
     30     private long mRxPackets = 0;
     31     private long mTxBytes = 0;
     32     private long mTxPackets = 0;
     33 
     34     public BandwidthStats (long rxBytes, long rxPackets, long txBytes, long txPackets) {
     35         mRxBytes = rxBytes;
     36         mRxPackets = rxPackets;
     37         mTxBytes = txBytes;
     38         mTxPackets = txPackets;
     39     }
     40 
     41     public BandwidthStats() {
     42     }
     43 
     44     /**
     45      * Compute percent difference between a and b.
     46      * @param a
     47      * @param b
     48      * @return % difference of a and b.
     49      */
     50     static float computePercentDifference(float a, float b) {
     51         if (a == b) {
     52             return 0;
     53         }
     54         if (a == 0) {
     55             CLog.d("Invalid value for a: %f", a);
     56             return Float.MIN_VALUE;
     57         }
     58         return ( a - b) / a * 100;
     59     }
     60 
     61     public long getRxBytes() {
     62         return mRxBytes;
     63     }
     64 
     65     public void setRxBytes(long rxBytes) {
     66         this.mRxBytes = rxBytes;
     67     }
     68 
     69     public long getRxPackets() {
     70         return mRxPackets;
     71     }
     72 
     73     public void setRxPackets(long rxPackets) {
     74         this.mRxPackets = rxPackets;
     75     }
     76 
     77     public long getTxBytes() {
     78         return mTxBytes;
     79     }
     80 
     81     public void setTxBytes(long txBytes) {
     82         this.mTxBytes = txBytes;
     83     }
     84 
     85     public long getTxPackets() {
     86         return mTxPackets;
     87     }
     88 
     89     public void setTxPackets(long txPackets) {
     90         this.mTxPackets = txPackets;
     91     }
     92 
     93     public CompareResult compareAll(BandwidthStats stats, float mDifferenceThreshold) {
     94         CompareResult result = new CompareResult();
     95         result.addRecord(this.compareRb(stats), mDifferenceThreshold);
     96         result.addRecord(this.compareRp(stats), mDifferenceThreshold);
     97         result.addRecord(this.compareTb(stats), mDifferenceThreshold);
     98         result.addRecord(this.compareTp(stats), mDifferenceThreshold);
     99         return result;
    100     }
    101 
    102     private ComparisonRecord compareTp(BandwidthStats stats) {
    103         float difference = BandwidthStats.computePercentDifference(
    104                 this.mTxPackets, stats.mTxPackets);
    105 
    106         ComparisonRecord result = new ComparisonRecord("Tp", this.mTxPackets, stats.mTxPackets,
    107                 difference);
    108 
    109         return result;
    110     }
    111 
    112     private ComparisonRecord compareTb(BandwidthStats stats) {
    113         float difference = BandwidthStats.computePercentDifference(
    114                 this.mTxBytes, stats.mTxBytes);
    115 
    116         ComparisonRecord result = new ComparisonRecord("Tb", this.mTxBytes, stats.mTxBytes,
    117                 difference);
    118 
    119         return result;
    120     }
    121 
    122     private ComparisonRecord compareRp(BandwidthStats stats) {
    123         float difference = BandwidthStats.computePercentDifference(
    124                 this.mRxPackets, stats.mRxPackets);
    125 
    126         ComparisonRecord result = new ComparisonRecord("Rp", this.mRxPackets, stats.mRxPackets,
    127                 difference);
    128 
    129         return result;
    130     }
    131 
    132     private ComparisonRecord compareRb(BandwidthStats stats) {
    133         float difference = BandwidthStats.computePercentDifference(
    134                 this.mRxBytes, stats.mRxBytes);
    135 
    136         ComparisonRecord result = new ComparisonRecord("Rb", this.mRxBytes, stats.mRxBytes,
    137                 difference);
    138 
    139         return result;
    140     }
    141 
    142     /**
    143      * Holds the record of comparing a single stat like transmitted packets. All comparisons are
    144      * recorded so it is easier to see which one failed later.
    145      */
    146     public static class ComparisonRecord {
    147         private String mStatName;
    148         private long mFirst;
    149         private long mSecond;
    150         private float mDifference;
    151 
    152         public ComparisonRecord(String statName, long first, long second, float difference) {
    153             this.mStatName = statName;
    154             this.mFirst = first;
    155             this.mSecond = second;
    156             this.mDifference = difference;
    157         }
    158 
    159         public String getStatName() {
    160             return mStatName;
    161         }
    162 
    163         public long getFirst() {
    164             return mFirst;
    165         }
    166 
    167         public long getSecond() {
    168             return mSecond;
    169         }
    170 
    171         public float getDifference() {
    172             return mDifference;
    173         }
    174 
    175         @Override
    176         public String toString() {
    177             return String.format("%s expected %d actual %d difference is %f%%",
    178                     mStatName, mFirst, mSecond, mDifference);
    179         }
    180     }
    181 
    182     /**
    183      * Holds the result of comparing bandwidth stats from different sources. The result is passed
    184      * if all individual stats are within a threshold. Also keeps track of which individual stats
    185      * failed the comparison for debugging purposes.
    186      *
    187      */
    188     public static class CompareResult {
    189         private boolean mPassed;
    190         private List<ComparisonRecord> mFailures;
    191 
    192         public CompareResult() {
    193             mPassed = true;
    194             mFailures = new ArrayList<ComparisonRecord>();
    195         }
    196 
    197         public void addRecord(ComparisonRecord record, float threshold) {
    198             if (record.getDifference() < threshold) {
    199                 mPassed &= true;
    200             } else {
    201                 mPassed = false;
    202                 mFailures.add(record);
    203             }
    204         }
    205 
    206         public boolean getResult() {
    207             return mPassed;
    208         }
    209 
    210         public Collection<ComparisonRecord> getFailures() {
    211             return mFailures;
    212         }
    213     }
    214 }
    215