lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

core: timing/logging, performance. add a time threshold (user-specified?) and only log processes which duration exceed this

Open upugo-dev opened this issue 7 years ago • 9 comments

Feature request summary

Add a log level or some flag/option to the logging that only logs audits/gatherers with a duration exceeding some specified threshold.

What is the motivation or use case for changing this? Of course it is possible to access the timing object AFTER the lighthouse program has completed sucesfully and filter the entries however you like, BUT this relies on the program completing sucesfully.

If there is some audit/gatherer that takes a long time, the program may exit prematurely (either by an impatient user, or by some programtic safeguard- we have a timer built into our wrapper function that kills lighthouse if it takes over X seconds to run)

In this case, the LHR does not exist because the program was exited, so the timings object cannot be used to identify which particular process was taking a while.

Turning on the logging at info-level would give you information and timings irrespective of the existence of an LHR, but for every process; if you have many many audits and gatherers running, its a lot to trawl through and find the poorly performing ones.

How is this beneficial to Lighthouse? Adding more control, some kind of filtering capability, more fine-tuned logging levels to the lighthouse logging capabilites, would be very useful in these cases, especially in assisting developers in writing new audits.

upugo-dev avatar Mar 28 '19 12:03 upugo-dev

Some good points @upugo-dev! As a workaround in the interim you could probably require in lighthouse-logger and call takeEntries yourself if you're doing this in node.

https://github.com/GoogleChrome/lighthouse/blob/21dda6a1906884043927ec92fbcac7678242e87e/lighthouse-logger/index.js#L234-L239

patrickhulce avatar Mar 28 '19 14:03 patrickhulce

Hey Patrick, thanks for the reply once again. Oh wow, that works perfectly thanks. I had reached the same solution but was trying with the getTimeEntries() method which returns a blank array.... should have tried the other method!

So if I am killing the lighthouse process running in a puppeteer browser with a browser.close() (a pretty hard hammer to hit it with, I know), what will happen to the timing entry of the audit that is currently running?

upugo-dev avatar Mar 28 '19 15:03 upugo-dev

what will happen to the timing entry of the audit that is currently running?

I haven't tried it but my guess is we'll never call it's timeEnd and so it won't be in there, but that's also pretty much the case you would get with the proposal right now. There's not really a way to avoid that unless you listen to enough log messages to detect a new audit starting rather than finishing.

patrickhulce avatar Mar 28 '19 16:03 patrickhulce

Interesting, thanks for your insight. I'm starting to think that I might need to build in a custom audit timeout function. e.g. kill/skip any audit after a specidief threshold duration. Would there be any way to access an array of "audits completed" and "audits remaining" in the "lighthouse killed prematurely" case?

upugo-dev avatar Mar 28 '19 18:03 upugo-dev

Not really, the logs are probably the only way right now. We've discussed adding the ability to cancel/timeout gatherers, but it's a rather large undertaking that we're not prioritizing at the moment.

patrickhulce avatar Mar 28 '19 19:03 patrickhulce

Sure, I can understand that. Would be a super helpful feature, but I suppose in quite a niche/edge case context.

For now, I guess I could compare timing entries against my config.json 'audits' list to determine how many were completed/remaining. The remaining problem is capturing the 'in progress' audit.

Would there be a way to turn on 'info' level logging, but pipe it to somewhere else (not console) so that my program runs nice and quietly, but I am still able to use the logs to capture the 'in progress' audit at the time of premature exit?

upugo-dev avatar Mar 29 '19 11:03 upugo-dev

You can run the Lighthouse CLI in a child process, listen on stderr, and just print out what you actually want printed in your program. This is probably the nicer way of easily killing Lighthouse too if it exceeds some time limit. It should be fairly straightforward to keep your puppeteer code as it is and just substitute the lighthouse bit.

example I came up with just now that is probably buggy

const {spawn} = require('child_process')

const lhInChildProcess = spawn('/path/to/lighthouse', [url, `--port=${chromePort}`, ...])

lhInChildProcess.stderr.on('data', chunk => {
  const message = chunk.toString()
  // parse the message and do what you need to with it
})

patrickhulce avatar Mar 29 '19 14:03 patrickhulce

Ah thanks patrick, need to run the lighthouse node module though. I managed a workaround with intercept-stdout package, supressing logs that match 'status' but stashing them in a running array, then logging the last element in the array at a timeout event. Pretty hacky, but this is hopefully a tool that I can remove once we have fixed our poorly-performing audits.

upugo-dev avatar Mar 29 '19 19:03 upugo-dev

Closing as everything seems good-ish here :)

Can comment here or open a new issue if there's more we can do to help make this easier.

brendankenny avatar Jun 08 '19 00:06 brendankenny