Macaca [基于 Node.js 的自动化测试-Macaca] - 获取 Android 应用的性能

达峰的夏天 · 2016年07月01日 · 最后由 willys 回复于 2017年10月23日 · 2388 次阅读

上一篇 - Macaca 如何实现多任务

前言

在测试流程中,我们通常会加入一些性能采集,例如网络、cpu、线程情况、内存等等,本文介绍下如何用最简单的方式,获取 Android 应用的一些性能参数。

原理很常规,使用 adb shell 配合即可,但是需要使用 Node.js 对 adb 命令封装一下。

怎么做?

接下来介绍 supern 写的 android-performance 模块如何使用。

将 引入项目即可,下面的代码来自 android-performance 的单元测试,用以说明如何一次性取到所有指标。

// file: /test/android-performance.test.js

it('should all in one with promise', function(done) {
  const perf = new AndroidPerformance();
  const p1 = new Promise((resolve, reject) => {
    perf
      .initDevice()
      .then(() => perf.getMeminfoByPackageName(pkgName))
      .then(res => {
        resolve({
          item: 'Meminfo',
          data: res
        });
      });
  });

  const p2 = new Promise((resolve, reject) => {
    perf
      .initDevice()
      .then(() => perf.getPid(pkgName))
      .then(pid => {
        return perf
          .getThreadCountByPid(pid)
          .then(d => {
            resolve({
              item: 'ThreadCount',
              data: d
            });
          })
          .catch(e => {
            resolve(null);
          });
      })
      .catch(e => {
        resolve(null);
      });
  });

  const p3 = new Promise((resolve, reject) => {
    perf
      .initDevice()
      .then(() => perf.getPid(pkgName))
      .then(pid => {
        return perf
          .getUidByPid(pid)
          .then(uid => {
            return perf
              .getTrafficByUid(uid)
              .then(d => {
                resolve({
                  item: 'Traffic',
                  data: d
                });
              })
              .catch(e => {
                resolve(null);
              });
          })
          .catch(e => {
            resolve(null);
          });
      })
      .catch(e => {
        resolve(null);
      });
  });

  const p4 = new Promise((resolve, reject) => {
    perf
      .initDevice()
      .then(() => perf.getPid(pkgName))
      .then(pid => {
        return perf
          .getCPUByPid(pid)
          .then(d => {
            resolve({
              item: 'cpu',
              data: d
            });
          })
          .catch(e => {
            resolve(null);
          });
      });
  });

  Promise.all([p1, p2, p3, p4]).then(result => {
    console.log(`performance:${JSON.stringify(result)}`);
    done();
  });
});

可以得到如下结果:

如何加到用例里

const wd = require('macaca-wd');

const androidPerfPin = function() {
  // ...
  // 封装性能探针即可
};

// 加到调用链扩展里
wd.addPromiseChainMethod('androidPerfPin', androidPerfPin);

然后就可以使用熟悉的语法啦

it('xxxxxxxx', function() {
  return driver
    .elmentById('xxxxxxxxx')
    .click()
    .androidPerfPin()
    .screenshot();
    ...
});

本篇介绍了如何采集 Android 性能,下篇准备介绍如何采集页面的性能。


欢迎讨论,互相学习。

微博: http://weibo.com/xudafeng
Github: https://github.com/xudafeng

下一篇 - Android 输入中文的实现

共收到 9 条回复 时间 点赞
达峰的夏天 [该话题已被删除] 中提及了此贴 07月01日 20:06

虽然说功能听完善的,Nodejs 我也用了段时间,但总感觉拿它写测试用例,感觉怪怪的

#2 楼 @codeskyblue 楼上应该喜欢 Python 吧,哈哈,欢迎私聊

你采数据是什么逻辑去采集呢?
我说的是 某一个操作你是操作前?还是操作中?操作后?或者全部取求平均??
appium 自动化的时候去采集数据的时候,会偏高一点点,不知道你这种方式会不会?

#4 楼 @testly 示例中提供的是操作后,什么时候采集是看自己需求自由控制的

#5 楼 @xdf ok 了解了

提醒注意下:dumpsys meminfo 和 dumpsys cpuinfo 不要混用,dumpsys meminfo 操作会引起 cpu 数据升高不准

#7 楼 @sandman 多谢提醒,如果 vm 被触发 gc 会直接影响性能参数的,这里是个参考值,没有绝对准确的概念。没有混用

结合 TencentGT http://gt.qq.com 是不是要好点,只是提个想法

达峰的夏天 [该话题已被删除] 中提及了此贴 07月05日 11:46
达峰的夏天 [该话题已被删除] 中提及了此贴 07月08日 17:13
达峰的夏天 关闭了讨论 09月12日 16:02
达峰的夏天 重新开启了讨论 09月12日 16:02

运行有错误:UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): exec dumpsys meminfo com.demo error with: TypeError: Cannot read property 'trim' of undefined

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册