From 740bf073b2bec66b36c2fc685556eb3d80b5df90 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sun, 15 Nov 2020 01:52:18 +0100 Subject: [PATCH] feat: timing plugin and Server-Timing --- packages/nitro/src/config.ts | 4 ++-- packages/nitro/src/rollup/config.ts | 8 ++++---- packages/nitro/src/rollup/timing.ts | 31 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 packages/nitro/src/rollup/timing.ts diff --git a/packages/nitro/src/config.ts b/packages/nitro/src/config.ts index 6a2c05f7ab..5fdef9687d 100644 --- a/packages/nitro/src/config.ts +++ b/packages/nitro/src/config.ts @@ -37,7 +37,7 @@ export interface SLSOptions { minify: boolean externals: boolean rollupConfig?: any - logStartup: boolean + timing: boolean inlineChunks: boolean renderer: string analyze: boolean @@ -72,7 +72,7 @@ export function getoptions (nuxtOptions: Nuxt['options'], serverless: SLSConfig) staticAssets: nuxtOptions.generate.staticAssets, outName: '_nuxt.js', - logStartup: true, + timing: true, inlineChunks: true, minify: false, externals: false, diff --git a/packages/nitro/src/rollup/config.ts b/packages/nitro/src/rollup/config.ts index d63328d374..71dce30371 100644 --- a/packages/nitro/src/rollup/config.ts +++ b/packages/nitro/src/rollup/config.ts @@ -14,8 +14,10 @@ import analyze from 'rollup-plugin-analyzer' import hasha from 'hasha' import { SLSOptions } from '../config' import { resolvePath, MODULE_DIR } from '../utils' + import { dynamicRequire } from './dynamic-require' import { externals } from './externals' +import { timing } from './timing' const mapArrToVal = (val, arr) => arr.reduce((p, c) => ({ ...p, [c]: val }), {}) @@ -107,10 +109,8 @@ export const getRollupConfig = (options: SLSOptions) => { plugins: [] } - if (options.logStartup) { - rollupConfig.output.intro += 'global._startTime = global.process.hrtime();' - // eslint-disable-next-line no-template-curly-in-string - rollupConfig.output.outro += 'global._endTime = global.process.hrtime(global._startTime); global._coldstart = ((global._endTime[0] * 1e9) + global._endTime[1]) / 1e6; console.log(`λ Cold start took: ${global._coldstart}ms (${typeof __filename !== "undefined" ? __filename.replace(process.cwd(), "") : ""})`);' + if (options.timing) { + rollupConfig.plugins.push(timing()) } // https://github.com/rollup/plugins/tree/master/packages/replace diff --git a/packages/nitro/src/rollup/timing.ts b/packages/nitro/src/rollup/timing.ts new file mode 100644 index 0000000000..97b26f1e53 --- /dev/null +++ b/packages/nitro/src/rollup/timing.ts @@ -0,0 +1,31 @@ +import { extname } from 'path' +import type { Plugin, RenderedChunk } from 'rollup' + +export interface Options { } + +const TIMING = 'global.__timing__' + +const iife = code => `(function() { ${code.trim()} })();`.replace(/\n/g, '') + +const HELPER = TIMING + '=' + iife(` +const hrtime = global.process.hrtime; +const start = () => hrtime(); +const end = s => { const d = hrtime(s); return ((d[0] * 1e9) + d[1]) / 1e6; }; + +const _s = {}; +const metrics = []; +const logStart = id => { _s[id] = hrtime(); }; +const logEnd = id => { const t = end(_s[id]); delete _s[id]; metrics.push([id, t]); console.log('◈', id, t, 'ms'); }; +return { hrtime, start, end, metrics, logStart, logEnd }; +`) + +export function timing (_opts: Options = {}): Plugin { + return { + name: 'timing', + renderChunk (code, chunk: RenderedChunk) { + let name = chunk.fileName || '' + name = name.replace(extname(name), '') + return "'use strict';" + HELPER + `${TIMING}.logStart('import:${name}');` + code + `;${TIMING}.logEnd('import:${name}');` + } + } +}