Skip to content

Commit

Permalink
feat($core): debug to see performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Dec 2, 2018
1 parent 43479a2 commit 0876491
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/@vuepress/cli/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { chalk } = require('@vuepress/shared-utils')
const { chalk, performance } = require('@vuepress/shared-utils')
const semver = require('semver')

try {
Expand Down Expand Up @@ -33,6 +33,8 @@ exports.bootstrap = function ({
const { path, logger, env } = require('@vuepress/shared-utils')
const { dev, build, eject } = require('@vuepress/core')

performance.start()

cli
.version(pkg.version)
.help()
Expand Down
5 changes: 3 additions & 2 deletions packages/@vuepress/core/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
module.exports = async function build (sourceDir, cliOptions = {}) {
process.env.NODE_ENV = 'production'

const { path } = require('@vuepress/shared-utils')
const webpack = require('webpack')
const readline = require('readline')
const escape = require('escape-html')

const { chalk, fs, logger, env } = require('@vuepress/shared-utils')
const { chalk, fs, path, logger, env, performance } = require('@vuepress/shared-utils')
const prepare = require('./prepare/index')
const createClientConfig = require('./webpack/createClientConfig')
const createServerConfig = require('./webpack/createServerConfig')
Expand Down Expand Up @@ -83,6 +82,8 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
// DONE.
const relativeDir = path.relative(cwd, outDir)
logger.success(`${chalk.green('Success!')} Generated static files in ${chalk.cyan(relativeDir)}.\n`)
const { duration } = performance.stop()
logger.debug(`It took a total of ${duration}ms to run the ${chalk.cyan('vuepress build')}.`)

// --- helpers ---

Expand Down
6 changes: 4 additions & 2 deletions packages/@vuepress/core/lib/webpack/DevLogPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

const { chalk, logger } = require('@vuepress/shared-utils')
const { chalk, logger, performance } = require('@vuepress/shared-utils')

/**
* Expose DevLogPlugin class.
Expand Down Expand Up @@ -35,7 +35,9 @@ module.exports = class DevLogPlugin {
)
if (isFirst) {
isFirst = false
console.log(`\n${chalk.gray('>')} VuePress dev server listening at ${chalk.cyan(displayUrl)}`)
console.log(`${chalk.gray('>')} VuePress dev server listening at ${chalk.cyan(displayUrl)}`)
const { duration } = performance.stop()
logger.debug(`It took a total of ${duration}ms to run the ${chalk.cyan('vuepress dev')} for the first time.`)
}
})
compiler.hooks.invalid.tap('vuepress-log', clearScreen)
Expand Down
2 changes: 2 additions & 0 deletions packages/@vuepress/shared-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ exports.fallback = require('./lib/fallback')
exports.slugify = require('./lib/slugify')
exports.tryChain = require('./lib/tryChain')
exports.toAbsolutePath = require('./lib/toAbsolutePath')

exports.performance = require('./lib/performance')
23 changes: 23 additions & 0 deletions packages/@vuepress/shared-utils/lib/performance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const os = require('os')

class Performance {
constructor () {
this._totalMemory = os.totalmem()
}

start () {
this._startTime = Date.now()
this._startFreeMemory = os.freemem()
}

stop () {
this._endTime = Date.now()
this._endFreeMemory = os.freemem()
return {
duration: this._endTime - this._startTime,
memoryDiff: this._endFreeMemory - this._startFreeMemory
}
}
}

module.exports = new Performance()

0 comments on commit 0876491

Please sign in to comment.