Skip to content

Commit

Permalink
feat(devtools): performance events
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed May 19, 2021
1 parent 1355ee2 commit f7c54ca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/runtime-core/src/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const enum DevtoolsHooks {
COMPONENT_UPDATED = 'component:updated',
COMPONENT_ADDED = 'component:added',
COMPONENT_REMOVED = 'component:removed',
COMPONENT_EMIT = 'component:emit'
COMPONENT_EMIT = 'component:emit',
PERFORMANCE_START = 'perf:start',
PERFORMANCE_END = 'perf:end'
}

interface DevtoolsHook {
Expand Down Expand Up @@ -73,6 +75,28 @@ function createDevtoolsComponentHook(hook: DevtoolsHooks) {
}
}

export const devtoolsPerfStart = /*#__PURE__*/ createDevtoolsPerformanceHook(
DevtoolsHooks.PERFORMANCE_START
)

export const devtoolsPerfEnd = /*#__PURE__*/ createDevtoolsPerformanceHook(
DevtoolsHooks.PERFORMANCE_END
)

function createDevtoolsPerformanceHook(hook: DevtoolsHooks) {
return (component: ComponentInternalInstance, type: string, time: number) => {
if (!devtools) return
devtools.emit(
hook,
component.appContext.app,
component.uid,
component,
type,
time
)
}
}

export function devtoolsComponentEmit(
component: ComponentInternalInstance,
event: string,
Expand Down
9 changes: 9 additions & 0 deletions packages/runtime-core/src/profiling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ComponentInternalInstance, formatComponentName } from './component'
import { devtoolsPerfEnd, devtoolsPerfStart } from './devtools'

let supported: boolean
let perf: any
Expand All @@ -10,6 +11,10 @@ export function startMeasure(
if (instance.appContext.config.performance && isSupported()) {
perf.mark(`vue-${type}-${instance.uid}`)
}

if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
devtoolsPerfStart(instance, type, supported ? perf.now() : Date.now())
}
}

export function endMeasure(instance: ComponentInternalInstance, type: string) {
Expand All @@ -25,6 +30,10 @@ export function endMeasure(instance: ComponentInternalInstance, type: string) {
perf.clearMarks(startTag)
perf.clearMarks(endTag)
}

if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
devtoolsPerfEnd(instance, type, supported ? perf.now() : Date.now())
}
}

function isSupported() {
Expand Down

0 comments on commit f7c54ca

Please sign in to comment.