Skip to content

Commit

Permalink
Default to elapsed time where possible (#154)
Browse files Browse the repository at this point in the history
Fixes #72
  • Loading branch information
hadley committed Jul 3, 2024
1 parent 712a737 commit e72eeed
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# profvis (development version)

* `profvis()` now uses elapsed time where possible (#72).

profvis 0.3.8
=============================

Expand Down
24 changes: 23 additions & 1 deletion R/profvis.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
#'
#' @param prof_input The path to an [Rprof()] data file. Not
#' compatible with `expr` or `prof_output`.
#' @param timing The type of timing to use. Either `"elapsed"` (the
#' default) for wall clock time, or `"cpu"` for CPU time. Wall clock time
#' includes time spent waiting for other processes (e.g. waiting for a
#' web page to download) so is generally more useful.
#'
#' If `NULL`, the default, will use elapsed time where possible, i.e.
#' on Windows or on R 4.4.0 or greater.
#' @param width Width of the htmlwidget.
#' @param height Height of the htmlwidget
#' @param split Direction of split. Either `"v"` (the default) for
Expand Down Expand Up @@ -90,6 +97,7 @@ profvis <- function(expr = NULL,
interval = 0.01,
prof_output = NULL,
prof_input = NULL,
timing = NULL,
width = NULL,
height = NULL,
split = c("h", "v"),
Expand All @@ -109,6 +117,16 @@ profvis <- function(expr = NULL,
message("Intervals smaller than ~5ms will probably not result in accurate timings.")
}

if (is.null(timing)) {
if (has_event() || Sys.info()[["sysname"]] == "Windows") {
timing <- "elapsed"
} else {
timing <- "cpu"
}
} else {
timing <- arg_match(timing, c("elapsed", "cpu"))
}

if (!is.null(expr_q)) {
# Change the srcfile to add "<expr>" as the filename. Code executed from the
# console will have "" here, and code executed in a knitr code block will
Expand Down Expand Up @@ -152,6 +170,7 @@ profvis <- function(expr = NULL,
line.profiling = TRUE,
gc.profiling = TRUE,
memory.profiling = TRUE,
event = if (has_event()) timing,
filter.callframes = if (has_simplify()) simplify
))

Expand All @@ -161,7 +180,10 @@ profvis <- function(expr = NULL,
}
repeat {
inject(Rprof(prof_output, !!!rprof_args))
with_profvis_handlers(expr)
cnd <- with_profvis_handlers(expr)
if (!is.null(cnd)) {
break
}
Rprof(NULL)

lines <- readLines(prof_output)
Expand Down
4 changes: 4 additions & 0 deletions R/rprof.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ utils::globalVariables("do")
has_simplify <- function() {
getRversion() >= "4.0.3"
}

has_event <- function() {
getRversion() >= "4.4.0"
}
9 changes: 9 additions & 0 deletions man/profvis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-profvis.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ test_that("Irrelevant stack is trimmed from profiles (#123)", {
out <- repro_profvis(f(), simplify = TRUE)
expect_equal(profvis_modal_value(out$x$message$prof), "pause f")
})

test_that("defaults to elapsed timing", {
skip_on_cran()
skip_on_covr()
skip_if_not(has_event())

f <- function() Sys.sleep(TEST_PAUSE_TIME)

out <- repro_profvis(f(), rerun = "Sys.sleep")
expect_equal(profvis_modal_value(out$x$message$prof), "Sys.sleep f")
})

0 comments on commit e72eeed

Please sign in to comment.