From c579cfae2fc71941d11b35f1431a2379a655b3f2 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 14 Oct 2020 11:35:35 +0200 Subject: [PATCH 1/6] thumbnail: Factor out thumbnailing functionality This will make it easier to add some profiling functionality to the tests. --- libgnome-desktop/test-desktop-thumbnail.c | 42 +++++++++++++++-------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/libgnome-desktop/test-desktop-thumbnail.c b/libgnome-desktop/test-desktop-thumbnail.c index 510e88b6..e63bf263 100644 --- a/libgnome-desktop/test-desktop-thumbnail.c +++ b/libgnome-desktop/test-desktop-thumbnail.c @@ -24,42 +24,54 @@ #define GNOME_DESKTOP_USE_UNSTABLE_API #include -int main (int argc, char **argv) +static gboolean +thumbnail_file (GnomeDesktopThumbnailFactory *factory, + const char *in_path, + const char *out_path) { GdkPixbuf *pixbuf; - GnomeDesktopThumbnailFactory *factory; char *content_type; g_autoptr(GFile) file = NULL; g_autofree char *path = NULL; g_autofree char *uri = NULL; - if (argc != 3) { - g_print ("Usage: %s [INPUT FILE] [OUTPUT FILE]\n", argv[0]); - return 1; - } - - file = g_file_new_for_commandline_arg (argv[1]); + file = g_file_new_for_commandline_arg (in_path); path = g_file_get_path (file); if (!path) { - g_warning ("Could not get path for %s", argv[1]); - return 1; + g_warning ("Could not get path for %s", in_path); + return FALSE; } content_type = g_content_type_guess (path, NULL, 0, NULL); - factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE); uri = g_file_get_uri (file); pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail (factory, uri, content_type); g_free (content_type); if (pixbuf == NULL) { - g_warning ("gnome_desktop_thumbnail_factory_generate_thumbnail() failed to generate a thumbnail for %s", argv[1]); - return 1; + g_warning ("gnome_desktop_thumbnail_factory_generate_thumbnail() failed to generate a thumbnail for %s", in_path); + return FALSE; + } + + if (!gdk_pixbuf_save (pixbuf, out_path, "png", NULL, NULL)) { + g_warning ("gdk_pixbuf_save failed for %s", in_path); + return FALSE; } - if (!gdk_pixbuf_save (pixbuf, argv[2], "png", NULL, NULL)) { - g_warning ("gdk_pixbuf_save failed for %s", argv[1]); + return TRUE; +} + +int main (int argc, char **argv) +{ + GnomeDesktopThumbnailFactory *factory; + + if (argc != 3) { + g_print ("Usage: %s [INPUT FILE] [OUTPUT FILE]\n", argv[0]); return 1; } + factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE); + if (!thumbnail_file (factory, argv[1], argv[2])) + return 1; + return 0; } -- GitLab From 41267156aa7923be0ecad40eeb884b040c6fe860 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 14 Oct 2020 11:38:41 +0200 Subject: [PATCH 2/6] thumbnail: Plug memory leaks in test application --- libgnome-desktop/test-desktop-thumbnail.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/libgnome-desktop/test-desktop-thumbnail.c b/libgnome-desktop/test-desktop-thumbnail.c index e63bf263..0ded2030 100644 --- a/libgnome-desktop/test-desktop-thumbnail.c +++ b/libgnome-desktop/test-desktop-thumbnail.c @@ -29,8 +29,8 @@ thumbnail_file (GnomeDesktopThumbnailFactory *factory, const char *in_path, const char *out_path) { - GdkPixbuf *pixbuf; - char *content_type; + g_autoptr(GdkPixbuf) pixbuf = NULL; + g_autofree char *content_type = NULL; g_autoptr(GFile) file = NULL; g_autofree char *path = NULL; g_autofree char *uri = NULL; @@ -45,7 +45,6 @@ thumbnail_file (GnomeDesktopThumbnailFactory *factory, content_type = g_content_type_guess (path, NULL, 0, NULL); uri = g_file_get_uri (file); pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail (factory, uri, content_type); - g_free (content_type); if (pixbuf == NULL) { g_warning ("gnome_desktop_thumbnail_factory_generate_thumbnail() failed to generate a thumbnail for %s", in_path); @@ -62,7 +61,7 @@ thumbnail_file (GnomeDesktopThumbnailFactory *factory, int main (int argc, char **argv) { - GnomeDesktopThumbnailFactory *factory; + g_autoptr(GnomeDesktopThumbnailFactory) factory = NULL; if (argc != 3) { g_print ("Usage: %s [INPUT FILE] [OUTPUT FILE]\n", argv[0]); -- GitLab From 2cac8672262831dd0d246a74661d812f45637b35 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 14 Oct 2020 11:51:29 +0200 Subject: [PATCH 3/6] thumbnail: Use GOption to parse command-line args in test --- libgnome-desktop/test-desktop-thumbnail.c | 29 ++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/libgnome-desktop/test-desktop-thumbnail.c b/libgnome-desktop/test-desktop-thumbnail.c index 0ded2030..6a03425d 100644 --- a/libgnome-desktop/test-desktop-thumbnail.c +++ b/libgnome-desktop/test-desktop-thumbnail.c @@ -21,6 +21,8 @@ * Authors: Bastien Nocera */ +#include + #define GNOME_DESKTOP_USE_UNSTABLE_API #include @@ -62,14 +64,35 @@ thumbnail_file (GnomeDesktopThumbnailFactory *factory, int main (int argc, char **argv) { g_autoptr(GnomeDesktopThumbnailFactory) factory = NULL; + char **filenames = NULL; + int ret = 0; + g_autoptr(GOptionContext) option_context = NULL; + g_autoptr(GError) error = NULL; + const GOptionEntry options[] = { + { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, "[INPUT FILE] [OUTPUT FILE]", NULL }, + { NULL} + }; + + setlocale (LC_ALL, ""); + option_context = g_option_context_new (""); + g_option_context_add_main_entries (option_context, options, NULL); + + ret = g_option_context_parse (option_context, &argc, &argv, &error); + if (!ret) { + g_print ("Failed to parse arguments: %s", error->message); + return EXIT_FAILURE; + } - if (argc != 3) { - g_print ("Usage: %s [INPUT FILE] [OUTPUT FILE]\n", argv[0]); + if (filenames == NULL || + g_strv_length (filenames) != 2) { + g_autofree char *help = NULL; + help = g_option_context_get_help (option_context, TRUE, NULL); + g_print ("%s", help); return 1; } factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE); - if (!thumbnail_file (factory, argv[1], argv[2])) + if (!thumbnail_file (factory, filenames[0], filenames[1])) return 1; return 0; -- GitLab From b9a3f6c07c5ac64dd14ddfda73a93790edf47d44 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 14 Oct 2020 11:55:12 +0200 Subject: [PATCH 4/6] thumbnailer: Add iterations options to test --- libgnome-desktop/test-desktop-thumbnail.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/libgnome-desktop/test-desktop-thumbnail.c b/libgnome-desktop/test-desktop-thumbnail.c index 6a03425d..f3814de3 100644 --- a/libgnome-desktop/test-desktop-thumbnail.c +++ b/libgnome-desktop/test-desktop-thumbnail.c @@ -64,14 +64,19 @@ thumbnail_file (GnomeDesktopThumbnailFactory *factory, int main (int argc, char **argv) { g_autoptr(GnomeDesktopThumbnailFactory) factory = NULL; + gint num_iterations = 1; + gboolean shared_factory = FALSE; char **filenames = NULL; int ret = 0; g_autoptr(GOptionContext) option_context = NULL; g_autoptr(GError) error = NULL; const GOptionEntry options[] = { + { "shared-factory", 's', 0, G_OPTION_ARG_NONE, &shared_factory, "Whether to share the Thumbnail Factory (default: off)", NULL }, + { "num-iterations", 'n', 0, G_OPTION_ARG_INT, &num_iterations, "Number of times to run thumbnail operation (default: 1)", NULL }, { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, "[INPUT FILE] [OUTPUT FILE]", NULL }, { NULL} }; + int i; setlocale (LC_ALL, ""); option_context = g_option_context_new (""); @@ -84,16 +89,24 @@ int main (int argc, char **argv) } if (filenames == NULL || - g_strv_length (filenames) != 2) { + g_strv_length (filenames) != 2 || + num_iterations < 1) { g_autofree char *help = NULL; help = g_option_context_get_help (option_context, TRUE, NULL); g_print ("%s", help); return 1; } - factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE); - if (!thumbnail_file (factory, filenames[0], filenames[1])) - return 1; + for (i = 0; i < num_iterations; i++) { + if (factory == NULL) + factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE); + + if (!thumbnail_file (factory, filenames[0], filenames[1])) + return 1; + + if (!shared_factory) + g_clear_object (&factory); + } return 0; } -- GitLab From 06f0e9f8bf9f0474993000de323fefcef5cadd51 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 14 Oct 2020 12:18:07 +0200 Subject: [PATCH 5/6] thumbnail: Print time elapsed when requested during test --- libgnome-desktop/test-desktop-thumbnail.c | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/libgnome-desktop/test-desktop-thumbnail.c b/libgnome-desktop/test-desktop-thumbnail.c index f3814de3..e50a3743 100644 --- a/libgnome-desktop/test-desktop-thumbnail.c +++ b/libgnome-desktop/test-desktop-thumbnail.c @@ -64,8 +64,10 @@ thumbnail_file (GnomeDesktopThumbnailFactory *factory, int main (int argc, char **argv) { g_autoptr(GnomeDesktopThumbnailFactory) factory = NULL; + g_autoptr(GTimer) timer = NULL; gint num_iterations = 1; gboolean shared_factory = FALSE; + gboolean show_timer = FALSE; char **filenames = NULL; int ret = 0; g_autoptr(GOptionContext) option_context = NULL; @@ -73,10 +75,13 @@ int main (int argc, char **argv) const GOptionEntry options[] = { { "shared-factory", 's', 0, G_OPTION_ARG_NONE, &shared_factory, "Whether to share the Thumbnail Factory (default: off)", NULL }, { "num-iterations", 'n', 0, G_OPTION_ARG_INT, &num_iterations, "Number of times to run thumbnail operation (default: 1)", NULL }, + { "show-timer", 't', 0, G_OPTION_ARG_NONE, &show_timer, "Whether to show time statistics for operations", NULL }, { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, "[INPUT FILE] [OUTPUT FILE]", NULL }, { NULL} }; int i; + gdouble first_iter_elapsed = 0.0; + gdouble following_elapsed = 0.0; setlocale (LC_ALL, ""); option_context = g_option_context_new (""); @@ -97,16 +102,40 @@ int main (int argc, char **argv) return 1; } + timer = g_timer_new (); + for (i = 0; i < num_iterations; i++) { + g_timer_start (timer); + if (factory == NULL) factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE); if (!thumbnail_file (factory, filenames[0], filenames[1])) return 1; + if (i == 0) + first_iter_elapsed = g_timer_elapsed (timer, NULL); + else + following_elapsed += g_timer_elapsed (timer, NULL); + if (!shared_factory) g_clear_object (&factory); } + if (show_timer) { + if (num_iterations == 1) { + g_print ("Elapsed time: %d msec\n", + (int) (first_iter_elapsed * 1000)); + } else if (num_iterations > 1) { + g_print ("First iteration: %d msec\n", + (int) (first_iter_elapsed * 1000)); + g_print ("Average time: %d msec (%d iterations)\n", + (int) (following_elapsed * 1000 / (num_iterations - 1)), + num_iterations - 1); + } else { + g_assert_not_reached (); + } + } + return 0; } -- GitLab From 0df9ab03e61db6e48149888f210c9f71f6a81877 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Wed, 14 Oct 2020 11:37:01 +0200 Subject: [PATCH 6/6] thumbnail: Update copyright for test application --- libgnome-desktop/test-desktop-thumbnail.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libgnome-desktop/test-desktop-thumbnail.c b/libgnome-desktop/test-desktop-thumbnail.c index e50a3743..e402cf0e 100644 --- a/libgnome-desktop/test-desktop-thumbnail.c +++ b/libgnome-desktop/test-desktop-thumbnail.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012,2017 Red Hat, Inc. + * Copyright (C) 2012,2017,2020 Red Hat, Inc. * * This file is part of the Gnome Library. * -- GitLab