1/* 2 * Copyright (C) 2015 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 17package android.net; 18 19import com.google.caliper.BeforeExperiment; 20import com.google.caliper.Param; 21 22public class NetworkStatsBenchmark { 23 private static final String UNDERLYING_IFACE = "wlan0"; 24 private static final String TUN_IFACE = "tun0"; 25 private static final int TUN_UID = 999999999; 26 27 @Param({"100", "1000"}) 28 private int mSize; 29 private NetworkStats mNetworkStats; 30 31 @BeforeExperiment 32 protected void setUp() throws Exception { 33 mNetworkStats = new NetworkStats(0, mSize + 2); 34 int uid = 0; 35 NetworkStats.Entry recycle = new NetworkStats.Entry(); 36 for (int i = 0; i < mSize; i++) { 37 recycle.iface = (i < mSize / 2) ? TUN_IFACE : UNDERLYING_IFACE; 38 recycle.uid = uid; 39 recycle.set = i % 2; 40 recycle.tag = NetworkStats.TAG_NONE; 41 recycle.rxBytes = 60000; 42 recycle.rxPackets = 60; 43 recycle.txBytes = 150000; 44 recycle.txPackets = 1500; 45 recycle.operations = 0; 46 mNetworkStats.addValues(recycle); 47 if (recycle.set == 1) { 48 uid++; 49 } 50 } 51 recycle.iface = UNDERLYING_IFACE; 52 recycle.uid = TUN_UID; 53 recycle.set = NetworkStats.SET_FOREGROUND; 54 recycle.tag = NetworkStats.TAG_NONE; 55 recycle.rxBytes = 90000 * mSize; 56 recycle.rxPackets = 40 * mSize; 57 recycle.txBytes = 180000 * mSize; 58 recycle.txPackets = 1200 * mSize; 59 recycle.operations = 0; 60 mNetworkStats.addValues(recycle); 61 } 62 63 public void timeMigrateTun(int reps) { 64 for (int i = 0; i < reps; i++) { 65 NetworkStats stats = mNetworkStats.clone(); 66 stats.migrateTun(TUN_UID, TUN_IFACE, UNDERLYING_IFACE); 67 } 68 } 69 70 /** 71 * Since timeMigrateTun() includes a clone() call on the NetworkStats object, 72 * we need to measure the cost of the clone() call itself in order to get more 73 * accurate measurement on the migrateTun() method. 74 */ 75 public void timeClone(int reps) { 76 for (int i = 0; i < reps; i++) { 77 NetworkStats stats = mNetworkStats.clone(); 78 } 79 } 80} 81