Skip to content

Commit

Permalink
feat(vue2): perf timeline, closes #1744
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Mar 9, 2022
1 parent 32b4611 commit dd47083
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
55 changes: 55 additions & 0 deletions packages/app-backend-vue2/src/components/perf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { DevtoolsApi } from '@vue-devtools/app-backend-api'
import { HookEvents, SharedData } from '@vue-devtools/shared-utils'
import { instanceMap } from './tree'

const COMPONENT_HOOKS = {
beforeCreate: { start: 'create' },
created: { end: 'create' },
beforeMount: { start: 'mount' },
mounted: { end: 'mount' },
beforeUpdate: { start: 'update' },
updated: { end: 'update' },
beforeDestroyed: { start: 'destroy' },
destroyed: { end: 'destroy' },
}

export function initPerf (api: DevtoolsApi, app, Vue) {
// Global mixin
Vue.mixin({
beforeCreate () {
applyPerfHooks(api, this, app)
},
})

// Apply to existing components
instanceMap?.forEach(vm => applyPerfHooks(api, vm, app))
}

export function applyPerfHooks (api: DevtoolsApi, vm, app) {
if (vm.$options.$_devtoolsPerfHooks) return
vm.$options.$_devtoolsPerfHooks = true

for (const hook in COMPONENT_HOOKS) {
const { start, end } = COMPONENT_HOOKS[hook]
const handler = function (this: any) {
if (SharedData.performanceMonitoringEnabled) {
api.ctx.hook.emit(
start ? HookEvents.PERFORMANCE_START : HookEvents.PERFORMANCE_END,
app,
this._uid,
this,
start ?? end,
api.now(),
)
}
}
const currentValue = vm.$options[hook]
if (Array.isArray(currentValue)) {
vm.$options[hook] = [handler, ...currentValue]
} else if (typeof currentValue === 'function') {
vm.$options[hook] = [handler, currentValue]
} else {
vm.$options[hook] = [handler]
}
}
}
2 changes: 2 additions & 0 deletions packages/app-backend-vue2/src/components/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AppRecord, BackendContext, DevtoolsApi } from '@vue-devtools/app-backen
import { classify, kebabize } from '@vue-devtools/shared-utils'
import { ComponentTreeNode, ComponentInstance } from '@vue/devtools-api'
import { getRootElementsFromComponentInstance } from './el'
import { applyPerfHooks } from './perf.js'
import { getInstanceName, getRenderKey, getUniqueId, isBeingDestroyed } from './util'

export let instanceMap: Map<any, any>
Expand Down Expand Up @@ -324,6 +325,7 @@ function mark (instance) {
instance.$on('hook:beforeDestroy', function () {
instanceMap.delete(refId)
})
applyPerfHooks(api, instance, appRecord.options.app)
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/app-backend-vue2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { backendInjections, getComponentName } from '@vue-devtools/shared-utils'
import { ComponentInstance } from '@vue/devtools-api'
import { editState, getCustomInstanceDetails, getInstanceDetails } from './components/data'
import { getInstanceOrVnodeRect, findRelatedComponent, getRootElementsFromComponentInstance } from './components/el'
import { initPerf } from './components/perf.js'
import { getComponentParents, instanceMap, walkTree } from './components/tree'
import { getInstanceName } from './components/util'
import { wrapVueForEvents } from './events'
Expand Down Expand Up @@ -91,6 +92,9 @@ export const backend = defineBackend({

// Plugin
setupPlugin(api, app, Vue)

// Perf
initPerf(api, app, Vue)
},
})

Expand Down
12 changes: 6 additions & 6 deletions packages/app-backend-vue2/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function setupPlugin (api: DevtoolsApi, app: App, Vue) {
api.addTimelineEvent({
layerId: ROUTER_CHANGES_LAYER_ID,
event: {
time: Date.now(),
time: api.now(),
title: to.path,
data: {
from,
Expand Down Expand Up @@ -169,7 +169,7 @@ export function setupPlugin (api: DevtoolsApi, app: App, Vue) {
api.addTimelineEvent({
layerId: VUEX_MUTATIONS_ID,
event: {
time: Date.now(),
time: api.now(),
title: mutation.type,
data,
},
Expand All @@ -187,7 +187,7 @@ export function setupPlugin (api: DevtoolsApi, app: App, Vue) {
api.addTimelineEvent({
layerId: VUEX_ACTIONS_ID,
event: {
time: Date.now(),
time: api.now(),
title: action.type,
data,
},
Expand All @@ -203,7 +203,7 @@ export function setupPlugin (api: DevtoolsApi, app: App, Vue) {
data.payload = action.payload
}
action._id = actionId++
action._time = Date.now()
action._time = api.now()
data.state = state

api.addTimelineEvent({
Expand All @@ -219,7 +219,7 @@ export function setupPlugin (api: DevtoolsApi, app: App, Vue) {
},
after: (action, state) => {
const data: any = {}
const duration = Date.now() - action._time
const duration = api.now() - action._time
data.duration = {
_custom: {
type: 'duration',
Expand All @@ -236,7 +236,7 @@ export function setupPlugin (api: DevtoolsApi, app: App, Vue) {
api.addTimelineEvent({
layerId: VUEX_ACTIONS_ID,
event: {
time: Date.now(),
time: api.now(),
title: action.type,
groupId: action._id,
subtitle: 'end',
Expand Down

0 comments on commit dd47083

Please sign in to comment.