From 08764918b7a9401026cdd664575427b55ae171fb Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 3 Dec 2018 05:03:19 +0800 Subject: [PATCH] feat($core): debug to see performance --- packages/@vuepress/cli/index.js | 4 +++- packages/@vuepress/core/lib/build.js | 5 ++-- .../core/lib/webpack/DevLogPlugin.js | 6 +++-- packages/@vuepress/shared-utils/index.js | 2 ++ .../@vuepress/shared-utils/lib/performance.js | 23 +++++++++++++++++++ 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 packages/@vuepress/shared-utils/lib/performance.js diff --git a/packages/@vuepress/cli/index.js b/packages/@vuepress/cli/index.js index da05bdb24c..d10d102c7a 100644 --- a/packages/@vuepress/cli/index.js +++ b/packages/@vuepress/cli/index.js @@ -1,4 +1,4 @@ -const { chalk } = require('@vuepress/shared-utils') +const { chalk, performance } = require('@vuepress/shared-utils') const semver = require('semver') try { @@ -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() diff --git a/packages/@vuepress/core/lib/build.js b/packages/@vuepress/core/lib/build.js index 22f90b8f78..ac623af16c 100644 --- a/packages/@vuepress/core/lib/build.js +++ b/packages/@vuepress/core/lib/build.js @@ -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') @@ -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 --- diff --git a/packages/@vuepress/core/lib/webpack/DevLogPlugin.js b/packages/@vuepress/core/lib/webpack/DevLogPlugin.js index 4578c18619..ec99352b3a 100644 --- a/packages/@vuepress/core/lib/webpack/DevLogPlugin.js +++ b/packages/@vuepress/core/lib/webpack/DevLogPlugin.js @@ -4,7 +4,7 @@ * Module dependencies. */ -const { chalk, logger } = require('@vuepress/shared-utils') +const { chalk, logger, performance } = require('@vuepress/shared-utils') /** * Expose DevLogPlugin class. @@ -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) diff --git a/packages/@vuepress/shared-utils/index.js b/packages/@vuepress/shared-utils/index.js index 49c2b134bc..5178c71668 100644 --- a/packages/@vuepress/shared-utils/index.js +++ b/packages/@vuepress/shared-utils/index.js @@ -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') diff --git a/packages/@vuepress/shared-utils/lib/performance.js b/packages/@vuepress/shared-utils/lib/performance.js new file mode 100644 index 0000000000..3ae3eff9e0 --- /dev/null +++ b/packages/@vuepress/shared-utils/lib/performance.js @@ -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()