Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .quiet argument to press() #149

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# bench (development version)

* `press()` gains a new `.quiet` argument to silence progress messages (#145).

* Fixed an issue in `bench_time_trans()` and `bench_bytes_trans()` where pretty
breaks were not being applied correctly (#140, @plietar, @simonpcouch).

Expand Down
23 changes: 9 additions & 14 deletions R/press.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#' @param .grid A pre-built grid of values to use, typically a [data.frame] or
#' [tibble]. This is useful if you only want to benchmark a subset of all
#' possible combinations.
#' @param .quiet If `TRUE`, progress messages will not be emitted.
#' @export
#' @examples
#' # Helper function to create a simple data.frame of the specified dimensions
Expand All @@ -42,9 +43,14 @@
#' )
#' }
#' )
press <- function(..., .grid = NULL) {
press <- function(..., .grid = NULL, .quiet = FALSE) {
args <- rlang::quos(...)

assert(
"`.quiet` must be `TRUE` or `FALSE`",
isTRUE(.quiet) || isFALSE(.quiet)
)

unnamed <- names(args) == ""

if (sum(unnamed) < 1) {
Expand All @@ -70,9 +76,7 @@ press <- function(..., .grid = NULL) {
)
}

quiet <- bench_press_quiet()

if (!quiet) {
if (!.quiet) {
status <- format(tibble::as_tibble(parameters), n = Inf)
message(glue::glue("Running with:\n{status[[2]]}"))
}
Expand All @@ -86,7 +90,7 @@ press <- function(..., .grid = NULL) {
assign(var, value, envir = e)
}

if (!quiet) {
if (!.quiet) {
message(status[[row + 3L]])
}

Expand All @@ -108,12 +112,3 @@ press <- function(..., .grid = NULL) {
]
bench_mark(tibble::as_tibble(cbind(res[1], parameters, res[-1])))
}

bench_press_quiet <- function() {
# Internal option to silence `press()` during testing
isTRUE(getOption("bench.press_quiet", default = FALSE))
}

local_press_quiet <- function(frame = rlang::caller_env()) {
rlang::local_options(bench.press_quiet = TRUE, .frame = frame)
}
4 changes: 3 additions & 1 deletion man/press.Rd

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

23 changes: 10 additions & 13 deletions tests/testthat/test-press.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
describe("press", {
it("Adds parameters to output", {
local_press_quiet()

res <- press(
x = 1,
mark(1, max_iterations = 10)
mark(1, max_iterations = 10),
.quiet = TRUE
)
expect_equal(colnames(res), c("expression", "x", summary_cols, data_cols))
expect_equal(nrow(res), 1)

res2 <- press(
x = 1:3,
mark(1, max_iterations = 10)
mark(1, max_iterations = 10),
.quiet = TRUE
)
expect_equal(colnames(res2), c("expression", "x", summary_cols, data_cols))
expect_equal(nrow(res2), 3)
Expand Down Expand Up @@ -43,12 +43,11 @@ describe("press", {
})

it("expands the grid if has named parameters", {
local_press_quiet()

res <- press(
x = c(1, 2),
y = c(1, 3),
mark(list(x, y), max_iterations = 10)
mark(list(x, y), max_iterations = 10),
.quiet = TRUE
)

expect_equal(res$x, c(1, 2, 1, 2))
Expand All @@ -60,11 +59,10 @@ describe("press", {
})

it("takes values as-is if given in .grid", {
local_press_quiet()

res <- press(
.grid = data.frame(x = c(1, 2), y = c(1, 3)),
mark(list(x, y), max_iterations = 10)
mark(list(x, y), max_iterations = 10),
.quiet = TRUE
)

expect_equal(res$x, c(1, 2))
Expand All @@ -74,15 +72,14 @@ describe("press", {
})

it("runs `setup` with the parameters evaluated", {
local_press_quiet()

x <- 1
res <- press(
y = 2,
{
x <- y
mark(x)
}
},
.quiet = TRUE
)

expect_equal(res$result[[1]], 2)
Expand Down
Loading