forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nodejs/master
merge remote repo
- Loading branch information
Showing
29 changed files
with
396 additions
and
58 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,59 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const { spawn } = require('child_process'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
dur: [1], | ||
code: ['1', '"string"', 'process.versions', 'process'] | ||
}); | ||
|
||
function spawnProcess(code) { | ||
const cmd = process.execPath || process.argv[0]; | ||
const argv = ['-p', code]; | ||
return spawn(cmd, argv); | ||
} | ||
|
||
function start(state, code, bench, getNode) { | ||
const node = getNode(code); | ||
let stdout = ''; | ||
let stderr = ''; | ||
|
||
node.stdout.on('data', (data) => { | ||
stdout += data; | ||
}); | ||
|
||
node.stderr.on('data', (data) => { | ||
stderr += data; | ||
}); | ||
|
||
node.on('exit', (code) => { | ||
if (code !== 0) { | ||
console.error('------ stdout ------'); | ||
console.error(stdout); | ||
console.error('------ stderr ------'); | ||
console.error(stderr); | ||
throw new Error(`Error during node startup, exit code ${code}`); | ||
} | ||
state.throughput++; | ||
|
||
if (state.go) { | ||
start(state, code, bench, getNode); | ||
} else { | ||
bench.end(state.throughput); | ||
} | ||
}); | ||
} | ||
|
||
function main({ dur, code }) { | ||
const state = { | ||
go: true, | ||
throughput: 0 | ||
}; | ||
|
||
setTimeout(() => { | ||
state.go = false; | ||
}, dur * 1000); | ||
|
||
bench.start(); | ||
start(state, code, bench, spawnProcess); | ||
} |
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 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 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 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,67 @@ | ||
'use strict'; | ||
|
||
// This implements a light-weight printer that writes to stdout/stderr | ||
// directly to avoid the overhead in the console abstraction. | ||
|
||
const { formatWithOptions } = require('internal/util/inspect'); | ||
const { writeString } = internalBinding('fs'); | ||
const { handleErrorFromBinding } = require('internal/fs/utils'); | ||
const { guessHandleType } = internalBinding('util'); | ||
const { log } = require('internal/console/global'); | ||
|
||
const kStdout = 1; | ||
const kStderr = 2; | ||
const handleType = [undefined, undefined, undefined]; | ||
function getFdType(fd) { | ||
if (handleType[fd] === undefined) { | ||
handleType[fd] = guessHandleType(fd); | ||
} | ||
return handleType[fd]; | ||
} | ||
|
||
function formatAndWrite(fd, obj, ignoreErrors, colors = false) { | ||
const str = `${formatWithOptions({ colors }, obj)}\n`; | ||
const ctx = {}; | ||
writeString(fd, str, null, undefined, undefined, ctx); | ||
if (!ignoreErrors) { | ||
handleErrorFromBinding(ctx); | ||
} | ||
} | ||
|
||
let colors; | ||
function getColors() { | ||
if (colors === undefined) { | ||
colors = require('internal/tty').getColorDepth() > 2; | ||
} | ||
return colors; | ||
} | ||
|
||
// TODO(joyeecheung): replace more internal process._rawDebug() | ||
// and console.log() usage with this if possible. | ||
function print(fd, obj, ignoreErrors = true) { | ||
switch (getFdType(fd)) { | ||
case 'TTY': | ||
formatAndWrite(fd, obj, ignoreErrors, getColors()); | ||
break; | ||
case 'FILE': | ||
formatAndWrite(fd, obj, ignoreErrors); | ||
break; | ||
case 'PIPE': | ||
case 'TCP': | ||
// Fallback to console.log to handle IPC. | ||
if (process.channel && process.channel.fd === fd) { | ||
log(obj); | ||
} else { | ||
formatAndWrite(fd, obj, ignoreErrors); | ||
} | ||
break; | ||
default: | ||
log(obj); | ||
} | ||
} | ||
|
||
module.exports = { | ||
print, | ||
kStderr, | ||
kStdout | ||
}; |
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 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 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
Oops, something went wrong.