Collecting Network Traffic Data

1.This lesson teaches you to

  1. Tag Network Requests        标记网络类型
  2. Configure a Network Test Build Type  在as中配置测试模式才能测试网络
  3. Deploy the Network Test APK     在真机上部属网络调试应用
  4. Run Network Traffic Tool        NetWork Traffic 工具 

  The network traffic generated by an app can have a significant impact on the battery life of the device where it is running. In order to optimize that traffic, you need to both measure it and identify its source. Network requests can come directly from a user action, requests from your own app code, or from a server communicating with your app.

 网络连接可能发自本地应用向远程服务器发送请求,也可能来自远程服务器。

  The Network Traffic tool (part of the DDMS tools) enables you to view how and when your app transfers data over a network.

  This lesson shows you how to measure and categorize network requests by tagging your source code, then shows you how to deploy, test and visualize your apps's network traffic.

2.Tag Network Requests 标记网络类型

  Apps use the networking hardware on a device for various reasons. In order to properly optimize your app's use of networking resources, you must understand how frequently your app is using the network and for what reasons. For performance analysis purposes, you should break down use of network hardware into these categories:

 网络请求的3种类型:
  • User-initiated network requests - Requests initiated by the user, such as a user request for an updated articles list in a news app.

    用户手动发起,如更新应用。
  • App-initiated network requests - Requests initiated within Android app code that are not used to immediately satisfy a user action, such as an app request to cache the text of unread articles in a news app.
    应用内部代码向远程服务器发起请求。
  • Server-initiated network requests - Requests initiated by a server to your app that are not used to immediately satisfy a user action, such as notification of a newly available article in a news app.
    远程服务器向应用发送推送。

  This procedure shows you how to tag your app's source code with constants to categorize traffic as one of these three request types. The Network Traffic tool represents each type of traffic with a different color, so you can visualize and optimize each traffic stream separately. The technique described here reports network traffic based on the execution of threads in your app which you identify as a user, app or server source.

 Network Traffic 工具把每种网络请求按颜色分开。

 下面是使用TrafficStats类在代码中标识3种网络请求类型的方法:
  1. In your app's development project, define three constants to represent the different types of network use:

     public static final int USER_INITIATED = 0x1000;
    public static final int APP_INITIATED = 0x2000;
    public static final int SERVER_INITIATED =0x3000;
  2. Find networking code in your app by searching for the most common classes used for this purpose:
    1. In Android Studio, choose Edit > Find > Find in Path.
    2. Paste the following string into the Text to find field:
      extends GcmTaskService|
      extends JobService|
      extends AbstractThreadedSyncAdapter|
      HttpUrlConnection|Volley|Glide|HttpClient
    3. Check Regular expression.
    4. Check File mask(s) and type *.java.
    5. Click the Find button.
  3. Based on your findings in the previous step, tag your app's use of network traffic by adding the setThreadStatsTag(int) method to each execution thread in your app that uses network resources, as shown in the following code example.
     if (BuildConfig.NETWORK-TEST && Build.VERSION.SDK_INT >= ) {
    try {
    TrafficStats.setThreadStatsTag(USER_INITIATED);
    // make network request using HttpClient.execute()
    } finally {
    TrafficStats.clearThreadStatsTag();
    }
    }

    Note: Ensure the tagging does not get into your production code by making inclusion of this code conditional, based on the build type used to generate the APK. In the example above, the BuildConfig.NETWORK-TEST field identifies this APK as a test version.

  Note: This technique for tagging network traffic from your app depends on how the APIs that you are using access and manage network sockets. Some networking libraries may not allow the TrafficStats utilities to tag traffic from your app.

 并不是所有的网络库都支持 TrafficStats 。Network Traffic tool 工具教程 Detailed Network Usage in DDMS

  For more information about tagging and tracking network traffic with the Network Traffic tool, see Detailed Network Usage in DDMS.

