google-chromechromiumgoogle-chrome-headless

What does the argument --virtual-time-budget of Chrome CLI really mean?


I'm aware of the documentation of the argument --virtual-time-budget in the source of Chromium, but I don't feel I understand it:

// If set the system waits the specified number of virtual milliseconds before
// deeming the page to be ready.  For determinism virtual time does not advance
// while there are pending network fetches (i.e no timers will fire). Once all
// network fetches have completed, timers fire and if the system runs out of
// virtual time is fastforwarded so the next timer fires immediately, until the
// specified virtual time budget is exhausted.
const char kVirtualTimeBudget[] = "virtual-time-budget";

I did some experiments, and the results were confusing to me:

# I'm on macOS; you may change this alias according to your own OS
$ alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
$ chrome --version
Google Chrome 70.0.3538.110

$ time chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/
real    0m0.912s
user    0m0.264s
sys     0m0.219s

$ time chrome --headless --disable-gpu --print-to-pdf --virtual-time-budget=10000 https://www.chromestatus.com/
real    0m2.502s
user    0m0.347s
sys     0m0.244s

$ time chrome --headless --disable-gpu --print-to-pdf --virtual-time-budget=100000 https://www.chromestatus.com/
real    0m15.432s
user    0m0.759s
sys     0m0.406s

$ time chrome --headless --disable-gpu --print-to-pdf --virtual-time-budget=1000000 https://www.chromestatus.com/
real    0m15.755s
user    0m0.755s
sys     0m0.401s

I thought Chrome would wait for 0, 10, 100, and 1000 seconds in the above four examples before printing to PDF, but the actual waiting time seemed to be far off. My question is, how to make Chrome wait definitely for X seconds before printing a page to PDF? I'm only considering the Chrome CLI at the moment, and I'm not looking for tools like Puppeteer.


Solution

  • I can answer your title question easily(which explains your results). --virtual-time-budget, states how long the process will wait for a page to load, not that it will wait that long. If the result of the request is available(no more network requests are pending), it will return the results immediately.

    The information returned should be correct, unless there is an AJAX request or other Javascript in the mix. If so, you must resort to Javascript/DOM manipulation to resolve the issue.