~ubuntu-branches/ubuntu/saucy/indicator-appmenu/saucy-updates

« back to all changes in this revision

Viewing changes to tests/hud-performance.c

  • Committer: Package Import Robot
  • Author(s): Automatic PS uploader, Mathieu Trudel-Lapierre, Automatic PS uploader
  • Date: 2013-02-20 09:41:54 UTC
  • mfrom: (1.1.40)
  • Revision ID: package-import@ubuntu.com-20130220094154-zrxsx0vgay436wyt
Tags: 13.01.0daily13.02.20-0ubuntu1
[ Mathieu Trudel-Lapierre ]
* Artificially bump upstream major version to please hud.

[ Automatic PS uploader ]
* Automatic snapshot from revision 234

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2012 Canonical Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU General Public License version 3, as
6
 
 * published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
 
 * PURPOSE.  See the GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License along
14
 
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Author: Ryan Lortie <desrt@desrt.ca>
17
 
 */
18
 
 
19
 
#include "hudsettings.h"
20
 
#include "hudquery.h"
21
 
#include "hudtoken.h"
22
 
#include "hudsource.h"
23
 
 
24
 
#include <glib-object.h>
25
 
 
26
 
#include "word-list.h"
27
 
 
28
 
/* Max nested depth of menu items */
29
 
#define MAX_DEPTH 6
30
 
 
31
 
/* Max number of items per submenu */
32
 
#define MAX_ITEMS 20
33
 
 
34
 
/* Max number of words per label.
35
 
 * NB: keep MAX_WORDS * MAX_DEPTH under 32
36
 
 */
37
 
#define MAX_WORDS 4
38
 
 
39
 
/* Longest word in the word-list (upper bound) */
40
 
#define MAX_LETTERS 20
41
 
 
42
 
/* hardcode some parameters for reasons of determinism.
43
 
 */
44
 
HudSettings hud_settings = {
45
 
  .indicator_penalty = 50,
46
 
  .add_penalty = 10,
47
 
  .drop_penalty = 10,
48
 
  .end_drop_penalty = 1,
49
 
  .swap_penalty = 15,
50
 
  .max_distance = 30
51
 
};
52
 
 
53
 
typedef struct
54
 
{
55
 
  GObject object;
56
 
 
57
 
  GHashTable *items;
58
 
} RandomSource;
59
 
 
60
 
typedef GObjectClass RandomSourceClass;
61
 
 
62
 
static void random_source_iface_init (HudSourceInterface *iface);
63
 
G_DEFINE_TYPE_WITH_CODE (RandomSource, random_source, G_TYPE_OBJECT,
64
 
                         G_IMPLEMENT_INTERFACE (HUD_TYPE_SOURCE, random_source_iface_init))
65
 
 
66
 
static void
67
 
random_source_search (HudSource    *hud_source,
68
 
                      GPtrArray    *results_array,
69
 
                      HudTokenList *search_tokens)
70
 
{
71
 
  RandomSource *source = (RandomSource *) hud_source;
72
 
  GHashTableIter iter;
73
 
  gpointer item;
74
 
 
75
 
  g_hash_table_iter_init (&iter, source->items);
76
 
  while (g_hash_table_iter_next (&iter, &item, NULL))
77
 
    {
78
 
      HudResult *result;
79
 
 
80
 
      result = hud_result_get_if_matched (item, search_tokens, 0);
81
 
      if (result)
82
 
        g_ptr_array_add (results_array, result);
83
 
    }
84
 
}
85
 
 
86
 
static void
87
 
random_source_ignore_use (HudSource *source)
88
 
{
89
 
}
90
 
 
91
 
static gchar *
92
 
make_word (GRand *rand,
93
 
           gchar *buffer)
94
 
{
95
 
  const gchar *word;
96
 
  gint choice;
97
 
  gint len;
98
 
 
99
 
  choice = g_rand_int_range (rand, 0, G_N_ELEMENTS (word_list));
100
 
  word = word_list[choice];
101
 
 
102
 
  while (*word)
103
 
    *buffer++ = *word++;
104
 
 
105
 
  return buffer;
106
 
}
107
 
 
108
 
static gchar *
109
 
make_words (GRand *rand,
110
 
            gint   n_words)
111
 
{
112
 
  gchar *buffer;
113
 
  gchar *ptr;
114
 
  gint i;
115
 
 
116
 
  buffer = g_malloc ((MAX_LETTERS + 1) * n_words);
117
 
 
118
 
  ptr = buffer;
119
 
  for (i = 0; i < n_words; i++)
120
 
    {
121
 
      if (i)
122
 
        *ptr++ = ' ';
123
 
 
124
 
      ptr = make_word (rand, ptr);
125
 
    }
126
 
 
127
 
  *ptr = '\0';
128
 
 
129
 
  return buffer;
130
 
}
131
 
 
132
 
static HudStringList *
133
 
random_source_make_name (GRand         *rand,
134
 
                         HudStringList *context)
