Round benchmark also in freqQuery log
Change-Id: I3ccc833367e7f29e4fb0948b782ca0a6a49d1916
diff --git a/R/KorAPQuery.R b/R/KorAPQuery.R
index 45a8415..dc9294b 100644
--- a/R/KorAPQuery.R
+++ b/R/KorAPQuery.R
@@ -234,7 +234,16 @@
if (!is.null(res$meta$cached)) {
log_info(verbose, " [cached]\n")
} else if (!is.null(res$meta$benchmark)) {
- log_info(verbose, ", took ", res$meta$benchmark, "\n", sep = "")
+ # Round the benchmark time to 2 decimal places for better readability
+ # If it's a string ending with 's', extract the number, round it, and re-add 's'
+ if (is.character(res$meta$benchmark) && grepl("s$", res$meta$benchmark)) {
+ time_value <- as.numeric(sub("s$", "", res$meta$benchmark))
+ formatted_time <- paste0(round(time_value, 2), "s")
+ log_info(verbose, ", took ", formatted_time, "\n", sep = "")
+ } else {
+ # Fallback if the format is different than expected
+ log_info(verbose, ", took ", res$meta$benchmark, "\n", sep = "")
+ }
} else {
log_info(verbose, "\n")
}
@@ -548,8 +557,10 @@
# This ensures consistent alignment
max_page_width <- nchar(as.character(total_pages))
# Add the actual page number that was fetched (0-based + 1 for display) with proper padding
- page_display <- paste0(page_display,
- sprintf(" (actual page %*d)", max_page_width, current_offset_page + 1))
+ page_display <- paste0(
+ page_display,
+ sprintf(" (actual page %*d)", max_page_width, current_offset_page + 1)
+ )
}
# Always show the absolute page number and total pages (for clarity)
else {
diff --git a/tests/testthat/test-benchmark-formatting-mock.R b/tests/testthat/test-benchmark-formatting-mock.R
new file mode 100644
index 0000000..3f43877
--- /dev/null
+++ b/tests/testthat/test-benchmark-formatting-mock.R
@@ -0,0 +1,22 @@
+test_that("benchmark time formatting function works correctly", {
+ # Create a mock environment to test the formatting function
+ format_benchmark_time <- function(time_string) {
+ if (is.character(time_string) && grepl("s$", time_string)) {
+ time_value <- as.numeric(sub("s$", "", time_string))
+ paste0(round(time_value, 2), "s")
+ } else {
+ time_string
+ }
+ }
+
+ # Test with various inputs
+ expect_equal(format_benchmark_time("3.395072759s"), "3.4s")
+ expect_equal(format_benchmark_time("0.123456s"), "0.12s")
+ expect_equal(format_benchmark_time("1.999s"), "2s")
+ expect_equal(format_benchmark_time("0.001s"), "0s")
+
+ # Test with non-matching inputs
+ expect_equal(format_benchmark_time("invalid"), "invalid")
+ expect_equal(format_benchmark_time(NULL), NULL)
+ expect_equal(format_benchmark_time(123), 123)
+})