Chrome CLI的虚拟时间预算这一论点到底是什么意思?

人气:369 发布:2022-10-16 标签: google-chrome google-chrome-headless chromium

问题描述

我知道Chromium的参数--virtual-time-budgetin the source的文档,但我觉得我不理解它:

// 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'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

我以为在上面的四个例子中,Chrome会等0、10、100和1000秒才能打印成PDF,但实际的等待时间似乎很远。我的问题是,如何让Chrome在将页面打印成PDF之前确切地等待X秒?我目前只是在考虑Chrome CLI,我不会寻找像Puppeteer这样的工具。

推荐答案

我可以轻松回答您的标题问题(这解释了您的结果)。--虚拟时间预算,说明进程将等待页面加载的时间,而不是它将等待的时间。如果请求的结果可用(没有更多的网络请求挂起),它将立即返回结果。

返回的信息应该是正确的,除非混合中有AJAX请求或其他Java脚本。如果是这样的话,您必须求助于Java脚本/DOM操作来解决这个问题。

529