-
Notifications
You must be signed in to change notification settings - Fork 919
feat: add a command to download the Hermes Sampling Profiler to a local machine #1246
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
Merged
thymikee
merged 68 commits into
react-native-community:master
from
MLH-Fellowship:master
Sep 4, 2020
Merged
Changes from 66 commits
Commits
Show all changes
68 commits
Select commit
Hold shift + click to select a range
1167db0
added adb commands
jessieAnhNguyen 2047721
feat: implemented download hermes profile command
jessieAnhNguyen 4af2cbe
deleted unused adbkit packages
jessieAnhNguyen 98e1ecf
changed files excluded node modules
jessieAnhNguyen 24a248b
handled edge case when there's no file
jessieAnhNguyen b720baf
deleted unnecessary changes
jessieAnhNguyen 4c0fb45
deleted unnecessary changes 2
jessieAnhNguyen defc7b7
Merge pull request #2 from MLH-Fellowship/jessie
jessieAnhNguyen b5d1d3c
added transformer files
jessieAnhNguyen 89837ae
implemented --verbose flag
jessieAnhNguyen 23cd80c
Merge branch 'jessie-cli2' into test/transformer
jessieAnhNguyen 3e7c6f4
integrated transformer tool with CLI
jessieAnhNguyen cbc0c28
implemented --raw flag
jessieAnhNguyen 28a55d4
deleted changelog comments and fixed small changes
jessieAnhNguyen 680eccb
changed wording
jessieAnhNguyen ef6bb10
deleted unused event types and remove unused dev dependencies
jessieAnhNguyen 2285b38
generated source map through
jessieAnhNguyen 86c8110
Merge pull request #4 from MLH-Fellowship/test/transformer
jessieAnhNguyen 652290d
fixed SHA-1 error
jessieAnhNguyen deffdd2
Merge branch 'master' of https://github.com/MLH-Fellowship/cli into f…
jessieAnhNguyen f560481
implemented the source map steps
jessieAnhNguyen e3fe062
added transformer package and profile-hermes.md
jessieAnhNguyen 1cff3c8
Merge pull request #6 from MLH-Fellowship/feat/generate-source-map
jevakallio ca07916
Rename command to profile-hermes
jessieAnhNguyen 867b6b2
Improve command line output examples
jessieAnhNguyen 7ee8fc6
Extract source map functionality to its own file
jessieAnhNguyen 4e0dd95
Readability improvements (@todo: fix @TODO comments)
jessieAnhNguyen c422253
upgrade hermes-profile-transformer package
jessieAnhNguyen 3d9d04e
implemented @TODO: remove axios and use node-fetch
jessieAnhNguyen 29e55ca
deleted small unintended changes and removed axios
jessieAnhNguyen 1078bdb
removed axios package and deleted unnecessary changes
jessieAnhNguyen e050ab2
removed source-map related packages
jessieAnhNguyen b955db9
added new line after each logger
jessieAnhNguyen e086ec9
Merge pull request #7 from MLH-Fellowship/chore/pre-publish
jessieAnhNguyen 423424f
Merge branch 'master' of https://github.com/MLH-Fellowship/cli
jessieAnhNguyen b37a804
upgraded hermes-profile-transformer package to latest
jessieAnhNguyen 2f93778
moved profile-hermes folder out of cli to a separate package cli-hermes
jessieAnhNguyen ed90c7e
deleted the command from projectCommands in cli
jessieAnhNguyen aacd874
re-added the command in the projectCommands
saphal1998 86cba71
removed unused dependencies from cli
saphal1998 48b1b0f
added cli-hermes to the list of yarn link
jessieAnhNguyen e53ee19
Merge pull request #9 from MLH-Fellowship/movePackage
jessieAnhNguyen e3ff688
removed unused @types/ip dependency
jessieAnhNguyen f5e51e1
Merge remote-tracking branch 'upstream/master'
jessieAnhNguyen a0ad665
added @types/ip dependency in cli-hermes
jessieAnhNguyen 1735396
moved @types/ip to dev dependency
jessieAnhNguyen 1b0438b
added type definition for cli-hermes
saphal1998 5ebb4ba
fixed type definition
jessieAnhNguyen 236b038
Added cli-hermes ref to tsconfig in packages/cli
saphal1998 8da90e5
Merge branch 'master' of github.com:MLH-Fellowship/cli
saphal1998 f7cb658
Removed unnecessary code comment
jessieAnhNguyen c85506e
made changes based on PR review
jessieAnhNguyen 125836c
Merge branch 'master' of https://github.com/MLH-Fellowship/cli
jessieAnhNguyen b7dbb5e
added a verbose log
jessieAnhNguyen 27f14d9
added cli-platform-android as a dependency
jessieAnhNguyen 02263ae
added cli-platform-android as a dependency in cli
jessieAnhNguyen 8fc1b01
Fixing path in tsconfig
saphal1998 bfc08b2
Merge conflict
saphal1998 4478249
Added type defs of platform-android to cli-hermes, path fixed
saphal1998 93acfcd
Fixed chalk version on cli-hermes
saphal1998 9607160
removed unnecessary comment
jessieAnhNguyen 4e50329
Merge remote-tracking branch 'upstream/master'
jessieAnhNguyen dcabe01
Merge branch 'master' of https://github.com/MLH-Fellowship/cli
jessieAnhNguyen 3e97a31
changed --filename flag and throw real error
jessieAnhNguyen f4b4041
Merge pull request #10 from MLH-Fellowship/review/edit
jessieAnhNguyen 75095c5
Fix catch block in profile-hermes/index to match the error thrown
saphal1998 32a917e
Typecast error as CLIError
saphal1998 8d6fb8f
Apply suggestions from code review
saphal1998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@react-native-community/cli-hermes", | ||
"version": "4.11.0", | ||
"license": "MIT", | ||
"main": "build/index.js", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"types": "build/index.d.ts", | ||
"dependencies": { | ||
"@react-native-community/cli-tools": "^4.11.0", | ||
"@react-native-community/cli-platform-android": "^4.11.0", | ||
"chalk": "^3.0.0", | ||
"hermes-profile-transformer": "^0.0.6", | ||
jessieAnhNguyen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"ip": "^1.1.5" | ||
}, | ||
"files": [ | ||
"build", | ||
"!*.d.ts", | ||
"!*.map" | ||
], | ||
"devDependencies": { | ||
"@react-native-community/cli-types": "^4.10.1", | ||
"@types/ip": "^1.1.0" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import profileHermes from './profileHermes/index'; | ||
export default profileHermes; |
128 changes: 128 additions & 0 deletions
128
packages/cli-hermes/src/profileHermes/downloadProfile.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import {Config} from '@react-native-community/cli-types'; | ||
import {execSync} from 'child_process'; | ||
import {logger, CLIError} from '@react-native-community/cli-tools'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import os from 'os'; | ||
import transformer from 'hermes-profile-transformer'; | ||
import {findSourcemap, generateSourcemap} from './sourcemapUtils'; | ||
import { | ||
getAndroidProject, | ||
getPackageName, | ||
} from '@react-native-community/cli-platform-android'; | ||
/** | ||
* Get the last modified hermes profile | ||
* @param packageName | ||
*/ | ||
function getLatestFile(packageName: string): string { | ||
try { | ||
const file = execSync(`adb shell run-as ${packageName} ls cache/ -tp | grep -v /$ | egrep '\.cpuprofile' | head -1 | ||
`); | ||
return file.toString().trim(); | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
} | ||
|
||
function execSyncWithLog(command: string) { | ||
logger.debug(`${command}`); | ||
return execSync(command); | ||
} | ||
|
||
/** | ||
* Pull and convert a Hermes tracing profile to Chrome tracing profile | ||
* @param ctx | ||
* @param dstPath | ||
* @param fileName | ||
* @param sourceMapPath | ||
* @param raw | ||
* @param generateSourceMap | ||
*/ | ||
export async function downloadProfile( | ||
ctx: Config, | ||
dstPath: string, | ||
filename?: string, | ||
sourcemapPath?: string, | ||
raw?: boolean, | ||
shouldGenerateSourcemap?: boolean, | ||
port?: string, | ||
) { | ||
try { | ||
const androidProject = getAndroidProject(ctx); | ||
thymikee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const packageName = getPackageName(androidProject); | ||
|
||
// If file name is not specified, pull the latest file from device | ||
const file = filename || (await getLatestFile(packageName)); | ||
if (!file) { | ||
throw new CLIError( | ||
'There is no file in the cache/ directory. Did you record a profile from the developer menu?', | ||
); | ||
} | ||
|
||
logger.info(`File to be pulled: ${file}`); | ||
|
||
// If destination path is not specified, pull to the current directory | ||
dstPath = dstPath || ctx.root; | ||
|
||
logger.debug('Internal commands run to pull the file:'); | ||
|
||
// Copy the file from device's data to sdcard, then pull the file to a temp directory | ||
execSyncWithLog(`adb shell run-as ${packageName} cp cache/${file} /sdcard`); | ||
|
||
// If --raw, pull the hermes profile to dstPath | ||
if (raw) { | ||
execSyncWithLog(`adb pull /sdcard/${file} ${dstPath}`); | ||
logger.success(`Successfully pulled the file to ${dstPath}/${file}`); | ||
} | ||
|
||
// Else: transform the profile to Chrome format and pull it to dstPath | ||
else { | ||
const osTmpDir = os.tmpdir(); | ||
const tempFilePath = path.join(osTmpDir, file); | ||
|
||
execSyncWithLog(`adb pull /sdcard/${file} ${tempFilePath}`); | ||
|
||
// If path to source map is not given | ||
if (!sourcemapPath) { | ||
// Get or generate the source map | ||
if (shouldGenerateSourcemap) { | ||
sourcemapPath = await generateSourcemap(port); | ||
} else { | ||
sourcemapPath = await findSourcemap(ctx, port); | ||
} | ||
|
||
// Run without source map | ||
if (!sourcemapPath) { | ||
logger.warn( | ||
'Cannot find source maps, running the transformer without it', | ||
); | ||
logger.info( | ||
'Instructions on how to get source maps: set `bundleInDebug: true` in your app/build.gradle file, inside the `project.ext.react` map.', | ||
); | ||
} | ||
} | ||
|
||
// Run transformer tool to convert from Hermes to Chrome format | ||
const events = await transformer( | ||
tempFilePath, | ||
sourcemapPath, | ||
'index.bundle', | ||
); | ||
|
||
const transformedFilePath = `${dstPath}/${path.basename( | ||
file, | ||
'.cpuprofile', | ||
)}-converted.json`; | ||
fs.writeFileSync( | ||
transformedFilePath, | ||
JSON.stringify(events, undefined, 4), | ||
'utf-8', | ||
); | ||
logger.success( | ||
`Successfully converted to Chrome tracing format and pulled the file to ${transformedFilePath}`, | ||
); | ||
} | ||
} catch (e) { | ||
throw e; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import {logger} from '@react-native-community/cli-tools'; | ||
import {Config} from '@react-native-community/cli-types'; | ||
import {downloadProfile} from './downloadProfile'; | ||
|
||
type Options = { | ||
filename?: string; | ||
raw?: boolean; | ||
sourcemapPath?: string; | ||
generateSourcemap?: boolean; | ||
port?: string; | ||
}; | ||
|
||
async function profileHermes( | ||
[dstPath]: Array<string>, | ||
ctx: Config, | ||
options: Options, | ||
) { | ||
try { | ||
logger.info( | ||
'Downloading a Hermes Sampling Profiler from your Android device...', | ||
); | ||
if (!options.filename) { | ||
logger.info('No filename is provided, pulling latest file'); | ||
} | ||
await downloadProfile( | ||
ctx, | ||
dstPath, | ||
options.filename, | ||
options.sourcemapPath, | ||
options.raw, | ||
options.generateSourcemap, | ||
options.port, | ||
); | ||
} catch (err) { | ||
throw err; | ||
} | ||
} | ||
|
||
export default { | ||
name: 'profile-hermes [destinationDir]', | ||
description: | ||
'Pull and convert a Hermes tracing profile to Chrome tracing profile, then store it in the directory <destinationDir> of the local machine', | ||
func: profileHermes, | ||
options: [ | ||
{ | ||
name: '--filename [string]', | ||
description: | ||
'File name of the profile to be downloaded, eg. sampling-profiler-trace8593107139682635366.cpuprofile', | ||
}, | ||
{ | ||
name: '--raw', | ||
description: | ||
'Pulls the original Hermes tracing profile without any transformation', | ||
}, | ||
{ | ||
name: '--sourcemap-path [string]', | ||
description: | ||
'The local path to your source map file, eg. /tmp/sourcemap.json', | ||
}, | ||
{ | ||
name: '--generate-sourcemap', | ||
description: 'Generates the JS bundle and source map', | ||
}, | ||
{ | ||
name: '--port [number]', | ||
default: process.env.RCT_METRO_PORT || 8081, | ||
parse: (val: number) => String(val), | ||
thymikee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
], | ||
examples: [ | ||
{ | ||
desc: | ||
'Download the Hermes Sampling Profiler to the directory <destinationDir> on the local machine', | ||
cmd: 'profile-hermes /tmp', | ||
}, | ||
], | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.