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

feat: add a command to download the Hermes Sampling Profiler to a local machine #1246

Merged
merged 68 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
1167db0
added adb commands
jessieAnhNguyen Jul 7, 2020
2047721
feat: implemented download hermes profile command
jessieAnhNguyen Jul 21, 2020
4af2cbe
deleted unused adbkit packages
jessieAnhNguyen Jul 22, 2020
98e1ecf
changed files excluded node modules
jessieAnhNguyen Jul 22, 2020
24a248b
handled edge case when there's no file
jessieAnhNguyen Jul 22, 2020
b720baf
deleted unnecessary changes
jessieAnhNguyen Jul 22, 2020
4c0fb45
deleted unnecessary changes 2
jessieAnhNguyen Jul 22, 2020
defc7b7
Merge pull request #2 from MLH-Fellowship/jessie
jessieAnhNguyen Jul 27, 2020
b5d1d3c
added transformer files
jessieAnhNguyen Jul 28, 2020
89837ae
implemented --verbose flag
jessieAnhNguyen Jul 28, 2020
23cd80c
Merge branch 'jessie-cli2' into test/transformer
jessieAnhNguyen Jul 28, 2020
3e7c6f4
integrated transformer tool with CLI
jessieAnhNguyen Jul 28, 2020
cbc0c28
implemented --raw flag
jessieAnhNguyen Jul 28, 2020
28a55d4
deleted changelog comments and fixed small changes
jessieAnhNguyen Jul 30, 2020
680eccb
changed wording
jessieAnhNguyen Jul 30, 2020
ef6bb10
deleted unused event types and remove unused dev dependencies
jessieAnhNguyen Jul 30, 2020
2285b38
generated source map through
jessieAnhNguyen Aug 6, 2020
86c8110
Merge pull request #4 from MLH-Fellowship/test/transformer
jessieAnhNguyen Aug 10, 2020
652290d
fixed SHA-1 error
jessieAnhNguyen Aug 10, 2020
deffdd2
Merge branch 'master' of https://github.com/MLH-Fellowship/cli into f…
jessieAnhNguyen Aug 10, 2020
f560481
implemented the source map steps
jessieAnhNguyen Aug 10, 2020
e3fe062
added transformer package and profile-hermes.md
jessieAnhNguyen Aug 11, 2020
1cff3c8
Merge pull request #6 from MLH-Fellowship/feat/generate-source-map
jevakallio Aug 11, 2020
ca07916
Rename command to profile-hermes
jessieAnhNguyen Aug 11, 2020
867b6b2
Improve command line output examples
jessieAnhNguyen Aug 11, 2020
7ee8fc6
Extract source map functionality to its own file
jessieAnhNguyen Aug 11, 2020
4e0dd95
Readability improvements (@todo: fix @TODO comments)
jessieAnhNguyen Aug 11, 2020
c422253
upgrade hermes-profile-transformer package
jessieAnhNguyen Aug 11, 2020
3d9d04e
implemented @TODO: remove axios and use node-fetch
jessieAnhNguyen Aug 13, 2020
29e55ca
deleted small unintended changes and removed axios
jessieAnhNguyen Aug 13, 2020
1078bdb
removed axios package and deleted unnecessary changes
jessieAnhNguyen Aug 13, 2020
e050ab2
removed source-map related packages
jessieAnhNguyen Aug 13, 2020
b955db9
added new line after each logger
jessieAnhNguyen Aug 13, 2020
e086ec9
Merge pull request #7 from MLH-Fellowship/chore/pre-publish
jessieAnhNguyen Aug 13, 2020
423424f
Merge branch 'master' of https://github.com/MLH-Fellowship/cli
jessieAnhNguyen Aug 13, 2020
b37a804
upgraded hermes-profile-transformer package to latest
jessieAnhNguyen Aug 14, 2020
2f93778
moved profile-hermes folder out of cli to a separate package cli-hermes
jessieAnhNguyen Aug 18, 2020
ed90c7e
deleted the command from projectCommands in cli
jessieAnhNguyen Aug 18, 2020
aacd874
re-added the command in the projectCommands
saphal1998 Aug 18, 2020
86cba71
removed unused dependencies from cli
saphal1998 Aug 18, 2020
48b1b0f
added cli-hermes to the list of yarn link
jessieAnhNguyen Aug 18, 2020
e53ee19
Merge pull request #9 from MLH-Fellowship/movePackage
jessieAnhNguyen Aug 18, 2020
e3ff688
removed unused @types/ip dependency
jessieAnhNguyen Aug 18, 2020
f5e51e1
Merge remote-tracking branch 'upstream/master'
jessieAnhNguyen Aug 18, 2020
a0ad665
added @types/ip dependency in cli-hermes
jessieAnhNguyen Aug 18, 2020
1735396
moved @types/ip to dev dependency
jessieAnhNguyen Aug 18, 2020
1b0438b
added type definition for cli-hermes
saphal1998 Aug 18, 2020
5ebb4ba
fixed type definition
jessieAnhNguyen Aug 18, 2020
236b038
Added cli-hermes ref to tsconfig in packages/cli
saphal1998 Aug 18, 2020
8da90e5
Merge branch 'master' of github.com:MLH-Fellowship/cli
saphal1998 Aug 18, 2020
f7cb658
Removed unnecessary code comment
jessieAnhNguyen Aug 19, 2020
c85506e
made changes based on PR review
jessieAnhNguyen Aug 19, 2020
125836c
Merge branch 'master' of https://github.com/MLH-Fellowship/cli
jessieAnhNguyen Aug 19, 2020
b7dbb5e
added a verbose log
jessieAnhNguyen Aug 19, 2020
27f14d9
added cli-platform-android as a dependency
jessieAnhNguyen Aug 19, 2020
02263ae
added cli-platform-android as a dependency in cli
jessieAnhNguyen Aug 19, 2020
8fc1b01
Fixing path in tsconfig
saphal1998 Aug 19, 2020
bfc08b2
Merge conflict
saphal1998 Aug 19, 2020
4478249
Added type defs of platform-android to cli-hermes, path fixed
saphal1998 Aug 19, 2020
93acfcd
Fixed chalk version on cli-hermes
saphal1998 Aug 19, 2020
9607160
removed unnecessary comment
jessieAnhNguyen Aug 21, 2020
4e50329
Merge remote-tracking branch 'upstream/master'
jessieAnhNguyen Aug 21, 2020
dcabe01
Merge branch 'master' of https://github.com/MLH-Fellowship/cli
jessieAnhNguyen Aug 21, 2020
3e97a31
changed --filename flag and throw real error
jessieAnhNguyen Sep 1, 2020
f4b4041
Merge pull request #10 from MLH-Fellowship/review/edit
jessieAnhNguyen Sep 1, 2020
75095c5
Fix catch block in profile-hermes/index to match the error thrown
saphal1998 Sep 3, 2020
32a917e
Typecast error as CLIError
saphal1998 Sep 3, 2020
8d6fb8f
Apply suggestions from code review
saphal1998 Sep 4, 2020
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
Prev Previous commit
Next Next commit
implemented @todo: remove axios and use node-fetch
add the usage to commands.md
delete the profile-hermes.md
  • Loading branch information
