Skip to content

Commit

Permalink
add basic benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzwalthert committed Sep 27, 2020
1 parent 3555174 commit 3751fc2
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .Rbuildignore
Expand Up @@ -19,4 +19,5 @@ revdep
^tests/testmanual$
^\.pre-commit-config\.yaml$
^brew\-log$
^\.github$
^\.github/$
^bench/$
61 changes: 61 additions & 0 deletions .github/workflows/benchmarking.yaml
@@ -0,0 +1,61 @@
on:
push:
branches:
- master
pull_request:
branches:
- master

name: Continuous Benchmarks

jobs:
build:
runs-on: macOS-latest
steps:
- name: Checkout repo
with:
fetch-depth: 0
uses: actions/checkout@master
- name: Ensure master branch is fetched
if: ${{ github.event_name == 'pull_request' }}
run: git branch $GITHUB_BASE_REF remotes/origin/$GITHUB_BASE_REF; git branch
- name: Setup R
uses: r-lib/actions/setup-r@master
- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Cache R packages
if: runner.os != 'Windows'
uses: actions/cache@v1
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ hashFiles('.github/R-version') }}-1-

- name: Install dependencies
run: |
Rscript -e "install.packages(c('gert', 'ggplot2', 'purrr'))" -e "remotes::install_deps(dependencies = TRUE); remotes::install_github('r-lib/bench')"
R CMD INSTALL .
- name: Checkout benchmarking repo
uses: actions/checkout@v2
with:
repository: r-lib/here
ref: v0.1
path: bench/sources/here
- name: Fetch existing benchmarks
run: Rscript -e '#bench::cb_fetch()'
- name: Run benchmarks
run: Rscript -e 'bench::cb_run()'
- name: Show benchmarks
run: git notes --ref benchmarks show
- uses: actions/upload-artifact@v2
with:
name: visual-benchmarks
path: bench/plots/
- name: Push benchmarks
if: ${{ github.event_name == 'push' }}
run: Rscript -e "bench::cb_push()"
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Expand Up @@ -13,6 +13,7 @@ repos:
exclude: >
(?x)^(
data/.*|
\.github/.*\.yaml|
(.*/|)\.Rprofile|
(.*/|)\.Renviron|
(.*/|)\.gitignore|
Expand All @@ -35,6 +36,10 @@ repos:
- id: parsable-R
- id: no-browser-statement
- id: deps-in-desc
exclude: >
(?x)^(
bench/.*
)$
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0
hooks:
Expand Down
32 changes: 32 additions & 0 deletions bench/01-declarations.R
@@ -0,0 +1,32 @@
#' Plot benchmarks against master, if benchmarks there exist
#'
#'
#' @param new_bm A new benchmark object.
#' @param new_bm_label The label of the new benchmark used as a title.
#' @param file The file path to write the plot.
plot_against_master <- function(new_bm,
new_bm_label = deparse(substitute(new_bm)),
file = paste0("plots/", new_bm_label, ".pdf")) {
new_bm <- bench::as_bench_mark(new_bm)
branches <- gert::git_branch_list()
master <- branches[branches$name == "master", "commit", drop = TRUE]
bm <- bench::cb_read()
hash_is_master <- bm$commit_hash == master
if (any(hash_is_master) && Sys.getenv("GITHUB_BASE_REF") != "") {
# if a pull request
reference <- bm[hash_is_master, "benchmarks"][[1]][[1]]
reference$expression <- bench:::new_bench_expr(Sys.getenv("GITHUB_BASE_REF"))
new_bm$expression <- bench:::new_bench_expr(Sys.getenv("GITHUB_HEAD_REF"))
new_bm <- rbind(reference, new_bm)
}
plot <- ggplot2::ggplot(new_bm) +
ggplot2::geom_boxplot(ggplot2::aes(
x = name, ymin = p0,
ymax = p100, lower = p25,
middle = p50, upper = p75
),
stat = "identity"
)

ggplot2::ggsave(file, plot)
}
36 changes: 36 additions & 0 deletions bench/02-basic.R
@@ -0,0 +1,36 @@
# drop all notes
# git update-ref -d refs/notes/benchmarks

library(styler)
library(magrittr)
path <- "sources/here"
dir.create("plots")
cache_clear(ask = FALSE)
cache_activate()
cache_info()

marker <- purrr::partial(
bench::mark,
min_iterations = 20,
check = FALSE,
filter_gc = FALSE,
memory = TRUE # skip uncached first round
)

with_cache <- marker(
with_cache = {
style_pkg(path)
}
)
cache_info()
gert::git_reset("hard", repo = path)
cache_deactivate()

without_cache <- marker(
without_cache = {
style_pkg(path)
}
)
latest_bm <- bench::cb_read()$benchmarks[[1]]
split(latest_bm, latest_bm$name) %>%
purrr::imap(plot_against_master)

0 comments on commit 3751fc2

Please sign in to comment.