> ## Documentation Index
> Fetch the complete documentation index at: https://qawolf-mintlify-79e74a8b.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Measure performance (Android)

> Measure frame rate and CPU usage on the Android emulator during automated flows.

Use `adb` commands via `device.adb(...)` to collect FPS and CPU metrics during a flow and assert on rendering performance and CPU load.

See the [Android `dumpsys` documentation](https://developer.android.com/tools/dumpsys) for more on SurfaceFlinger stats.

<Note>
  Performance measurements run on the Android emulator. FPS values reflect emulator rendering, not physical device GPU performance. CPU values are per-core percentages and can exceed 100% on multi-core emulators (typically 4 cores).
</Note>

## Examples

**Assert average FPS**

```typescript theme={null}
await device.adb("shell dumpsys SurfaceFlinger --timestats -clear -enable");

// perform the rendering-heavy interaction you want to measure

const resTxt = await device.adb("shell dumpsys SurfaceFlinger --timestats -dump");
const match = resTxt.match(/averageFPS\s*=\s*(?<avgFps>\d+\.\d*)/);
const avgFps = Number(match.groups["avgFps"]);
expect(avgFps).toBeGreaterThan(40);
```

<Warning>
  If the device is idle when you collect stats, the reported FPS may be near zero because there is no active rendering. Always start measuring immediately before a rendering-heavy action.
</Warning>

**Sample CPU usage**

```typescript theme={null}
const packageName = await driver.getCurrentPackage();
const processId = await device.adb(`shell pidof ${packageName}`);
const stdout = await device.adb(`shell top -n 1 -p ${processId}`);

const match = stdout.match(/\S+\s+\S+\s+\S+\s+\S+\s+(\S+)/);
const cpuUsage = parseFloat(match[1]);
expect(cpuUsage).toBeLessThan(50);
```

## When to use

* Your app has animations or rendering-heavy screens and you need to assert on frame rate.
* Your app performs background processing and you need to verify CPU usage stays within acceptable limits.
* Your flow includes a workflow that should complete without excessive CPU load.
* Your team has performance budgets you want to enforce in CI.

## Full sample test

```typescript theme={null}
import { device, flow } from "@qawolf/flows/android";

export default flow(
  "Measure performance",
  { target: "Android - Pixel", launch: true },
  async ({ driver, test }) => {
    await test("assert FPS", async () => {
      await device.adb("shell dumpsys SurfaceFlinger --timestats -clear -enable");

      // perform the rendering-heavy interaction you want to measure

      const resTxt = await device.adb("shell dumpsys SurfaceFlinger --timestats -dump");
      const match = resTxt.match(/averageFPS\s*=\s*(?<avgFps>\d+\.\d*)/);
      const avgFps = Number(match.groups["avgFps"]);
      expect(avgFps).toBeGreaterThan(40);
    });

    await test("assert CPU usage", async () => {
      const packageName = await driver.getCurrentPackage();
      const processId = await device.adb(`shell pidof ${packageName}`);
      const stdout = await device.adb(`shell top -n 1 -p ${processId}`);

      const match = stdout.match(/\S+\s+\S+\s+\S+\s+\S+\s+(\S+)/);
      const cpuUsage = parseFloat(match[1]);
      expect(cpuUsage).toBeLessThan(50);
    });
  },
);
```
