-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix long execution times caused by isAdRelated #330
Conversation
Thanks for running the workflows! I'll review and attempt to fix the issues with the Smoke Tests. I can reproduce it locally so looks like it caught a legit problem. It seems that the Lighthouse CI tests tend to fail, but I'll still look into it and see if I can resolve it. |
I've executed the smoke tests locally with and without the fix and I get the same failures. Seems that something else may have previously introduced a regression in these tests. I've not had time to take a closer look, but should be able to later today. |
After spending some time with this, I'm seeing the following:
|
Thanks for the contribution Zack! Really appreciate the time you spent putting together such a thorough analysis and finding such a simple fix :) This is really quite an impressive delta in time! It does appear that the failures are not due to your test, I'll update the test assertions later today to unblock this. We definitely want to credit you in the release notes for this valuable fix, please confirm that that would be ok. |
Excellent! We are excited to get this running in our test suite.
Definitely ok! Thanks! |
Updated the tests in #333, I went ahead and merged the changes in here. |
Thank you for moving this along so quickly @jburger424! |
@jburger424 thanks for merging this PR. We are looking to integrate the publisher ads plugin into our lighthouse workflow and would like for it to include these performance improvements as it is a non-trivial impact on the execution time without it. When do you anticipate a new version will be published to npm that includes this change? Thank you! |
Problem
Running Lighthouse tests with the
publisher-ads-lighthouse-plugin
increases runtime significantly.Via the CLI on an M1 MacBook Pro, the following increase in runtime was observed for the following sites
There is a relationship between the number of resources loaded on a page and the increase in execution time.
This issue was initially detected when adding the plugin to a suite of tests evaluating ~160 different domains. Average Lighthouse run time increased from 20s to 34s.
Reproduction Steps
To reproduce:
Run
Note the
8s
time attributed (incorrectly) toAuditing: Deprecated GPT API Usage
Solution
Through profiling and manually removing audits, the audit causing this issue is "Cumulative ad shift", not "Deprecated GPT API Usage" as the output suggests. To demonstrate this, comment out the following two lines in
plugin.js
and run the reproduction steps above:The performance issue will disappear, implicating the "Cumulative ad shift" audit as the source of the problem.
Generating a flame chart using
clinic
will clearly show the issue:The function taking a lot of time is
onParseError
from Node. This function is called whentoURL
receives anundefined
value. In thetry
statement, the value is passed tonew URL()
and throws. Apparently, parsing and throwing this error is expensive.In my testing, I console logged
url
intoURL
and found that it isundefined
up to ~1 million times when testing a site that loads a lot (e.g., 500) of resources.The function's comment states:
As such, my fix in this PR is to validate the URL before it ever gets to
toURL
inisAdRelated
.isAdRelated
seems to be the root of the majority of thetoURL
calls. As such, this PR changes the function to returnfalse
whenever it receives falseyurl
value. If it's falsey, there is no way this is ad related so it makes sense that all of the other logic in this method can be skipped.The fix in this PR reduces the runtime dramatically (8s to 104ms):
Note that no tests were previously written for
isAdRelated
. This PR adds those tests and attempts to evaluate each branch. The tests pass before and after applying the patch, except for the tests of the undefined url that are handled with the patch.