jessieAnhNguyen committed Aug 13, 2020
commit 3d9d04e5701236d7f663f0135c4c43c12115253b
44 changes: 44 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ React Native CLI comes with following commands:
- [`uninstall`](#uninstall)
- [`unlink`](#unlink)
- [`upgrade`](#upgrade)
- [`profile-hermes`](#profile-hermes)

### `bundle`

Expand Down Expand Up @@ -546,3 +547,46 @@ Upgrade your app's template files to the specified or latest npm version using [
Using this command is a recommended way of upgrading relatively simple React Native apps with not too many native libraries linked. The more iOS and Android build files are modified, the higher chance for a conflicts. The command will guide you on how to continue upgrade process manually in case of failure.

_Note: If you'd like to upgrade using this method from React Native version lower than 0.59.0, you may use a standalone version of this CLI: `npx @react-native-community/cli upgrade`._

### `profile-hermes`

Usage:

```sh
react-native profile-hermes [destinationDir] <flag>
```

Pull and convert a Hermes tracing profile to Chrome tracing profile, then store it in the directory <destinationDir> of the local machine.

- `destinationDir` is optional, if provided, pull the file to that directory
> default: pull to the current React Native app root directory

#### Options

#### `--fileName [string]`
thymikee marked this conversation as resolved.
Show resolved Hide resolved
saphal1998 marked this conversation as resolved.
Show resolved Hide resolved

File name of the profile to be downloaded, eg. sampling-profiler-trace8593107139682635366.cpuprofile.

> default: pull the latest file

#### `--verbose`

Lists commands and steps that are run internally to pull the file from Android device

#### `--raw`

Pulls the original Hermes tracing profile without any transformation

#### `--sourcemap-path [string]`

The local path to your source map file if you generated it manually, ex. `/tmp/sourcemap.json`

#### `--generate-sourcemap`

Generate the JS bundle and source map in `os.tmpdir()`

### Notes on source map

This step is recommended in order for the source map to be generated:

If you are planning on building a debug APK, that will run without the packager, by invoking `./gradlew assembleDebug` you can simply set `bundleInDebug: true` in your app/build.gradle file, inside the `project.ext.react` map.
107 changes: 0 additions & 107 deletions docs/profile-hermes.md

This file was deleted.

6 changes: 3 additions & 3 deletions packages/cli/src/commands/profile-hermes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export default {
{
name: '--verbose',
description:
'Lists adb commands that are run internally when pulling the file from Android device',
'Lists commands and steps that are run internally when pulling the file from Android device',
},
{
name: '--raw',
description:
'Pull the original Hermes tracing profile without any transformation',
'Pulls the original Hermes tracing profile without any transformation',
},
{
name: '--sourcemap-path [string]',
Expand All @@ -67,7 +67,7 @@ export default {
},
{
name: '--generate-sourcemap',
description: 'Generate the JS bundle and source map',
description: 'Generates the JS bundle and source map',
},
],
examples: [
Expand Down
49 changes: 19 additions & 30 deletions packages/cli/src/commands/profile-hermes/sourcemapUtils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {Config} from '@react-native-community/cli-types';
import {execSync} from 'child_process';
import {logger} from '@react-native-community/cli-tools';
import {logger, CLIError} from '@react-native-community/cli-tools';
import fs from 'fs';
import path from 'path';
import os from 'os';
import axios, {AxiosResponse} from 'axios';
import {SourceMap} from 'hermes-profile-transformer';
import ip from 'ip';
import nodeFetch from 'node-fetch';

function getTempFilePath(filename: string) {
return path.join(os.tmpdir(), filename);
Expand All @@ -30,26 +29,24 @@ function writeJsonSync(targetPath: string, data: any) {
}
}

// @TODO
// Remove AxiosResponse, return Promise<SourceMap> where SourceMap
// is imported from the hermes-profiler-transformer
async function getSourcemapFromServer(): Promise<AxiosResponse<SourceMap>> {
async function getSourcemapFromServer(): Promise<SourceMap> {
logger.debug('Getting source maps from Metro packager server');
const DEBUG_SERVER_PORT = '8081';
const IP_ADDRESS = ip.address();
const PLATFORM = 'android';
const requestURL = `http://${IP_ADDRESS}:${DEBUG_SERVER_PORT}/index.map?platform=${PLATFORM}&dev=true`;

// @TODO
// Use node-fetch instead of axios
// Check for return http status code, if > 400 it's an error and
// we should return null instead of the source map string

return (await axios.get(requestURL)) as AxiosResponse<SourceMap>;
const requestURL = `http://${IP_ADDRESS}:${DEBUG_SERVER_PORT}/index.map?platform=${PLATFORM}&dev=true`;
const result = await nodeFetch(requestURL);
if (result.status >= 400) {
logger.error('Cannot get source map from running metro server');
throw new CLIError(`Fetch request failed with status ${result.status}.`);
}
const data = await result.json();
return data as SourceMap;
}

/**
* Generate a sourcemap either by fetching it from a running metro server
* or by running react-native bundle with sourcemaps enables
* Generate a sourcemap by fetching it from a running metro server
*/
export async function generateSourcemap(): Promise<string> {
// fetch the source map to a temp directory
Expand All @@ -60,22 +57,14 @@ export async function generateSourcemap(): Promise<string> {
if (logger.isVerbose()) {
logger.debug('Using source maps from Metro packager server');
}
writeJsonSync(sourceMapPath, sourceMapResult.data);
} else {
if (logger.isVerbose()) {
logger.debug('Generating source maps using `react-native bundle`');
}
execSync(
`react-native bundle --entry-file index.js --bundle-output ${getTempFilePath(
'index.bundle',
)} --sourcemap-output ${sourceMapPath}`,
writeJsonSync(sourceMapPath, sourceMapResult);
logger.info(
`Successfully generated the source map and stored it in ${sourceMapPath}`,
);
} else {
logger.error('Cannot generate source maps from Metro packager server`');
}

logger.info(
`Successfully generated the source map and stored it in ${sourceMapPath}`,
);

return sourceMapPath;
}

Expand Down Expand Up @@ -124,7 +113,7 @@ export async function findSourcemap(ctx: Config): Promise<string> {
logger.debug('Using source maps from Metro packager server');
}
const tempPath = getTempFilePath('index.map');
writeJsonSync(tempPath, sourcemapResult.data);
writeJsonSync(tempPath, sourcemapResult);

return tempPath;
}
Expand Down