Skip to content

Commit 71b41ef

Browse files
committed
seconds_timeout
1 parent 459ed3d commit 71b41ef

File tree

10 files changed

+63
-15
lines changed

10 files changed

+63
-15
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: proffer
22
Title: Profile R Code and Visualize with 'Pprof'
3-
Version: 0.2.1.9000
3+
Version: 0.2.1.9001
44
Encoding: UTF-8
55
Language: en-US
66
License: MIT + file LICENSE
@@ -43,6 +43,7 @@ Imports:
4343
pingr (>= 2.0.1),
4444
processx (>= 3.4.0),
4545
profile (>= 1.0),
46+
R.utils,
4647
RProtoBuf (>= 0.4.14),
4748
utils,
4849
withr (>= 2.1.2)

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export(serve_rprof)
1212
export(test_pprof)
1313
export(to_pprof)
1414
export(to_rprof)
15+
importFrom(R.utils,withTimeout)
1516
importFrom(RProtoBuf,readProtoFiles)
1617
importFrom(cli,cli_alert_danger)
1718
importFrom(cli,cli_alert_info)

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# proffer 0.2.1.9000 (development)
2-
1+
# proffer 0.2.1.9001 (development)
32

3+
* Add `seconds_timeout`.
44

55
# proffer 0.2.1
66

R/package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#' @importFrom parallelly freePort
2020
#' @importFrom processx process
2121
#' @importFrom profile read_rprof write_pprof
22+
#' @importFrom R.utils withTimeout
2223
#' @importFrom RProtoBuf readProtoFiles
2324
#' @importFrom utils browseURL Rprof
2425
#' @importFrom withr with_path

R/pprof.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ test_pprof <- function(
4040
#' Results are collected with [record_pprof()].
4141
#' @return A `processx::process$new()` handle. Use this handle
4242
#' to take down the server with `$kill()`.
43+
#' @inheritParams record_rprof
4344
#' @inheritParams serve_pprof
4445
#' @param expr R code to run and profile.
4546
#' @param ... Additional arguments passed on to [Rprof()]
@@ -53,13 +54,18 @@ test_pprof <- function(
5354
#' }
5455
pprof <- function(
5556
expr,
57+
seconds_timeout = Inf,
5658
host = "localhost",
5759
port = proffer::random_port(),
5860
browse = interactive(),
5961
verbose = TRUE,
6062
...
6163
) {
62-
pprof <- record_pprof(expr, ...)
64+
pprof <- record_pprof(
65+
expr = expr,
66+
seconds_timeout = seconds_timeout,
67+
...
68+
)
6369
serve_pprof(
6470
pprof = pprof,
6571
host = host,

R/record.R

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
#' Profiles are recorded with [record_rprof()]
55
#' and then converted with [to_pprof()].
66
#' @return Path to a file with pprof samples.
7-
#' @param expr An R expression to profile.
7+
#' @inheritParams record_rprof
88
#' @param pprof Path to a file with pprof samples.
99
#' Also returned from the function.
10-
#' @param ... Additional arguments passed on to [Rprof()]
11-
#' via [record_rprof()].
1210
#' @examples
1311
#' if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) {
1412
#' # Returns a path to pprof samples.
1513
#' record_pprof(replicate(1e2, sample.int(1e4)))
1614
#' }
17-
record_pprof <- function(expr, pprof = tempfile(), ...) {
18-
rprof <- record_rprof(expr, ...)
15+
record_pprof <- function(
16+
expr,
17+
seconds_timeout = Inf,
18+
pprof = tempfile(),
19+
...
20+
) {
21+
rprof <- record_rprof(
22+
expr = expr,
23+
seconds_timeout = seconds_timeout,
24+
...
25+
)
1926
to_pprof(rprof, pprof = pprof)
2027
pprof
2128
}
@@ -25,6 +32,10 @@ record_pprof <- function(expr, pprof = tempfile(), ...) {
2532
#' @description Run R code and record Rprof samples.
2633
#' @return Path to a file with Rprof samples.
2734
#' @param expr An R expression to profile.
35+
#' @param seconds_timeout Maximum number of seconds of elapsed time
36+
#' to profile `expr`. When the timeout is reached, `proffer` stops running
37+
#' `expr` and returns the profiling samples taken during the
38+
#' `seconds_timeout` time window.
2839
#' @param rprof Path to a file with Rprof samples.
2940
#' Also returned from the function.
3041
#' @param ... Additional arguments passed on to [Rprof()].
@@ -33,10 +44,15 @@ record_pprof <- function(expr, pprof = tempfile(), ...) {
3344
#' # Returns a path to Rprof samples.
3445
#' record_rprof(replicate(1e2, sample.int(1e4)))
3546
#' }
36-
record_rprof <- function(expr, rprof = tempfile(), ...) {
47+
record_rprof <- function(
48+
expr,
49+
seconds_timeout = Inf,
50+
rprof = tempfile(),
51+
...
52+
) {
3753
on.exit(Rprof(NULL))
3854
Rprof(filename = rprof, ...)
39-
expr
55+
R.utils::withTimeout(expr, timeout = seconds_timeout, onTimeout = "silent")
4056
rprof
4157
}
4258

man/pprof.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/record_pprof.Rd

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/record_rprof.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/local/test-interactive.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ test_that("pprof()", {
3838
px$kill()
3939
})
4040

41+
test_that("pprof() can time out", {
42+
skip("interactive only")
43+
start <- as.numeric(proc.time()["elapsed"])
44+
px <- pprof(while (TRUE) slow_function(), seconds_timeout = 5)
45+
message(as.numeric(proc.time()["elapsed"]) - start)
46+
px$kill()
47+
})
48+
4149
test_that("test_pprof()", {
4250
skip("interactive only")
4351
# Should launch a browser and show a message.

0 commit comments

Comments
 (0)