image: Add benchmark flag to measure performance of image operations

This commit is contained in:
Bananymous 2024-10-13 22:05:13 +03:00
parent 88b8ca5b29
commit 6a46a25f48
1 changed files with 36 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include <LibImage/Image.h> #include <LibImage/Image.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/framebuffer.h> #include <sys/framebuffer.h>
@ -78,10 +79,13 @@ int main(int argc, char** argv)
return usage(argv[0], 1); return usage(argv[0], 1);
bool scale = false; bool scale = false;
bool benchmark = false;
for (int i = 1; i < argc - 1; i++) for (int i = 1; i < argc - 1; i++)
{ {
if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--scale") == 0) if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--scale") == 0)
scale = true; 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) else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
return usage(argv[0], 0); return usage(argv[0], 0);
else else
@ -90,7 +94,11 @@ int main(int argc, char** argv)
auto image_path = BAN::StringView(argv[argc - 1]); 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); auto image_or_error = LibImage::Image::load_from_file(image_path);
clock_gettime(CLOCK_MONOTONIC, &load_end);
if (image_or_error.is_error()) if (image_or_error.is_error())
{ {
fprintf(stderr, "Could not load image '%.*s': %s\n", fprintf(stderr, "Could not load image '%.*s': %s\n",
@ -101,6 +109,34 @@ int main(int argc, char** argv)
return 1; 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); render_to_framebuffer(image_or_error.release_value(), scale);
for (;;) for (;;)