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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
#include <opensync/opensync.h>
#include <opensync/opensync_internals.h>
#include "engine.h"
#include "engine_internals.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <glib.h>
#include <sys/time.h>
#include <sys/stat.h>
GMutex *working;
GMutex *working2;
GList *changes;
GList *tests;
osync_bool alwaysempty;
osync_bool noaccess = FALSE;
typedef struct OSyncPluginTest {
char *name;
double (*test)(OSyncEngine *engine, OSyncMember *file, int num, const char *);
double alltime;
double connecttime;
double readtime;
double writetime;
double othertime;
int num;
} OSyncPluginTest;
static void usage (char *name, int ecode)
{
fprintf (stderr, "Usage: %s <pluginname>\n", name);
fprintf (stderr, "--config <filename>\tSet the config file to use\n");
fprintf (stderr, "--type <object type>\tSets the objtype to test\n");
fprintf (stderr, "--empty\tOnly deleta all data. Do not test\n");
exit (ecode);
}
gboolean only_random = FALSE;
char *localdir = NULL;
static void sync_now(OSyncEngine *engine)
{
OSyncError *error = NULL;
printf(".");
fflush(stdout);
if (!osengine_sync_and_block(engine, &error)) {
printf("Error while starting synchronization: %s\n", osync_error_print(&error));
osync_error_free(&error);
exit(1);
}
printf(".");
fflush(stdout);
}
static void check_empty(void)
{
printf(".");
fflush(stdout);
char *command = g_strdup_printf("test \"x$(ls %s)\" = \"x\"", localdir);
int ret = system(command);
g_free(command);
if (ret)
abort();
printf(".");
fflush(stdout);
}
double starttime;
double currenttime;
double connecttime;
double readtime;
double writetime;
double _second() /* note that some compilers like AIX xlf do not require the trailing '_' */
{
struct timeval tp;
int rtn;
rtn=gettimeofday(&tp, NULL);
return ((double)tp.tv_sec+(1.e-6)*tp.tv_usec);
}
void engine_status(OSyncEngine *engine, OSyncEngineUpdate *status, void *user_data)
{
switch (status->type) {
case ENG_ENDPHASE_CON:
osync_trace(TRACE_INTERNAL, "++++++++++++++++ Connection Phase ended ++++++++++++++");
currenttime = _second();
connecttime = currenttime - starttime;
break;
case ENG_ENDPHASE_READ:
osync_trace(TRACE_INTERNAL, "++++++++++++++++ Read Phase ended ++++++++++++++");
readtime = _second() - currenttime;
currenttime = _second();
break;
case ENG_ENDPHASE_WRITE:
osync_trace(TRACE_INTERNAL, "++++++++++++++++ Write Phase ended ++++++++++++++");
writetime = _second() - currenttime;
currenttime = _second();
break;
default:
;
}
}
double check_sync(OSyncEngine *engine, const char *name, int num)
{
int ret;
printf(".");
fflush(stdout);
starttime = _second();
osync_trace(TRACE_INTERNAL, "++++++++++++++++ Test \"%s %i\" starting ++++++++++++++", name, num);
osengine_set_enginestatus_callback(engine, engine_status, NULL);
sync_now(engine);
osengine_set_enginestatus_callback(engine, NULL, NULL);
int wasted = 0;
int alldeciders = 0;
osengine_get_wasted(engine, &alldeciders, &wasted);
osync_trace(TRACE_INTERNAL, "++++++++++++++++ Test \"%s %i\" ended (%i / %i (%i%%)) ++++++++++++++", name, num, wasted, alldeciders, (int)(((float)wasted / (float)alldeciders) * 100));
double thistime = _second() - starttime;
printf(".");
fflush(stdout);
char *tempdir = g_strdup_printf("%s/plgtest.XXXXXX", g_get_tmp_dir());
char *tmpresult = mktemp(tempdir);
if ((tmpresult == NULL) || (mkdir(tmpresult, 0700) != 0))
{
g_free(tempdir);
osync_trace(TRACE_INTERNAL, "unable to create temporary dir: %s", strerror(errno));
abort();
}
char *command = g_strdup_printf("mv %s/* %s &> /dev/null", localdir, tempdir);
ret = system(command);
if (ret)
{
g_free(tempdir);
g_free(command);
osync_trace(TRACE_INTERNAL, "Unable to move files to temporary dir: %d", ret);
abort();
}
g_free(command);
printf(".");
fflush(stdout);
check_empty();
printf(".");
fflush(stdout);
osync_group_set_slow_sync(engine->group, "data", TRUE);
sync_now(engine);
printf(".");
fflush(stdout);
command = g_strdup_printf("test \"x$(diff -x \".*\" %s %s)\" = \"x\"", localdir, tempdir);
int result = system(command);
g_free(command);
g_free(tempdir);
if (result)
abort();
printf(" success\n");
return thistime;
}
void add_data(OSyncMember *member, const char *objtype)
{
OSyncChange *change = osync_change_new();
if (!osync_member_make_random_data(member, change, objtype)) {
printf("Unable to create random data\n");
abort();
}
char *filename = NULL;
while (1) {
char *randstr = osync_rand_str(8);
filename = g_strdup_printf("%s/%s", localdir, randstr);
g_free(randstr);
char *command = g_strdup_printf("ls %s &> /dev/null", filename);
int ret = system(command);
g_free(command);
if (ret)
break;
g_free(filename);
}
OSyncError *error = NULL;
if (!osync_file_write(filename, osync_change_get_data(change), osync_change_get_datasize(change), 0700, &error)) {
printf("Unable to write to file %s\n", osync_error_print(&error));
abort();
}
g_free(filename);
}
void modify_data(OSyncMember *member, const char *objtype)
{
GDir *dir;
GError *gerror = NULL;
dir = g_dir_open(localdir, 0, &gerror);
if (!dir)
abort();
const char *de = NULL;
while ((de = g_dir_read_name(dir))) {
char *filename = g_build_filename(localdir, de, NULL);
OSyncChange *change = osync_change_new();
if (!osync_member_make_random_data(member, change, objtype)) {
printf("Unable to create random data\n");
abort();
}
OSyncError *error = NULL;
if (!osync_file_write(filename, osync_change_get_data(change), osync_change_get_datasize(change), 0700, &error)) {
printf("Unable to write to file %s\n", osync_error_print(&error));
abort();
}
g_free(filename);
}
g_dir_close(dir);
}
void delete_data(OSyncMember *member, const char *objtype)
{
char *command = g_strdup_printf("rm -f %s/*", localdir);
int ret = system(command);
if (ret)
{
osync_trace(TRACE_INTERNAL, "Unable to delete data: %d", ret);
abort();
}
g_free(command);
}
static void empty_all(OSyncEngine *engine)
{
printf(".");
fflush(stdout);
osync_group_set_slow_sync(engine->group, "data", TRUE);
sync_now(engine);
char *command = g_strdup_printf("rm -f %s/*", localdir);
int ret = system(command);
if (ret)
{
osync_trace(TRACE_INTERNAL, "Unable to delete data: %d", ret);
abort();
}
g_free(command);
sync_now(engine);
printf(".");
fflush(stdout);
check_empty();
}
double add_test(OSyncEngine *engine, OSyncMember *member, int num, const char *objtype)
{
printf("Test \"Add %i\" starting", num);
fflush(stdout);
empty_all(engine);
printf(".");
fflush(stdout);
int i = 0;
for (i = 0; i < num; i++)
add_data(member, objtype);
printf(".");
fflush(stdout);
return check_sync(engine, "Add", num);
}
double modify_test(OSyncEngine *engine, OSyncMember *member, int num, const char *objtype)
{
printf("Test \"Modify %i\" starting", num);
fflush(stdout);
empty_all(engine);
printf(".");
fflush(stdout);
int i = 0;
for (i = 0; i < num; i++)
add_data(member, objtype);
printf(".");
fflush(stdout);
check_sync(engine, "None", num);
printf(".");
fflush(stdout);
modify_data(member, objtype);
printf(".");
fflush(stdout);
return check_sync(engine, "Modify", num);
}
double delete_test(OSyncEngine *engine, OSyncMember *member, int num, const char *objtype)
{
printf("Test \"Delete %i\" starting", num);
fflush(stdout);
empty_all(engine);
printf(".");
fflush(stdout);
int i = 0;
for (i = 0; i < num; i++)
add_data(member, objtype);
printf(".");
fflush(stdout);
check_sync(engine, "None", num);
printf(".");
fflush(stdout);
delete_data(member, objtype);
printf(".");
fflush(stdout);
return check_sync(engine, "Delete", num);
}
static void run_all_tests(OSyncEngine *engine, OSyncMember *file, OSyncMember *target, const char *objtype)
{
GList *t;
for (t = tests; t; t = t->next) {
OSyncPluginTest *test = t->data;
test->alltime = test->test(engine, target, test->num, objtype);
test->connecttime = connecttime;
test->readtime = readtime;
test->writetime = writetime;
test->othertime = test->alltime - test->connecttime - test->readtime - test->writetime;
}
printf("\nOutcome:\n");
for (t = tests; t; t = t->next) {
OSyncPluginTest *test = t->data;
printf("Test \"%s\": All: %f Connect: %f(%i%%) Read: %f(%i%%) Write: %f(%i%%) Other: %f(%i%%)\n", test->name, test->alltime, test->connecttime, (int)((test->connecttime / test->alltime)* 100), test->readtime, (int)((test->readtime / test->alltime)* 100), test->writetime, (int)((test->writetime / test->alltime)* 100), test->othertime, (int)((test->othertime / test->alltime)* 100));
}
}
static void register_test(const char *name, double test(OSyncEngine *engine, OSyncMember *file, int num, const char *), int num)
{
OSyncPluginTest *newtest = g_malloc0(sizeof(OSyncPluginTest));
newtest->name = g_strdup(name);
newtest->test = test;
newtest->num = num;
tests = g_list_append(tests, newtest);
}
static void register_tests(void)
{
tests = NULL;
/*register_test("add_test1", add_test, 1);
register_test("add_test5", add_test, 5);
register_test("add_test10", add_test, 10);
register_test("add_test20", add_test, 20);*/
/*register_test("add_test50", add_test, 50);
register_test("add_test100", add_test, 100);
register_test("add_test200", add_test, 200);*/
/*register_test("modify_test1", modify_test, 1);
register_test("modify_test5", modify_test, 5);
register_test("modify_test10", modify_test, 10);
register_test("modify_test20", modify_test, 20);
register_test("modify_test50", modify_test, 50);*/
register_test("modify_test100", modify_test, 100);
//register_test("modify_test200", modify_test, 200);
/*register_test("delete_test1", delete_test, 1);
register_test("delete_test5", delete_test, 5);
register_test("delete_test10", delete_test, 10);
register_test("delete_test20", delete_test, 20);
register_test("delete_test50", delete_test, 50);*/
//register_test("delete_test100", delete_test, 100);
//register_test("delete_test200", delete_test, 200);
}
void change_content(void)
{
printf("changing content\n");
}
int main (int argc, char *argv[])
{
int i;
char *pluginname = NULL;
char *plugindir = NULL;
char *configfile = NULL;
char *objtype = NULL;
OSyncError *error = NULL;
if (argc < 2)
usage (argv[0], 1);
pluginname = argv[1];
for (i = 2; i < argc; i++) {
char *arg = argv[i];
if (!strcmp (arg, "--config")) {
configfile = argv[i + 1];
i++;
if (!configfile)
usage (argv[0], 1);
} else if (!strcmp (arg, "--type")) {
objtype = argv[i + 1];
i++;
if (!objtype)
usage (argv[0], 1);
} else if (!strcmp (arg, "--plugindir")) {
printf("plugindir %s\n", argv[i + 1]);
plugindir = argv[i + 1];
i++;
if (!plugindir)
usage (argv[0], 1);
} else if (!strcmp (arg, "--random")) {
only_random = TRUE;
} else if (!strcmp (arg, "--help")) {
usage (argv[0], 0);
} else {
usage (argv[0], 1);
}
}
OSyncEnv *env = osync_env_new();
osync_env_set_option(env, "LOAD_GROUPS", "FALSE");
if (plugindir)
osync_env_set_option(env, "PLUGINS_DIRECTORY", plugindir);
if (!osync_env_initialize(env, &error)) {
printf("Unable to initialize environment: %s\n", osync_error_print(&error));
osync_error_free(&error);
return 1;
}
char *testdir = g_strdup_printf("%s/plgtest.XXXXXX", g_get_tmp_dir());
char *result = mktemp(testdir);
if ((result == NULL) || (mkdir(result, 0700) != 0))
{
osync_trace(TRACE_EXIT_ERROR, "unable to create temporary dir: %s", strerror(errno));
return 1;
}
OSyncGroup *group = osync_group_new(env);
osync_group_set_name(group, osync_rand_str(8));
osync_group_set_configdir(group, testdir);
OSyncMember *member = osync_member_new(group);
char *config = NULL;
int size = 0;
if (configfile) {
if (!osync_file_read(configfile, &config, &size, &error)) {
fprintf(stderr, "Unable to read config: %s\n", osync_error_print(&error));
osync_error_free(&error);
return 1;
}
osync_member_set_config(member, config, size);
}
osync_member_set_pluginname(member, pluginname);
OSyncMember *file = osync_member_new(group);
localdir = g_strdup_printf("%s/plgtest.XXXXXX", g_get_tmp_dir());
result = mktemp(testdir);
if ((result == NULL) || (mkdir(result, 0700) != 0))
{
osync_trace(TRACE_EXIT_ERROR, "unable to create temporary dir: %s",
strerror(errno));
return 1;
}
config = g_strdup_printf("<config><path>%s</path><recursive>0</recursive></config>", localdir);
osync_member_set_config(file, config, strlen(config) + 1);
osync_member_set_pluginname(file, "file-sync");
if (!osync_group_save(group, &error)) {
printf("Error while creating syncengine: %s\n", osync_error_print(&error));
osync_error_free(&error);
goto error_free_env;
}
if (!g_thread_supported ()) g_thread_init (NULL);
OSyncEngine *engine = osengine_new(group, &error);
if (!engine) {
printf("Error while creating syncengine: %s\n", osync_error_print(&error));
osync_error_free(&error);
goto error_free_env;
}
if (!osengine_init(engine, &error)) {
printf("Error while initializing syncengine: %s\n", osync_error_print(&error));
osync_error_free(&error);
goto error_free_engine;
}
int count = 0;
if (only_random) {
do {
count++;
printf("++++++++++++++++++++++++++++++\n");
printf("Initializing new round #%i!\n", count);
if (g_random_int_range(0, 5) == 0) {
int i;
OSyncFormatEnv *env = osync_group_get_format_env(group);
for (i = 0; i < osync_conv_num_objtypes(env); i++) {
if (g_random_int_range(0, 5) == 0) {
OSyncObjType *type = osync_conv_nth_objtype(env, i);
osync_group_set_slow_sync(group, osync_objtype_get_name(type), TRUE);
printf("Requesting slow-sync for: %s\n", osync_objtype_get_name(type));
}
}
osync_conv_env_free(env);
}
change_content();
check_sync(engine, "Random", 1);
} while (g_random_int_range(0, 3) != 0);
printf("Finalizing engine\n");
osengine_finalize(engine);
osengine_free(engine);
engine = osengine_new(group, &error);
if (!engine) {
printf("Error while creating syncengine: %s\n", osync_error_print(&error));
osync_error_free(&error);
goto error_free_env;
}
if (!osengine_init(engine, &error)) {
printf("Error while initializing syncengine: %s\n", osync_error_print(&error));
osync_error_free(&error);
goto error_free_engine;
}
} else {
register_tests();
run_all_tests(engine, file, member, objtype);
}
printf("\nCompleted successfully\n");
return 0;
error_free_engine:
osengine_free(engine);
error_free_env:
osync_group_free(group);
osync_env_free(env);
return 1;
}
|