Skip to contents

Executes each function in a separate forked process and monitors its memory and cpu usage in real time

This function is best used with long running functions like arrow or duckdb requests that doesn't fit with classic benchmarking methods like utils::Rprof and profmem.

memory is extracted every interval sec with ps::ps_memory_info

  • start_mem is measured just before launching the function.

  • max_mem is the max of all measured mem of the forked child

cpu times and duration are given by proc.time

Usage

timemoir(..., verbose = TRUE, n = 1, interval = 0.1)

Arguments

...

functions to benchmark.

verbose

A boolean. If TRUE (default) print information messages.

n

An integer. number of time each function must run (default 1)

interval

(default 0.1) sleep interval between memory check in sec

Value

a tibble as a timemoir object with one row per benchmarked function and 7 columns:

  • fname, function name (as string).

  • duration duration (in sec) of the function or NA if function fails.

  • error, error message if function fails, NA otherwise.

  • start_mem memory used before function benchmark (in KB).

  • max_mem max used memory (in KB).

  • cpu_user user cpu used in sec

  • cpu_sys system cpu used in sec

An error message such as "Process is a zombie" typically indicates that the process was terminated after exceeding the allocated memory quota.

Examples


test_function <- function(n) {
  x <- rnorm(n)
  mean(x)
}
timemoir(test_function(1.2e7), test_function(1.5e7), test_function(1e7))
#> 
#> 
#> 
#> # A tibble: 3 × 7
#>   fname                  duration error start_mem max_mem cpu_user cpu_sys
#>   <chr>                     <dbl> <chr>     <int>   <dbl>    <dbl>   <dbl>
#> 1 test_function(1.2e+07)    0.632 NA       180232  275192    0.554   0.078
#> 2 test_function(1.5e+07)    0.76  NA       180232  298584    0.672   0.087
#> 3 test_function(1e+07)      0.563 NA       180232  255472    0.484   0.079