Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/cli/src/commands/dev.js
Expand Up @@ -4,6 +4,7 @@ import opener from 'opener'
import { common, server } from '../options'
import { eventsMapping, formatPath } from '../utils'
import { showBanner } from '../utils/banner'
import { showMemoryUsage } from '../utils/memory'

export default {
name: 'dev',
Expand Down Expand Up @@ -50,7 +51,7 @@ export default {
await nuxt.server.listen()

// Show banner when listening
showBanner(nuxt)
showBanner(nuxt, false)

// Opens the server listeners url in the default browser (only once)
if (argv.open) {
Expand All @@ -65,6 +66,9 @@ export default {
// Start Build
await builder.build()

// Print memory usage
showMemoryUsage()

// Return instance
return nuxt
},
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/utils/banner.js
@@ -1,10 +1,10 @@
import consola from 'consola'
import prettyBytes from 'pretty-bytes'
import env from 'std-env'
import chalk from 'chalk'
import { successBox } from './formatting'
import { getMemoryUsage } from './memory'

export function showBanner(nuxt) {
export function showBanner(nuxt, showMemoryUsage = true) {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
if (env.test) {
return
}
Expand All @@ -29,9 +29,9 @@ export function showBanner(nuxt) {
titleLines.push(`TypeScript support is ${chalk.green.bold('enabled')}`)
}

// https://nodejs.org/api/process.html#process_process_memoryusage
const { heapUsed, rss } = process.memoryUsage()
titleLines.push(`Memory usage: ${chalk.bold(prettyBytes(heapUsed))} (RSS: ${prettyBytes(rss)})`)
if (showMemoryUsage) {
titleLines.push(getMemoryUsage())
}

// Listeners
for (const listener of nuxt.server.listeners) {
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/src/utils/memory.js
@@ -0,0 +1,14 @@
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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is breaking banner.js

}

export function showMemoryUsage() {
const { heap, rss } = getMemoryUsage()
consola.info(`Memory usage: ${chalk.bold(prettyBytes(heap))} (RSS: ${prettyBytes(rss)})`)
}
34 changes: 33 additions & 1 deletion packages/cli/test/unit/utils.test.js
Expand Up @@ -3,6 +3,7 @@ import { consola } from '../utils'
import { loadNuxtConfig } from '../../src/utils/config'
import * as utils from '../../src/utils'
import { showBanner } from '../../src/utils/banner'
import { showMemoryUsage } from '../../src/utils/memory'
import * as fmt from '../../src/utils/formatting'

jest.mock('std-env', () => ({
Expand Down Expand Up @@ -121,7 +122,7 @@ describe('cli/utils', () => {
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 successBox = jest.fn().mockImplementation((m, t) => t + m)
jest.spyOn(fmt, 'successBox').mockImplementation(successBox)
Expand All @@ -148,10 +149,41 @@ describe('cli/utils', () => {
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[1].url}`))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('badgeMessage'))
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', () => {
jest.useFakeTimers()
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {})
Expand Down