Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Tune CPU perf data parsing #659

Merged
merged 2 commits into from
Sep 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions lib/commands/performance.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';
import { retryInterval } from 'asyncbox';
import log from '../logger';


let commands = {}, helpers = {}, extensions = {};
Expand Down Expand Up @@ -67,19 +68,25 @@ helpers.getCPUInfo = async function getCPUInfo (packageName, dataReadTimeout = 2
// It usually fails 30 times and success for the next time,
// Since then, he has continued to succeed.
return await retryInterval(dataReadTimeout, RETRY_PAUSE, async () => {
const cmd = ['dumpsys', 'cpuinfo', '|', 'grep', `'${packageName}'`];
const data = await this.adb.shell(cmd);
if (_.isEmpty(data)) {
throw new Error('No data from dumpsys');
let output;
try {
output = await this.adb.shell(['dumpsys', 'cpuinfo']);
} catch (e) {
if (e.stderr) {
log.info(e.stderr);
}
throw e;
}
// `data` will be something like
// +0% 2209/io.appium.android.apis: 0.1% user + 0.2% kernel
const userMatch = /([\d.]+)%\s+user/.exec(data);
const kernelMatch = /([\d.]+)%\s+kernel/.exec(data);
if (!userMatch || !kernelMatch) {
throw new Error(`Unable to parse cpu data: '${data}'`);
// `output` will be something like
// +0% 2209/io.appium.android.apis: 0.1% user + 0.2% kernel / faults: 70 minor
const usagesPattern =
new RegExp(`^.+\\/${_.escapeRegExp(packageName)}:\\D+([\\d.]+)%\\s+user\\s+\\+\\s+([\\d.]+)%\\s+kernel`, 'm');
const match = usagesPattern.exec(output);
if (!match) {
log.debug(output);
throw new Error(`Unable to parse cpu usage data for '${packageName}'. Check the server log for more details`);
}
return [_.clone(CPU_KEYS), [userMatch[1], kernelMatch[1]]];
return [CPU_KEYS, [match[1], match[2]]];
});
};

Expand Down
58 changes: 47 additions & 11 deletions test/unit/commands/performance-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,57 @@ describe('performance data', function () {
});
describe('getCPUInfo', function () {
it('should return cpu data', async function () {
adb.shell.withArgs(['dumpsys', 'cpuinfo', '|', 'grep', `'${PACKAGE_NAME}'`])
.returns(' +0% 2209/io.appium.android.apis: 14% user + 23% kernel');
(await driver.getCPUInfo(PACKAGE_NAME)).should.be.deep
.equal([CPU_KEYS, ['14', '23']]);
adb.shell.withArgs(['dumpsys', 'cpuinfo'])
.returns(`Load: 8.85 / 8.85 / 7.96
CPU usage from 339020ms to 38831ms ago (2020-09-05 12:55:08.950 to 2020-09-05 13:00:09.140) with 99% awake:
0.6% 811/com.android.systemui: 0.3% user + 0.3% kernel / faults: 564 minor 1 major
0.6% 282/[email protected]: 0% user + 0.6% kernel
0.3% 6663/com.google.android.youtube: 0.1% user + 0.2% kernel / faults: 4328 minor
0.1% 327/surfaceflinger: 0% user + 0.1% kernel
0.1% 511/system_server: 0% user + 0.1% kernel / faults: 741 minor
0.1% 295/[email protected]: 0% user + 0.1% kernel
0% 787/wpa_supplicant: 0% user + 0% kernel
0% 309/[email protected]: 0% user + 0% kernel
0% 433/llkd: 0% user + 0% kernel
0% 6602/com.google.android.videos: 0% user + 0% kernel
0% 2141/com.android.phone: 0% user + 0% kernel / faults: 144 minor
0% 306/[email protected]: 0% user + 0% kernel
0% 154/logd: 0% user + 0% kernel / faults: 21 minor 1 major
0% 7504/kworker/u4:1-flush-251:32: 0% user + 0% kernel
0% 16/ksoftirqd/1: 0% user + 0% kernel
0% 6825/kworker/0:0-mm_percpu_wq: 0% user + 0% kernel
0% 10/rcu_preempt: 0% user + 0% kernel
0% 458/hostapd_nohidl: 0% user + 0% kernel
0% 179/jbd2/vdc-8: 0% user + 0% kernel
0% 157/hwservicemanager: 0% user + 0% kernel
0% 1666/com.google.android.gms.persistent: 0% user + 0% kernel / faults: 156 minor
0% 270/statsd: 0% user + 0% kernel
0% 271/netd: 0% user + 0% kernel / faults: 9 minor
0% 341/logcat: 0% user + 0% kernel
0% 1072/com.android.networkstack.process: 0% user + 0% kernel / faults: 452 minor
0% 183/[email protected]: 0% user + 0% kernel
0% 7157/kworker/1:1-events_power_efficient: 0% user + 0% kernel
0% 7245/${PACKAGE_NAME}:rcs: 14.3% user + 28.2% kernel / faults: 30 minor
0% 9/ksoftirqd/0: 0% user + 0% kernel
0% 11/migration/0: 0% user + 0% kernel
0% 21/kauditd: 0% user + 0% kernel
0% 113/kworker/1:1H-kblockd: 0% user + 0% kernel
0% 155/lmkd: 0% user + 0% kernel
0% 156/servicemanager: 0% user + 0% kernel
0% 162/vold: 0% user + 0% kernel
0% 407/libgoldfish-rild: 0% user + 0% kernel / faults: 108 minor
0% 431/netmgr: 0% user + 0% kernel
0% 1020/[email protected]: 0% user + 0% kernel
0% 2345/com.google.android.gms: 0% user + 0% kernel / faults: 70 minor
+0% 7508/kworker/u4:2-phy0: 0% user + 0% kernel
0.2% TOTAL: 0% user + 0.1% kernel + 0% iowait + 0% softirq
`);
(await driver.getCPUInfo(PACKAGE_NAME)).should.eql([CPU_KEYS, ['14.3', '28.2']]);
asyncbox.retryInterval.calledWith(RETRY_COUNT, RETRY_PAUSE).should.be.true;
});
it('should throw error if no data', async function () {
adb.shell.returns(null);
await driver.getCPUInfo(PACKAGE_NAME, 1).should.be
.rejectedWith(/No data from dumpsys/);
});
it('should throw error if cpu data is not in valid format', async function () {
adb.shell.returns('invalid data');
await driver.getCPUInfo(PACKAGE_NAME, 1).should.be
.rejectedWith(/Unable to parse cpu data/);
await driver.getCPUInfo(PACKAGE_NAME, 1).should.eventually.be.rejected;
});
});
describe('getBatteryInfo', function () {
Expand Down