Skip to content

Commit 5b87b59

Browse files
authored
Merge pull request #62 from Kikobeats/next
chore(profiling): install time
2 parents 01ffa37 + b2df4b9 commit 5b87b59

3 files changed

Lines changed: 17 additions & 16 deletions

File tree

src/compile/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const path = require('path')
66
const transformDependencies = require('./transform-dependencies')
77
const installDependencies = require('./install-dependencies')
88
const detectDependencies = require('./detect-dependencies')
9+
const timeSpan = require('@kikobeats/time-span')()
910
const { debug, duration } = require('../debug')
1011
const template = require('../template')
1112
const build = require('./build')
@@ -26,22 +27,27 @@ const enqueueInstall = (tmpdir, dependencies, allow) => {
2627

2728
module.exports = async (snippet, { tmpdir = DEFAULT_TMPDIR, allow = {} } = {}) => {
2829
let content = template(snippet)
30+
const phases = { install: 0 }
2931

3032
const dependencies = detectDependencies(content)
3133
if (dependencies.length) {
3234
content = transformDependencies(content)
3335
mkdirSync(tmpdir, { recursive: true })
36+
const elapsed = timeSpan()
3437
await duration('npm:install', () => enqueueInstall(tmpdir, dependencies, allow), {
3538
dependencies
3639
})
40+
phases.install = elapsed()
3741
}
3842

3943
const cwd = dependencies.length ? tmpdir : process.cwd()
44+
const elapsed = timeSpan()
4045
const result = await duration('esbuild', () => build({ content, cwd }))
46+
phases.build = elapsed()
4147
debug('esbuild:output', { content: result.outputFiles[0].text.length })
4248
content = result.outputFiles[0].text
4349

44-
return content
50+
return { content, phases }
4551
}
4652

4753
module.exports.DEFAULT_TMPDIR = DEFAULT_TMPDIR

src/index.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ module.exports = ({ tmpdir } = {}) => {
4747
let total
4848
try {
4949
total = timeSpan()
50-
const content = await compilePromise
51-
const compileMs = total()
50+
const compiled = await compilePromise
5251

52+
const spawnElapsed = timeSpan()
5353
const subprocess = spawn({
5454
args: JSON.stringify(args),
5555
env: {
@@ -59,27 +59,21 @@ module.exports = ({ tmpdir } = {}) => {
5959
timeout
6060
})
6161
subprocess.stdin.on('error', () => {})
62-
Readable.from(content).pipe(subprocess.stdin)
62+
Readable.from(compiled.content).pipe(subprocess.stdin)
6363
const { stdout } = await subprocess
64+
const spawnMs = spawnElapsed()
6465
const { isFulfilled, value, profiling, logging } = JSON.parse(stdout)
65-
const totalMs = total()
6666
const { run, ...rest } = profiling
6767
const result = {
6868
...rest,
6969
phases: {
70-
compile: compileMs,
71-
spawn: totalMs - compileMs - run,
70+
...compiled.phases,
71+
spawn: spawnMs - run,
7272
run,
73-
total: totalMs
73+
total: total()
7474
}
7575
}
76-
debug('node', {
77-
cpu: `${Math.round(result.cpu)}ms`,
78-
memory: `${Math.round(result.memory / (1024 * 1024))}MiB`,
79-
phases: `compile=${Math.round(result.phases.compile)}ms spawn=${Math.round(
80-
result.phases.spawn
81-
)}ms run=${Math.round(result.phases.run)}ms total=${Math.round(result.phases.total)}ms`
82-
})
76+
debug('node', { cpu: result.cpu, memory: result.memory, ...result.phases })
8377

8478
return isFulfilled
8579
? { isFulfilled, value, profiling: result, logging }

test/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ test('memory profiling', async t => {
122122
t.is(value, undefined)
123123
t.is(typeof profiling.cpu, 'number')
124124
t.is(typeof profiling.memory, 'number')
125-
t.is(typeof profiling.phases.compile, 'number')
125+
t.is(typeof profiling.phases.install, 'number')
126+
t.is(typeof profiling.phases.build, 'number')
126127
t.is(typeof profiling.phases.spawn, 'number')
127128
t.is(typeof profiling.phases.run, 'number')
128129
t.is(typeof profiling.phases.total, 'number')

0 commit comments

Comments
 (0)