hook_js <- paste(readLines(file.path(wd, "hook.js")), collapse = "\n")
chrome <- chromote::Chromote$new()
# serve the pages over HTTP from localhost
port <- httpuv::randomPort()
server <- callr::r_bg(function(dir, port) httpuv::runStaticServer(dir, port = port, browse = FALSE),
list(pages_dir, port))
for (i in 1:50) {
if (!inherits(try(suppressWarnings(readLines(sprintf("http://127.0.0.1:%d/", port), n = 1)),
silent = TRUE), "try-error")) break
Sys.sleep(0.1)
}
# samples the largest renderer process's memory (RSS) every 20 ms until killed
sampler <- function(browser_pid, out) {
peak <- 0
writeLines("0", out) # started
repeat {
procs <- try(ps::ps_children(ps::ps_handle(browser_pid), recursive = TRUE), silent = TRUE)
if (!inherits(procs, "try-error")) {
for (p in procs) {
rss <- try({
if (any(grepl("--type=renderer", ps::ps_cmdline(p), fixed = TRUE))) ps::ps_memory_info(p)[["rss"]] else 0
}, silent = TRUE)
if (is.numeric(rss) && rss > peak) {
peak <- rss
writeLines(format(peak, scientific = FALSE), out)
}
}
}
Sys.sleep(0.02)
}
}
browser_pid <- chrome$get_browser()$get_process()$get_pid()
measure_page <- function(url, timeout = 300) {
s <- chromote::ChromoteSession$new(parent = chrome, width = 1000, height = 600)
on.exit(s$close(), add = TRUE)
s$Network$enable()
s$Network$setCacheDisabled(cacheDisabled = TRUE)
s$Page$enable() # needed for the injected script to run
s$Page$addScriptToEvaluateOnNewDocument(source = hook_js)
peak_file <- tempfile()
smp <- callr::r_bg(sampler, list(browser_pid, peak_file))
on.exit(smp$kill(), add = TRUE)
# wait for the sampler to start
for (i in 1:100) if (file.exists(peak_file)) break else Sys.sleep(0.05)
Sys.sleep(0.1)
s$Page$navigate(url, wait_ = FALSE) # large pages can take a while to commit
t0 <- Sys.time()
repeat {
done <- s$Runtime$evaluate("!!(window.__bench && window.__bench.done)", timeout_ = timeout)$result$value
if (isTRUE(done)) break
if (difftime(Sys.time(), t0, units = "secs") > timeout) stop("timed out loading ", url)
Sys.sleep(0.05)
}
Sys.sleep(0.3)
smp$kill()
b <- jsonlite::fromJSON(s$Runtime$evaluate(
"JSON.stringify(Object.assign({}, window.__bench, {js_bytes: performance.getEntriesByType('resource')
.filter(e => /\\.js$/.test(e.name)).reduce((a, e) => a + e.encodedBodySize, 0)}))"
)$result$value)
if (!is.null(b$error)) stop(url, ": ", b$error)
s$HeapProfiler$collectGarbage()
heap <- s$Runtime$getHeapUsage()
data.table(
load_sec = b$painted / 1000,
newplot_sec = (b$end - b$start) / 1000,
js_mb = b$js_bytes / 1e6,
peak_renderer_mb = as.numeric(readLines(peak_file, warn = FALSE)) / 1e6,
js_heap_mb = (heap$usedSize + if (is.null(heap$backingStorageSize)) 0 else heap$backingStorageSize) / 1e6
)
}
restart_chrome <- function() {
try(chrome$close(), silent = TRUE)
chrome <<- chromote::Chromote$new()
browser_pid <<- chrome$get_browser()$get_process()$get_pid()
}
# on a failure (e.g. Chrome not responding), restart Chrome and try the page
# once more; a page that fails twice (e.g. too large to load) is recorded as
# missing values, and not tried again in later rounds
failed <- character()
measure_retry <- function(url) {
missing <- data.table(load_sec = NA_real_, newplot_sec = NA_real_, js_mb = NA_real_,
peak_renderer_mb = NA_real_, js_heap_mb = NA_real_)
if (url %in% failed) return(missing)
for (attempt in 1:2) {
r <- tryCatch(measure_page(url), error = identity)
if (!inherits(r, "error")) return(r)
message("attempt ", attempt, " at ", url, " failed: ", conditionMessage(r))
restart_chrome()
}
failed <<- c(failed, url)
missing
}
set.seed(1)
browser <- rbindlist(lapply(seq_len(reps), function(rep) {
# shuffle the page order each round, so drift doesn't favour a variant
rbindlist(lapply(sample(nrow(pages)), function(i) {
cbind(pages[i, .(variant, case, size)], rep = rep,
measure_retry(sprintf("http://127.0.0.1:%d/%s", port, pages$path[i])))
}))
}))
server$kill()