Skip to content

Instantly share code, notes, and snippets.

@AndreiCalazans
Created May 12, 2022 13:33
Show Gist options
  • Save AndreiCalazans/81e719432f3a6756b9fc44eceeed22eb to your computer and use it in GitHub Desktop.
Save AndreiCalazans/81e719432f3a6756b9fc44eceeed22eb to your computer and use it in GitHub Desktop.
Script to run launch Android and retrieve some performance logs
const { exec } = require('child_process');
const _exec = async (command) => {
return new Promise((res) => {
exec(command, (error, stdout) => {
if (error !== null) {
console.log(`exec error: ${error}`);
}
res(stdout);
});
});
};
const sleep = () => new Promise((res) => setTimeout(res, 10000));
const main = async () => {
const clear = 'adb logcat -c';
const stop = 'adb shell am force-stop com.coinbase.android';
const open = 'adb shell monkey -p com.coinbase.android 1';
console.log('clearing logs');
await _exec(clear);
for (let i = 0; i < 9; i++) {
console.log('run - ', i);
await _exec(open);
await sleep();
await _exec(stop);
}
console.log('retrieving logs');
const logs = await _exec('adb logcat -d -s ReactNativeJS');
console.log('Result', logs.match(/mark.*\d\s/g));
return 0;
};
main();
@AndreiCalazans
Copy link
Author

Make sure to log with "mark ${eventName} - ${duration}"

@AndreiCalazans
Copy link
Author

new PerformanceObserver((list) => {
  const entries = list.getEntries();
  entries.forEach((entry) => {
    switch (entry.name) {
      case 'nativeLaunchStart': {
        performance.measure(
          'nativeLaunch',
          'nativeLaunchStart',
          'nativeLaunchEnd',
        );
        // mark nativeLaunchStart in the legacy perf code if not already marked
        if (!hasMark('nativeLaunchStart')) {
          legacyMark('nativeLaunchStart', entry.startTime);
        }
        break;
      }
      case 'downloadEnd': {
        performance.measure('download', 'downloadStart', 'downloadEnd');
        break;
      }
      case 'bridgeSetupEnd': {
        performance.measure(
          'bridgeSetup',
          'bridgeSetupStart',
          'bridgeSetupEnd',
        );
        break;
      }
      case 'buildNativeModuleRegistryEnd': {
        performance.measure(
          'buildNativeModuleRegistry',
          'buildNativeModuleRegistryStart',
          'buildNativeModuleRegistryEnd',
        );
        break;
      }
      case 'runJsBundleEnd': {
        performance.measure(
          'runJsBundle',
          'runJsBundleStart',
          'runJsBundleEnd',
        );
        break;
      }
      case 'preRunJsBundleStart': {
        performance.measure(
          'createReactContext',
          'createReactContextStart',
          'preRunJsBundleStart',
        );
        break;
      }
    }
  });
}).observe({ type: 'react-native-mark', buffered: true });

@AndreiCalazans
Copy link
Author

How to get the logs ref

import { PerformanceObserver } from 'react-native-performance';
const measureObserver = new PerformanceObserver((list, observer) => {
  list.getEntries().forEach((entry) => {
    console.log(`mark - ${entry.name} - ${entry.duration}ms`);
  });
});
measureObserver.observe({ type: 'measure', buffered: true });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment