add a stats object with timings (#1257)

pull/1299/head
Rich-Harris 7 years ago
parent 0f5912f316
commit 75c1fbcf7b

@ -0,0 +1,73 @@
const now = (typeof process !== 'undefined' && process.hrtime)
? () => {
const t = process.hrtime();
return t[0] * 1e3 + t[1] / 1e6;
}
: () => window.performance.now();
type Timing = {
label: string;
start: number;
end: number;
children: Timing[];
}
function collapseTimings(timings) {
const result = {};
timings.forEach(timing => {
result[timing.label] = Object.assign({
total: timing.end - timing.start
}, timing.children && collapseTimings(timing.children));
});
return result;
}
export default class Stats {
startTime: number;
currentTiming: Timing;
currentChildren: Timing[];
timings: Timing[];
stack: Timing[];
constructor() {
this.startTime = now();
this.stack = [];
this.currentChildren = this.timings = [];
}
start(label) {
const timing = {
label,
start: now(),
end: null,
children: []
};
this.currentChildren.push(timing);
this.stack.push(timing);
this.currentTiming = timing;
this.currentChildren = timing.children;
}
stop(label) {
if (label !== this.currentTiming.label) {
throw new Error(`Mismatched timing labels`);
}
this.currentTiming.end = now();
this.stack.pop();
this.currentTiming = this.stack[this.stack.length - 1];
this.currentChildren = this.currentTiming ? this.currentTiming.children : this.timings;
}
toJSON() {
const timings = Object.assign({
total: now() - this.startTime
}, collapseTimings(this.timings));
return {
timings
};
}
}

@ -2,6 +2,7 @@ import MagicString, { Bundle } from 'magic-string';
import isReference from 'is-reference'; import isReference from 'is-reference';
import { walk, childKeys } from 'estree-walker'; import { walk, childKeys } from 'estree-walker';
import { getLocator } from 'locate-character'; import { getLocator } from 'locate-character';
import Stats from '../Stats';
import deindent from '../utils/deindent'; import deindent from '../utils/deindent';
import CodeBuilder from '../utils/CodeBuilder'; import CodeBuilder from '../utils/CodeBuilder';
import getCodeFrame from '../utils/getCodeFrame'; import getCodeFrame from '../utils/getCodeFrame';
@ -76,6 +77,8 @@ childKeys.EachBlock = childKeys.IfBlock = ['children', 'else'];
childKeys.Attribute = ['value']; childKeys.Attribute = ['value'];
export default class Generator { export default class Generator {
stats: Stats;
ast: Parsed; ast: Parsed;
parsed: Parsed; parsed: Parsed;
source: string; source: string;
@ -123,8 +126,12 @@ export default class Generator {
name: string, name: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
options: CompileOptions, options: CompileOptions,
stats: Stats,
dom: boolean dom: boolean
) { ) {
stats.start('compile');
this.stats = stats;
this.ast = clone(parsed); this.ast = clone(parsed);
this.parsed = parsed; this.parsed = parsed;
@ -372,10 +379,13 @@ export default class Generator {
} }
}); });
this.stats.stop('compile');
return { return {
ast: this.ast, ast: this.ast,
js, js,
css, css,
stats: this.stats.toJSON(),
// TODO deprecate // TODO deprecate
code: js.code, code: js.code,

@ -11,6 +11,7 @@ import reservedNames from '../../utils/reservedNames';
import shared from './shared'; import shared from './shared';
import Generator from '../Generator'; import Generator from '../Generator';
import Stylesheet from '../../css/Stylesheet'; import Stylesheet from '../../css/Stylesheet';
import Stats from '../../Stats';
import Block from './Block'; import Block from './Block';
import { test } from '../../config'; import { test } from '../../config';
import { Parsed, CompileOptions, Node } from '../../interfaces'; import { Parsed, CompileOptions, Node } from '../../interfaces';
@ -34,9 +35,10 @@ export class DomGenerator extends Generator {
source: string, source: string,
name: string, name: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
options: CompileOptions options: CompileOptions,
stats: Stats
) { ) {
super(parsed, source, name, stylesheet, options, true); super(parsed, source, name, stylesheet, options, stats, true);
this.blocks = []; this.blocks = [];
this.readonly = new Set(); this.readonly = new Set();
@ -81,11 +83,12 @@ export default function dom(
parsed: Parsed, parsed: Parsed,
source: string, source: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
options: CompileOptions options: CompileOptions,
stats: Stats
) { ) {
const format = options.format || 'es'; const format = options.format || 'es';
const generator = new DomGenerator(parsed, source, options.name || 'SvelteComponent', stylesheet, options); const generator = new DomGenerator(parsed, source, options.name || 'SvelteComponent', stylesheet, options, stats);
const { const {
computations, computations,

@ -1,5 +1,6 @@
import deindent from '../../utils/deindent'; import deindent from '../../utils/deindent';
import Generator from '../Generator'; import Generator from '../Generator';
import Stats from '../../Stats';
import Stylesheet from '../../css/Stylesheet'; import Stylesheet from '../../css/Stylesheet';
import Block from './Block'; import Block from './Block';
import visit from './visit'; import visit from './visit';
@ -20,9 +21,10 @@ export class SsrGenerator extends Generator {
source: string, source: string,
name: string, name: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
options: CompileOptions options: CompileOptions,
stats: Stats
) { ) {
super(parsed, source, name, stylesheet, options, false); super(parsed, source, name, stylesheet, options, stats, false);
this.bindings = []; this.bindings = [];
this.renderCode = ''; this.renderCode = '';
this.appendTargets = []; this.appendTargets = [];
@ -45,11 +47,12 @@ export default function ssr(
parsed: Parsed, parsed: Parsed,
source: string, source: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
options: CompileOptions options: CompileOptions,
stats: Stats
) { ) {
const format = options.format || 'cjs'; const format = options.format || 'cjs';
const generator = new SsrGenerator(parsed, source, options.name || 'SvelteComponent', stylesheet, options); const generator = new SsrGenerator(parsed, source, options.name || 'SvelteComponent', stylesheet, options, stats);
const { computations, name, templateProperties } = generator; const { computations, name, templateProperties } = generator;

@ -2,6 +2,7 @@ import parse from './parse/index';
import validate from './validate/index'; import validate from './validate/index';
import generate from './generators/dom/index'; import generate from './generators/dom/index';
import generateSSR from './generators/server-side-rendering/index'; import generateSSR from './generators/server-side-rendering/index';
import Stats from './Stats';
import { assign } from './shared/index.js'; import { assign } from './shared/index.js';
import Stylesheet from './css/Stylesheet'; import Stylesheet from './css/Stylesheet';
import { Parsed, CompileOptions, Warning, PreprocessOptions, Preprocessor } from './interfaces'; import { Parsed, CompileOptions, Warning, PreprocessOptions, Preprocessor } from './interfaces';
@ -109,20 +110,28 @@ export function compile(source: string, _options: CompileOptions) {
const options = normalizeOptions(_options); const options = normalizeOptions(_options);
let parsed: Parsed; let parsed: Parsed;
const stats = new Stats();
try { try {
stats.start('parse');
parsed = parse(source, options); parsed = parse(source, options);
stats.stop('parse');
} catch (err) { } catch (err) {
options.onerror(err); options.onerror(err);
return; return;
} }
stats.start('stylesheet');
const stylesheet = new Stylesheet(source, parsed, options.filename, options.cascade !== false, options.dev); const stylesheet = new Stylesheet(source, parsed, options.filename, options.cascade !== false, options.dev);
stats.stop('stylesheet');
stats.start('validate');
validate(parsed, source, stylesheet, options); validate(parsed, source, stylesheet, options);
stats.stop('validate');
const compiler = options.generate === 'ssr' ? generateSSR : generate; const compiler = options.generate === 'ssr' ? generateSSR : generate;
return compiler(parsed, source, stylesheet, options); return compiler(parsed, source, stylesheet, options, stats);
}; };
export function create(source: string, _options: CompileOptions = {}) { export function create(source: string, _options: CompileOptions = {}) {

@ -93,4 +93,4 @@ export interface PreprocessOptions {
filename?: string filename?: string
} }
export type Preprocessor = (options: {content: string, attributes: Record<string, string | boolean>, filename?: string}) => { code: string, map?: SourceMap | string }; export type Preprocessor = (options: {content: string, attributes: Record<string, string | boolean>, filename?: string}) => { code: string, map?: SourceMap | string };
Loading…
Cancel
Save