tests/testthat/helper-performance.R

# From r-lib/bench
with_memory_prof <- function(expr) {
  f <- tempfile()
  on.exit(unlink(f))

  tryCatch(
    utils::Rprofmem(f, threshold = 1),
    error = function(...) skip("Can't profile memory on this system.")
  )
  on.exit(utils::Rprofmem(NULL), add = TRUE)

  res <- force(expr)
  utils::Rprofmem(NULL)

  bytes <- parse_allocations(f)$bytes
  bytes <- sum(bytes, na.rm = TRUE)
  new_bytes(bytes)
}
parse_allocations <- function(filename) {
  if (!is_installed("profmem")) {
    testthat::skip("profmem must be installed.")
  }
  readRprofmem <- env_get(ns_env("profmem"), "readRprofmem")

  tryCatch(
    readRprofmem(filename),
    error = function(cnd) {
      testthat::skip(sprintf(
        "Memory profiling failed: %s",
        conditionMessage(cnd)
      ))
    }
  )
}

Try the rlang package in your browser

Any scripts or data that you put into this service are public.

rlang documentation built on Nov. 4, 2023, 9:06 a.m.