Skip to content

Commit

Permalink
WIP Benchmark script
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusSkytte committed Mar 7, 2024
1 parent e2731af commit 2337483
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions data-raw/benchmark_update_snapshot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
devtools::load_all()

# Our benchmark data is the iris data set but repeated to increase the data size
data_generator <- function(repeats) {
purrr::map(
seq(repeats),
\(it) dplyr::mutate(iris, r = dplyr::row_number() + (it - 1) * nrow(iris))
) |>
purrr::reduce(rbind)
}

data_1 <- data_generator(1)
data_2 <- data_generator(2) |>
dplyr::mutate(
Sepal.Length = dplyr::if_else(Sepal.Length > median(Sepal.Length), Sepal.Length, Sepal.Length / 2)
)
data_3 <- data_generator(3) |>
dplyr::mutate(
Sepal.Length = dplyr::if_else(Sepal.Length > median(Sepal.Length), Sepal.Length, Sepal.Length / 2),
Sepal.Width = dplyr::if_else(Sepal.Width > median(Sepal.Width), Sepal.Width, Sepal.Width / 2)
)

conns <- get_test_conns()
conn <- conns[[1]]

# copy data to the conns
data_on_conn <- list(
dplyr::copy_to(conn, data_1, name = id("test.SCDB_data_1", conn), overwrite = TRUE, temporary = FALSE),
dplyr::copy_to(conn, data_2, name = id("test.SCDB_data_2", conn), overwrite = TRUE, temporary = FALSE),
dplyr::copy_to(conn, data_3, name = id("test.SCDB_data_3", conn), overwrite = TRUE, temporary = FALSE)
)

# Define the data to loop over for benchmark
ts <- list("2021-01-01", "2021-01-02", "2021-01-03")

# Define the SCDB update functions
scdb_update_step <- function(conn, data, ts) {
update_snapshot(data, conn, "SCDB_benchmark", timestamp = ts,
logger = Logger$new(output_to_console = FALSE, warn = FALSE))
}

scdb_updates <- function(conn, data_on_conn) {
purrr::walk2(data_on_conn, ts, \(data, ts) scdb_update_step(conn, data, ts))
DBI::dbRemoveTable(conn, name = "SCDB_benchmark")
}

# Construct the list of benchmarks
benchmark_exprs <- alist()
benchmark_on_conn <- local(bquote(scdb_updates(conn, data_on_conn)))
benchmark_exprs <- append(benchmark_exprs, benchmark_on_conn)
names(benchmark_exprs) <- names(conns)[[1]]

res <- microbenchmark::microbenchmark(list = benchmark_exprs, times = 10)

print(res)

0 comments on commit 2337483

Please sign in to comment.