forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.c
175 lines (151 loc) · 4.45 KB
/
storage.c
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
/**
* @file
*
* @brief Benchmark for storage plugins
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <stdio.h>
#include <stdlib.h>
#include <benchmarks.h>
#include <tests.h>
#define CSV_STR_FMT "%s;%s;%d\n"
#define NUM_PLUGINS 4
#define NUM_RUNS 7
KeySet * modules[NUM_PLUGINS];
Plugin * plugins[NUM_PLUGINS];
char * pluginNames[NUM_PLUGINS] = { "dump", "mmapstorage_crc", "mmapstorage", "quickdump" };
static void benchmarkDel (void)
{
ksDel (large);
}
static int benchmarkOpenPlugins (void)
{
for (size_t i = 0; i < NUM_PLUGINS; ++i)
{
modules[i] = ksNew (0, KS_END);
elektraModulesInit (modules[i], 0);
KeySet * conf = ksNew (0, KS_END);
Key * errorKey = keyNew ("/", KEY_END);
Plugin * plugin = elektraPluginOpen (pluginNames[i], modules[i], conf, errorKey);
const Key * metaWarnings = keyGetMeta (errorKey, "warnings");
if (metaWarnings) printf ("There are warnings for plugin: %s\n", pluginNames[i]);
const Key * metaError = keyGetMeta (errorKey, "error");
if (metaError) printf ("There are errors for plugin: %s\n", pluginNames[i]);
if (plugin == 0)
{
printf ("Could not open plugin: %s\n", pluginNames[i]);
return -1;
}
plugins[i] = plugin;
keyDel (errorKey);
}
return 0;
}
static void benchmarkIterate (KeySet * ks)
{
elektraCursor it;
ssize_t ksSize = ksGetSize (ks);
for (it = 0; it < ksSize; ++it)
{
ksAtCursor (ks, it);
__asm__ ("");
}
}
static int benchmarkIterateName (KeySet * ks)
{
const char * test = "foo";
int i = 0;
Key * cur;
elektraCursor it;
ssize_t ksSize = ksGetSize (ks);
for (it = 0; it < ksSize; ++it)
{
cur = ksAtCursor (ks, it);
i = strncmp (test, keyName (cur), 3);
}
return i;
}
static int benchmarkIterateValue (KeySet * ks)
{
const char * test = "bar";
int i = 0;
Key * cur;
elektraCursor it;
ssize_t ksSize = ksGetSize (ks);
for (it = 0; it < ksSize; ++it)
{
cur = ksAtCursor (ks, it);
i = strncmp (test, keyString (cur), 3);
}
return i;
}
int main (int argc, char ** argv)
{
// open all storage plugins
if (benchmarkOpenPlugins () == -1) return -1;
benchmarkCreate ();
benchmarkFillup ();
fprintf (stdout, "%s;%s;%s\n", "plugin", "operation", "microseconds");
for (size_t i = 0; i < NUM_PLUGINS; ++i)
{
init (argc, argv);
Plugin * plugin = plugins[i];
Key * parentKey = keyNew ("user:/benchmarks/storage", KEY_VALUE, tmpfilename, KEY_END);
for (size_t run = 0; run < NUM_RUNS; ++run)
{
timeInit ();
if (plugin->kdbSet (plugin, large, parentKey) != ELEKTRA_PLUGIN_STATUS_SUCCESS)
{
printf ("Error writing with plugin: %s\n", pluginNames[i]);
return -1;
}
fprintf (stdout, CSV_STR_FMT, pluginNames[i], "write keyset", timeGetDiffMicroseconds ());
KeySet * returned = ksNew (0, KS_END);
if (plugin->kdbGet (plugin, returned, parentKey) != ELEKTRA_PLUGIN_STATUS_SUCCESS)
{
printf ("Error reading with plugin: %s\n", pluginNames[i]);
return -1;
}
fprintf (stdout, CSV_STR_FMT, pluginNames[i], "read keyset", timeGetDiffMicroseconds ());
benchmarkIterate (returned);
fprintf (stdout, CSV_STR_FMT, pluginNames[i], "iterate keyset", timeGetDiffMicroseconds ());
ksDel (returned);
fprintf (stdout, CSV_STR_FMT, pluginNames[i], "delete keyset", timeGetDiffMicroseconds ());
KeySet * returned2 = ksNew (0, KS_END);
if (plugin->kdbGet (plugin, returned2, parentKey) != ELEKTRA_PLUGIN_STATUS_SUCCESS)
{
printf ("Error reading with plugin: %s\n", pluginNames[i]);
return -1;
}
fprintf (stdout, CSV_STR_FMT, pluginNames[i], "re-read keyset", timeGetDiffMicroseconds ());
ksDel (returned2);
timeInit ();
KeySet * returned3 = ksNew (0, KS_END);
if (plugin->kdbGet (plugin, returned3, parentKey) != ELEKTRA_PLUGIN_STATUS_SUCCESS)
{
printf ("Error reading with plugin: %s\n", pluginNames[i]);
return -1;
}
timeInit ();
benchmarkIterateName (returned3);
fprintf (stdout, CSV_STR_FMT, pluginNames[i], "strcmp key name", timeGetDiffMicroseconds ());
ksDel (returned3);
timeInit ();
KeySet * returned4 = ksNew (0, KS_END);
if (plugin->kdbGet (plugin, returned4, parentKey) != ELEKTRA_PLUGIN_STATUS_SUCCESS)
{
printf ("Error reading with plugin: %s\n", pluginNames[i]);
return -1;
}
timeInit ();
benchmarkIterateValue (returned4);
fprintf (stdout, CSV_STR_FMT, pluginNames[i], "strcmp key value", timeGetDiffMicroseconds ());
ksDel (returned4);
timeInit ();
}
clean_temp_home ();
keyDel (parentKey);
}
benchmarkDel ();
}