Merge pull request 'perf(corpus): faster remote sessions, with metadata tables cached by sha256 (#64)' (#82) from perf/remote-metadata-cache-64 into main
Mirror to GitHub / mirror (push) Failing after 5s
R-CMD-check / check (push) Successful in 3m46s

This commit was merged in pull request #82.
This commit is contained in:
jared committed 2026-11-12 21:49:44 -05:00
commit dfb5b4c711
7 files changed
+268 -11

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
# tests/testthat/test-http-metadata-cache.R
#
# uscogdata#64. On the remote path every CREATE VIEW re-requested the same
# file metadata over HTTP: 23 views at about 0.35 s each made session open
# take about 6 s against the public corpus. DuckDB's HTTP metadata cache keeps
# those responses for the life of the connection. Measured on 2026-11-12
# against the Hugging Face corpus, one run per arm: session open 5.6 s ->
# 1.6 s, first one-year query 1.2 s -> 0.8 s, the same query again
# 0.55 s -> 0.02 s.
#
# The fixture is local, so these tests can only check that the setting is on,
# not that it saves anything.
test_that("cog_open() turns on DuckDB's HTTP metadata cache (#64)", {
skip_if_no_corpus()
uscogdata:::cog_close()
on.exit(uscogdata:::cog_close(), add = TRUE)
con <- uscogdata:::cog_open()
v <- DBI::dbGetQuery(
con, "SELECT current_setting('enable_http_metadata_cache') AS v"
)$v[[1]]
expect_true(isTRUE(as.logical(v)))
})
+132
View File
@@ -0,0 +1,132 @@
# tests/testthat/test-metadata-cache.R
#
# uscogdata#64. On the remote path the ten metadata tables (about 3 MB) were
# read over HTTP every session. Kept in the cache directory and checked
# against the manifest's sha256, they cut session open from 1.7 s to 0.4 s
# against the public corpus (2026-11-12, one run per arm).
#
# The fixture is local, and a local corpus is never cached. These tests treat
# the fixture as if it were remote by handing .cache_metadata_tables() a
# fetch function that copies from it, so they run offline.
fake_remote <- "https://example.invalid/corpus/"
# A fetch that serves the fixture's bytes for the fake remote URL, and counts
# its calls.
counting_fetch <- function() {
env <- new.env(); env$n <- 0L
env$fetch <- function(src, dest) {
env$n <- env$n + 1L
rel <- sub(fake_remote, "", src, fixed = TRUE)
stopifnot(file.copy(file.path(fixture_corpus_path(), rel), dest,
overwrite = TRUE))
}
env
}
fixture_manifest <- function() {
jsonlite::fromJSON(file.path(fixture_corpus_path(), "manifest.json"),
simplifyVector = FALSE)
}
test_that("a local corpus is read in place, never cached (#64)", {
skip_if_no_corpus()
cd <- withr::local_tempdir()
url <- paste0(fixture_corpus_path(), "/")
expect_identical(.cache_metadata_tables(url, fixture_manifest(), cd), url)
expect_false(dir.exists(file.path(cd, "corpus")))
})
test_that("remote metadata tables are cached and verified (#64)", {
skip_if_no_corpus()
cd <- withr::local_tempdir()
m <- fixture_manifest()
f <- counting_fetch()
base <- .cache_metadata_tables(fake_remote, m, cd, fetch = f$fetch)
expect_false(identical(base, fake_remote))
expect_match(base, "/$")
expect_equal(f$n, length(m$files$metadata))
for (e in m$files$metadata) {
p <- file.path(cd, "corpus", e$path)
expect_true(file.exists(p), info = e$path)
expect_equal(digest::digest(p, algo = "sha256", file = TRUE), e$sha256)
}
# Second session: nothing fetched.
expect_identical(.cache_metadata_tables(fake_remote, m, cd, fetch = f$fetch),
base)
expect_equal(f$n, length(m$files$metadata))
})
test_that("a cached file that no longer matches the manifest is fetched again (#64)", {
skip_if_no_corpus()
cd <- withr::local_tempdir()
m <- fixture_manifest()
f <- counting_fetch()
.cache_metadata_tables(fake_remote, m, cd, fetch = f$fetch)
writeLines("stale", file.path(cd, "corpus", m$files$metadata[[1]]$path))
.cache_metadata_tables(fake_remote, m, cd, fetch = f$fetch)
expect_equal(f$n, length(m$files$metadata) + 1L)
p <- file.path(cd, "corpus", m$files$metadata[[1]]$path)
expect_equal(digest::digest(p, algo = "sha256", file = TRUE),
m$files$metadata[[1]]$sha256)
})
test_that("a download that fails its sha256 check falls back to remote (#64)", {
skip_if_no_corpus()
cd <- withr::local_tempdir()
m <- fixture_manifest()
bad_fetch <- function(src, dest) writeLines("not parquet", dest)
expect_message(
base <- .cache_metadata_tables(fake_remote, m, cd, fetch = bad_fetch),
"sha256"
)
expect_identical(base, fake_remote)
# Nothing unverified is left where a later session would find it.
left <- list.files(file.path(cd, "corpus"), recursive = TRUE)
expect_length(left, 0L)
})
test_that("a failed download falls back to remote (#64)", {
skip_if_no_corpus()
cd <- withr::local_tempdir()
err_fetch <- function(src, dest) stop("HTTP 429 Too Many Requests")
expect_message(
base <- .cache_metadata_tables(fake_remote, fixture_manifest(), cd,
fetch = err_fetch),
"429"
)
expect_identical(base, fake_remote)
})
test_that("a manifest entry without a sha256 disables the cache (#64)", {
skip_if_no_corpus()
cd <- withr::local_tempdir()
m <- fixture_manifest()
m$files$metadata[[1]]$sha256 <- NULL
f <- counting_fetch()
expect_identical(.cache_metadata_tables(fake_remote, m, cd, fetch = f$fetch),
fake_remote)
expect_equal(f$n, 0L)
})
test_that("views read metadata from the cache and partitions from the corpus (#64)", {
skip_if_no_corpus()
cd <- withr::local_tempdir()
m <- fixture_manifest()
meta <- .cache_metadata_tables(fake_remote, m, cd,
fetch = counting_fetch()$fetch)
url <- paste0(fixture_corpus_path(), "/")
con <- DBI::dbConnect(duckdb::duckdb())
on.exit(DBI::dbDisconnect(con, shutdown = TRUE), add = TRUE)
.register_views(con, url, m, meta_url = meta)
sql_of <- function(view) DBI::dbGetQuery(con, sprintf(
"SELECT sql FROM duckdb_views() WHERE view_name = '%s'", view))$sql
expect_match(sql_of("summary_categories"),
file.path(normalizePath(cd, winslash = "/"), "corpus"),
fixed = TRUE)
expect_match(sql_of("long"), fixture_corpus_path(), fixed = TRUE)
n <- DBI::dbGetQuery(con, "SELECT COUNT(*) AS n FROM spending_long")$n
expect_gt(n, 0)
})