mirror of
https://github.com/jlfwong/speedscope.git
synced 2024-11-26 07:35:55 +03:00
21167e69d8
Browsers have a limit on how big you can make strings. In Chrome on a 64 bit machine, this is around 512MB, which explains why in #340, a 600MB file fails to load. To work around this issue, we avoid making strings this large. To do so, we need two core changes: 1. Instead of sending large strings as the import mechanism to different file format importers, we introduce a new `TextFileContent` interface which exposes methods to get the lines in the file or the JSON representation. In the case of line splitting, we assume that no single line exceeds the 512MB limit. 2. We introduce a dependency on https://github.com/evanw/uint8array-json-parser to allow us to parse JSON files contained in `Uint8Array` objects To ensure that this code doesn't code rot without introducing 600MB test files or test file generation into the repository, we also re-run a small set of tests with a mocked maximum string size of 100 bytes. You can see that the chunked string representation code is getting executed via test coverage. Fixes #340
118 lines
3.4 KiB
JavaScript
Executable File
118 lines
3.4 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
const path = require('path')
|
|
const fs = require('fs')
|
|
const os = require('os')
|
|
const stream = require('stream')
|
|
|
|
const open = require('open')
|
|
|
|
const helpString = `Usage: speedscope [filepath]
|
|
|
|
If invoked with no arguments, will open a local copy of speedscope in your default browser.
|
|
Once open, you can browse for a profile to import.
|
|
|
|
If - is used as the filepath, will read from stdin instead.
|
|
|
|
cat /path/to/profile | speedscope -
|
|
`
|
|
|
|
function getProfileStream(relPath) {
|
|
const absPath = path.resolve(process.cwd(), relPath)
|
|
if (relPath === '-') {
|
|
// Read from stdin
|
|
return process.stdin
|
|
} else {
|
|
return fs.createReadStream(absPath)
|
|
}
|
|
}
|
|
|
|
function getProfileBuffer(relPath) {
|
|
const profileStream = getProfileStream(relPath)
|
|
const chunks = []
|
|
return new Promise((resolve, reject) => {
|
|
profileStream.pipe(
|
|
stream.Writable({
|
|
write(chunk, encoding, callback) {
|
|
chunks.push(chunk)
|
|
callback()
|
|
},
|
|
final() {
|
|
resolve(Buffer.concat(chunks))
|
|
},
|
|
}),
|
|
)
|
|
profileStream.on('error', ev => reject(ev))
|
|
})
|
|
}
|
|
|
|
async function main() {
|
|
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
|
console.log(helpString)
|
|
return
|
|
}
|
|
|
|
if (process.argv.includes('--version') || process.argv.includes('-v')) {
|
|
console.log('v' + require('../package.json').version)
|
|
return
|
|
}
|
|
|
|
if (process.argv.length > 3) {
|
|
throw new Error('At most one argument expected')
|
|
}
|
|
|
|
let urlToOpen = 'file://' + path.resolve(__dirname, '../dist/release/index.html')
|
|
|
|
if (process.argv.length === 3) {
|
|
const relPath = process.argv[2]
|
|
const sourceBuffer = await getProfileBuffer(relPath)
|
|
const filename = path.basename(relPath)
|
|
|
|
let jsSource
|
|
try {
|
|
const sourceBase64 = sourceBuffer.toString('base64')
|
|
jsSource = `speedscope.loadFileFromBase64(${JSON.stringify(filename)}, ${JSON.stringify(
|
|
sourceBase64,
|
|
)})`
|
|
} catch(e) {
|
|
if (e && e.message && /Cannot create a string longer than/.exec(e.message)) {
|
|
jsSource = `alert("Sorry, ${filename} is too large to be loaded via command-line argument! Try dragging it into speedscope instead.")`
|
|
} else {
|
|
throw e
|
|
}
|
|
}
|
|
|
|
const filePrefix = `speedscope-${+new Date()}-${process.pid}`
|
|
const jsPath = path.join(os.tmpdir(), `${filePrefix}.js`)
|
|
console.log(`Creating temp file ${jsPath}`)
|
|
fs.writeFileSync(jsPath, jsSource)
|
|
urlToOpen += `#localProfilePath=${jsPath}`
|
|
|
|
// For some silly reason, the OS X open command ignores any query parameters or hash parameters
|
|
// passed as part of the URL. To get around this weird issue, we'll create a local HTML file
|
|
// that just redirects.
|
|
const htmlPath = path.join(os.tmpdir(), `${filePrefix}.html`)
|
|
console.log(`Creating temp file ${htmlPath}`)
|
|
fs.writeFileSync(htmlPath, `<script>window.location=${JSON.stringify(urlToOpen)}</script>`)
|
|
|
|
urlToOpen = `file://${htmlPath}`
|
|
}
|
|
|
|
console.log('Opening', urlToOpen, 'in your default browser')
|
|
|
|
// We'd like to avoid blocking the terminal on the browsing closing,
|
|
// but for some reason this doesn't work at all on Windows if we
|
|
// don't use wait: true.
|
|
const wait = process.platform === "win32";
|
|
await open(urlToOpen, {wait})
|
|
}
|
|
|
|
main()
|
|
.then(() => {
|
|
process.exit(0)
|
|
})
|
|
.catch(e => {
|
|
console.log(e.stack + '\n')
|
|
console.log(helpString)
|
|
process.exit(1)
|
|
})
|