Title: | Profile R Code and Visualize with 'Pprof' |
---|---|
Description: | Like similar profiling tools, the 'proffer' package automatically detects sources of slowness in R code. The distinguishing feature of 'proffer' is its utilization of 'pprof', which supplies interactive visualizations that are efficient and easy to interpret. Behind the scenes, the 'profile' package converts native Rprof() data to a protocol buffer that 'pprof' understands. For the documentation of 'proffer', visit <https://r-prof.github.io/proffer/>. To learn about the implementations and methodologies of 'pprof', 'profile', and protocol buffers, visit <https://github.com/google/pprof>. <https://protobuf.dev>, and <https://github.com/r-prof/profile>, respectively. |
Authors: | William Michael Landau [aut, cre] , Eli Lilly and Company [cph] |
Maintainer: | William Michael Landau <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.2.9000 |
Built: | 2024-11-15 16:25:23 UTC |
Source: | https://github.com/r-prof/proffer |
It can be challenging to find sources of slowness in large workflows, and the proffer package can help. Proffer runs R code and displays summaries to show where the code is slowest. Proffer leverages the pprof utility to create highly efficient, clear, easy-to-read interactive displays that help users find ways to reduce runtime. The package also contains helpers to convert profiling data to and from pprof format and visualize existing profiling data files. For documentation, visit https://r-prof.github.io/proffer/.
William Michael Landau [email protected]
https://github.com/r-prof/proffer
# TBD if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { # Start a pprof virtual server in the background. px <- pprof(replicate(1e2, sample.int(1e4))) # Terminate the server. px$kill() }
# TBD if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { # Start a pprof virtual server in the background. px <- pprof(replicate(1e2, sample.int(1e4))) # Terminate the server. px$kill() }
Run R code and display profiling results
in a local interactive pprof server.
Results are collected with record_pprof()
.
pprof( expr, seconds_timeout = Inf, host = "localhost", port = proffer::random_port(), browse = interactive(), verbose = TRUE, ... )
pprof( expr, seconds_timeout = Inf, host = "localhost", port = proffer::random_port(), browse = interactive(), verbose = TRUE, ... )
expr |
R code to run and profile. |
seconds_timeout |
Maximum number of seconds of elapsed time
to profile |
host |
Host name. Set to |
port |
Port number for hosting the local pprof server. Chosen randomly by default. |
browse |
Logical, whether to open a browser to view the pprof server. |
verbose |
Logical, whether to print console messages
such as the URL of the local |
... |
Additional arguments passed on to |
A processx::process$new()
handle. Use this handle
to take down the server with $kill()
.
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { # Start a pprof virtual server in the background. px <- pprof(replicate(1e2, sample.int(1e4))) # Terminate the server. px$kill() }
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { # Start a pprof virtual server in the background. px <- pprof(replicate(1e2, sample.int(1e4))) # Terminate the server. px$kill() }
Check if pprof
and its dependencies are installed.
pprof_sitrep()
pprof_sitrep()
pprof_sitrep()
pprof_sitrep()
Choose a random free TCP port.
random_port(lower = 49152L, upper = 65535L)
random_port(lower = 49152L, upper = 65535L)
lower |
Integer of length 1, lower bound of the port number. |
upper |
Integer of length 1, upper bound of the port number. |
This function is a simple wrapper around
parallelly::freePort()
with the default port range
covering ephemeral ports only.
Port number, positive integer of length 1.
random_port()
random_port()
Run R code and record pprof samples.
Profiles are recorded with record_rprof()
and then converted with to_pprof()
.
record_pprof(expr, seconds_timeout = Inf, pprof = tempfile(), ...)
record_pprof(expr, seconds_timeout = Inf, pprof = tempfile(), ...)
expr |
An R expression to profile. |
seconds_timeout |
Maximum number of seconds of elapsed time
to profile |
pprof |
Path to a file with pprof samples. Also returned from the function. |
... |
Additional arguments passed on to |
Path to a file with pprof samples.
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { # Returns a path to pprof samples. record_pprof(replicate(1e2, sample.int(1e4))) }
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { # Returns a path to pprof samples. record_pprof(replicate(1e2, sample.int(1e4))) }
Run R code and record Rprof samples.
record_rprof(expr, seconds_timeout = Inf, rprof = tempfile(), ...)
record_rprof(expr, seconds_timeout = Inf, rprof = tempfile(), ...)
expr |
An R expression to profile. |
seconds_timeout |
Maximum number of seconds of elapsed time
to profile |
rprof |
Path to a file with Rprof samples. Also returned from the function. |
... |
Additional arguments passed on to |
Path to a file with Rprof samples.
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { # Returns a path to Rprof samples. record_rprof(replicate(1e2, sample.int(1e4))) }
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { # Returns a path to Rprof samples. record_rprof(replicate(1e2, sample.int(1e4))) }
Visualize profiling data with pprof.
serve_pprof( pprof, host = "localhost", port = proffer::random_port(), browse = interactive(), verbose = TRUE )
serve_pprof( pprof, host = "localhost", port = proffer::random_port(), browse = interactive(), verbose = TRUE )
pprof |
Path to pprof samples. |
host |
Host name. Set to |
port |
Port number for hosting the local pprof server. Chosen randomly by default. |
browse |
Logical, whether to open a browser to view the pprof server. |
verbose |
Logical, whether to print console messages
such as the URL of the local |
Uses a local interactive server. Navigate a browser to a URL in the message. The server starts in a background process
A processx::process$new()
handle. Use this handle
to take down the server with $kill()
.
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { pprof <- record_pprof(replicate(1e2, sample.int(1e4))) # Start a pprof virtual server in the background. px <- serve_pprof(pprof) # Terminate the server. px$kill() }
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { pprof <- record_pprof(replicate(1e2, sample.int(1e4))) # Start a pprof virtual server in the background. px <- serve_pprof(pprof) # Terminate the server. px$kill() }
Use pprof to visualize profiling data
produced by Rprof()
or record_rprof()
.
serve_rprof( rprof, host = "localhost", port = proffer::random_port(), browse = interactive(), verbose = TRUE )
serve_rprof( rprof, host = "localhost", port = proffer::random_port(), browse = interactive(), verbose = TRUE )
rprof |
Path to profiling samples generated
by |
host |
Host name. Set to |
port |
Port number for hosting the local pprof server. Chosen randomly by default. |
browse |
Logical, whether to open a browser to view the pprof server. |
verbose |
Logical, whether to print console messages
such as the URL of the local |
Uses a local interactive server. Navigate a browser to a URL in the message. The server starts in a background process
A processx::process$new()
handle. Use this handle
to take down the server with $kill()
.
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { rprof <- record_rprof(replicate(1e2, sample.int(1e4))) # Start a pprof virtual server in the background. px <- serve_rprof(rprof) # Terminate the server. px$kill() }
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { rprof <- record_rprof(replicate(1e2, sample.int(1e4))) # Start a pprof virtual server in the background. px <- serve_rprof(rprof) # Terminate the server. px$kill() }
pprof()
Do a test run of pprof()
to verify that the
system dependencies like pprof
work as expected.
test_pprof( host = "localhost", port = proffer::random_port(), browse = interactive(), verbose = TRUE )
test_pprof( host = "localhost", port = proffer::random_port(), browse = interactive(), verbose = TRUE )
host |
Host name. Set to |
port |
Port number for hosting the local pprof server. Chosen randomly by default. |
browse |
Logical, whether to open a browser to view the pprof server. |
verbose |
Logical, whether to print console messages
such as the URL of the local |
See https://github.com/r-prof/proffer#installation for setup instructions.
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { test_pprof() }
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { test_pprof() }
Convert Rprof samples to pprof format.
to_pprof(rprof, pprof = tempfile())
to_pprof(rprof, pprof = tempfile())
rprof |
Path to Rprof samples. |
pprof |
Path to pprof samples. |
Path to pprof samples.
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { rprof <- record_rprof(replicate(1e2, sample.int(1e4))) to_pprof(rprof) }
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { rprof <- record_rprof(replicate(1e2, sample.int(1e4))) to_pprof(rprof) }
Convert pprof samples to Rprof format.
to_rprof(pprof, rprof = tempfile())
to_rprof(pprof, rprof = tempfile())
pprof |
Path to pprof samples. |
rprof |
Path to Rprof samples. |
Path to pprof samples.
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { pprof <- record_pprof(replicate(1e2, sample.int(1e4))) to_rprof(pprof) }
if (identical(Sys.getenv("PROFFER_EXAMPLES"), "true")) { pprof <- record_pprof(replicate(1e2, sample.int(1e4))) to_rprof(pprof) }