From 6a46a25f4823119d23717ee4b98d28f014ffcaab Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 13 Oct 2024 22:05:13 +0300 Subject: [PATCH] image: Add benchmark flag to measure performance of image operations --- userspace/programs/image/main.cpp | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/userspace/programs/image/main.cpp b/userspace/programs/image/main.cpp index a44cf23e..1a11a618 100644 --- a/userspace/programs/image/main.cpp +++ b/userspace/programs/image/main.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -78,10 +79,13 @@ int main(int argc, char** argv) return usage(argv[0], 1); bool scale = false; + bool benchmark = false; for (int i = 1; i < argc - 1; i++) { if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--scale") == 0) scale = true; + else if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--benchmark") == 0) + benchmark = true; else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) return usage(argv[0], 0); else @@ -90,7 +94,11 @@ int main(int argc, char** argv) auto image_path = BAN::StringView(argv[argc - 1]); + timespec load_start, load_end; + clock_gettime(CLOCK_MONOTONIC, &load_start); auto image_or_error = LibImage::Image::load_from_file(image_path); + clock_gettime(CLOCK_MONOTONIC, &load_end); + if (image_or_error.is_error()) { fprintf(stderr, "Could not load image '%.*s': %s\n", @@ -101,6 +109,34 @@ int main(int argc, char** argv) return 1; } + if (benchmark) + { + const uint64_t start_ms = load_start.tv_sec * 1000 + load_start.tv_nsec / 1'000'000; + const uint64_t end_ms = load_end.tv_sec * 1000 + load_end.tv_nsec / 1'000'000; + const uint64_t duration_ms = end_ms - start_ms; + printf("image load took %" PRIu64 ".%03" PRIu64 " s\n", duration_ms / 1000, duration_ms % 1000); + + if (scale) + { + timespec scale_start, scale_end; + + clock_gettime(CLOCK_MONOTONIC, &scale_start); + auto scaled = MUST(image_or_error.value()->resize(1920, 1080, LibImage::Image::ResizeAlgorithm::Linear)); + clock_gettime(CLOCK_MONOTONIC, &scale_end); + + const uint64_t start_ms = scale_start.tv_sec * 1000 + scale_start.tv_nsec / 1'000'000; + const uint64_t end_ms = scale_end.tv_sec * 1000 + scale_end.tv_nsec / 1'000'000; + const uint64_t duration_ms = end_ms - start_ms; + printf("image scale (%" PRIu64 "x%" PRIu64 " to %dx%d) took %" PRIu64 ".%03" PRIu64 " s\n", + image_or_error.value()->width(), image_or_error.value()->height(), + 1920, 1080, + duration_ms / 1000, duration_ms % 1000 + ); + } + + return 0; + } + render_to_framebuffer(image_or_error.release_value(), scale); for (;;)