trivial: Add a self test to test the quirk database performance

This commit is contained in:
Richard Hughes 2018-08-28 20:42:13 +01:00
parent 570f839483
commit 4937ff5499
3 changed files with 73 additions and 21 deletions

View File

@ -98,7 +98,6 @@ fu_quirks_build_prefixed_key (const gchar *group, const gchar *key)
guid = g_strdup (group + len);
else
guid = as_utils_guid_from_string (group + len);
g_debug ("using %s for %s", guid, group + len);
return g_strdup_printf ("%s/%s", guid, key);
}
}
@ -211,6 +210,42 @@ fu_quirks_merge_values (const gchar *old, const gchar *new)
return g_strjoinv (",", resv);
}
/**
* fu_quirks_add_value: (skip)
* @self: A #FuQuirks
* @group: group, e.g. `DeviceInstanceId=USB\VID_0BDA&PID_1100`
* @key: group, e.g. `Name`
* @value: group, e.g. `Unknown Device`
*
* Adds a value to the quirk database. Normally this is achieved by loading a
* quirk file using fu_quirks_load().
*
* Since: 1.1.2
**/
void
fu_quirks_add_value (FuQuirks *self, const gchar *group, const gchar *key, const gchar *value)
{
const gchar *value_old;
g_autofree gchar *key_prefixed = NULL;
g_autofree gchar *value_new = NULL;
/* does the key already exists in our hash */
key_prefixed = fu_quirks_build_prefixed_key (group, key);
value_old = g_hash_table_lookup (self->hash, key_prefixed);
if (value_old != NULL) {
g_debug ("already found %s=%s, merging with %s",
key_prefixed, value_old, value);
value_new = fu_quirks_merge_values (value_old, value);
} else {
value_new = g_strdup (value);
}
/* insert the new value */
g_hash_table_insert (self->hash,
g_steal_pointer (&key_prefixed),
g_steal_pointer (&value_new));
}
static gboolean
fu_quirks_add_quirks_from_filename (FuQuirks *self, const gchar *filename, GError **error)
{
@ -229,31 +264,12 @@ fu_quirks_add_quirks_from_filename (FuQuirks *self, const gchar *filename, GErro
if (keys == NULL)
return FALSE;
for (guint j = 0; keys[j] != NULL; j++) {
const gchar *value_old;
g_autofree gchar *key = NULL;
g_autofree gchar *value = NULL;
g_autofree gchar *value_new = NULL;
/* get value from keyfile */
value = g_key_file_get_value (kf, groups[i], keys[j], error);
if (value == NULL)
return FALSE;
key = fu_quirks_build_prefixed_key (groups[i], keys[j]);
/* does the key already exists in our hash */
value_old = g_hash_table_lookup (self->hash, key);
if (value_old != NULL) {
g_debug ("already found %s=%s, merging with %s",
key, value_old, value);
value_new = fu_quirks_merge_values (value_old, value);
} else {
value_new = g_steal_pointer (&value);
}
/* insert the new value */
g_hash_table_insert (self->hash,
g_steal_pointer (&key),
g_steal_pointer (&value_new));
fu_quirks_add_value (self, groups[i], keys[j], value);
}
}
g_debug ("now %u quirk entries", g_hash_table_size (self->hash));

View File

@ -27,6 +27,10 @@ const gchar *fu_quirks_lookup_by_guid (FuQuirks *self,
gchar *fu_quirks_lookup_by_guids (FuQuirks *self,
GPtrArray *guids,
const gchar *key);
void fu_quirks_add_value (FuQuirks *self,
const gchar *group,
const gchar *key,
const gchar *value);
/**
* FU_QUIRKS_PLUGIN:

View File

@ -1273,6 +1273,37 @@ fu_plugin_quirks_func (void)
g_assert_cmpstr (tmp, ==, "clever");
}
static void
fu_plugin_quirks_performance_func (void)
{
g_autoptr(FuQuirks) quirks = fu_quirks_new ();
g_autoptr(GTimer) timer = g_timer_new ();
const gchar *keys[] = {
"Name", "Icon", "Children", "Plugin", "Flags",
"FirmwareSizeMin", "FirmwareSizeMax", NULL };
/* insert */
for (guint j = 0; j < 1000; j++) {
g_autofree gchar *group = NULL;
group = g_strdup_printf ("DeviceInstanceId=USB\\VID_0BDA&PID_%04X", j);
for (guint i = 0; keys[i] != NULL; i++)
fu_quirks_add_value (quirks, group, keys[i], "Value");
}
g_print ("insert=%.3fms ", g_timer_elapsed (timer, NULL) * 1000.f);
/* lookup */
g_timer_reset (timer);
for (guint j = 0; j < 1000; j++) {
g_autofree gchar *group = NULL;
group = g_strdup_printf ("DeviceInstanceId=USB\\VID_0BDA&PID_%04X", j);
for (guint i = 0; keys[i] != NULL; i++) {
const gchar *tmp = fu_quirks_lookup_by_id (quirks, group, keys[i]);
g_assert_cmpstr (tmp, ==, "Value");
}
}
g_print ("lookup=%.3fms ", g_timer_elapsed (timer, NULL) * 1000.f);
}
static void
fu_plugin_quirks_device_func (void)
{
@ -2400,6 +2431,7 @@ main (int argc, char **argv)
g_test_add_func ("/fwupd/plugin{delay}", fu_plugin_delay_func);
g_test_add_func ("/fwupd/plugin{module}", fu_plugin_module_func);
g_test_add_func ("/fwupd/plugin{quirks}", fu_plugin_quirks_func);
g_test_add_func ("/fwupd/plugin{quirks-performance}", fu_plugin_quirks_performance_func);
g_test_add_func ("/fwupd/plugin{quirks-device}", fu_plugin_quirks_device_func);
g_test_add_func ("/fwupd/keyring{gpg}", fu_keyring_gpg_func);
g_test_add_func ("/fwupd/keyring{pkcs7}", fu_keyring_pkcs7_func);