135
 
{
136
 
  HudStringList *name;
137
 
  gchar *label;
138
 
 
139
 
  label = make_words (rand, g_rand_int_range (rand, 1, MAX_WORDS + 1));
140
 
  name = hud_string_list_cons (label, context);
141
 
  g_free (label);
142
 
 
143
 
  return name;
144
 
}
145
 
 
146
 
static void
147
 
random_source_populate_table (GRand         *rand,
148
 
                              GHashTable    *items,
149
 
                              HudStringList *context,
150
 
                              gint           depth)
151
 
{
152
 
  gint n_items;
153
 
  gint i;
154
 
 
155
 
  n_items = g_rand_int_range (rand, 1, MAX_ITEMS + 1);
156
 
 
157
 
  for (i = 0; i < n_items; i++)
158
 
    {
159
 
      HudStringList *name;
160
 
      gboolean is_submenu;
161
 
      HudItem *item;
162
 
 
163
 
      name = random_source_make_name (rand, context);
164
 
 
165
 
      if (depth != MAX_DEPTH)
166
 
        /* Decrease the chances of a particular item being a submenu as we
167
 
         * go deeper into the menu structure.
168
 
         */
169
 
        is_submenu = g_rand_int_range (rand, 0, depth + 1) == 0;
170
 
      else
171
 
        /* At the maximum depth, prevent any items from being submenus. */
172
 
        is_submenu = FALSE;
173
 
 
174
 
      item = hud_item_new (name, NULL, NULL, !is_submenu);
175
 
      g_hash_table_add (items, item);
176
 
 
177
 
      if (is_submenu)
178
 
        random_source_populate_table (rand, items, name, depth + 1);
179
 
 
180
 
      hud_string_list_unref (name);
181
 
    }
182
 
}
183
 
 
184
 
static void
185
 
random_source_finalize (GObject *object)
186
 
{
187
 
  RandomSource *source = (RandomSource *) object;
188
 
 
189
 
  g_hash_table_unref (source->items);
190
 
 
191
 
  G_OBJECT_CLASS (random_source_parent_class)
192
 
    ->finalize (object);
193
 
}
194
 
 
195
 
static void
196
 
random_source_init (RandomSource *source)
197
 
{
198
 
  source->items = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
199
 
}
200
 
 
201
 
static void
202
 
random_source_iface_init (HudSourceInterface *iface)
203
 
{
204
 
  iface->use = random_source_ignore_use;
205
 
  iface->unuse = random_source_ignore_use;
206
 
  iface->search = random_source_search;
207
 
}
208
 
 
209
 
static void
210
 
random_source_class_init (RandomSourceClass *class)
211
 
{
212
 
  class->finalize = random_source_finalize;
213
 
}
214
 
 
215
 
static HudSource *
216
 
random_source_new (GRand *rand)
217
 
{
218
 
  RandomSource *source;
219
 
 
220
 
  source = g_object_new (random_source_get_type (), NULL);
221
 
  random_source_populate_table (rand, source->items, NULL, 0);
222
 
 
223
 
  return HUD_SOURCE (source);
224
 
}
225
 
 
226
 
void
227
 
test_query_performance (void)
228
 
{
229
 
  HudSource *source;
230
 
  HudQuery *query;
231
 
  GRand *rand;
232
 
  gint i;
233
 
 
234
 
  rand = g_rand_new_with_seed (1234);
235
 
  source = random_source_new (rand);
236
 
 
237
 
  for (i = 1; i <= 6; i++)
238
 
    {
239
 
      guint64 start_time;
240
 
      gchar *search;
241
 
      gint j;
242
 
 
243
 
      g_print ("\n");
244
 
 
245
 
      search = make_words (rand, i);
246
 
 
247
 
      /* simulate the user typing it in, one character at a time */
248
 
      for (j = 1; search[j - 1]; j++)
249
 
        {
250
 
          gchar *part_search = g_strndup (search, j);
251
 
 
252
 
          start_time = g_get_monotonic_time ();
253
 
          query = hud_query_new (source, part_search, 1u<<30);
254
 
          g_print ("%-60s: %dus (%d hits)\n", part_search,
255
 
                   (int) (g_get_monotonic_time () - start_time),
256
 
                   hud_query_get_n_results (query));
257
 
          hud_query_close (query);
258
 
          g_object_unref (query);
259
 
          g_free (part_search);
260
 
        }
261
 
 
262
 
      g_free (search);
263
 
    }
264
 
 
265
 
  g_object_unref (source);
266
 
  g_rand_free (rand);
267
 
}
268
 
 
269
 
int
270
 
main (int argc, char **argv)
271
 
{
272
 
  g_test_init (&argc, &argv, NULL);
273
 
 
274
 
  if (g_test_perf ())
275
 
    g_test_add_func ("/hud/query-performance", test_query_performance);
276
 
 
277
 
  return g_test_run ();
278
 
}