| 1 |
--- |
| 2 |
title: "benchmarks" |
| 3 |
author: "Stefan Fleck" |
| 4 |
date: "`r Sys.Date()`" |
| 5 |
output: |
| 6 |
html_document: |
| 7 |
toc: true |
| 8 |
number_sections: true |
| 9 |
editor_options: |
| 10 |
chunk_output_type: console |
| 11 |
--- |
| 12 |
|
| 13 |
```{r setup, include = FALSE} |
| 14 |
knitr::opts_chunk$set( |
| 15 |
collapse = TRUE, |
| 16 |
comment = "#>" |
| 17 |
) |
| 18 |
|
| 19 |
library(devtools) |
| 20 |
library(bench) |
| 21 |
library(dplyr) |
| 22 |
library(lgr) |
| 23 |
|
| 24 |
``` |
| 25 |
|
| 26 |
```{r} |
| 27 |
ITS <- seq_len(1e2) |
| 28 |
|
| 29 |
|
| 30 |
``` |
| 31 |
|
| 32 |
A simple comparison of the perfromance of various Appenders |
| 33 |
|
| 34 |
# Setup loggers |
| 35 |
```{r, results="hide"} |
| 36 |
app <- list( |
| 37 |
disabled = AppenderFile$new(tempfile()), |
| 38 |
plain = AppenderFile$new(tempfile()), |
| 39 |
rotating = AppenderFileRotating$new(tempfile()), |
| 40 |
rotating_date = AppenderFileRotatingDate$new(tempfile(), cache_backups = FALSE), |
| 41 |
rotating_date_cached = AppenderFileRotatingDate$new(tempfile(), cache_backups = TRUE), |
| 42 |
json = AppenderJson$new(tempfile()) |
| 43 |
) |
| 44 |
|
| 45 |
# configure AppenderRotatingDate so that it is forced to |
| 46 |
# check the date stamp on each log (the wors-case scenario) |
| 47 |
td <- list() |
| 48 |
td$rotating <- file.path(tempdir(), "rotating") |
| 49 |
td$rotating_date <- file.path(tempdir(), "rotating_date") |
| 50 |
td$rotating_date_cached <- file.path(tempdir(), "rotating_date_cached") |
| 51 |
lapply(td, dir.create) |
| 52 |
|
| 53 |
# set backup dirs so that they don't conflict |
| 54 |
app$rotating$ |
| 55 |
set_backup_dir(td$rotating) |
| 56 |
|
| 57 |
app$rotating_date$ |
| 58 |
set_size(-1)$ |
| 59 |
set_age("1 year")$ |
| 60 |
set_backup_dir(td$rotating_date) |
| 61 |
|
| 62 |
app$rotating_date_cached$ |
| 63 |
set_size(-1)$ |
| 64 |
set_age("1 year")$ |
| 65 |
set_backup_dir(td$rotating_date_cached) |
| 66 |
|
| 67 |
app$rotating$rotate() |
| 68 |
app$rotating_date$rotate() |
| 69 |
app$rotating_date$rotate() |
| 70 |
app$rotating_date_cached$rotate() |
| 71 |
app$rotating_date_cached$rotate() |
| 72 |
|
| 73 |
stopifnot(isTRUE(app$rotating_date_cached$.__enclos_env__$private$bq$cache_backups)) |
| 74 |
stopifnot(isFALSE(app$rotating_date$.__enclos_env__$private$bq$cache_backups)) |
| 75 |
|
| 76 |
stopifnot(nrow(app$rotating$backups) == 0) |
| 77 |
stopifnot(nrow(app$rotating_date$backups) == 1) |
| 78 |
stopifnot(nrow(app$rotating_date_cached$backups) == 1) |
| 79 |
|
| 80 |
loggers = list( |
| 81 |
disabled = get_logger("disabled")$set_propagate(FALSE)$add_appender(app$disabled)$set_threshold(0), |
| 82 |
plain = get_logger("plain")$set_propagate(FALSE)$add_appender(app$plain), |
| 83 |
rotating = get_logger("rotating")$set_propagate(FALSE)$add_appender(app$rotating), |
| 84 |
rotating_date = get_logger("rotating_date")$set_propagate(FALSE)$add_appender(app$rotating_date), |
| 85 |
rotating_date_cached = get_logger("rotating_date_cached")$set_propagate(FALSE)$add_appender(app$rotating_date_cached), |
| 86 |
json = get_logger("json")$set_propagate(FALSE)$add_appender(app$json) |
| 87 |
) |
| 88 |
|
| 89 |
loggers$rotating$info("test") |
| 90 |
loggers$rotating$info("test") |
| 91 |
loggers$rotating_date$info("test") |
| 92 |
|
| 93 |
``` |
| 94 |
|
| 95 |
```{r eval = FALSE, echo = FALSE} |
| 96 |
|
| 97 |
profvis::profvis({ |
| 98 |
for (i in 1:10) loggers$rotating_date$fatal("test") |
| 99 |
}) |
| 100 |
|
| 101 |
|
| 102 |
profvis::profvis({ |
| 103 |
for (i in 1:10) loggers$rotating_date_cached$fatal("test") |
| 104 |
}) |
| 105 |
``` |
| 106 |
|
| 107 |
|
| 108 |
```{r} |
| 109 |
exprs <- lapply( |
| 110 |
loggers, |
| 111 |
function(.x) bquote(for (i in ITS) .(.x)$fatal("a test message")) |
| 112 |
) |
| 113 |
|
| 114 |
``` |
| 115 |
|
| 116 |
```{r} |
| 117 |
res <- mark(exprs = exprs, iterations = 100) |
| 118 |
|
| 119 |
|
| 120 |
res %>% |
| 121 |
dplyr::transmute( |
| 122 |
expression = format(expression), |
| 123 |
median = format(median), |
| 124 |
mem_alloc = format(mem_alloc) |
| 125 |
) %>% |
| 126 |
knitr::kable() |
| 127 |
|
| 128 |
``` |
| 129 |
```{r} |
| 130 |
plot(res) |
| 131 |
``` |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
```{r} |
| 136 |
lapply(app, function(a) unlink(a$file)) |
| 137 |
lapply(td, function(d) unlink(d, recursive = TRUE)) |
| 138 |
|
| 139 |
``` |
| 140 |
|