| 1 |
# Supporting functions for performance timers. These are primarily used to make |
| 2 |
# performance information available in Shiny documents (see ::run), but can |
| 3 |
# also display a summary of performance after any render: |
| 4 |
# |
| 5 |
# > rmarkdown::render( ... ) |
| 6 |
# > rmarkdown:::perf_timer_summary() |
| 7 |
|
| 8 |
# private environment to store performance timers |
| 9 |
.perf_timers <- new.env(parent = emptyenv()) |
| 10 |
|
| 11 |
# convenience for returning ms elapsed since the R session began |
| 12 |
elapsed_ms <- function() { |
| 13 |
proc.time()[3] * 1000 |
| 14 |
} |
| 15 |
|
| 16 |
# clears all perf timer state |
| 17 |
perf_timer_reset_all <- function() { |
| 18 |
env <- environment(perf_timer_reset_all) |
| 19 |
do.call("unlockBinding", list(".perf_timers", env)) |
| 20 |
env$.perf_timers <- new.env(parent = emptyenv()) |
| 21 |
lockBinding(".perf_timers", env) |
| 22 |
invisible(.perf_timers) |
| 23 |
} |
| 24 |
|
| 25 |
# record a start time for a perf timer |
| 26 |
perf_timer_start <- function(timer_name) { |
| 27 |
assign(timer_name, list(start = elapsed_ms()), .perf_timers) |
| 28 |
} |
| 29 |
|
| 30 |
# record a stop time for a perf timer |
| 31 |
perf_timer_stop <- function(timer_name) { |
| 32 |
if (!exists(timer_name, envir = .perf_timers)) |
| 33 |
return() |
| 34 |
timer <- .perf_timers[[timer_name]] |
| 35 |
timer$stop = elapsed_ms() |
| 36 |
assign(timer_name, timer, .perf_timers) |
| 37 |
} |
| 38 |
|
| 39 |
# return a formatted data frame with a performance timer summary |
| 40 |
perf_timer_summary <- function() { |
| 41 |
time <- sapply(ls(.perf_timers), function(timer_name) { |
| 42 |
timer <- .perf_timers[[timer_name]] |
| 43 |
timer$stop - timer$start |
| 44 |
}) |
| 45 |
names(time) <- ls(.perf_timers) |
| 46 |
data.frame(time) |
| 47 |
} |
| 48 |
|
| 49 |
# return a formatted JSON string with a performance timer summary |
| 50 |
# (for use in rmd_perf.js) |
| 51 |
perf_timers_as_json <- function() { |
| 52 |
summary <- perf_timer_summary() |
| 53 |
json <- paste(lapply(row.names(summary), function(t) { |
| 54 |
paste("{ marker: '", t, "',", |
| 55 |
" elapsed: ", as.integer(summary[t,]), " }", |
| 56 |
sep = "") |
| 57 |
}), |
| 58 |
collapse = ", ") |
| 59 |
json <- paste("[", json, "]") |
| 60 |
json |
| 61 |
} |
| 62 |
|
| 63 |
# inject performance timings into rmd_perf.js; write performance timing |
| 64 |
# supporting files to files_dir and return an HTML dependency object suitable |
| 65 |
# for inclusion in the document for which the timings were collected |
| 66 |
create_performance_dependency <- function(files_dir) { |
| 67 |
performance_js <- pkg_file("rmd/h/rmarkdown/rmd_perf.js") |
| 68 |
js_lines <- gsub("RMARKDOWN_PERFORMANCE_TIMINGS", perf_timers_as_json(), |
| 69 |
read_utf8(performance_js), fixed = TRUE) |
| 70 |
perf_js_file <- file.path(files_dir, "rmd_perf.js") |
| 71 |
write_utf8(js_lines, perf_js_file) |
| 72 |
file.copy(pkg_file("rmd/h/rmarkdown/rmd_perf.css"), |
| 73 |
file.path(files_dir, "rmd_perf.css")) |
| 74 |
htmlDependency( |
| 75 |
name = "rmarkdown-performance", |
| 76 |
version = "0.1", |
| 77 |
src = files_dir, |
| 78 |
script = "rmd_perf.js", |
| 79 |
stylesheet = "rmd_perf.css") |
| 80 |
} |