Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Benchmark for storage plugins
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <stdio.h>
10 : #include <stdlib.h>
11 :
12 : #include <benchmarks.h>
13 : #include <tests.h>
14 :
15 : #define CSV_STR_FMT "%s;%s;%d\n"
16 :
17 : #define NUM_PLUGINS 4
18 : #define NUM_RUNS 7
19 :
20 : KeySet * modules[NUM_PLUGINS];
21 : Plugin * plugins[NUM_PLUGINS];
22 : char * pluginNames[NUM_PLUGINS] = { "dump", "mmapstorage_crc", "mmapstorage", "quickdump" };
23 :
24 : char * tmpfilename;
25 :
26 : static void benchmarkDel (void)
27 : {
28 0 : ksDel (large);
29 : }
30 :
31 0 : static int benchmarkOpenPlugins (void)
32 : {
33 0 : for (size_t i = 0; i < NUM_PLUGINS; ++i)
34 : {
35 0 : modules[i] = ksNew (0, KS_END);
36 0 : elektraModulesInit (modules[i], 0);
37 0 : KeySet * conf = ksNew (0, KS_END);
38 0 : Key * errorKey = keyNew ("", KEY_END);
39 0 : Plugin * plugin = elektraPluginOpen (pluginNames[i], modules[i], conf, errorKey);
40 :
41 0 : const Key * metaWarnings = keyGetMeta (errorKey, "warnings");
42 0 : if (metaWarnings) printf ("There are warnings for plugin: %s\n", pluginNames[i]);
43 0 : const Key * metaError = keyGetMeta (errorKey, "error");
44 0 : if (metaError) printf ("There are errors for plugin: %s\n", pluginNames[i]);
45 :
46 0 : if (plugin == 0)
47 : {
48 0 : printf ("Could not open plugin: %s\n", pluginNames[i]);
49 0 : return -1;
50 : }
51 :
52 0 : plugins[i] = plugin;
53 0 : keyDel (errorKey);
54 : }
55 : return 0;
56 : }
57 :
58 0 : static void benchmarkIterate (KeySet * ks)
59 : {
60 0 : ksRewind (ks);
61 : Key * cur;
62 0 : while ((cur = ksNext (ks)))
63 : {
64 0 : __asm__("");
65 : }
66 0 : }
67 :
68 0 : static int benchmarkIterateName (KeySet * ks)
69 : {
70 0 : ksRewind (ks);
71 : Key * cur;
72 0 : const char * test = "foo";
73 0 : int i = 0;
74 0 : while ((cur = ksNext (ks)))
75 : {
76 0 : i = strncmp (test, keyName (cur), 3);
77 : }
78 0 : return i;
79 : }
80 :
81 0 : static int benchmarkIterateValue (KeySet * ks)
82 : {
83 0 : ksRewind (ks);
84 : Key * cur;
85 0 : const char * test = "bar";
86 0 : int i = 0;
87 0 : while ((cur = ksNext (ks)))
88 : {
89 0 : i = strncmp (test, keyString (cur), 3);
90 : }
91 0 : return i;
92 : }
93 :
94 0 : int main (int argc, char ** argv)
95 : {
96 : // open all storage plugins
97 0 : if (benchmarkOpenPlugins () == -1) return -1;
98 0 : benchmarkCreate ();
99 0 : benchmarkFillup ();
100 :
101 0 : fprintf (stdout, "%s;%s;%s\n", "plugin", "operation", "microseconds");
102 0 : for (size_t i = 0; i < NUM_PLUGINS; ++i)
103 : {
104 0 : init (argc, argv);
105 :
106 0 : Plugin * plugin = plugins[i];
107 0 : Key * parentKey = keyNew ("user/benchmarks/storage", KEY_VALUE, tmpfilename, KEY_END);
108 :
109 0 : for (size_t run = 0; run < NUM_RUNS; ++run)
110 : {
111 0 : timeInit ();
112 0 : if (plugin->kdbSet (plugin, large, parentKey) != ELEKTRA_PLUGIN_STATUS_SUCCESS)
113 : {
114 0 : printf ("Error writing with plugin: %s\n", pluginNames[i]);
115 0 : return -1;
116 : }
117 0 : fprintf (stdout, CSV_STR_FMT, pluginNames[i], "write keyset", timeGetDiffMicroseconds ());
118 :
119 0 : KeySet * returned = ksNew (0, KS_END);
120 0 : if (plugin->kdbGet (plugin, returned, parentKey) != ELEKTRA_PLUGIN_STATUS_SUCCESS)
121 : {
122 0 : printf ("Error reading with plugin: %s\n", pluginNames[i]);
123 0 : return -1;
124 : }
125 0 : fprintf (stdout, CSV_STR_FMT, pluginNames[i], "read keyset", timeGetDiffMicroseconds ());
126 0 : benchmarkIterate (returned);
127 0 : fprintf (stdout, CSV_STR_FMT, pluginNames[i], "iterate keyset", timeGetDiffMicroseconds ());
128 0 : ksDel (returned);
129 0 : fprintf (stdout, CSV_STR_FMT, pluginNames[i], "delete keyset", timeGetDiffMicroseconds ());
130 :
131 0 : KeySet * returned2 = ksNew (0, KS_END);
132 0 : if (plugin->kdbGet (plugin, returned2, parentKey) != ELEKTRA_PLUGIN_STATUS_SUCCESS)
133 : {
134 0 : printf ("Error reading with plugin: %s\n", pluginNames[i]);
135 0 : return -1;
136 : }
137 0 : fprintf (stdout, CSV_STR_FMT, pluginNames[i], "re-read keyset", timeGetDiffMicroseconds ());
138 0 : ksDel (returned2);
139 0 : timeInit ();
140 :
141 0 : KeySet * returned3 = ksNew (0, KS_END);
142 0 : if (plugin->kdbGet (plugin, returned3, parentKey) != ELEKTRA_PLUGIN_STATUS_SUCCESS)
143 : {
144 0 : printf ("Error reading with plugin: %s\n", pluginNames[i]);
145 0 : return -1;
146 : }
147 0 : timeInit ();
148 0 : benchmarkIterateName (returned3);
149 0 : fprintf (stdout, CSV_STR_FMT, pluginNames[i], "strcmp key name", timeGetDiffMicroseconds ());
150 0 : ksDel (returned3);
151 0 : timeInit ();
152 :
153 0 : KeySet * returned4 = ksNew (0, KS_END);
154 0 : if (plugin->kdbGet (plugin, returned4, parentKey) != ELEKTRA_PLUGIN_STATUS_SUCCESS)
155 : {
156 0 : printf ("Error reading with plugin: %s\n", pluginNames[i]);
157 0 : return -1;
158 : }
159 0 : timeInit ();
160 0 : benchmarkIterateValue (returned4);
161 0 : fprintf (stdout, CSV_STR_FMT, pluginNames[i], "strcmp key value", timeGetDiffMicroseconds ());
162 0 : ksDel (returned4);
163 0 : timeInit ();
164 : }
165 0 : clean_temp_home ();
166 0 : keyDel (parentKey);
167 : }
168 :
169 : benchmarkDel ();
170 : }
|