performance-threaded.c 9.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
/* GObject - GLib Type, Object, Parameter and Signal Library
 * Copyright (C) 2009 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <math.h>
#include <string.h>
#include <glib-object.h>
#include "testcommon.h"

#define DEFAULT_TEST_TIME 2 /* seconds */

static GType
simple_register_class (const char *name, GType parent, ...)
{
  GInterfaceInfo interface_info = { NULL, NULL, NULL };
  va_list args;
  GType type, interface;

  va_start (args, parent);
  type = g_type_register_static_simple (parent, name, sizeof (GObjectClass),
      NULL, parent == G_TYPE_INTERFACE ? 0 : sizeof (GObject), NULL, 0);
  for (;;)
    {
      interface = va_arg (args, GType);
      if (interface == 0)
        break;
      g_type_add_interface_static (type, interface, &interface_info);
    }
  va_end (args);

  return type;
}

/* test emulating liststore behavior for interface lookups */

static GType liststore;
static GType liststore_interfaces[6];

static gpointer 
register_types (void)
{
  static volatile gsize inited = 0;
  if (g_once_init_enter (&inited))
    {
      liststore_interfaces[0] = simple_register_class ("GtkBuildable", G_TYPE_INTERFACE, 0);
      liststore_interfaces[1] = simple_register_class ("GtkTreeDragDest", G_TYPE_INTERFACE, 0);
      liststore_interfaces[2] = simple_register_class ("GtkTreeModel", G_TYPE_INTERFACE, 0);
      liststore_interfaces[3] = simple_register_class ("GtkTreeDragSource", G_TYPE_INTERFACE, 0);
      liststore_interfaces[4] = simple_register_class ("GtkTreeSortable", G_TYPE_INTERFACE, 0);
      liststore_interfaces[5] = simple_register_class ("UnrelatedInterface", G_TYPE_INTERFACE, 0);

      liststore = simple_register_class ("GtkListStore", G_TYPE_OBJECT, 
          liststore_interfaces[0], liststore_interfaces[1], liststore_interfaces[2],
          liststore_interfaces[3], liststore_interfaces[4], (GType) 0);

      g_once_init_leave (&inited, 1);
    }
  return NULL;
}

static void 
liststore_is_a_run (gpointer data)
{
  guint i;

  for (i = 0; i < 1000; i++)
    {
      g_assert (g_type_is_a (liststore, liststore_interfaces[0]));
      g_assert (g_type_is_a (liststore, liststore_interfaces[1]));
      g_assert (g_type_is_a (liststore, liststore_interfaces[2]));
      g_assert (g_type_is_a (liststore, liststore_interfaces[3]));
      g_assert (g_type_is_a (liststore, liststore_interfaces[4]));
      g_assert (!g_type_is_a (liststore, liststore_interfaces[5]));
    }
}

static gpointer
liststore_get_class (void)
{
  register_types ();
  return g_type_class_ref (liststore);
}

static void 
liststore_interface_peek_run (gpointer klass)
{
  guint i;
  gpointer iface;

  for (i = 0; i < 1000; i++)
    {
      iface = g_type_interface_peek (klass, liststore_interfaces[0]);
      g_assert (iface);
      iface = g_type_interface_peek (klass, liststore_interfaces[1]);
      g_assert (iface);
      iface = g_type_interface_peek (klass, liststore_interfaces[2]);
      g_assert (iface);
      iface = g_type_interface_peek (klass, liststore_interfaces[3]);
      g_assert (iface);
      iface = g_type_interface_peek (klass, liststore_interfaces[4]);
      g_assert (iface);
    }
}

static void 
liststore_interface_peek_same_run (gpointer klass)
{
  guint i;
  gpointer iface;

  for (i = 0; i < 1000; i++)
    {
      iface = g_type_interface_peek (klass, liststore_interfaces[0]);
      g_assert (iface);
      iface = g_type_interface_peek (klass, liststore_interfaces[0]);
      g_assert (iface);
      iface = g_type_interface_peek (klass, liststore_interfaces[0]);
      g_assert (iface);
      iface = g_type_interface_peek (klass, liststore_interfaces[0]);
      g_assert (iface);
      iface = g_type_interface_peek (klass, liststore_interfaces[0]);
      g_assert (iface);
    }
}

#if 0
/* DUMB test doing nothing */

static gpointer 
no_setup (void)
{
  return NULL;
}

static void 
no_run (gpointer data)
{
}
#endif

static void 
no_reset (gpointer data)
{
}

static void 
no_teardown (gpointer data)
{
}

typedef struct _PerformanceTest PerformanceTest;
struct _PerformanceTest {
  const char *name;

  gpointer (*setup) (void);
  void (*run) (gpointer data);
  void (*reset) (gpointer data);
  void (*teardown) (gpointer data);
};

