Skip to content

Commit

Permalink
feat: collect perf results from outside oclif
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Sep 27, 2023
1 parent 6702a53 commit 3a1c6e7
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 99 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"chai-as-promised": "^7.1.1",
"commitlint": "^12.1.4",
"cross-env": "^7.0.3",
"eslint": "^8.49.0",
"eslint": "^8.50.0",
"eslint-config-oclif": "^5.0.0",
"eslint-config-oclif-typescript": "^2.0.1",
"fancy-test": "^3.0.0-beta.2",
Expand All @@ -69,8 +69,8 @@
"nock": "^13.3.0",
"shx": "^0.3.4",
"sinon": "^11.1.2",
"tsd": "^0.29.0",
"ts-node": "^10.9.1",
"tsd": "^0.29.0",
"tslib": "^2.5.0",
"typescript": "^5"
},
Expand Down
20 changes: 10 additions & 10 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {URL, fileURLToPath} from 'node:url'
import {arch, userInfo as osUserInfo, release, tmpdir, type} from 'node:os'
import {compact, ensureArgObject, getHomeDir, getPlatform, isProd, requireJson} from '../util'
import {join, sep} from 'node:path'

// eslint-disable-next-line sort-imports
import {OCLIF_MARKER_OWNER, Performance} from '../performance'
import {Command} from '../command'
import {Performance} from '../performance'
import PluginLoader from './plugin-loader'
import {format} from 'node:util'
import {getHelpFlagAdditions} from '../help'
Expand Down Expand Up @@ -155,7 +155,7 @@ export class Config implements IConfig {
// eslint-disable-next-line complexity
public async load(): Promise<void> {
settings.performanceEnabled = (settings.performanceEnabled === undefined ? this.options.enablePerf : settings.performanceEnabled) ?? false
const marker = Performance.mark('config.load')
const marker = Performance.mark(OCLIF_MARKER_OWNER, 'config.load')
this.pluginLoader = new PluginLoader({root: this.options.root, plugins: this.options.plugins})
Config._rootPlugin = await this.pluginLoader.loadRoot()

Expand Down Expand Up @@ -231,7 +231,7 @@ export class Config implements IConfig {
}

async loadPluginsAndCommands(opts?: {force: boolean}): Promise<void> {
const pluginsMarker = Performance.mark('config.loadAllPlugins')
const pluginsMarker = Performance.mark(OCLIF_MARKER_OWNER, 'config.loadAllPlugins')
const {plugins, errors} = await this.pluginLoader.loadChildren({
devPlugins: this.options.devPlugins,
userPlugins: this.options.userPlugins,
Expand All @@ -243,7 +243,7 @@ export class Config implements IConfig {
this.plugins = plugins
pluginsMarker?.stop()

const commandsMarker = Performance.mark('config.loadAllCommands')
const commandsMarker = Performance.mark(OCLIF_MARKER_OWNER, 'config.loadAllCommands')
for (const plugin of this.plugins.values()) {
this.loadCommands(plugin)
this.loadTopics(plugin)
Expand All @@ -262,7 +262,7 @@ export class Config implements IConfig {
timeout?: number,
captureErrors?: boolean,
): Promise<Hook.Result<Hooks[T]['return']>> {
const marker = Performance.mark(`config.runHook#${event}`)
const marker = Performance.mark(OCLIF_MARKER_OWNER, `config.runHook#${event}`)
debug('start %s hook', event)
const search = (m: any): Hook<T> => {
if (typeof m === 'function') return m
Expand Down Expand Up @@ -310,7 +310,7 @@ export class Config implements IConfig {
const hooks = p.hooks[event] || []

for (const hook of hooks) {
const marker = Performance.mark(`config.runHook#${p.name}(${hook})`)
const marker = Performance.mark(OCLIF_MARKER_OWNER, `config.runHook#${p.name}(${hook})`)
try {
/* eslint-disable no-await-in-loop */
const {isESM, module, filePath} = await loadWithData(p, join(p.root, hook))
Expand Down Expand Up @@ -350,7 +350,7 @@ export class Config implements IConfig {
}

public async runCommand<T = unknown>(id: string, argv: string[] = [], cachedCommand: Command.Loadable | null = null): Promise<T> {
const marker = Performance.mark(`config.runCommand#${id}`)
const marker = Performance.mark(OCLIF_MARKER_OWNER, `config.runCommand#${id}`)
debug('runCommand %s %o', id, argv)
let c = cachedCommand ?? this.findCommand(id)
if (!c) {
Expand Down Expand Up @@ -657,7 +657,7 @@ export class Config implements IConfig {
}

private loadCommands(plugin: IPlugin) {
const marker = Performance.mark(`config.loadCommands#${plugin.name}`, {plugin: plugin.name})
const marker = Performance.mark(OCLIF_MARKER_OWNER, `config.loadCommands#${plugin.name}`, {plugin: plugin.name})
for (const command of plugin.commands) {
// set canonical command id
if (this._commands.has(command.id)) {
Expand Down Expand Up @@ -705,7 +705,7 @@ export class Config implements IConfig {
}

private loadTopics(plugin: IPlugin) {
const marker = Performance.mark(`config.loadTopics#${plugin.name}`, {plugin: plugin.name})
const marker = Performance.mark(OCLIF_MARKER_OWNER, `config.loadTopics#${plugin.name}`, {plugin: plugin.name})
for (const topic of compact(plugin.topics)) {
const existing = this._topics.get(topic.name)
if (existing) {
Expand Down
9 changes: 5 additions & 4 deletions src/config/plugin-loader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as Plugin from './plugin'
import {Plugin as IPlugin, Options} from '../interfaces/plugin'
import {isProd, readJson} from '../util'
// eslint-disable-next-line sort-imports
import {OCLIF_MARKER_OWNER, Performance} from '../performance'
import {Debug} from './util'
import {PJSON} from '../interfaces'
import {Performance} from '../performance'
import {join} from 'node:path'

// eslint-disable-next-line new-cap
Expand Down Expand Up @@ -42,7 +43,7 @@ export default class PluginLoader {
const plugins = [...this.plugins.values()]
rootPlugin = plugins.find(p => p.root === this.options.root) ?? plugins[0]
} else {
const marker = Performance.mark('plugin.load#root')
const marker = Performance.mark(OCLIF_MARKER_OWNER, 'plugin.load#root')
rootPlugin = new Plugin.Plugin({root: this.options.root})
await rootPlugin.load()
marker?.addDetails({
Expand Down Expand Up @@ -107,7 +108,7 @@ export default class PluginLoader {

private async loadPlugins(root: string, type: string, plugins: (string | { root?: string; name?: string; tag?: string })[], parent?: Plugin.Plugin): Promise<void> {
if (!plugins || plugins.length === 0) return
const mark = Performance.mark(`config.loadPlugins#${type}`)
const mark = Performance.mark(OCLIF_MARKER_OWNER, `config.loadPlugins#${type}`)
debug('loading plugins', plugins)
await Promise.all((plugins || []).map(async plugin => {
try {
Expand All @@ -127,7 +128,7 @@ export default class PluginLoader {
}

if (this.plugins.has(name)) return
const pluginMarker = Performance.mark(`plugin.load#${name}`)
const pluginMarker = Performance.mark(OCLIF_MARKER_OWNER, `plugin.load#${name}`)
const instance = new Plugin.Plugin(opts)
await instance.load()
pluginMarker?.addDetails({
Expand Down
9 changes: 5 additions & 4 deletions src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {Plugin as IPlugin, PluginOptions} from '../interfaces/plugin'
import {compact, exists, isProd, readJson, requireJson} from '../util'
import {dirname, join, parse, relative, sep} from 'node:path'
import {loadWithData, loadWithDataFromManifest} from '../module-loader'
// eslint-disable-next-line sort-imports
import {OCLIF_MARKER_OWNER, Performance} from '../performance'
import {Command} from '../command'
import {Manifest} from '../interfaces/manifest'
import {PJSON} from '../interfaces/pjson'
import {Performance} from '../performance'
import {Topic} from '../interfaces/topic'
import {inspect} from 'node:util'
import {sync} from 'globby'
Expand Down Expand Up @@ -208,7 +209,7 @@ export class Plugin implements IPlugin {
public get commandIDs(): string[] {
if (!this.commandsDir) return []

const marker = Performance.mark(`plugin.commandIDs#${this.name}`, {plugin: this.name})
const marker = Performance.mark(OCLIF_MARKER_OWNER, `plugin.commandIDs#${this.name}`, {plugin: this.name})
this._debug(`loading IDs from ${this.commandsDir}`)
const patterns = [
'**/*.+(js|cjs|mjs|ts|tsx)',
Expand All @@ -233,7 +234,7 @@ export class Plugin implements IPlugin {
public async findCommand(id: string, opts?: {must: boolean}): Promise<Command.Class | undefined>

public async findCommand(id: string, opts: {must?: boolean} = {}): Promise<Command.Class | undefined> {
const marker = Performance.mark(`plugin.findCommand#${this.name}.${id}`, {id, plugin: this.name})
const marker = Performance.mark(OCLIF_MARKER_OWNER, `plugin.findCommand#${this.name}.${id}`, {id, plugin: this.name})

const fetch = async () => {
if (!this.commandsDir) return
Expand Down Expand Up @@ -290,7 +291,7 @@ export class Plugin implements IPlugin {
}
}

const marker = Performance.mark(`plugin.manifest#${this.name}`, {plugin: this.name})
const marker = Performance.mark(OCLIF_MARKER_OWNER, `plugin.manifest#${this.name}`, {plugin: this.name})
if (!ignoreManifest) {
const manifest = await readManifest()
if (manifest) {
Expand Down
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as Interfaces from './interfaces'
import {URL, fileURLToPath} from 'node:url'
import {format, inspect} from 'node:util'
import {getHelpFlagAdditions, loadHelpClass, normalizeArgv} from './help'

// eslint-disable-next-line sort-imports
import {OCLIF_MARKER_OWNER, Performance} from './performance'
import {Config} from './config'
import {Performance} from './performance'
import {stdout} from './cli-ux/stream'

const debug = require('debug')('oclif:main')
Expand Down Expand Up @@ -33,9 +33,9 @@ export const versionAddition = (argv: string[], config?: Interfaces.Config): boo
}

export async function run(argv?: string[], options?: Interfaces.LoadOptions): Promise<unknown> {
const marker = Performance.mark('main.run')
const marker = Performance.mark(OCLIF_MARKER_OWNER, 'main.run')

const initMarker = Performance.mark('main.run#init')
const initMarker = Performance.mark(OCLIF_MARKER_OWNER, 'main.run#init')

const collectPerf = async () => {
marker?.stop()
Expand Down
Loading

0 comments on commit 3a1c6e7

Please sign in to comment.