3.Configure a Network Test Build Type 在as中配置测试模式才能测试网络

  When you run performance tests, your APK should be as close as possible to the production build. In order to achieve this for your network testing, create a network-test build type, rather than using debug build type.

  1. Open your app in Android Studio.
  2. Create a debuggable build type for your network test by modifying your project's build.gradle file as shown in the following code example:
     android {
    ...
    buildTypes {
    debug {
    // debuggable true is default for the debug buildType
    }
    network-test {
    debuggable true
    }
    }
    ...

4.Deploy the Network Test APK 部属网络测试的apk

  To deploy the APK generated by the network-test build type configured in the previous proceedure:

  1. Check that Developer Options are enabled on your test device. For information about how to check and enable this option, see Using Hardware Devices.
  2. Using a USB cable, connect your test device to your development computer.
  3. In Android Studio, select Build Variants on the left edge of the window.
  4. Click the Sync Project with Gradle Files button to populate the Build Variants list with network-test for the app module.
  5. Choose network-test from the list.
  6. Deploy the debuggable version of your app to your device by choosing Run > Debug.

5.Run Network Traffic Tool

  The Network Traffic tool in Android Studio helps you see how your app uses network resources in real time, while it is running.

  To improve the repeatability of your testing, you should start with a known initial state for your app by clearing app data. The following procedure includes a step that shows you how to clear all app data including previously cached data and networking data. This step puts your app back to a state where it must re-cache all previously cached data. Do not skip this step.

 Network Traffic工具测试app网络请求时,就保证app启动时没有缓存数据。注意,这步是必需的。

  To start the Network Traffic tool and visualize the network requests:

  1. Start the Network Traffic tool by launching Android Studio and choosing Tools > Android > Android Device Monitor. When asked, allow incoming network connections.
  2. In the Android Device Monitor window, click the DDMS button along the top and choose the Network Statistics tab. If you don't see this tab, widen the window and then try Window > Reset Perspective.
  3. Select your app to debug from the list of debuggable apps on your device in the Devices tab, then click theStart button in the Network Statistics tab.

    Note: You may be prompted to Allow USB Debugging on your device. Select OK to allow debugging to proceed.

  4. Clear your app data using the following adb command:
    adb shell pm clear package.name.of.app
  5. Start your app and run a testing plan that exercises your app's primary use cases. Your plan should also allow for app idle time, where the user is not interacting with the app, to allow app-initiated and server-initiated network access to occur.
  6. Repeat the test by clearing the app data and running your test plan again. You should repeat the test a few times to verify the repeatability of your performance data.

  Use of tagging for network traffic helps you visually distinguish each request category by producing a different color for each network traffic in the Network Traffic tool, as shown in Figure 1.

    Figure 1. Network traffic tagged for the three categories.

Android 性能优化(5)网络优化 (1) Collecting Network Traffic Data 用Network Traffic tool :收集传输数据的更多相关文章

  1. Android性能优化典范第二季

      Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitma ...

  2. Android性能优化典范(二)

    Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...

  3. android app性能优化大汇总(google官方Android性能优化典范 - 第2季)

    Google前几天刚发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的 ...

  4. Android性能优化典范 - 第2季

    Google发布了Android性能优化典范第2季的课程,一共20个短视频,包括的内容大致有:电量优化,网络优化,Wear上如何做优化,使用对象池来提高效率,LRU Cache,Bitmap的缩放,缓 ...

  5. Android性能优化问题总结

    性能优化这块,分为UI性能优化.内存优化.数据库优化.网络优化.耗电优化等等.可以从1.如何发现问题,2.怎么解决问题,3.解决效果对比,这几个方面去描述.举个简单例子——UI优化,可以从 UI出现什 ...

  6. Android 性能优化探究

    使用ViewStub动态载入布局.避免一些不常常的视图长期握住引用: ViewStub的一些特点: 1. ViewStub仅仅能Inflate一次,之后ViewStub对象被置空:某个被ViewStu ...

  7. 我把阿里、腾讯、字节跳动、美团等Android性能优化实战整合成了一个PDF文档

    安卓开发大军浩浩荡荡,经过近十年的发展,Android技术优化日异月新,如今Android 11.0 已经发布,Android系统性能也已经非常流畅,可以在体验上完全媲美iOS. 但是,到了各大厂商手 ...

  8. 【腾讯Bugly干货分享】Android性能优化典范——第6季

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/580d91208d80e49771f0a07c 导语 这里是Android性能优 ...

  9. android 性能优化

    本章介绍android高级开发中,对于性能方面的处理.主要包括电量,视图,内存三个性能方面的知识点. 1.视图性能 (1)Overdraw简介 Overdraw就是过度绘制,是指在一帧的时间内(16. ...

  10. Android性能优化典范第一季

    2015年伊始,Google发布了关于Android性能优化典范的专题,一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有关 ...

随机推荐

  1. easyui north 穿透

    穿透layout的north 原理 穿透下面的层只要使该层的position置于fix即可 如果该层还有下一级,则下一层级的position置于absolute即可 示例代码 #menu.active ...

  2. git: 保存帐号信息

    One line command: git config credential.helper store

  3. java编程思想-复用类

    /* 一个文件中只能有一个public类 并且此public类必须与文件名相同 */ class WaterSource { private String s; WaterSource() { Sys ...

  4. 使用Scroller制作滑块开关ToggleButton

    Scroller这个类在自己定义view中使用的还算是非常频繁的,和它名字一样.我们通常是在控制滑动的时候使用Scroller,以便让view滑动起来不那么生硬.在官方的解释上,Scroller是一个 ...

  5. Binary Tree Maximum Path Sum 自底向上求解(重重重重)

    题目: 链接 解答: 自底向上求解.left_max right_max分别返回了左右子树的最大路径和,假设左右子树最大路径和小于0.那么返回零. 用这个最大路径和和根节点的值相加.来更新最大值,同一 ...

  6. C++进阶之虚函数表

    C++通过继承(inheritance)和虚函数(virtual function)来实现多态性.所谓多态,简单地说就是,将基类的指针或引用绑定到子类的实例,然后通过基类的指针或引用调用实际子类的成员 ...

  7. Android Api Demos登顶之路(四十五)Loader-->Cursor

    这个demo演示了类载入器的用法.关于类载入器的使用我们在前面的demo中已经介绍过了 在此再小小的复习一下. 类载入器的使用步骤: * 1.获取类载入器的管理者LoaderManager manag ...

  8. cocos2dx塔防游戏逻辑

    cocos2dx 塔防游戏逻辑 1.欢迎界面 2.tield制作游戏地图,空块设置cantouch属性为1 3.设置地图锚点,把锚点增加一个锚点容器,给怪物的行走函数传入 该锚点容器參数,让怪物依照锚 ...

  9. C语言中结构体变量之间赋值

    近期,我阅读了某新员工小刘写的C语言代码,发现其对结构体变量之间的赋值不是非常熟悉. 对于两个同样类型的结构体变量,他均採用的是逐个成员变量直接赋值的形式.例如以下的代码演示样例: 如上代码所看到的, ...

  10. PCCs系数

    package ai; public class pccs { public static void main(String[] args) { double same[][]=new double[ ...