static const PerformanceTest tests[] = {
  { "liststore-is-a",
    register_types,
    liststore_is_a_run,
    no_reset,
    no_teardown },
  { "liststore-interface-peek",
    liststore_get_class,
    liststore_interface_peek_run,
    no_reset,
    g_type_class_unref },
  { "liststore-interface-peek-same",
    liststore_get_class,
    liststore_interface_peek_same_run,
    no_reset,
    g_type_class_unref },
#if 0
  { "nothing",
    no_setup,
    no_run,
    no_reset,
    no_teardown }
#endif
};

static gboolean verbose = FALSE;
static int n_threads = 0;
static gboolean list = FALSE;
static int test_length = DEFAULT_TEST_TIME;

static GOptionEntry cmd_entries[] = {
  {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
   "Print extra information", NULL},
  {"threads", 't', 0, G_OPTION_ARG_INT, &n_threads,
   "number of threads to run in parrallel", NULL},
  {"seconds", 's', 0, G_OPTION_ARG_INT, &test_length,
   "Time to run each test in seconds", NULL},
  {"list", 'l', 0, G_OPTION_ARG_NONE, &list, 
   "List all available tests and exit", NULL},
  {NULL}
};

static gpointer
run_test_thread (gpointer user_data)
{
  const PerformanceTest *test = user_data;
  gpointer data;
  double elapsed;
  GTimer *timer, *total;
  GArray *results;

  total = g_timer_new ();
  g_timer_start (total);

  /* Set up test */
  timer = g_timer_new ();
  data = test->setup ();
  results = g_array_new (FALSE, FALSE, sizeof (double));

  /* Run the test */
  while (g_timer_elapsed (total, NULL) < test_length)
    {
      g_timer_reset (timer);
      g_timer_start (timer);
      test->run (data);
      g_timer_stop (timer);
      elapsed = g_timer_elapsed (timer, NULL);
      g_array_append_val (results, elapsed);
      test->reset (data);
    }

  /* Tear down */
  test->teardown (data);
  g_timer_destroy (timer);
  g_timer_destroy (total);

  return results;
}

static int
compare_doubles (gconstpointer a, gconstpointer b)
{
  double d = *(double *) a - *(double *) b;

  if (d < 0)
    return -1;
  if (d > 0)
    return 1;
  return 0;
}

static void
print_results (GArray *array)
{
  double min, max, avg;
  guint i;

  g_array_sort (array, compare_doubles);

  /* FIXME: discard outliers */

  min = g_array_index (array, double, 0) * 1000;
  max = g_array_index (array, double, array->len - 1) * 1000;
  avg = 0;
  for (i = 0; i < array->len; i++)
    {
      avg += g_array_index (array, double, i);
    }
  avg = avg / array->len * 1000;

  g_print ("  %u runs, min/avg/max = %.3f/%.3f/%.3f ms\n", array->len, min, avg, max);
}

static void
run_test (const PerformanceTest *test)
{
  GArray *results;

  g_print ("Running test \"%s\"\n", test->name);

  if (n_threads == 0) {
    results = run_test_thread ((gpointer) test);
  } else {
    guint i;
    GThread **threads;
    GArray *thread_results;
      
    threads = g_new (GThread *, n_threads);
    for (i = 0; i < n_threads; i++) {
      threads[i] = g_thread_create (run_test_thread, (gpointer) test, TRUE, NULL);
      g_assert (threads[i] != NULL);
    }

    results = g_array_new (FALSE, FALSE, sizeof (double));
    for (i = 0; i < n_threads; i++) {
      thread_results = g_thread_join (threads[i]);
      g_array_append_vals (results, thread_results->data, thread_results->len);
      g_array_free (thread_results, TRUE);
    }
    g_free (threads);
  }

  print_results (results);
  g_array_free (results, TRUE);
}

static const PerformanceTest *
find_test (const char *name)
{
  int i;
  for (i = 0; i < G_N_ELEMENTS (tests); i++)
    {
      if (strcmp (tests[i].name, name) == 0)
	return &tests[i];
    }
  return NULL;
}

int
main (int   argc,
      char *argv[])
{
  const PerformanceTest *test;
  GOptionContext *context;
  GError *error = NULL;
  int i;

  context = g_option_context_new ("GObject performance tests");
  g_option_context_add_main_entries (context, cmd_entries, NULL);
  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      g_printerr ("%s: %s\n", argv[0], error->message);
      return 1;
    }

  if (list)
    {
      for (i = 0; i < G_N_ELEMENTS (tests); i++)
        {
          g_print ("%s\n", tests[i].name);
        }
      return 0;
    }

  if (argc > 1)
    {
      for (i = 1; i < argc; i++)
	{
	  test = find_test (argv[i]);
	  if (test)
	    run_test (test);
	}
    }
  else
    {
      for (i = 0; i < G_N_ELEMENTS (tests); i++)
	run_test (&tests[i]);
    }

  return 0;
}