用了这个第三方库
https://github.com/ChromiumWebApps/chromium/tree/master/build/android/pylib

然后如下代码:

# -*- coding: utf8 -*-import os, time#, sysfrom pylib import android_commands, surface_stats_collectorresultList = []deviceText = os.popen('adb devices')textList = deviceText.readlines()deviceName = textList[1].split()[0]adb = android_commands.AndroidCommands(deviceName)collector = surface_stats_collector.SurfaceStatsCollector(adb, activityName,0.5)collector.DisableWarningAboutEmptyData()collector.Start()for i in range(50):    #循环50次,主要方便自己的实现,其他实现方法请另行实现;    time.sleep(0.3)    results = collector.SampleResults()    if not results:        pass    else:         resultList.append(int(results[0].value))        results[0].print_str()collector.Stop()a = resultList[3:-3]print u"平均值:"+str(float(sum(a)/len(a)))+u" ; 最小值:"+str(min(a))

代码写得不怎的,请无视。

运行上述脚本,在对应的activity操作,即可统计当前操作的fps值了。

跑一边看看:

关于 android 通过 python 统计 fps_第1张图片

看库的源码,原理是使用"adb shell dumpsys SurfaceFlinger --latency "进行循环统计实现的。

代码中有如下注释:

def _GetSurfaceFlingerFrameData(self):    """Returns collected SurfaceFlinger frame timing data.    Returns:      A tuple containing:      - The display's nominal refresh period in seconds.      - A list of timestamps signifying frame presentation times in seconds.      The return value may be (None, None) if there was no data collected (for      example, if the app was closed before the collector thread has finished).    """    # adb shell dumpsys SurfaceFlinger --latency     # prints some information about the last 128 frames displayed in    # that window.    # The data returned looks like this:    # 16954612    # 7657467895508   7657482691352   7657493499756    # 7657484466553   7657499645964   7657511077881    # 7657500793457   7657516600576   7657527404785    # (...)    #    # The first line is the refresh period (here 16.95 ms), it is followed    # by 128 lines w/ 3 timestamps in nanosecond each:    # A) when the app started to draw    # B) the vsync immediately preceding SF submitting the frame to the h/w    # C) timestamp immediately after SF submitted that frame to the h/w    #    # The difference between the 1st and 3rd timestamp is the frame-latency.    # An interesting data is when the frame latency crosses a refresh period    # boundary, this can be calculated this way:    #    # ceil((C - A) / refresh-period)    #    # (each time the number above changes, we have a "jank").    # If this happens a lot during an animation, the animation appears    # janky, even if it runs at 60 fps in average.    #    # We use the special "SurfaceView" window name because the statistics for    # the activity's main window are not updated when the main web content is    # composited into a SurfaceView.    results = self._adb.RunShellCommand('dumpsys SurfaceFlinger --latency ' + self._activity)    if not len(results):      return (None, None)

英语烂,脑容量有限,一直看不明白,有大神帮帮忙解析一下哈(原谅我伸手党)。



 =================================================以下是回复帖=================================================================

1.

循环50次, 间隔0.3s, 得到50个数据。然后掐头去尾, 计算下平均值。好像是大概10s内的fps平均值。
adb shell dumpsys SurfaceFlinger 本身能打印出fps 里面会有更细致的帧数计算数据。细节我也了解的不太多。

surface_stats_collector.SurfaceStatsCollector函数里面应该有你想要的逻辑。 你可以帖一下欣赏下。

2.

一般某个过程发生了丢帧, 是在几秒内的, 10s有点太长, 不过还可以接受. 大部分的卡顿都在2s以上. 10s内平均也能差不多通过平均值看出来.

更多相关文章

  1. android操作sdcard
  2. android 获取本机号码及应加的权限 代码
  3. 代码设置RelativeLayout相对位置设置
  4. android瀑布流代码
  5. 【Android】监听SDCard安装和卸载的代码片段(测试通过)
  6. Android 操作权限大全 (android.permission)
  7. android : 控件在代码中设置属性-setWidth(int pixels)或setHeig
  8. 自定义Activity标题栏(Title bar)和窗体显示状态操作(requestWind

随机推荐

  1. Android的Menu
  2. Android的两种数据存储方式分析(一)
  3. 更新Activity的几个方法
  4. Android系统信息获取 之九:TelephonyManag
  5. Android(安卓)时间显示控件 TextClock
  6. 如何用eclipse搭建Android的开发环境
  7. Android(安卓)使用level-list改变对应的
  8. android显示键盘时的页面自动调整
  9. ionic3/ag4编写模仿京东商城的demo
  10. 《Android系统开发》笔记