fix(cli): show memory usage after build for `nuxt dev` (#5514)

This commit is contained in:
Pim 2019-04-12 19:19:46 +02:00 committed by Pooya Parsa
parent 9980a9d681
commit 19fbbb6ef6
4 changed files with 61 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import opener from 'opener'
import { common, server } from '../options' import { common, server } from '../options'
import { eventsMapping, formatPath } from '../utils' import { eventsMapping, formatPath } from '../utils'
import { showBanner } from '../utils/banner' import { showBanner } from '../utils/banner'
import { showMemoryUsage } from '../utils/memory'
export default { export default {
name: 'dev', name: 'dev',
@ -50,7 +51,7 @@ export default {
await nuxt.server.listen() await nuxt.server.listen()
// Show banner when listening // Show banner when listening
showBanner(nuxt) showBanner(nuxt, false)
// Opens the server listeners url in the default browser (only once) // Opens the server listeners url in the default browser (only once)
if (argv.open) { if (argv.open) {
@ -65,6 +66,9 @@ export default {
// Start Build // Start Build
await builder.build() await builder.build()
// Print memory usage
showMemoryUsage()
// Return instance // Return instance
return nuxt return nuxt
}, },

View File

@ -1,10 +1,10 @@
import consola from 'consola' import consola from 'consola'
import prettyBytes from 'pretty-bytes'
import env from 'std-env' import env from 'std-env'
import chalk from 'chalk' import chalk from 'chalk'
import { successBox } from './formatting' import { successBox } from './formatting'
import { getFormattedMemoryUsage } from './memory'
export function showBanner(nuxt) { export function showBanner(nuxt, showMemoryUsage = true) {
if (env.test) { if (env.test) {
return return
} }
@ -29,9 +29,9 @@ export function showBanner(nuxt) {
titleLines.push(`TypeScript support is ${chalk.green.bold('enabled')}`) titleLines.push(`TypeScript support is ${chalk.green.bold('enabled')}`)
} }
// https://nodejs.org/api/process.html#process_process_memoryusage if (showMemoryUsage) {
const { heapUsed, rss } = process.memoryUsage() titleLines.push(getFormattedMemoryUsage())
titleLines.push(`Memory usage: ${chalk.bold(prettyBytes(heapUsed))} (RSS: ${prettyBytes(rss)})`) }
// Listeners // Listeners
for (const listener of nuxt.server.listeners) { for (const listener of nuxt.server.listeners) {

View File

@ -0,0 +1,18 @@
import chalk from 'chalk'
import consola from 'consola'
import prettyBytes from 'pretty-bytes'
export function getMemoryUsage() {
// https://nodejs.org/api/process.html#process_process_memoryusage
const { heapUsed, rss } = process.memoryUsage()
return { heap: heapUsed, rss }
}
export function getFormattedMemoryUsage() {
const { heap, rss } = getMemoryUsage()
return `Memory usage: ${chalk.bold(prettyBytes(heap))} (RSS: ${prettyBytes(rss)})`
}
export function showMemoryUsage() {
consola.info(getFormattedMemoryUsage())
}

View File

@ -3,6 +3,7 @@ import { consola } from '../utils'
import { loadNuxtConfig } from '../../src/utils/config' import { loadNuxtConfig } from '../../src/utils/config'
import * as utils from '../../src/utils' import * as utils from '../../src/utils'
import { showBanner } from '../../src/utils/banner' import { showBanner } from '../../src/utils/banner'
import { showMemoryUsage } from '../../src/utils/memory'
import * as fmt from '../../src/utils/formatting' import * as fmt from '../../src/utils/formatting'
jest.mock('std-env', () => ({ jest.mock('std-env', () => ({
@ -121,7 +122,7 @@ describe('cli/utils', () => {
expect(fmt.indent(4, '-')).toBe('----') expect(fmt.indent(4, '-')).toBe('----')
}) })
test('showBanner prints full-info box', () => { test('showBanner prints full-info box with memory usage', () => {
const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {}) const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
const successBox = jest.fn().mockImplementation((m, t) => t + m) const successBox = jest.fn().mockImplementation((m, t) => t + m)
jest.spyOn(fmt, 'successBox').mockImplementation(successBox) jest.spyOn(fmt, 'successBox').mockImplementation(successBox)
@ -148,10 +149,41 @@ describe('cli/utils', () => {
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js')) expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js'))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching(`Listening on: ${listeners[0].url}`)) expect(stdout).toHaveBeenCalledWith(expect.stringMatching(`Listening on: ${listeners[0].url}`))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching(`Listening on: ${listeners[1].url}`)) expect(stdout).toHaveBeenCalledWith(expect.stringMatching(`Listening on: ${listeners[1].url}`))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('badgeMessage')) expect(stdout).toHaveBeenCalledWith(expect.stringMatching('badgeMessage'))
stdout.mockRestore() stdout.mockRestore()
}) })
test('showBanner doesnt print memory usage', () => {
const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
const successBox = jest.fn().mockImplementation((m, t) => t + m)
jest.spyOn(fmt, 'successBox').mockImplementation(successBox)
showBanner({
options: {
cli: {
badgeMessages: []
}
},
server: {
listeners: []
}
}, false)
expect(successBox).toHaveBeenCalledTimes(1)
expect(stdout).toHaveBeenCalledTimes(1)
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js'))
expect(stdout).not.toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
stdout.mockRestore()
})
test('showMemoryUsage prints memory usage', () => {
showMemoryUsage()
expect(consola.info).toHaveBeenCalledTimes(1)
expect(consola.info).toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
})
test('forceExit exits after timeout', () => { test('forceExit exits after timeout', () => {
jest.useFakeTimers() jest.useFakeTimers()
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {}) const exit = jest.spyOn(process, 'exit').mockImplementation(() => {})