Created
June 6, 2025 12:54
-
-
Save jrosell/b6433e0d9f8e87cffc4d6e9fe28a71bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rlang::check_installed(c("yyjsonr", "plumber2", "httr2", "bench", "callr", "palmerpenguins", "ggbeeswarm", "ggplot2")) | |
library(plumber2) | |
library(palmerpenguins) | |
data(package = 'palmerpenguins') | |
get_penguins <- \(n = 100) { | |
np <- nrow(penguins) | |
idx <- sample(1:np, n, replace = TRUE) | |
penguins[idx, ] | |
} | |
read_penguins <- \(body, query, request) { | |
started_at <- as.POSIXct(request$headers$Started_At) | |
elapsed <- as.numeric(Sys.time() - started_at) | |
elapsed | |
} | |
register_serializer("yyjson", mime_type = "application/json", fun = \(...) { | |
\(x) { | |
yyjsonr::write_json_str(x, ...) | |
} | |
}) | |
register_parser("yyjson", mime_type = "application/json", fun = \(...) { | |
\(raw, directives) { | |
yyjsonr::read_json_raw(raw, ...) | |
} | |
}) | |
app <- api(port = 3000L) |> | |
api_get("/serialize", get_penguins, serializers = get_serializers("json")) |> | |
api_get("/serialize-yy", get_penguins, serializers = get_serializers("yyjson")) |> | |
api_post("/deserialize", read_penguins, serializers = get_serializers("text"), parsers = get_parsers("json")) |> | |
api_post("/deserialize-yy", read_penguins, serializers = get_serializers("text"), parsers = get_parsers("yyjson")) |> | |
api_run() | |
get_pengos <- \(n) { | |
start <- Sys.time() | |
httr2::request("http://127.0.0.1:3000/serialize?") |> | |
httr2::req_url_query(n = n) |> | |
httr2::req_perform() | |
Sys.time() - start | |
} | |
get_pengos_yy <- \(n) { | |
start <- Sys.time() | |
httr2::request("http://127.0.0.1:3000/serialize-yy?") |> | |
httr2::req_url_query(n = n) |> | |
httr2::req_perform() | |
Sys.time() - start | |
} | |
benches <- bench::press( | |
n = c(1e1, 1e2, 1e3, 1e4, 1e5, 1e6), | |
niter = 25, | |
{ | |
bench::mark( | |
jsonlite = get_pengos(n), | |
yyjsonr = get_pengos_yy(n), | |
iterations = niter, | |
check = FALSE | |
) | |
} | |
) | |
plot(benches) + ggplot2::labs(title = "plumber2 serialize json") | |
library(palmerpenguins) | |
data(package = 'palmerpenguins') | |
deserialize <- \(n) { | |
np <- nrow(penguins) | |
idx <- sample(1:np, n, replace = TRUE) | |
httr2::request("http://127.0.0.1:3000/deserialize") |> | |
httr2::req_headers("Started_At" = as.character(Sys.time())) |> | |
httr2::req_body_json(penguins[idx, ]) |> | |
httr2::req_perform() | |
} | |
deserialize_yy <- \(n) { | |
np <- nrow(penguins) | |
idx <- sample(1:np, n, replace = TRUE) | |
httr2::request("http://127.0.0.1:3000/deserialize-yy") |> | |
httr2::req_headers("Started_At" = as.character(Sys.time())) |> | |
httr2::req_body_json(penguins[idx, ]) |> | |
httr2::req_perform() | |
} | |
benches_de <- bench::press( | |
n = c(1e1, 1e2, 1e3, 1e4, 1e5), | |
niter = 25, | |
{ | |
bench::mark( | |
jsonlite = deserialize(n), | |
yyjsonr = deserialize_yy(n), | |
iterations = niter, | |
check = FALSE | |
) | |
} | |
) | |
plot(benches_de) + ggplot2::labs(title = "plumber2 deserialize json") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment