Skip to content

Commit c50338f

Browse files
Merge pull request #674 from lorenzwalthert/benchmarking
- Add benchmarking infra (#674).
2 parents 6f9d137 + b663a40 commit c50338f

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

.Rbuildignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ revdep
1919
^tests/testmanual$
2020
^\.pre-commit-config\.yaml$
2121
^brew\-log$
22-
^\.github$
22+
^\.github/$
23+
^bench/$

.github/workflows/benchmarking.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
on:
2+
push:
3+
branches:
4+
- master
5+
pull_request:
6+
branches:
7+
- master
8+
9+
name: Continuous Benchmarks
10+
11+
jobs:
12+
build:
13+
runs-on: macOS-latest
14+
steps:
15+
- name: Checkout repo
16+
with:
17+
fetch-depth: 0
18+
uses: actions/checkout@master
19+
- name: Ensure base branch is fetched
20+
if: ${{ github.event_name == 'pull_request' }}
21+
run: git branch $GITHUB_BASE_REF remotes/origin/$GITHUB_BASE_REF; git branch
22+
- name: Setup R
23+
uses: r-lib/actions/setup-r@master
24+
- name: Query dependencies
25+
run: |
26+
install.packages('remotes')
27+
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
28+
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
29+
shell: Rscript {0}
30+
31+
- name: Cache R packages
32+
if: runner.os != 'Windows'
33+
uses: actions/cache@v1
34+
with:
35+
path: ${{ env.R_LIBS_USER }}
36+
key: ${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
37+
restore-keys: ${{ hashFiles('.github/R-version') }}-1-
38+
39+
- name: Install dependencies
40+
run: |
41+
Rscript -e "install.packages(c('gert', 'ggplot2', 'purrr'))" -e "remotes::install_deps(dependencies = TRUE); remotes::install_github('r-lib/bench')"
42+
R CMD INSTALL .
43+
- name: Checkout benchmarking repo
44+
uses: actions/checkout@v2
45+
with:
46+
repository: lorenzwalthert/here
47+
ref: ca9c8e69c727def88d8ba1c8b85b0e0bcea87b3f
48+
path: bench/sources/here
49+
- name: Fetch existing benchmarks
50+
run: Rscript -e 'rlang::with_handlers(bench::cb_fetch(), error = function(e) paste("Could not fetch benchmarks, skipping. The error was", conditionMessage(e)))'
51+
- name: Run benchmarks
52+
run: Rscript -e 'bench::cb_run()'
53+
- name: Show benchmarks
54+
run: git notes --ref benchmarks show
55+
- uses: actions/upload-artifact@v2
56+
with:
57+
name: visual-benchmarks
58+
path: bench/plots/
59+
- name: Push benchmarks
60+
if: ${{ github.event_name == 'push' }}
61+
run: Rscript -e "bench::cb_push()"

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ repos:
1313
exclude: >
1414
(?x)^(
1515
data/.*|
16+
\.github/.*\.yaml|
1617
(.*/|)\.Rprofile|
1718
(.*/|)\.Renviron|
1819
(.*/|)\.gitignore|
@@ -35,6 +36,10 @@ repos:
3536
- id: parsable-R
3637
- id: no-browser-statement
3738
- id: deps-in-desc
39+
exclude: >
40+
(?x)^(
41+
bench/.*
42+
)$
3843
- repo: https://github.com/pre-commit/pre-commit-hooks
3944
rev: v3.2.0
4045
hooks:

bench/01-declarations.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#' Plot benchmarks against a base branch, if benchmarks there exist
2+
#'
3+
#' @param new_bm A new benchmark object.
4+
#' @param new_bm_label The label of the new benchmark used as a title.
5+
#' @param file The file path to write the plot.
6+
plot_against_base <- function(new_bm,
7+
new_bm_label = deparse(substitute(new_bm)),
8+
file = paste0("plots/", new_bm_label, ".pdf")) {
9+
new_bm <- bench::as_bench_mark(new_bm)
10+
new_bm$expression <- bench:::new_bench_expr(Sys.getenv("GITHUB_HEAD_REF"))
11+
name <- unique(new_bm$name)
12+
stopifnot(length(name) == 1)
13+
branches <- gert::git_branch_list()
14+
last_commit_base_branch <- branches[branches$name == Sys.getenv("GITHUB_BASE_REF"), "commit", drop = TRUE]
15+
bm <- bench::cb_read()
16+
commit_is_reference <- bm$commit_hash == last_commit_base_branch
17+
if (any(commit_is_reference) && Sys.getenv("GITHUB_BASE_REF") != "") {
18+
# if a pull request
19+
reference <- bm[commit_is_reference, "benchmarks"][[1]][[1]]
20+
if (nrow(reference) > 0 && "name" %in% names(reference)) {
21+
reference <- reference %>%
22+
dplyr::filter(.data$name %in% !!name)
23+
reference$expression <- bench:::new_bench_expr(Sys.getenv("GITHUB_BASE_REF"))
24+
new_bm <- dplyr::bind_rows(reference, new_bm)
25+
}
26+
}
27+
new_bm$branch <- factor(new_bm$expression)
28+
plot <- ggplot2::ggplot(new_bm) +
29+
ggplot2::geom_boxplot(ggplot2::aes(
30+
x = branch, ymin = p0,
31+
ymax = p100, lower = p25,
32+
middle = p50, upper = p75
33+
),
34+
stat = "identity"
35+
) +
36+
ggplot2::ggtitle(name)
37+
38+
ggplot2::ggsave(file, plot)
39+
}

bench/02-basic.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# drop all notes
2+
# git update-ref -d refs/notes/benchmarks
3+
4+
library(styler)
5+
library(magrittr)
6+
path <- "sources/here"
7+
dir.create("plots")
8+
cache_clear(ask = FALSE)
9+
cache_activate()
10+
cache_info()
11+
12+
marker <- purrr::partial(
13+
bench::mark,
14+
min_iterations = 20,
15+
check = FALSE,
16+
filter_gc = FALSE,
17+
memory = TRUE # skip uncached first round
18+
)
19+
20+
with_cache <- marker(
21+
with_cache = {
22+
style_pkg(path, filetype = c("R", "rmd"))
23+
}
24+
)
25+
cache_info()
26+
gert::git_reset_hard(repo = path)
27+
cache_deactivate()
28+
29+
without_cache <- marker(
30+
without_cache = {
31+
style_pkg(path, filetype = c("R", "rmd"))
32+
}
33+
)
34+
latest_bm <- bench::cb_read()$benchmarks[[1]]
35+
split(latest_bm, latest_bm$name) %>%
36+
purrr::imap(plot_against_base)

0 commit comments

Comments
 (0)