Skip to content

Commit

Permalink
feat(job-runner-lab): lab console logger
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Oct 17, 2022
1 parent d9acf02 commit 1b1d13d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
Copyright 2022 ByteDance and/or its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { truncate } from 'lodash'
import { Protocol } from 'puppeteer-core'

import { logger } from '@perfsee/job-runner-shared'

import { Driver } from '../../helpers'

export class ConsoleLogger implements LH.PerfseeGathererInstance {
name = 'ConsoleLogger' as const
private messageHandler?: (...params: any) => any

constructor() {}

async beforePass(ctx: LH.Gatherer.PassContext) {
const driver = ctx.driver as Driver

const onMessage = (event: Protocol.Log.EntryAddedEvent) => {
logger.verbose(
`From page [${event.entry.source}:${event.entry.level}:${event.entry.url ?? ''}:${
event.entry.lineNumber ?? ''
}]: ${truncate(event.entry.text, { length: 200 })}`,
)
}

this.messageHandler = onMessage

driver.on('Log.entryAdded', onMessage)
await driver.sendCommand('Log.enable')
}

pass() {}

async afterPass(ctx: LH.Gatherer.PassContext) {
await ctx.driver.sendCommand('Log.disable')
await ctx.driver.off('Log.entryAdded', this.messageHandler)
this.messageHandler = undefined
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ limitations under the License.

export * from './screencast'
export * from './request-interception'
export * from './console-logger'
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import run from 'lighthouse'
import defaultConfig from 'lighthouse/lighthouse-core/config/default-config'

import { NetworkRequests, WhiteScreen } from './audits'
import { RequestInterception, Screencast } from './gatherers'
import { ConsoleLogger, RequestInterception, Screencast } from './gatherers'

type KeyAuditName = 'first-contentful-paint' | 'interactive'

Expand Down Expand Up @@ -98,7 +98,7 @@ export async function lighthouse(url?: string, { customFlags, ...flags }: LH.Fla
passes: defaultConfig.passes.map((pass: LH.PerfseePassJson) => {
pass = {
...pass,
gatherers: [new RequestInterception(customFlags?.headers), ...(pass.gatherers ?? [])],
gatherers: [new RequestInterception(customFlags?.headers), new ConsoleLogger(), ...(pass.gatherers ?? [])],
}

if (pass.passName === 'defaultPass') {
Expand Down
1 change: 1 addition & 0 deletions types/lighthouse/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module LH {
RequestInterception: null
Screencast: ScreencastGathererResult | null
CpuProfiler: Crdp.Profiler.Profile
ConsoleLogger: null
}

export interface PerfseeGathererInstance extends Gatherer.GathererInstance {
Expand Down

0 comments on commit 1b1d13d

Please sign in to comment.