[cairo] [PATCH] Make UTF-8 output from cairo-perf-diff-files optional.

M Joonas Pihlaja jpihlaja at cc.helsinki.fi
Sat Dec 16 12:21:36 PST 2006


Hi,

I'd like to push this patch that adds options to disable the
UTF-8 change bars and replace them with ASCII '*' gfx.  UTF-8
really does some damage to my terminal, and I always forget to
pipe stuff through a pager to make it safe.

Cheers,

Joonas
-------------- next part --------------
From 34e862670b902fda85a2fd9c746eb012ae74f274 Mon Sep 17 00:00:00 2001
From: M Joonas Pihlaja <jpihlaja at cc.helsinki.fi>
Date: Sat, 16 Dec 2006 22:16:15 +0200
Subject: [PATCH] Make UTF-8 output from cairo-perf-diff-files optional.

UTF-8 kills some terminals in a way "reset" or "stty sane" can't fix.
---
 perf/cairo-perf-diff-files.c |  151 +++++++++++++++++++++++++++++-------------
 1 files changed, 106 insertions(+), 45 deletions(-)

diff --git a/perf/cairo-perf-diff-files.c b/perf/cairo-perf-diff-files.c
index e6bb095..2a55864 100644
--- a/perf/cairo-perf-diff-files.c
+++ b/perf/cairo-perf-diff-files.c
@@ -129,6 +129,14 @@ typedef enum {
     TEST_REPORT_STATUS_ERROR
 } test_report_status_t;
 
+typedef struct _cairo_perf_diff_files_args {
+    char const *old_filename;
+    char const *new_filename;
+    double min_change;
+    int use_utf;
+    int print_change_bars;
+} cairo_perf_diff_files_args_t;
+
 /* Ad-hoc parsing, macros with a strong dependence on the calling
  * context, and plenty of other ugliness is here.  But at least it's
  * not perl... */
@@ -429,46 +437,55 @@ cairo_perf_report_sort_and_compute_stats
 
 #define CHANGE_BAR_WIDTH 70
 static void
-print_change_bar (double change, double max_change)
+print_change_bar (double change, double max_change, int use_utf)
 {
     int units_per_cell = (int) ceil (max_change / CHANGE_BAR_WIDTH);
+    static char const *ascii_boxes[8] = {
+	"****","***" ,"***", "**",
+	"**",  "*",   "*",   ""
+    };
+    static char const *utf_boxes[8] = {
+	"???","???","???","???",
+	"???","???","???","???"
+    };
+    char const **boxes = use_utf ? utf_boxes : ascii_boxes;
 
     /* For a 1.0x speedup we want a zero-size bar to show "no
      * change". */
     change -= 1.0;
 
     while (change > units_per_cell) {
-	printf("???");
+	printf(boxes[0]);
 	change -= units_per_cell;
     }
 
     change /= units_per_cell;
 
-    if (change > 7.5/8.0)
-	printf("???");
-    else if (change > 6.5/8.0)
-	printf("???");
-    else if (change > 5.5/8.0)
-	printf("???");
-    else if (change > 4.5/8.0)
-	printf("???");
-    else if (change > 3.5/8.0)
-	printf("???");
-    else if (change > 2.5/8.0)
-	printf("???");
-    else if (change > 1.5/8.0)
-	printf("???");
-    else if (change > 0.5/8.0)
-	printf("???");
+     if (change > 7.5/8.0)
+	printf(boxes[0]);
+     else if (change > 6.5/8.0)
+	printf(boxes[1]);
+     else if (change > 5.5/8.0)
+	printf(boxes[1]);
+     else if (change > 4.5/8.0)
+	printf(boxes[2]);
+     else if (change > 3.5/8.0)
+	printf(boxes[3]);
+     else if (change > 2.5/8.0)
+	printf(boxes[4]);
+     else if (change > 1.5/8.0)
+	printf(boxes[5]);
+     else if (change > 0.5/8.0)
+	printf(boxes[6]);
 
     printf ("\n");
 }
 
 #define MAX(a,b) ((a) > (b) ? (a) : (b))
 static void
