-
Notifications
You must be signed in to change notification settings - Fork 0
Add common pipeline timestamp #4
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6c5c76e
pipe_id fingerprint approach: identifies a unique expression form
CyGei 026d025
pipe_id to identify each execution run - globally
CyGei d8ee350
pipe_id to identify each execution run - per log
CyGei d4145c9
solving issue Add pipe_id tracking for time_pipe() logs
CyGei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ Suggests: | |
| testthat (>= 3.0.0), | ||
| crayon, | ||
| dplyr, | ||
| ggplot2, | ||
| stringr, | ||
| tictoc, | ||
| knitr, | ||
| rmarkdown | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ | |
| export(get_log) | ||
| export(rm_log) | ||
| export(time_pipe) | ||
| importFrom(stats,setNames) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,40 @@ | ||
| #' Retrieve a stored timing log | ||
| #' Retrieve a timing log (or all logs) | ||
| #' | ||
| #' @param log Character. Name of the data frame to load from `.pipetime_env`. | ||
| #' Return a stored timing log from `.pipetime_env`. | ||
| #' If `log = NULL`, return all logs as a named list. | ||
| #' | ||
| #' @param log Character string or `NULL`. Name of the log to retrieve. If `NULL`, all logs are returned. | ||
| #' | ||
| #' @return Either: | ||
| #' - A data frame with columns: | ||
| #' - `timestamp` (`POSIXct`): Pipeline start time | ||
| #' - `label` (`character`): Operation label | ||
| #' - `duration` (`numeric`): Elapsed time since pipeline start | ||
| #' - `unit` (`character`): Time unit used | ||
| #' - Or, if `log = NULL`, a named list of such data frames. | ||
| #' | ||
| #' @seealso [rm_log()] | ||
| #' | ||
| #' @importFrom stats setNames | ||
| #' | ||
| #' @return A data frame of timing logs. | ||
| #' @export | ||
| get_log <- function(log) { | ||
| if (exists(log, envir = .pipetime_env, inherits = FALSE)) { | ||
| get(log, envir = .pipetime_env) | ||
| get_log <- function(log = NULL) { | ||
| logs <- setdiff(ls(envir = .pipetime_env), "start_times") | ||
| if (!length(logs)) { | ||
| return(list()) | ||
| } | ||
|
|
||
| if (is.null(log)) { | ||
| # Return all logs | ||
| stats::setNames( | ||
| lapply(logs, function(x) get(x, envir = .pipetime_env)), | ||
| logs | ||
| ) | ||
| } else { | ||
| stop("No data frame named '", log, "' found in .pipetime_env.") | ||
| stopifnot(is.character(log), length(log) == 1) | ||
| if (!exists(log, envir = .pipetime_env, inherits = FALSE)) { | ||
| stop("No log named '", log, "' found in .pipetime_env.") | ||
| } | ||
| get(log, envir = .pipetime_env) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,36 @@ | ||
| #' Remove a stored timing log | ||
| #' Remove a timing log (or all logs) | ||
| #' | ||
| #' @param log Character. Name of the timing log to delete from `.pipetime_env`. | ||
| #' Delete a timing log from `.pipetime_env`. | ||
| #' If `log = NULL`, all logs are removed, but only when `force = TRUE`. | ||
| #' | ||
| #' @param log Character string or `NULL`. Name of the log to remove. If `NULL`, all logs are targeted. | ||
| #' @param force Logical. To remove all logs, `force` must be `TRUE`. Default: `FALSE`. | ||
| #' | ||
| #' @return Invisibly, `TRUE`. | ||
| #' @seealso [get_log()] | ||
| #' @export | ||
| rm_log <- function(log) { | ||
| if (!is.character(log) || length(log) != 1) { | ||
| stop("`log` must be a single character string.") | ||
| rm_log <- function(log = NULL, force = FALSE) { | ||
| logs <- setdiff(ls(envir = .pipetime_env), "start_times") | ||
| if (!length(logs)) { | ||
| warning("No logs to remove.") | ||
| return(invisible(FALSE)) | ||
| } | ||
| if (exists(log, envir = .pipetime_env, inherits = FALSE)) { | ||
| rm(list = log, envir = .pipetime_env) | ||
| invisible(TRUE) | ||
|
|
||
| if (is.null(log)) { | ||
| if (!force) { | ||
| stop("To remove all logs, set force = TRUE.") | ||
| } | ||
| rm(list = logs, envir = .pipetime_env) | ||
| .pipetime_env$start_times <- list() | ||
| } else { | ||
| warning("No data frame named '", log, "' found in pipetime environment.") | ||
| invisible(FALSE) | ||
| if (!is.character(log) || length(log) != 1) { | ||
| stop("`log` must be a single character string.") | ||
| } | ||
| if (!exists(log, envir = .pipetime_env, inherits = FALSE)) { | ||
| stop("No log named '", log, "' found in .pipetime_env.") | ||
| } | ||
| rm(list = log, envir = .pipetime_env) | ||
| .pipetime_env$start_times[[log]] <- NULL | ||
| } | ||
| invisible(TRUE) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,26 @@ | ||
| #' Measure execution time in a pipeline | ||
| #' | ||
| #' Records the runtime of pipeline (|>) operation. | ||
| #' Can print the timing to the console and optionally log it to a data frame in `.pipetime_env`. | ||
| #' Defaults can be set via `options()`. | ||
| #' Records the runtime of a pipeline (`|>`) from its start to the point where `time_pipe()` is called. | ||
| #' Prints results to the console and/or logs them in `.pipetime_env`. | ||
| #' Defaults can be set via `options(pipetime.*)`. | ||
| #' | ||
| #' @param .data Input object passed through the pipeline. | ||
| #' @param label Optional. Name for the operation. Defaults to the expression if not provided. | ||
| #' @param log Character or NULL. Name of a data frame to store logs in `.pipetime_env`. Defaults to NULL (no storage). | ||
| #' @param console Logical. Print timing to the console? Defaults to TRUE. | ||
| #' @param unit Character. Time unit passed to [base::difftime()]. One of `"secs"`, `"mins"`, `"hours"`, `"days"`, or `"weeks"`. Defaults to `"secs"`. | ||
| #' @param label Character string. Operation name. Defaults to the expression if `NULL`. | ||
| #' @param log Character string or `NULL`. Name of a log data frame in `.pipetime_env`. Default: `NULL`. | ||
| #' @param console Logical. Print timing to console? Default: `TRUE`. | ||
| #' @param unit Character string. Time unit for [base::difftime()]. One of `"secs"`, `"mins"`, `"hours"`, `"days"`, `"weeks"`. Default: `"secs"`. | ||
| #' | ||
| #' @return The input object, unchanged. Timing information is printed or stored separately. | ||
| #' @return `.data`, unchanged. Timing information is printed and/or stored separately. | ||
| #' | ||
| #' @details | ||
| #' `time_pipe()` measures the elapsed time of the pipeline from its start to the point where `time_pipe()` is called. | ||
| #' `time_pipe()` measures elapsed time from pipeline start to the call. | ||
| #' If `log` is set, results are appended to a data frame in `.pipetime_env` with columns: | ||
| #' - `timestamp`: Pipeline start time (`POSIXct`) | ||
| #' - `label`: Operation label | ||
| #' - `duration`: Elapsed time since pipeline start (`numeric`) | ||
| #' - `unit`: Time unit used | ||
| #' | ||
| #' Stored logs can be retrieved with [get_log()]. | ||
| #' | ||
| #' @examples | ||
| #' library(dplyr) | ||
|
|
@@ -24,29 +31,37 @@ | |
| #' time_pipe("total pipeline") | ||
| #' | ||
| #' @export | ||
| #' | ||
| time_pipe <- function( | ||
| .data, | ||
| label = NULL, | ||
| log = getOption("pipetime.log", NULL), | ||
| console = getOption("pipetime.console", TRUE), | ||
| unit = getOption("pipetime.unit", "secs") | ||
| ) { | ||
| unit <- match.arg( | ||
| unit, | ||
| choices = c("secs", "mins", "hours", "days", "weeks") | ||
| ) | ||
| # Track pipeline start | ||
| if (!is.null(log)) { | ||
| if (is.null(.pipetime_env$start_times[[log]])) { | ||
| .pipetime_env$start_times[[log]] <- Sys.time() | ||
| on.exit(.pipetime_env$start_times[[log]] <- NULL, add = TRUE) | ||
| } | ||
| start_time <- .pipetime_env$start_times[[log]] | ||
| } else { | ||
| start_time <- Sys.time() | ||
| } | ||
CyGei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| start <- Sys.time() | ||
| # Force evaluation and calculate duration | ||
| result <- .data | ||
| end <- Sys.time() | ||
| end_time <- Sys.time() | ||
| duration <- as.numeric(difftime(end_time, start_time, units = unit)) | ||
|
||
|
|
||
| # Generate label if not provided | ||
| if (is.null(label)) { | ||
| expr <- substitute(.data) | ||
| label <- gsub("\\s+", "", paste(deparse(expr), collapse = "")) | ||
| label <- paste(deparse(substitute(.data)), collapse = "") | ||
| label <- gsub("\\s+", " ", trimws(label)) | ||
| } | ||
|
|
||
| emit(start, end, label, unit, console, log) | ||
| # Output results | ||
| emit_time(start_time, duration, label, unit, console, log) | ||
|
|
||
| result | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| # Environment for pipetime | ||
| .pipetime_env <- new.env(parent = emptyenv()) | ||
| .pipetime_env$start_times <- list() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.