1#' Benchmark plot creation time.
2#' Broken down into construct, build, render and draw times.
3#'
4#' @param x code to create ggplot2 plot
5#' @export
6#' @keywords internal
7#' @examples
8#' benchplot(ggplot(mtcars, aes(mpg, wt)) + geom_point())
9#' benchplot(ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(. ~ cyl))
10#'
11#' # With tidy eval:
12#' p <- expr(ggplot(mtcars, aes(mpg, wt)) + geom_point())
13#' benchplot(!!p)
14
15benchplot <- function(x) {
16  x <- enquo(x)
17  construct <- system.time(x <- eval_tidy(x))
18  if (!inherits(x, "ggplot")) {
19    abort("`x` must be a ggplot object")
20  }
21
22  build <- system.time(data <- ggplot_build(x))
23  render <- system.time(grob <- ggplot_gtable(data))
24  draw <- system.time(grid.draw(grob))
25
26  times <- rbind(construct, build, render, draw)[, 1:3]
27  times <- rbind(times, colSums(times))
28
29  cbind(
30    step = c("construct", "build", "render", "draw", "TOTAL"),
31    mat_2_df(times)
32  )
33}
34