This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathchangetracking.c
201 lines (168 loc) · 4.1 KB
/
changetracking.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
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
#include <benchmarks.h>
#include <getopt.h>
#include <stdbool.h>
size_t keys = 5000;
char * keyNameFormat = "user:/test/sw/org/myapp/#0/current/section/subsection/key%zu";
char * keyValueFormat = "value%zu";
char * keyValueModifiedFormat = "value-modified%zu";
KeySet * (*keySetBuilder) (void) = NULL;
bool verbose = false;
bool harmonizeKeys = false;
/**
* @brief Power function.
*
* @param p basis
* @param q exponent
*
* @retval size_t p^q
*/
static size_t getPower (size_t p, size_t q)
{
size_t result = 1;
for (size_t t = 0; t < q; ++t)
{
result *= p;
}
return result;
}
/**
* binary tree
*/
static void shapefBinaryBranch (const size_t initSize, size_t size ELEKTRA_UNUSED, size_t level, int32_t * seed ELEKTRA_UNUSED,
KsShapeFunctionReturn * ret, void * data ELEKTRA_UNUSED)
{
size_t subKeys = 2;
ret->label = 0;
if (getPower (subKeys, level) > initSize)
{
ret->subKeys = 0;
}
else
{
ret->subKeys = subKeys;
}
}
static KeySet * buildBinaryTree (void)
{
KeySetShape shape;
shape.minWordLength = 1;
shape.maxWordLength = 1;
shape.special = 0;
shape.parent = 7;
shape.shapeInit = NULL;
shape.shapef = shapefBinaryBranch;
shape.shapeDel = NULL;
int32_t seed = 0xBEEF;
KeySet * generated = generateKeySet (keys, &seed, &shape);
KeySet * ks = ksNew (ksGetSize (generated), KS_END);
char buffer[2048] = "";
for (elektraCursor i = 0; i < ksGetSize (generated); i++)
{
Key * k = keyDup (ksAtCursor (generated, i), KEY_CP_ALL);
snprintf (buffer, 2047, "user:/test%s", keyName (k));
keySetName (k, buffer);
ksAppendKey (ks, k);
}
ksDel (generated);
return ks;
}
static KeySet * buildLinearTree (void)
{
KeySet * ks = ksNew (0, KS_END);
char nameBuffer[1024] = "";
char valueBuffer[1024] = "";
for (size_t i = 0; i < keys; i++)
{
snprintf (nameBuffer, 1023, keyNameFormat, i);
snprintf (valueBuffer, 1023, keyValueFormat, i);
ksAppendKey (ks, keyNew (nameBuffer, KEY_VALUE, valueBuffer, KEY_END));
}
return ks;
}
static void processCommandLineArguments (int argc, char ** argv)
{
struct option long_options[] = { { "key-count", required_argument, 0, 'c' },
{ "harmonize-names", no_argument, 0, 'h' },
{ "verbose", no_argument, 0, 'v' },
{ "binary-tree", no_argument, 0, 'b' },
{ 0, 0, 0, 0 } };
keySetBuilder = buildLinearTree;
while (1)
{
int option_index = 0;
int c = getopt_long (argc, argv, "", long_options, &option_index);
if (c == -1)
{
break;
}
switch (c)
{
case 'c':
keys = atoi (optarg);
break;
case 'v':
verbose = true;
break;
case 'h':
harmonizeKeys = true;
keyNameFormat = "user:/test/key%08zu";
keyValueFormat = "value%08zu";
keyValueModifiedFormat = "value-modified%08zu";
break;
case 'b':
keySetBuilder = buildBinaryTree;
break;
default:
break;
}
}
}
int main (int argc, char ** argv)
{
processCommandLineArguments (argc, argv);
if (verbose)
{
printf ("Number of keys: %zu\n", keys);
printf ("Harmonize key names: %s\n", harmonizeKeys ? "true" : "false");
}
Key * parentKey = keyNew ("user:/test", KEY_END);
KeySet * contract = ksNew (0, KS_END);
KDB * kdb = kdbOpen (contract, parentKey);
KeySet * ks = ksNew (0, KS_END);
kdbGet (kdb, ks, parentKey);
KeySet * generated = keySetBuilder ();
ksAppend (ks, generated);
ksDel (generated);
timeInit ();
kdbSet (kdb, ks, parentKey);
int insertingTime = timeGetDiffMicroseconds ();
kdbClose (kdb, parentKey);
ksDel (ks);
ks = ksNew (0, KS_END);
kdb = kdbOpen (contract, parentKey);
kdbGet (kdb, ks, parentKey);
size_t modified = 0;
for (size_t i = keys / 2; i < keys; i++)
{
char valueBuffer[1024] = "";
Key * key = ksAtCursor (ks, i);
snprintf (valueBuffer, 1023, keyValueModifiedFormat, i);
keySetString (key, valueBuffer);
modified++;
}
timeInit ();
kdbSet (kdb, ks, parentKey);
int modifyTime = timeGetDiffMicroseconds ();
if (verbose)
{
printf ("Modified keys: %zu\n", modified);
printf ("\n");
printf ("Insert Time (us); Modification Time (us)\n");
}
printf ("%d;%d\n", insertingTime, modifyTime);
kdbClose (kdb, parentKey);
ksDel (contract);
ksDel (ks);
keyDel (parentKey);
return 0;
}