Skip to content

Commit 2076445

Browse files
authored
feat: collect perf results from outside oclif (#797)
* feat: collect perf results from outside oclif * chore: pr feedback * refactor: naming for non-core perf
1 parent c696a24 commit 2076445

7 files changed

Lines changed: 146 additions & 134 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"chai-as-promised": "^7.1.1",
6060
"commitlint": "^17.7.2",
6161
"cross-env": "^7.0.3",
62-
"eslint": "^8.49.0",
62+
"eslint": "^8.50.0",
6363
"eslint-config-oclif": "^5.0.0",
6464
"eslint-config-oclif-typescript": "^2.0.1",
6565
"eslint-config-prettier": "^9.0.0",

src/config/config.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import {CLIError, error, exit, warn} from '../errors'
44
import {Debug, collectUsableIds, getCommandIdPermutations} from './util'
55
import {Hook, Hooks, PJSON, Topic} from '../interfaces'
66
import {Plugin as IPlugin, Options} from '../interfaces/plugin'
7+
import { OCLIF_MARKER_OWNER, Performance } from '../performance'
78
import {URL, fileURLToPath} from 'node:url'
89
import {arch, userInfo as osUserInfo, release, tmpdir, type} from 'node:os'
910
import {compact, isProd} from '../util/util'
1011
import {getHomeDir, getPlatform} from '../util/os'
11-
import {join, sep} from 'node:path'
12+
import { join, sep } from 'node:path'
1213
import {Command} from '../command'
13-
import {Performance} from '../performance'
1414
import PluginLoader from './plugin-loader'
1515
import WSL from 'is-wsl'
1616
import {format} from 'node:util'
@@ -153,9 +153,8 @@ export class Config implements IConfig {
153153

154154
// eslint-disable-next-line complexity
155155
public async load(): Promise<void> {
156-
settings.performanceEnabled =
157-
(settings.performanceEnabled === undefined ? this.options.enablePerf : settings.performanceEnabled) ?? false
158-
const marker = Performance.mark('config.load')
156+
settings.performanceEnabled = (settings.performanceEnabled === undefined ? this.options.enablePerf : settings.performanceEnabled) ?? false
157+
const marker = Performance.mark(OCLIF_MARKER_OWNER, 'config.load')
159158
this.pluginLoader = new PluginLoader({root: this.options.root, plugins: this.options.plugins})
160159
Config._rootPlugin = await this.pluginLoader.loadRoot()
161160

@@ -235,7 +234,7 @@ export class Config implements IConfig {
235234
}
236235

237236
async loadPluginsAndCommands(opts?: {force: boolean}): Promise<void> {
238-
const pluginsMarker = Performance.mark('config.loadAllPlugins')
237+
const pluginsMarker = Performance.mark(OCLIF_MARKER_OWNER, 'config.loadAllPlugins')
239238
const {plugins, errors} = await this.pluginLoader.loadChildren({
240239
devPlugins: this.options.devPlugins,
241240
userPlugins: this.options.userPlugins,
@@ -247,7 +246,7 @@ export class Config implements IConfig {
247246
this.plugins = plugins
248247
pluginsMarker?.stop()
249248

250-
const commandsMarker = Performance.mark('config.loadAllCommands')
249+
const commandsMarker = Performance.mark(OCLIF_MARKER_OWNER, 'config.loadAllCommands')
251250
for (const plugin of this.plugins.values()) {
252251
this.loadCommands(plugin)
253252
this.loadTopics(plugin)
@@ -266,7 +265,7 @@ export class Config implements IConfig {
266265
timeout?: number,
267266
captureErrors?: boolean,
268267
): Promise<Hook.Result<Hooks[T]['return']>> {
269-
const marker = Performance.mark(`config.runHook#${event}`)
268+
const marker = Performance.mark(OCLIF_MARKER_OWNER, `config.runHook#${event}`)
270269
debug('start %s hook', event)
271270
const search = (m: any): Hook<T> => {
272271
if (typeof m === 'function') return m
@@ -314,7 +313,7 @@ export class Config implements IConfig {
314313
const hooks = p.hooks[event] || []
315314

316315
for (const hook of hooks) {
317-
const marker = Performance.mark(`config.runHook#${p.name}(${hook})`)
316+
const marker = Performance.mark(OCLIF_MARKER_OWNER, `config.runHook#${p.name}(${hook})`)
318317
try {
319318
/* eslint-disable no-await-in-loop */
320319
const {isESM, module, filePath} = await loadWithData(p, join(p.root, hook))
@@ -358,7 +357,7 @@ export class Config implements IConfig {
358357
argv: string[] = [],
359358
cachedCommand: Command.Loadable | null = null,
360359
): Promise<T> {
361-
const marker = Performance.mark(`config.runCommand#${id}`)
360+
const marker = Performance.mark(OCLIF_MARKER_OWNER, `config.runCommand#${id}`)
362361
debug('runCommand %s %o', id, argv)
363362
let c = cachedCommand ?? this.findCommand(id)
364363
if (!c) {
@@ -681,7 +680,7 @@ export class Config implements IConfig {
681680
}
682681

683682
private loadCommands(plugin: IPlugin) {
684-
const marker = Performance.mark(`config.loadCommands#${plugin.name}`, {plugin: plugin.name})
683+
const marker = Performance.mark(OCLIF_MARKER_OWNER, `config.loadCommands#${plugin.name}`, {plugin: plugin.name})
685684
for (const command of plugin.commands) {
686685
// set canonical command id
687686
if (this._commands.has(command.id)) {
@@ -731,7 +730,7 @@ export class Config implements IConfig {
731730
}
732731

733732
private loadTopics(plugin: IPlugin) {
734-
const marker = Performance.mark(`config.loadTopics#${plugin.name}`, {plugin: plugin.name})
733+
const marker = Performance.mark(OCLIF_MARKER_OWNER, `config.loadTopics#${plugin.name}`, {plugin: plugin.name})
735734
for (const topic of compact(plugin.topics)) {
736735
const existing = this._topics.get(topic.name)
737736
if (existing) {

src/config/plugin-loader.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as Plugin from './plugin'
22
import {Plugin as IPlugin, Options} from '../interfaces/plugin'
3+
import {OCLIF_MARKER_OWNER, Performance} from '../performance'
34
import {Debug} from './util'
45
import {PJSON} from '../interfaces'
5-
import {Performance} from '../performance'
66
import {isProd} from '../util/util'
77
import {join} from 'node:path'
88
import {readJson} from '../util/fs'
@@ -43,7 +43,7 @@ export default class PluginLoader {
4343
const plugins = [...this.plugins.values()]
4444
rootPlugin = plugins.find((p) => p.root === this.options.root) ?? plugins[0]
4545
} else {
46-
const marker = Performance.mark('plugin.load#root')
46+
const marker = Performance.mark(OCLIF_MARKER_OWNER, 'plugin.load#root')
4747
rootPlugin = new Plugin.Plugin({root: this.options.root, isRoot: true})
4848
await rootPlugin.load()
4949
marker?.addDetails({
@@ -121,7 +121,7 @@ export default class PluginLoader {
121121
parent?: Plugin.Plugin,
122122
): Promise<void> {
123123
if (!plugins || plugins.length === 0) return
124-
const mark = Performance.mark(`config.loadPlugins#${type}`)
124+
const mark = Performance.mark(OCLIF_MARKER_OWNER, `config.loadPlugins#${type}`)
125125
debug('loading plugins', plugins)
126126
await Promise.all(
127127
(plugins || []).map(async (plugin) => {
@@ -142,7 +142,7 @@ export default class PluginLoader {
142142
}
143143

144144
if (this.plugins.has(name)) return
145-
const pluginMarker = Performance.mark(`plugin.load#${name}`)
145+
const pluginMarker = Performance.mark(OCLIF_MARKER_OWNER, `plugin.load#${name}`)
146146
const instance = new Plugin.Plugin(opts)
147147
await instance.load()
148148
pluginMarker?.addDetails({

src/config/plugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import {CLIError, error} from '../errors'
22
import {Debug, getCommandIdPermutations, resolvePackage} from './util'
33
import {Plugin as IPlugin, PluginOptions} from '../interfaces/plugin'
4+
import {OCLIF_MARKER_OWNER, Performance} from '../performance'
45
import {compact, isProd, mapValues} from '../util/util'
56
import {dirname, join, parse, relative, sep} from 'node:path'
67
import {exists, readJson, requireJson} from '../util/fs'
78
import {loadWithData, loadWithDataFromManifest} from '../module-loader'
89
import {Command} from '../command'
910
import {Manifest} from '../interfaces/manifest'
1011
import {PJSON} from '../interfaces/pjson'
11-
import {Performance} from '../performance'
1212
import {Topic} from '../interfaces/topic'
1313
import {cacheCommand} from '../util/cache-command'
1414
import {inspect} from 'node:util'
@@ -206,7 +206,7 @@ export class Plugin implements IPlugin {
206206
public get commandIDs(): string[] {
207207
if (!this.commandsDir) return []
208208

209-
const marker = Performance.mark(`plugin.commandIDs#${this.name}`, {plugin: this.name})
209+
const marker = Performance.mark(OCLIF_MARKER_OWNER, `plugin.commandIDs#${this.name}`, {plugin: this.name})
210210
this._debug(`loading IDs from ${this.commandsDir}`)
211211
const patterns = ['**/*.+(js|cjs|mjs|ts|tsx)', '!**/*.+(d.ts|test.ts|test.js|spec.ts|spec.js)?(x)']
212212
const ids = sync(patterns, {cwd: this.commandsDir}).map((file) => {
@@ -227,7 +227,7 @@ export class Plugin implements IPlugin {
227227
public async findCommand(id: string, opts?: {must: boolean}): Promise<Command.Class | undefined>
228228

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

232232
const fetch = async () => {
233233
if (!this.commandsDir) return
@@ -286,7 +286,7 @@ export class Plugin implements IPlugin {
286286
}
287287
}
288288

289-
const marker = Performance.mark(`plugin.manifest#${this.name}`, {plugin: this.name})
289+
const marker = Performance.mark(OCLIF_MARKER_OWNER, `plugin.manifest#${this.name}`, {plugin: this.name})
290290
if (!ignoreManifest) {
291291
const manifest = await readManifest()
292292
if (manifest) {

src/main.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as Interfaces from './interfaces'
2+
import {OCLIF_MARKER_OWNER, Performance} from './performance'
23
import {URL, fileURLToPath} from 'node:url'
34
import {format, inspect} from 'node:util'
45
import {getHelpFlagAdditions, loadHelpClass, normalizeArgv} from './help'
5-
66
import {Config} from './config'
7-
import {Performance} from './performance'
87
import {stdout} from './cli-ux/stream'
98

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

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

38-
const initMarker = Performance.mark('main.run#init')
37+
const initMarker = Performance.mark(OCLIF_MARKER_OWNER, 'main.run#init')
3938

4039
const collectPerf = async () => {
4140
marker?.stop()

0 commit comments

Comments
 (0)