-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a script to easily test Sucrase perf on a project (#341)
This should make it easier to benchmark and profile real-world code.
- Loading branch information
1 parent
e460053
commit 41476b0
Showing
6 changed files
with
119 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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,66 @@ | ||
#!/usr/bin/env node | ||
/* eslint-disable no-console */ | ||
import parseArgs from "yargs-parser"; | ||
|
||
// @ts-ignore: May not be built, just ignore for now. | ||
import * as sucrase from "../dist/index"; // eslint-disable-line import/no-unresolved | ||
import {FileInfo, loadProjectFiles} from "./loadProjectFiles"; | ||
|
||
async function main(): Promise<void> { | ||
const args = parseArgs(process.argv.slice(2), {boolean: ["profile"]}); | ||
const projectPath = args._[0]; | ||
const shouldProfile = args.profile; | ||
const numTimes = args.times || 1; | ||
|
||
const projectFiles = await loadProjectFiles(projectPath); | ||
if (numTimes === 1) { | ||
console.log(`Running Sucrase on ${projectPath}`); | ||
} else { | ||
console.log(`Running Sucrase ${numTimes} times on ${projectPath}`); | ||
} | ||
const totalLines = projectFiles | ||
.map(({code}) => code.split("\n").length) | ||
.reduce((a, b) => a + b, 0); | ||
console.log(`Found ${projectFiles.length} files with ${totalLines} lines`); | ||
|
||
if (shouldProfile) { | ||
console.log(`Make sure you have Chrome DevTools for Node open.`); | ||
// tslint:disable-next-line no-any | ||
(console as any).profile(`Sucrase ${projectPath}`); | ||
for (let i = 0; i < numTimes; i++) { | ||
for (const fileInfo of projectFiles) { | ||
runTransform(fileInfo); | ||
} | ||
} | ||
// tslint:disable-next-line no-any | ||
(console as any).profileEnd(`Sucrase ${projectPath}`); | ||
} else { | ||
const startTime = process.hrtime(); | ||
for (let i = 0; i < numTimes; i++) { | ||
for (const fileInfo of projectFiles) { | ||
runTransform(fileInfo); | ||
} | ||
} | ||
const totalTime = process.hrtime(startTime); | ||
const timeSeconds = totalTime[0] + totalTime[1] / 1e9; | ||
console.log(`Time taken: ${Math.round(timeSeconds * 1000) / 1000}s`); | ||
console.log(`Speed: ${Math.round((totalLines * numTimes) / timeSeconds)} lines per second`); | ||
} | ||
} | ||
|
||
function runTransform({code, path}: FileInfo): void { | ||
if (path.endsWith(".js") || path.endsWith(".jsx")) { | ||
sucrase.transform(code, {transforms: ["jsx", "imports", "flow"], filePath: path}); | ||
} else if (path.endsWith(".ts")) { | ||
sucrase.transform(code, {transforms: ["imports", "typescript"], filePath: path}); | ||
} else if (path.endsWith(".tsx")) { | ||
sucrase.transform(code, {transforms: ["jsx", "imports", "typescript"], filePath: path}); | ||
} else { | ||
throw new Error(`Unrecognized file type: ${path}`); | ||
} | ||
} | ||
|
||
main().catch((e) => { | ||
console.error(e); | ||
process.exitCode = 1; | ||
}); |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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