summaryrefslogtreecommitdiff
path: root/tests/gobject/performance.c
blob: e4cb736a03df72c3f5d77adcacda02b343c1478b (plain)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
/* 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, see <http://www.gnu.org/licenses/>.
 */

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

#define WARM_UP_N_RUNS 50
#define ESTIMATE_ROUND_TIME_N_RUNS 5
#define DEFAULT_TEST_TIME 15 /* seconds */
 /* The time we want each round to take, in seconds, this should
  * be large enough compared to the timer resolution, but small
  * enought that the risk of any random slowness will miss the
  * running window */
#define TARGET_ROUND_TIME 0.004

static gboolean verbose = FALSE;
static int test_length = DEFAULT_TEST_TIME;

static GOptionEntry cmd_entries[] = {
  {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
   "Print extra information", NULL},
  {"seconds", 's', 0, G_OPTION_ARG_INT, &test_length,
   "Time to run each test in seconds", NULL},
  {NULL}
};

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

  gpointer (*setup) (PerformanceTest *test);
  void (*init) (PerformanceTest *test,
		gpointer data,
		double factor);
  void (*run) (PerformanceTest *test,
	       gpointer data);
  void (*finish) (PerformanceTest *test,
		  gpointer data);
  void (*teardown) (PerformanceTest *test,
		    gpointer data);
  void (*print_result) (PerformanceTest *test,
			gpointer data,
			double time);
};

static void
run_test (PerformanceTest *test)
{
  gpointer data = NULL;
  guint64 i, num_rounds;
  double elapsed, min_elapsed, factor;
  GTimer *timer;

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

  /* Set up test */
  timer = g_timer_new ();
  data = test->setup (test);

  if (verbose)
    g_print ("Warming up\n");

  /* Warm up the test by doing a few runs */
  for (i = 0; i < WARM_UP_N_RUNS; i++)
    {
      test->init (test, data, 1.0);
      test->run (test, data);
      test->finish (test, data);
    }

  if (verbose)
    g_print ("Estimating round time\n");

  /* Estimate time for one run by doing a few test rounds */
  min_elapsed = 0;
  for (i = 0; i < ESTIMATE_ROUND_TIME_N_RUNS; i++)
    {
      test->init (test, data, 1.0);
      g_timer_start (timer);
      test->run (test, data);
      g_timer_stop (timer);
      test->finish (test, data);

      elapsed = g_timer_elapsed (timer, NULL);
      if (i == 0)
	min_elapsed = elapsed;
      else
	min_elapsed = MIN (min_elapsed, elapsed);
    }

  factor = TARGET_ROUND_TIME / min_elapsed;

  if (verbose)
    g_print ("Uncorrected round time: %f.4 secs, correction factor %f.2\n", min_elapsed, factor);

  /* Calculate number of rounds needed */
  num_rounds = (test_length / TARGET_ROUND_TIME) + 1;

  if (verbose)
    g_print ("Running %"G_GINT64_MODIFIER"d rounds\n", num_rounds);

  /* Run the test */
  for (i = 0; i < num_rounds; i++)
    {
      test->init (test, data, factor);
      g_timer_start (timer);
      test->run (test, data);
      g_timer_stop (timer);
      test->finish (test, data);
      elapsed = g_timer_elapsed (timer, NULL);

      if (i == 0)
	min_elapsed = elapsed;
      else
	min_elapsed = MIN (min_elapsed, elapsed);
    }

  if (verbose)
    g_print ("Minimum corrected round time: %f secs\n", min_elapsed);

  /* Print the results */
  test->print_result (test, data, min_elapsed);

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

/*************************************************************
 * Simple object is a very simple small GObject subclass
 * with no properties, no signals, implementing no interfaces
 *************************************************************/

static GType simple_object_get_type (void);
#define SIMPLE_TYPE_OBJECT        (simple_object_get_type ())
typedef struct _SimpleObject      SimpleObject;
typedef struct _SimpleObjectClass   SimpleObjectClass;

struct _SimpleObject
{
  GObject parent_instance;
  int val;
};

struct _SimpleObjectClass
{
  GObjectClass parent_class;
};

G_DEFINE_TYPE (SimpleObject, simple_object, G_TYPE_OBJECT);

static void
simple_object_finalize (GObject *object)
{
  G_OBJECT_CLASS (simple_object_parent_class)->finalize (object);
}

static void
simple_object_class_init (SimpleObjectClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->finalize = simple_object_finalize;
}

static void
simple_object_init (SimpleObject *simple_object)
{
  simple_object->val = 42;
}

typedef struct _TestIfaceClass TestIfaceClass;
typedef struct _TestIfaceClass TestIface1Class;
typedef struct _TestIfaceClass TestIface2Class;
typedef struct _TestIfaceClass TestIface3Class;
typedef struct _TestIfaceClass TestIface4Class;
typedef struct _TestIfaceClass TestIface5Class;
typedef struct _TestIface TestIface;

struct _TestIfaceClass
{
  GTypeInterface base_iface;
  void (*method) (TestIface *obj);
};

static GType test_iface1_get_type (void);
static GType test_iface2_get_type (void);
static GType test_iface3_get_type (void);
static GType test_iface4_get_type (void);
static GType test_iface5_get_type (void);

#define TEST_TYPE_IFACE1 (test_iface1_get_type ())
#define TEST_TYPE_IFACE2 (test_iface2_get_type ())
#define TEST_TYPE_IFACE3 (test_iface3_get_type ())
#define TEST_TYPE_IFACE4 (test_iface4_get_type ())
#define TEST_TYPE_IFACE5 (test_iface5_get_type ())

static DEFINE_IFACE (TestIface1, test_iface1,  NULL, NULL)
static DEFINE_IFACE (TestIface2, test_iface2,  NULL, NULL)
static DEFINE_IFACE (TestIface3, test_iface3,  NULL, NULL)
static DEFINE_IFACE (TestIface4, test_iface4,  NULL, NULL)
static DEFINE_IFACE (TestIface5, test_iface5,  NULL, NULL)

/*************************************************************
 * Complex object is a GObject subclass with a properties,
 * construct properties, signals and implementing an interface.
 *************************************************************/

static GType complex_object_get_type (void);
#define COMPLEX_TYPE_OBJECT        (complex_object_get_type ())
typedef struct _ComplexObject      ComplexObject;
typedef struct _ComplexObjectClass ComplexObjectClass;

struct _ComplexObject
{
  GObject parent_instance;
  int val1;
  int val2;
};

struct _ComplexObjectClass
{
  GObjectClass parent_class;

  void (*signal) (ComplexObject *obj);
  void (*signal_empty) (ComplexObject *obj);
};

static void complex_test_iface_init (gpointer         g_iface,
				     gpointer         iface_data);

G_DEFINE_TYPE_EXTENDED (ComplexObject, complex_object,
			G_TYPE_OBJECT, 0,
			G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE1,
					       complex_test_iface_init)
			G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE2,
					       complex_test_iface_init)
			G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE3,
					       complex_test_iface_init)
			G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE4,
					       complex_test_iface_init)
			G_IMPLEMENT_INTERFACE (TEST_TYPE_IFACE5,
					       complex_test_iface_init)
			);

#define COMPLEX_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), COMPLEX_TYPE_OBJECT, ComplexObject))

enum {
  PROP_0,
  PROP_VAL1,
  PROP_VAL2
};

enum {
  COMPLEX_SIGNAL,
  COMPLEX_SIGNAL_EMPTY,
  COMPLEX_SIGNAL_GENERIC,
  COMPLEX_SIGNAL_GENERIC_EMPTY,
  COMPLEX_SIGNAL_ARGS,
  COMPLEX_LAST_SIGNAL
};

static guint complex_signals[COMPLEX_LAST_SIGNAL] = { 0 };

static void
complex_object_finalize (GObject *object)
{
  G_OBJECT_CLASS (complex_object_parent_class)->finalize (object);
}

static void
complex_object_set_property (GObject         *object,
			     guint            prop_id,
			     const GValue    *value,
			     GParamSpec      *pspec)
{
  ComplexObject *complex = COMPLEX_OBJECT (object);

  switch (prop_id)
    {
    case PROP_VAL1:
      complex->val1 = g_value_get_int (value);
      break;
    case PROP_VAL2:
      complex->val2 = g_value_get_int (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
complex_object_get_property (GObject         *object,
			     guint            prop_id,
			     GValue          *value,
			     GParamSpec      *pspec)
{
  ComplexObject *complex = COMPLEX_OBJECT (object);

  switch (prop_id)
    {
    case PROP_VAL1:
      g_value_set_int (value, complex->val1);
      break;
    case PROP_VAL2:
      g_value_set_int (value, complex->val2);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
complex_object_real_signal (ComplexObject *obj)
{
}

static void
complex_object_class_init (ComplexObjectClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->finalize = complex_object_finalize;
  object_class->set_property = complex_object_set_property;
  object_class->get_property = complex_object_get_property;

  class->signal = complex_object_real_signal;

  complex_signals[COMPLEX_SIGNAL] =
    g_signal_new ("signal",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (ComplexObjectClass, signal),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  complex_signals[COMPLEX_SIGNAL_EMPTY] =
    g_signal_new ("signal-empty",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (ComplexObjectClass, signal_empty),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  complex_signals[COMPLEX_SIGNAL_GENERIC] =
    g_signal_new ("signal-generic",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (ComplexObjectClass, signal),
		  NULL, NULL,
		  NULL,
		  G_TYPE_NONE, 0);
  complex_signals[COMPLEX_SIGNAL_GENERIC_EMPTY] =
    g_signal_new ("signal-generic-empty",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (ComplexObjectClass, signal_empty),
		  NULL, NULL,
		  NULL,
		  G_TYPE_NONE, 0);

  complex_signals[COMPLEX_SIGNAL_ARGS] =
    g_signal_new ("signal-args",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (ComplexObjectClass, signal),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__UINT_POINTER,
                  G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_POINTER);

  g_object_class_install_property (object_class,
				   PROP_VAL1,
				   g_param_spec_int ("val1",
 						     "val1",
 						     "val1",
 						     0,
 						     G_MAXINT,
 						     42,
 						     G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_VAL2,
				   g_param_spec_int ("val2",
 						     "val2",
 						     "val2",
 						     0,
 						     G_MAXINT,
 						     43,
 						     G_PARAM_READWRITE));


}

static void
complex_object_iface_method (TestIface *obj)
{
  ComplexObject *complex = COMPLEX_OBJECT (obj);
  complex->val1++;
}

static void
complex_test_iface_init (gpointer         g_iface,
			 gpointer         iface_data)
{
  TestIfaceClass *iface = g_iface;
  iface->method = complex_object_iface_method;
}

static void
complex_object_init (ComplexObject *complex_object)
{
  complex_object->val2 = 43;
}

/*************************************************************
 * Test object construction performance
 *************************************************************/

#define NUM_OBJECT_TO_CONSTRUCT 10000

struct ConstructionTest {
  GObject **objects;
  int n_objects;
  GType type;
};

static gpointer
test_construction_setup (PerformanceTest *test)
{
  struct ConstructionTest *data;

  data = g_new0 (struct ConstructionTest, 1);
  data->type = ((GType (*)(void))test->extra_data)();

  return data;
}

static void
test_construction_init (PerformanceTest *test,
			gpointer _data,
			double count_factor)
{
  struct ConstructionTest *data = _data;
  int n;

  n = NUM_OBJECT_TO_CONSTRUCT * count_factor;
  if (data->n_objects != n)
    {
      data->n_objects = n;
      data->objects = g_new (GObject *, n);
    }
}

static void
test_construction_run (PerformanceTest *test,
		       gpointer _data)
{
  struct ConstructionTest *data = _data;
  GObject **objects = data->objects;
  GType type = data->type;
  int i, n_objects;

  n_objects = data->n_objects;
  for (i = 0; i < n_objects; i++)
    objects[i] = g_object_new (type, NULL);
}

static void
test_construction_finish (PerformanceTest *test,
			  gpointer _data)
{
  struct ConstructionTest *data = _data;
  int i;

  for (i = 0; i < data->n_objects; i++)
    g_object_unref (data->objects[i]);
}

static void
test_construction_teardown (PerformanceTest *test,
			    gpointer _data)
{
  struct ConstructionTest *data = _data;
  g_free (data->objects);
  g_free (data);
}

static void
test_construction_print_result (PerformanceTest *test,
				gpointer _data,
				double time)
{
  struct ConstructionTest *data = _data;

  g_print ("Number of constructed objects per second: %.0f\n",
	   data->n_objects / time);
}

/*************************************************************
 * Test runtime type check performance
 *************************************************************/

#define NUM_KILO_CHECKS_PER_ROUND 50

struct TypeCheckTest {
  GObject *object;
  int n_checks;
};

static gpointer
test_type_check_setup (PerformanceTest *test)
{
  struct TypeCheckTest *data;

  data = g_new0 (struct TypeCheckTest, 1);
  data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);

  return data;
}

static void
test_type_check_init (PerformanceTest *test,
		      gpointer _data,
		      double factor)
{
  struct TypeCheckTest *data = _data;

  data->n_checks = factor * NUM_KILO_CHECKS_PER_ROUND;
}


/* Work around g_type_check_instance_is_a being marked "pure",
   and thus only called once for the loop. */
gboolean (*my_type_check_instance_is_a) (GTypeInstance *type_instance,
					 GType          iface_type) = &g_type_check_instance_is_a;

static void
test_type_check_run (PerformanceTest *test,
		     gpointer _data)
{
  struct TypeCheckTest *data = _data;
  volatile GObject *object = data->object;
  volatile GType type, types[5];
  int i, j;

  types[0] = test_iface1_get_type ();
  types[1] = test_iface2_get_type ();
  types[2] = test_iface3_get_type ();
  types[3] = test_iface4_get_type ();
  types[4] = test_iface5_get_type ();

  for (i = 0; i < data->n_checks; i++)
    {
      type = types[i%5];
      for (j = 0; j < 1000; j++)
	{
	  my_type_check_instance_is_a ((GTypeInstance *)object,
				       type);
	}
    }
}

static void
test_type_check_finish (PerformanceTest *test,
			gpointer data)
{
}

static void
test_type_check_print_result (PerformanceTest *test,
			      gpointer _data,
			      double time)
{
  struct TypeCheckTest *data = _data;
  g_print ("Million type checks per second: %.2f\n",
	   data->n_checks / (1000*time));
}

static void
test_type_check_teardown (PerformanceTest *test,
			  gpointer _data)
{
  struct TypeCheckTest *data = _data;

  g_object_unref (data->object);
  g_free (data);
}

/*************************************************************
 * Test signal emissions performance (common code)
 *************************************************************/

#define NUM_EMISSIONS_PER_ROUND 10000

struct EmissionTest {
  GObject *object;
  int n_checks;
  int signal_id;
};

static void
test_emission_run (PerformanceTest *test,
                             gpointer _data)
{
  struct EmissionTest *data = _data;
  GObject *object = data->object;
  int i;

  for (i = 0; i < data->n_checks; i++)
    g_signal_emit (object, data->signal_id, 0);
}

static void
test_emission_run_args (PerformanceTest *test,
                        gpointer _data)
{
  struct EmissionTest *data = _data;
  GObject *object = data->object;
  int i;

  for (i = 0; i < data->n_checks; i++)
    g_signal_emit (object, data->signal_id, 0, 0, NULL);
}

/*************************************************************
 * Test signal unhandled emissions performance
 *************************************************************/

static gpointer
test_emission_unhandled_setup (PerformanceTest *test)
{
  struct EmissionTest *data;

  data = g_new0 (struct EmissionTest, 1);
  data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
  data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
  return data;
}

static void
test_emission_unhandled_init (PerformanceTest *test,
                              gpointer _data,
                              double factor)
{
  struct EmissionTest *data = _data;

  data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
}

static void
test_emission_unhandled_finish (PerformanceTest *test,
                                gpointer data)
{
}

static void
test_emission_unhandled_print_result (PerformanceTest *test,
                                      gpointer _data,
                                      double time)
{
  struct EmissionTest *data = _data;

  g_print ("Emissions per second: %.0f\n",
	   data->n_checks / time);
}

static void
test_emission_unhandled_teardown (PerformanceTest *test,
                                  gpointer _data)
{
  struct EmissionTest *data = _data;

  g_object_unref (data->object);
  g_free (data);
}

/*************************************************************
 * Test signal handled emissions performance
 *************************************************************/

static void
test_emission_handled_handler (ComplexObject *obj, gpointer data)
{
}

static gpointer
test_emission_handled_setup (PerformanceTest *test)
{
  struct EmissionTest *data;

  data = g_new0 (struct EmissionTest, 1);
  data->object = g_object_new (COMPLEX_TYPE_OBJECT, NULL);
  data->signal_id = complex_signals[GPOINTER_TO_INT (test->extra_data)];
  g_signal_connect (data->object, "signal",
                    G_CALLBACK (test_emission_handled_handler),
                    NULL);
  g_signal_connect (data->object, "signal-empty",
                    G_CALLBACK (test_emission_handled_handler),
                    NULL);
  g_signal_connect (data->object, "signal-generic",
                    G_CALLBACK (test_emission_handled_handler),
                    NULL);
  g_signal_connect (data->object, "signal-generic-empty",
                    G_CALLBACK (test_emission_handled_handler),
                    NULL);
  g_signal_connect (data->object, "signal-args",
                    G_CALLBACK (test_emission_handled_handler),
                    NULL);

  return data;
}

static void
test_emission_handled_init (PerformanceTest *test,
                            gpointer _data,
                            double factor)
{
  struct EmissionTest *data = _data;

  data->n_checks = factor * NUM_EMISSIONS_PER_ROUND;
}

static void
test_emission_handled_finish (PerformanceTest *test,
                              gpointer data)
{
}

static void
test_emission_handled_print_result (PerformanceTest *test,
                                    gpointer _data,
                                    double time)
{
  struct EmissionTest *data = _data;

  g_print ("Emissions per second: %.0f\n",
	   data->n_checks / time);
}

static void
test_emission_handled_teardown (PerformanceTest *test,
                                gpointer _data)
{
  struct EmissionTest *data = _data;

  g_object_unref (data->object);
  g_free (data);
}

/*************************************************************
 * Main test code
 *************************************************************/

static PerformanceTest tests[] = {
  {
    "simple-construction",
    simple_object_get_type,
    test_construction_setup,
    test_construction_init,
    test_construction_run,
    test_construction_finish,
    test_construction_teardown,
    test_construction_print_result
  },
  {
    "complex-construction",
    complex_object_get_type,
    test_construction_setup,
    test_construction_init,
    test_construction_run,
    test_construction_finish,
    test_construction_teardown,
    test_construction_print_result
  },
  {
    "type-check",
    NULL,
    test_type_check_setup,
    test_type_check_init,
    test_type_check_run,
    test_type_check_finish,
    test_type_check_teardown,
    test_type_check_print_result
  },
  {
    "emit-unhandled",
    GINT_TO_POINTER (COMPLEX_SIGNAL),
    test_emission_unhandled_setup,
    test_emission_unhandled_init,
    test_emission_run,
    test_emission_unhandled_finish,
    test_emission_unhandled_teardown,
    test_emission_unhandled_print_result
  },
  {
    "emit-unhandled-empty",
    GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
    test_emission_unhandled_setup,
    test_emission_unhandled_init,
    test_emission_run,
    test_emission_unhandled_finish,
    test_emission_unhandled_teardown,
    test_emission_unhandled_print_result
  },
  {
    "emit-unhandled-generic",
    GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
    test_emission_unhandled_setup,
    test_emission_unhandled_init,
    test_emission_run,
    test_emission_unhandled_finish,
    test_emission_unhandled_teardown,
    test_emission_unhandled_print_result
  },
  {
    "emit-unhandled-generic-empty",
    GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
    test_emission_unhandled_setup,
    test_emission_unhandled_init,
    test_emission_run,
    test_emission_unhandled_finish,
    test_emission_unhandled_teardown,
    test_emission_unhandled_print_result
  },
  {
    "emit-unhandled-args",
    GINT_TO_POINTER (COMPLEX_SIGNAL_ARGS),
    test_emission_unhandled_setup,
    test_emission_unhandled_init,
    test_emission_run_args,
    test_emission_unhandled_finish,
    test_emission_unhandled_teardown,
    test_emission_unhandled_print_result
  },
  {
    "emit-handled",
    GINT_TO_POINTER (COMPLEX_SIGNAL),
    test_emission_handled_setup,
    test_emission_handled_init,
    test_emission_run,
    test_emission_handled_finish,
    test_emission_handled_teardown,
    test_emission_handled_print_result
  },
  {
    "emit-handled-empty",
    GINT_TO_POINTER (COMPLEX_SIGNAL_EMPTY),
    test_emission_handled_setup,
    test_emission_handled_init,
    test_emission_run,
    test_emission_handled_finish,
    test_emission_handled_teardown,
    test_emission_handled_print_result
  },
  {
    "emit-handled-generic",
    GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC),
    test_emission_handled_setup,
    test_emission_handled_init,
    test_emission_run,
    test_emission_handled_finish,
    test_emission_handled_teardown,
    test_emission_handled_print_result
  },
  {
    "emit-handled-generic-empty",
    GINT_TO_POINTER (COMPLEX_SIGNAL_GENERIC_EMPTY),
    test_emission_handled_setup,
    test_emission_handled_init,
    test_emission_run,
    test_emission_handled_finish,
    test_emission_handled_teardown,
    test_emission_handled_print_result
  },
  {
    "emit-handled-args",
    GINT_TO_POINTER (COMPLEX_SIGNAL_ARGS),
    test_emission_handled_setup,
    test_emission_handled_init,
    test_emission_run_args,
    test_emission_handled_finish,
    test_emission_handled_teardown,
    test_emission_handled_print_result
  }
};

static 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[])
{
  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 (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;
}