Skip to content

Commit

Permalink
seconds_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
wlandau committed Nov 10, 2024
1 parent 459ed3d commit 71b41ef
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 15 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: proffer
Title: Profile R Code and Visualize with 'Pprof'
Version: 0.2.1.9000
Version: 0.2.1.9001
Encoding: UTF-8
Language: en-US
License: MIT + file LICENSE
Expand Down Expand Up @@ -43,6 +43,7 @@ Imports:
pingr (>= 2.0.1),
processx (>= 3.4.0),
profile (>= 1.0),
R.utils,
RProtoBuf (>= 0.4.14),
utils,
withr (>= 2.1.2)
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(serve_rprof)
export(test_pprof)
export(to_pprof)
export(to_rprof)
importFrom(R.utils,withTimeout)
importFrom(RProtoBuf,readProtoFiles)
importFrom(cli,cli_alert_danger)
importFrom(cli,cli_alert_info)
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# proffer 0.2.1.9000 (development)

# proffer 0.2.1.9001 (development)

* Add `seconds_timeout`.

# proffer 0.2.1

Expand Down
1 change: 1 addition & 0 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#' @importFrom parallelly freePort
#' @importFrom processx process
#' @importFrom profile read_rprof write_pprof
#' @importFrom R.utils withTimeout
#' @importFrom RProtoBuf readProtoFiles
#' @importFrom utils browseURL Rprof
#' @importFrom withr with_path
Expand Down
8 changes: 7 additions & 1 deletion R/pprof.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ test_pprof <- function(
#' Results are collected with [record_pprof()].
#' @return A `processx::process$new()` handle. Use this handle
#' to take down the server with `$kill()`.
#' @inheritParams record_rprof
#' @inheritParams serve_pprof
#' @param expr R code to run and profile.
#' @param ... Additional arguments passed on to [Rprof()]
Expand All @@ -53,13 +54,18 @@ test_pprof <- function(
#' }
pprof <- function(
expr,
seconds_timeout = Inf,
host = "localhost",
port = proffer::random_port(),
browse = interactive(),
verbose = TRUE,
...
) {
pprof <- record_pprof(expr, ...)
pprof <- record_pprof(
expr = expr,
seconds_timeout = seconds_timeout,
...
)
serve_pprof(
pprof = pprof,
host = host,
Expand Down
30 changes: 23 additions & 7 deletions R/record.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@
#' Profiles are recorded with [record_rprof()]
#' and then converted with [to_pprof()].
#' @return Path to a file with pprof samples.
#' @param expr An R expression to profile.
#' @inheritParams record_rprof
#' @param pprof Path to a file with pprof samples.
#' Also returned from the function.
#' @param ... Additional arguments passed on to [Rprof()]
#' via [record_rprof()].
#' @examples
#' if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) {
#' # Returns a path to pprof samples.
#' record_pprof(replicate(1e2, sample.int(1e4)))
#' }
record_pprof <- function(expr, pprof = tempfile(), ...) {
rprof <- record_rprof(expr, ...)
record_pprof <- function(
expr,
seconds_timeout = Inf,
pprof = tempfile(),
...
) {
rprof <- record_rprof(
expr = expr,
seconds_timeout = seconds_timeout,
...
)
to_pprof(rprof, pprof = pprof)
pprof
}
Expand All @@ -25,6 +32,10 @@ record_pprof <- function(expr, pprof = tempfile(), ...) {
#' @description Run R code and record Rprof samples.
#' @return Path to a file with Rprof samples.
#' @param expr An R expression to profile.
#' @param seconds_timeout Maximum number of seconds of elapsed time
#' to profile `expr`. When the timeout is reached, `proffer` stops running
#' `expr` and returns the profiling samples taken during the
#' `seconds_timeout` time window.
#' @param rprof Path to a file with Rprof samples.
#' Also returned from the function.
#' @param ... Additional arguments passed on to [Rprof()].
Expand All @@ -33,10 +44,15 @@ record_pprof <- function(expr, pprof = tempfile(), ...) {
#' # Returns a path to Rprof samples.
#' record_rprof(replicate(1e2, sample.int(1e4)))
#' }
record_rprof <- function(expr, rprof = tempfile(), ...) {
record_rprof <- function(
expr,
seconds_timeout = Inf,
rprof = tempfile(),
...
) {
on.exit(Rprof(NULL))
Rprof(filename = rprof, ...)
expr
R.utils::withTimeout(expr, timeout = seconds_timeout, onTimeout = "silent")
rprof
}

Expand Down
6 changes: 6 additions & 0 deletions man/pprof.Rd

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

10 changes: 7 additions & 3 deletions man/record_pprof.Rd

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

7 changes: 6 additions & 1 deletion man/record_rprof.Rd

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

8 changes: 8 additions & 0 deletions tests/local/test-interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ test_that("pprof()", {
px$kill()
})

test_that("pprof() can time out", {
skip("interactive only")
start <- as.numeric(proc.time()["elapsed"])
px <- pprof(while (TRUE) slow_function(), seconds_timeout = 5)
message(as.numeric(proc.time()["elapsed"]) - start)
px$kill()
})

test_that("test_pprof()", {
skip("interactive only")
# Should launch a browser and show a message.
Expand Down

0 comments on commit 71b41ef

Please sign in to comment.