summaryrefslogtreecommitdiff
path: root/tests/dbus1.c
blob: 122016005db1abe340a8931aced64554d242f3e5 (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
/**
 * Copyright © 2010 Canonical Limited
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the licence, or (at
 * your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 **/

#include <dconf-dbus-1.h>

#include <stdbool.h>
#include <unistd.h>
#include <string.h>

static DConfDBusClient *backend;

static void
free_variant (gpointer data)
{
  if (data != NULL)
    g_variant_unref (data);
}

static GVariant *
do_read (const gchar *key)
{
  return dconf_dbus_client_read (backend, key);
}

static gboolean
do_write (const gchar *key,
          GVariant    *value)
{
  return dconf_dbus_client_write (backend, key, value);
}

static gboolean
do_write_tree (GTree *tree)
{
  g_assert_not_reached ();
}

static void
do_sync (void)
{
/*  g_assert_not_reached (); */
}

#define RANDOM_ELEMENT(array) \
  array[g_test_rand_int_range(0, G_N_ELEMENTS(array))]

static gchar *
random_key (void)
{
  const gchar * const words[] = {
    "alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf",
    "hotel", "india", "juliet", "kilo", "lima", "mike", "november",
    "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform",
    "victor", "whiskey", "xray", "yankee", "zulu"
  };
  const gchar *parts[8];
  gint n, i;

  n = g_test_rand_int_range (2, 8);
  parts[0] = "";
  for (i = 1; i < n; i++)
    parts[i] = RANDOM_ELEMENT (words);
  parts[n] = NULL;

  return g_strjoinv ("/", (gchar **) parts);
}

static GVariant *
random_value (void)
{
  switch (g_test_rand_int_range (0, 3))
    {
    case 0:
      return g_variant_new_int32 (g_test_rand_int ());

    case 1:
      return g_variant_new_boolean (g_test_rand_bit ());

    case 2:
      {
        gint length = g_test_rand_int_range (0, 24);
        gchar buffer[24];
        gint i;

        for (i = 0; i < length; i++)
          buffer[i] = 'a' + g_test_rand_int_range (0, 26);
        buffer[i] = '\0';

        return g_variant_new_string (buffer);
      }

    default:
      g_assert_not_reached ();
    }
}

static GTree *
random_tree (void)
{
  GTree *tree;
  gint n;

  tree = g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
                          g_free, free_variant);
  n = g_test_rand_int_range (1, 20);

  while (n--)
    g_tree_insert (tree, random_key (), g_variant_ref_sink (random_value ()));

  return tree;
}

static void
apply_change (GHashTable  *table,
              const gchar *key,
              GVariant    *value)
{
  if (value)
    g_hash_table_insert (table, g_strdup (key), g_variant_ref_sink (value));
  else
    g_hash_table_insert (table, g_strdup (key), NULL);
}

static gboolean
apply_one_change (gpointer key,
                  gpointer value,
                  gpointer user_data)
{
  apply_change (user_data, key, value);
  return FALSE;
}

static void
apply_change_tree (GHashTable *table,
                   GTree      *tree)
{
  g_tree_foreach (tree, apply_one_change, table);
}

static GHashTable *implicit;
static GHashTable *explicit;

static void
watch_func (DConfDBusClient *client,
            const gchar     *key,
            gpointer         user_data)
{
  GVariant *value;

  /* ensure that we see no dupes from the bus */
/*  g_assert (origin_tag == do_write); */
  g_assert (client == backend);

  value = do_read (key);
  apply_change (implicit, key, value);
  g_variant_unref (value);
}

static void
setup (void)
{
  gchar *file;

  file = g_build_filename (g_get_user_config_dir (),
                           "dconf/test", NULL);
  unlink (file);
  g_free (file);

  g_setenv ("DCONF_PROFILE", "test", false);

  backend = dconf_dbus_client_new ("test", NULL, NULL);
  dconf_dbus_client_subscribe (backend, "/", watch_func, NULL);

  implicit = g_hash_table_new_full (g_str_hash, g_str_equal,
                                    g_free, free_variant);
  explicit = g_hash_table_new_full (g_str_hash, g_str_equal,
                                    g_free, free_variant);

  sleep(1);
}

static void
make_random_change (void)
{
  if (1)
    {
      GVariant *value;
      gchar *key;

      key = random_key ();
      value = random_value ();
      apply_change (explicit, key, value);
      do_write (key, value);

      g_free (key);
    }
  else
    {
      GTree *tree;

      tree = random_tree ();
      apply_change_tree (explicit, tree);
      do_write_tree (tree);

      g_tree_unref (tree);
    }
}

guint64 dconf_time;
guint64 ghash_time;
guint64 lookups;
gboolean dots;

static void
verify_consistency (void)
{
  GHashTableIter iter;
  gpointer key, value;

  if (dots)
    g_print (".");
  else
    g_print ("(%d)", g_hash_table_size (explicit));

  g_assert (g_hash_table_size (explicit) == g_hash_table_size (implicit));
  g_hash_table_iter_init (&iter, explicit);
  while (g_hash_table_iter_next (&iter, &key, &value))
    {
      if (value)
        {
          GVariant *other;

          ghash_time -= g_get_monotonic_time ();
          other = g_hash_table_lookup (implicit, key);
          ghash_time += g_get_monotonic_time ();
          g_assert (g_variant_equal (value, other));


          dconf_time -= g_get_monotonic_time ();
          other = do_read (key);
          dconf_time += g_get_monotonic_time ();
          g_assert (g_variant_equal (value, other));
          g_variant_unref (other);
        }
      else
        {
          g_assert (g_hash_table_lookup (implicit, key) == NULL);
          g_assert (do_read (key) == NULL);
        }

      lookups++;
    }
}

#if 0
static void
dump_table (void)
{
  GHashTableIter iter;
  gpointer key, value;

  g_print ("{");
  g_hash_table_iter_init (&iter, explicit);
  while (g_hash_table_iter_next (&iter, &key, &value))
    if (value)
      {
        gchar *printed;

        if (value)
          printed = g_variant_print (value, FALSE);
        else
          printed = g_strdup ("None");

        g_print ("'%s': %s, ", (gchar *) key, printed);
        g_free (printed);
      }
  g_print ("}");
}
#endif

static void
test (void)
{
  int i;

  g_print ("Testing dconf...");
  for (i = 0; i < 1000; i++)
    {
      g_print (" %d", i);
      make_random_change ();
      verify_consistency ();
    }

  g_print ("\n");
  g_print ("GSettings lookup time:  %f µs/lookup\n",
           ((double) dconf_time / lookups));
  g_print ("GHashTable lookup time: %f µs/lookup\n",
           ((double) ghash_time / lookups));

  dconf_time = 0;
  ghash_time = 0;
  lookups = 0;

  g_print ("\nWaiting for dconf-service to catch up...");
  do_sync ();
  g_print (" done.\n");

  g_print ("Measuring dconf read performance...");
  dots = TRUE;
  for (i = 0; i < 1000; i++)
    verify_consistency ();
  g_print ("\n");

  g_print ("dconf lookup time:      %f µs/lookup\n",
           ((double) dconf_time / lookups));
  g_print ("GHashTable lookup time: %f µs/lookup\n",
           ((double) ghash_time / lookups));

  g_hash_table_unref (explicit);
  g_hash_table_unref (implicit);
}

int
main (int argc, char **argv)
{
  g_test_init (&argc, &argv, NULL);

  setup ();

  test ();

  return g_test_run ();
}