Uh oh!
There was an error while loading. Please reload this page.
perf: memoize the browser's CDP connection - #61
Open
hugo-fasone wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
On any suite that calls
importPage(), runtime is dominated by redundant CDPreconnections rather than by actual browser work. Measured on a project with 2 suites /
5 tests (PHP 8.3, headless Chromium in a container, PS 9.1 target): 23.67 s, of
which roughly 15 s was pure bootstrap.
Launching Chromium accounts for only 208 ms — the cost is elsewhere.
Root cause
TestsSuite::getBrowser()re-reads the socket file and issues a freshBrowserFactory::connectToBrowser()on every call, never reusing the instance. Twoeffects compound:
connectToBrowser()getPages()TestsSuite::getBrowser()TestsSuite::getPage()getBrowser())The multiplier lives in
CommonPage::__call(), which evaluatesgetPage()threetimes — condition,
method_exists, invocation:And
importPage()calls$pageInstance->setUserAgent(...), which does not exist on thelibrary's
Pageclasses and therefore goes through that__call. Full chain:A suite importing 3 pages thus paid ~11 s before running its first test.
Changes
1.
TestsSuite::getBrowser()— reuse the connected instanceTwo statics (
$browserInstance,$browserInstanceSocket) plus a guard at the top of themethod. Invalidation has three conditions, so a dead browser is never handed back:
@unlinkbyExecuteSuite);getConnection()->isConnected()is false (browser closed or crashed).isConnected()performs no CDP round-trip — it only reads the local socket state inwrench\Client— so the guard stays free.ExecuteSuite::getBrowser(force: false)?->close()still retrieves the live instance to close it at the end of the run.
2.
TestsSuite::getPage()— one enumeration per attemptgetBrowser()is hoisted into a variable, andgetPages()is only re-issued after anactual
createPage()(the first list is otherwise already current). The ×3 retry loop andthe
applyExtraHttpHeaders()semantics are unchanged.3.
CommonPage::__call()— evaluategetPage()onceResults
getBrowser()(already connected)getPage()init()= 1 ×importPage()Compatibility
No API change and no observable behaviour change:
__call()still returns nothing — I deliberately did not add areturnoncall_user_func_array, since that would change the return value of every proxied calland is out of scope for a performance fix;
getPage()keeps itsreturn $pages[0](same semantics on an empty list) and its retryloop;
is the more honest behaviour: the previous silent relaunch dropped the session and
cookies, producing failures that were hard to interpret.
Testing
vendor/bin/phpunit --testsuite Unit: OK (117 tests, 442 assertions) on PHP 8.3.importPage()path (3 pages, with BO credentials) exercised separately: importssucceed and the resulting failure is the login assertion, as expected.