-cairo_perf_report_diff (cairo_perf_report_t	*old,
-			cairo_perf_report_t	*new,
-			double			 min_change)
+cairo_perf_report_diff (cairo_perf_report_t			*old,
+			cairo_perf_report_t			*new,
+			cairo_perf_diff_files_args_t const	*args)
 {
     int i, i_old, i_new;
     test_report_t *o, *n;
@@ -476,7 +493,7 @@ cairo_perf_report_diff (cairo_perf_repor
     test_diff_t *diff, *diffs;
     int num_diffs = 0;
     int printed_speedup = 0, printed_slowdown = 0;
-    double change, max_change;
+    double min_change = args->min_change, change, max_change;
 
     diffs = xmalloc (MAX (old->tests_count, new->tests_count) * sizeof (test_diff_t));
 
@@ -577,7 +594,8 @@ cairo_perf_report_diff (cairo_perf_repor
 	else
 	    printf ("slowdown\n");
 
-	print_change_bar (change, max_change);
+	if (args->print_change_bars)
+	    print_change_bar (change, max_change, args->use_utf);
     }
 
     free (diffs);
@@ -586,42 +604,85 @@ cairo_perf_report_diff (cairo_perf_repor
 static void
 usage (const char *argv0)
 {
-    fprintf (stderr, "Usage: %s file1 file2 [minimum_significant_change[%%]]\n", argv0);
+    char const *basename = strrchr(argv0, '/');
+    basename = basename ? basename+1 : argv0;
+    fprintf (stderr,
+	     "Usage: %s [--no-utf] [--no-bars] file1 file2 [minimum_significant_change[%%]]\n\n",
+	     basename);
     fprintf (stderr,
 	     "Computes significant performance differences for cairo performance reports.\n"
 	     "Each file should be the output of the cairo-perf program (or \"make perf\").\n"
 	     "The third argument is used to supress all changes below some threshold.\n"
 	     "The default value of 5%% ignores any speeedup or slowdown of 5%% or less,\n"
-	     "A value of 0 will cause all output to be reported.\n");
+	     "A value of 0 will cause all output to be reported.\n"
+	     "\n"
+	     "--no-utf    Use ascii stars instead of utf-8 change bars.\n"
+	     "            Four stars are printed per factor of speedup.\n"
+	     "--no-bars   Don't display change bars at all.\n"
+	);
+    exit(1);
 }
 
-int
-main (int argc, const char *argv[])
+static void
+parse_args(int				  argc,
+	   char const			**argv,
+	   cairo_perf_diff_files_args_t  *args)
 {
-    const char *old_filename, *new_filename;
-    cairo_perf_report_t old, new;
-    double min_change;
-    char *end;
+#define is_yesno_opt(opt) !(strcmp ("--no-" opt, argv[i]) && strcmp ("--" opt, argv[i]))
+    int i;
+    int have_minchange = 0;
 
-    if (argc < 3) {
-	usage (argv[0]);
-	return 1;
+    for (i=1; i<argc; i++) {
+	if (is_yesno_opt("utf")) {
+	    args->use_utf = 0 != strncmp ("--no-", argv[i], 4);
+	}
+	else if (is_yesno_opt("bars")) {
+	    args->print_change_bars = 0 != strncmp ("--no-", argv[i], 4);
+	}
+	else if (!args->old_filename) {
+	    args->old_filename = argv[i];
+	}
+	else if (!args->new_filename) {
+	    args->new_filename = argv[i];
+	}
+	else if (!have_minchange) {
+	    char *end = NULL;
+	    args->min_change = strtod (argv[i], &end);
+	    if (*end && *end) {
+		if (0 == strcmp (end, "%")) {
+		    args->min_change /= 100;
+		} else {
+		    usage (argv[0]);
+		}
+	    }
+	}
+	else {
+	    usage (argv[0]);
+	}
     }
+    if ( !args->old_filename || !args->new_filename )
+	usage (argv[0]);
+#undef is_yesno_opt
+}
 
-    old_filename = argv[1];
-    new_filename = argv[2];
+int
+main (int argc, const char *argv[])
+{
+    cairo_perf_diff_files_args_t args = {
+	NULL,			/* old filename */
+	NULL,			/* new filename */
+	0.05,			/* min change */
+	1,			/* use UTF-8? */
+	1,			/* display change bars? */
+    };
+    cairo_perf_report_t old, new;
 
-    min_change = 0.05;
-    if (argc >= 4) {
-	min_change = strtod (argv[3], &end);
-	if (*end && *end == '%')
-	    min_change = min_change / 100.0;
-    }
+    parse_args (argc, argv, &args);
 
-    cairo_perf_report_load (&old, old_filename);
-    cairo_perf_report_load (&new, new_filename);
+    cairo_perf_report_load (&old, args.old_filename);
+    cairo_perf_report_load (&new, args.new_filename);
 
-    cairo_perf_report_diff (&old, &new, min_change);
+    cairo_perf_report_diff (&old, &new, &args);
 
     return 0;
 }
-- 
1.4.1.1



More information about the cairo mailing list