0

I am a front-end developer and I want to monitor webview performance. I have two scenarios:

  1. Use deeplink to jump directly to my webview page
  2. Use pidof MY_PACKAGE_NAME to get process id
  3. Monitor app's main process's cpu by top( previously ps, but apparently top is more appropriate )
  4. Inject or not an endless loop using javascript inside webview
  5. Cancel the endless loop and calculate the average cpu percentage

The result shows the endless loop case's avg cpu is lower than the no loop one.

This is counter-intuitive, I understand that modern app has multi-process structure, but why on earth endless loop's case has lower cpu.

I am using playwright to connect and send adb command to communicate with my android device. Below is the result:

{
  "loop": {
    "avg": 25.7,
    "p25": 0,
    "p50": 3.5,
    "p75": 21.2,
    "p80": 65.2,
    "p90": 92.8,
    "p95": 99.8,
    "p99": 190.8,
    "max": 306
  },
  "no-loop": {
    "avg": 13.3,
    "p25": 0,
    "p50": 3.4,
    "p75": 7.1,
    "p80": 10.3,
    "p90": 21,
    "p95": 81.3,
    "p99": 195.6,
    "max": 274
  }
}

For those interested in source code, below's the source code written in typescript.

import { _android as android } from "playwright";

const [device] = await android.devices();

await device.shell('am force-stop MY_PACKAGE_NAME');
const url = encodeURIComponent("MY_H5_URL");
const deeplink = `myapp://browser/?url=${url}`;
await device.shell(`am start -a android.intent.action.VIEW -d "${deeplink}"`);
const webview = await device.webView({
  pkg: "MY_PACKAGE_NAME:web",
});
const page = await webview.page();
const packagePid = await device.shell(`pidof MY_PACKAGE_NAME`).then(buffer => buffer.toString().trim());
setInterval(async () => {
  await device.shell(`top -b -n 1 -d 0.1 -o PID,%CPU,RES -p ${packagePid}`)
  // ...Omit the collecting logic for simplicity
}, 1000);
// deliberately run some CPU intensive task in Webview
await page.evaluate(() => {
  const startTime = Date.now();
  while (Date.now() - startTime < 20_000) { // endless loop for 20 seconds
      Math.sqrt(Math.random());
  }
});

// Calculate the CPU stats from the top outputs collected
// ...Omit the calculation for simplicity
await device.close();
6
  • Your loop doesn't actually do anything. It might have been optimized out. Also, webviews are partially in its own process, I'm not sure where the JS is actually executed, but it might not be in your app Commented Sep 12 at 14:28
  • @GabeSechan The loop is executed inside chromium webview main thread, I can confirm that if I change a button background color it will change. I use an endless loop for simulating a fully busy chromium webview main thread. Commented Sep 15 at 2:57
  • Keep in mind that most Android CPUs have two or even three different CPU core types, so measuring CPU time or percent usage may not get you meaningful results as for each cpu core type 100% means something different. Commented Sep 15 at 7:52
  • @Robert top command will produce cpu% more than 100%, which means it will consider multi-core usage. You mean what top command produces is meaningless cause cpu core has different types? So is there any workaround to measure a fair cpu percentage considering different cpu core types. Intuitively, intensive webview should have more cpu load than none-intensive one. Commented Sep 17 at 7:18
  • 1
    @crazyones110 I was referring to the big.little-concept used in many ARM CPUs. CPUs have fast/performance cores and efficiency cores in one CPU. That means 100% on a fast core is not the same as 100% on an efficiency core as the fast core can be may be two times faster than an efficiency core. Depending on how you execute something the scheduler may assign the task to an fast or an efficient core. If you now measure the CPU percentage of different approaches and they are assigned to different core types the result is not comparable. Commented Sep 17 at 7:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.