forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffappend.c
246 lines (200 loc) · 7.16 KB
/
diffappend.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <benchmarks.h>
#include <getopt.h>
#include <kdbdiff.h>
#include <kdbprivate.h>
int g_count = 50000;
int g_iterations = 10;
bool g_verbose = false;
static void processCommandLineArguments (int argc, char ** argv)
{
struct option long_options[] = { { "count", required_argument, 0, 'c' },
{ "iterations", required_argument, 0, 'i' },
{ "verbose", no_argument, 0, 'v' },
{ 0, 0, 0, 0 } };
while (1)
{
int option_index = 0;
int c = getopt_long (argc, argv, "", long_options, &option_index);
if (c == -1)
{
break;
}
switch (c)
{
case 'c':
g_count = atoi (optarg);
break;
case 'v':
g_verbose = true;
break;
case 'i':
g_iterations = atoi (optarg);
break;
default:
break;
}
}
}
ElektraDiff * createBaseDiff (size_t count, size_t * keyCount)
{
KeySet * added = ksNew (count, KS_END);
KeySet * modifiedOld = ksNew (count, KS_END);
KeySet * modifiedNew = ksNew (count, KS_END);
KeySet * removed = ksNew (count, KS_END);
for (size_t i = 0; i < count; i++)
{
char * name = elektraFormat ("user:/test/originally_added_%zu", i);
char * value = elektraFormat ("original_value_%zu", i);
ksAppendKey (added, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
name = elektraFormat ("user:/test/originally_modified_%zu", i);
value = elektraFormat ("original_modified_old_value_%zu", i);
ksAppendKey (modifiedOld, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
name = elektraFormat ("user:/test/originally_modified_%zu", i);
value = elektraFormat ("original_modified_new_value_%zu", i);
ksAppendKey (modifiedNew, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
name = elektraFormat ("user:/test/originally_removed_%zu", i);
value = elektraFormat ("original_value_%zu", i);
ksAppendKey (removed, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
}
*keyCount = ksGetSize (added) + ksGetSize (removed) + ksGetSize (modifiedOld) + ksGetSize (modifiedNew);
ElektraDiff * diff = elektraDiffNew (added, removed, modifiedOld, modifiedNew, keyNew ("user:/test", KEY_END));
return diff;
}
ElektraDiff * createDiffToAppend (size_t count, size_t * keyCount)
{
KeySet * added = ksNew (count, KS_END);
KeySet * modifiedOld = ksNew (count, KS_END);
KeySet * modifiedNew = ksNew (count, KS_END);
KeySet * removed = ksNew (count, KS_END);
size_t quarter = count / 4;
size_t half = count / 2;
for (size_t i = 0; i < quarter; i++)
{
// Add new keys
char * name = elektraFormat ("user:/test/newly_added_%zu", i);
char * value = elektraFormat ("original_value_%zu", i);
ksAppendKey (added, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
// Add previously removed keys with new value --> modified
name = elektraFormat ("user:/test/originally_removed_%zu", i);
value = elektraFormat ("new_value_%zu", i);
ksAppendKey (added, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
// Modify previously added keys
name = elektraFormat ("user:/test/originally_added_%zu", i);
value = elektraFormat ("original_value_%zu", i);
ksAppendKey (modifiedOld, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
name = elektraFormat ("user:/test/originally_added_%zu", i);
value = elektraFormat ("newly_modified_new_value_%zu", i);
ksAppendKey (modifiedNew, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
// Modify previously modified keys
name = elektraFormat ("user:/test/originally_modified_%zu", i);
value = elektraFormat ("original_modified_new_value_%zu", i);
ksAppendKey (modifiedOld, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
name = elektraFormat ("user:/test/originally_modified_%zu", i);
value = elektraFormat ("newly_modified_new_value_%zu", i);
ksAppendKey (modifiedNew, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
// Modify new keys
name = elektraFormat ("user:/test/newly_modified_%zu", i);
value = elektraFormat ("original_modified_new_value_%zu", i);
ksAppendKey (modifiedOld, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
name = elektraFormat ("user:/test/newly_modified_%zu", i);
value = elektraFormat ("newly_modified_new_value_%zu", i);
ksAppendKey (modifiedNew, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
// Remove new keys
name = elektraFormat ("user:/test/newly_removed_%zu", i);
value = elektraFormat ("original_value_%zu", i);
ksAppendKey (removed, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
}
for (size_t i = half; i < half + quarter; i++)
{
// Remove previously added --> unchanged
char * name = elektraFormat ("user:/test/originally_added_%zu", i);
char * value = elektraFormat ("original_value_%zu", i);
ksAppendKey (removed, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
// Remove previously modified -> removed
name = elektraFormat ("user:/test/originally_modified_%zu", i);
value = elektraFormat ("original_modified_new_value_%zu", i);
ksAppendKey (removed, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
}
for (size_t i = half + quarter; i < count; i++)
{
// Modify previously modified key back -> unchanged
char * name = elektraFormat ("user:/test/originally_modified_%zu", i);
char * value = elektraFormat ("original_modified_new_value_%zu", i);
ksAppendKey (modifiedOld, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
name = elektraFormat ("user:/test/originally_modified_%zu", i);
value = elektraFormat ("original_modified_old_value_%zu", i);
ksAppendKey (modifiedNew, keyNew (name, KEY_VALUE, value, KEY_END));
elektraFree (name);
elektraFree (value);
}
*keyCount = ksGetSize (added) + ksGetSize (removed) + ksGetSize (modifiedOld) + ksGetSize (modifiedNew);
ElektraDiff * diff = elektraDiffNew (added, removed, modifiedOld, modifiedNew, keyNew ("user:/test", KEY_END));
return diff;
}
int main (int argc, char ** argv)
{
processCommandLineArguments (argc, argv);
size_t diff1keys = 0;
size_t diff2keys = 0;
ElektraDiff * diff1 = createBaseDiff (g_count, &diff1keys);
ElektraDiff * diff2 = createDiffToAppend (g_count, &diff2keys);
Key * parentKey = keyNew ("user:/test", KEY_END);
if (g_verbose)
{
printf ("Diff1 keys: %zu\n", diff1keys);
printf ("Diff2 keys: %zu\n", diff2keys);
}
long sum = 0;
for (int i = 0; i < g_iterations; i++)
{
ElektraDiff * target = elektraDiffDup (diff1);
timeInit ();
elektraDiffAppend (target, diff2, parentKey);
int us = timeGetDiffMicroseconds ();
sum += us;
printf ("%d\n", us);
elektraDiffDel (target);
}
if (g_verbose)
{
double avgus = (double) sum / g_iterations;
double avgms = avgus / 1000;
printf ("Average: %.2f us\n", avgus);
printf ("Average: %.2f ms\n", avgms);
}
keyDel (parentKey);
elektraDiffDel (diff1);
elektraDiffDel (diff2);
}