summaryrefslogtreecommitdiff
path: root/utils/benchmark/tracker-benchmark.c
blob: d4471863c3584db9b6b0ada209db79e39db78734 (plain)
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
/*
 * Copyright (C) 2022, Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Author: Carlos Garnacho <carlosg@gnome.org>
 */

#include <libtracker-sparql/tracker-sparql.h>
#include <locale.h>
#include <glib.h>
#include <stdio.h>

static gchar *database_path = NULL;
static gint batch_size = 5000;
static gint duration = 30;

static GOptionEntry entries[] = {
	{ "database", 'p', 0, G_OPTION_ARG_FILENAME, &database_path,
	  "Location of the database",
	  "FILE"
	},
	{ "batch-size", 'b', 0, G_OPTION_ARG_INT, &batch_size,
	  "Update batch size",
	  "SIZE"
	},
	{ "duration", 'd', 0, G_OPTION_ARG_INT, &duration,
	  "Duration of individual benchmarks",
	  "DURATION"
	},
	{ NULL }
};

typedef gpointer (*DataCreateFunc) (void);

typedef void (*BenchmarkFunc) (TrackerSparqlConnection *conn,
                               DataCreateFunc           data_func,
                               double                  *elapsed,
                               int                     *elems,
                               double                  *min,
                               double                  *max);

enum {
	UNIT_SEC,
	UNIT_MSEC,
	UNIT_USEC,
};

static inline int
get_unit (gdouble value)
{
	/* Below msec, report in usecs */
	if (value < 0.001)
		return UNIT_USEC;
	else if (value < 1)
		return UNIT_MSEC;

	return UNIT_SEC;
}

static gdouble
transform_unit (gdouble value)
{
	int unit = get_unit (value);

	switch (unit) {
	case UNIT_USEC:
		return value * G_USEC_PER_SEC;
	case UNIT_MSEC:
		return value * 1000;
	case UNIT_SEC:
		return value;
	default:
		g_assert_not_reached ();
	}
}

static const gchar *
unit_string (gdouble value)
{
	int unit = get_unit (value);

	switch (unit) {
	case UNIT_USEC:
		return "usec";
	case UNIT_MSEC:
		return "msec";
	case UNIT_SEC:
		return "sec";
	default:
		g_assert_not_reached ();
	}
}

static inline gpointer
create_resource (void)
{
	TrackerResource *resource;

	resource = tracker_resource_new (NULL);
	tracker_resource_set_uri (resource, "rdf:type", "rdfs:Resource");

	return resource;
}

static inline gpointer
create_changing_resource (void)
{
	TrackerResource *resource;
	gchar buf[2] = { 0, 0 };
	static gint res = 0;
	static gint counter = 0;
	gchar *uri;

	/* In order to keep large batches, and guaranteeing modifications
	 * do not get coalesced, create one different URI per slot in the batch.
	 */
	uri = g_strdup_printf ("http://example.com/%d", res);
	resource = tracker_resource_new (uri);
	tracker_resource_set_uri (resource, "rdf:type", "rdfs:Resource");
	buf[0] = '0' + counter;
	tracker_resource_set_string (resource, "rdfs:label", (const gchar *) buf);
	counter = (counter + 1) % 10;
	res = (res + 1) % batch_size;
	g_free (uri);

	return resource;
}

static inline gpointer
create_query (void)
{
	return g_strdup ("SELECT ?u { ?u a rdfs:Resource } limit 1");
}

static inline TrackerBatch *
create_batch (TrackerSparqlConnection *conn,
              DataCreateFunc           data_func,
              gboolean                 sparql)
{
	TrackerBatch *batch;
	TrackerResource *resource;
	int i;

	batch = tracker_sparql_connection_create_batch (conn);

	for (i = 0; i < batch_size; i++) {
		resource = data_func ();

		if (sparql) {
			gchar *sparql;

			sparql = tracker_resource_print_sparql_update (resource,
			                                               tracker_sparql_connection_get_namespace_manager (conn),
			                                               NULL);
			tracker_batch_add_sparql (batch, sparql);
			g_free (sparql);
		} else {
			tracker_batch_add_resource (batch, NULL, resource);
		}

		g_object_unref (resource);
	}

	return batch;
}

static inline TrackerBatch *
create_batch_insert_delete (TrackerSparqlConnection *conn)
{
	TrackerBatch *batch;
	int i;

	batch = tracker_sparql_connection_create_batch (conn);

	for (i = 0; i < batch_size; i++) {
		gchar *uri;

		uri = g_strdup_printf ("http://example.com/%d", i >> 1);

		if (i % 2 == 0) {
			TrackerResource *resource;

			resource = tracker_resource_new (uri);
			tracker_resource_set_uri (resource, "rdf:type", "rdfs:Resource");
			tracker_batch_add_resource (batch, NULL, resource);
			g_object_unref (resource);
		} else {
			gchar *query;

			query = g_strdup_printf ("DELETE DATA { <%s> a rdfs:Resource }", uri);
			tracker_batch_add_sparql (batch, query);
			g_free (query);
		}

		g_free (uri);
	}

	return batch;
}

static int
consume_cursor (TrackerSparqlCursor *cursor)
{
	GError *error = NULL;
	int magic = 0;

	while (tracker_sparql_cursor_next (cursor, NULL, &error)) {
		const gchar *str;

		/* Some bit fiddling so the loop is not optimized out */
		str = tracker_sparql_cursor_get_string (cursor, 0, NULL);
		magic ^= str[0] == 'h';
	}

	tracker_sparql_cursor_close (cursor);

	return magic;
}

static void
benchmark_update_batch (TrackerSparqlConnection *conn,
                        DataCreateFunc           data_func,
                        double                  *elapsed,
                        int                     *elems,
                        double                  *min,
                        double                  *max)
{
	GTimer *timer;
	GError *error = NULL;

	timer = g_timer_new ();

	while (*elapsed < duration) {
		TrackerBatch *batch;
		double batch_elapsed;

		g_timer_reset (timer);
		batch = create_batch (conn, data_func, FALSE);
		tracker_batch_execute (batch, NULL, &error);
		g_assert_no_error (error);
		g_object_unref (batch);

		batch_elapsed = g_timer_elapsed (timer, NULL);
		*min = MIN (*min, batch_elapsed);
		*max = MAX (*max, batch_elapsed);
		*elapsed += batch_elapsed;
		*elems += 1;
	}

	/* We count things by resources, not batches */
	*min /= batch_size;
	*max /= batch_size;
	*elems *= batch_size;

	g_timer_destroy (timer);
}

static void
benchmark_update_sparql (TrackerSparqlConnection *conn,
                         DataCreateFunc           data_func,
                         double                  *elapsed,
                         int                     *elems,
                         double                  *min,
                         double                  *max)
{
	GTimer *timer;
	GError *error = NULL;

	timer = g_timer_new ();

	while (*elapsed < duration) {
		TrackerBatch *batch;
		double batch_elapsed;

		batch = create_batch (conn, data_func, TRUE);
		tracker_batch_execute (batch, NULL, &error);
		g_assert_no_error (error);
		g_object_unref (batch);

		batch_elapsed = g_timer_elapsed (timer, NULL);
		*min = MIN (*min, batch_elapsed);
		*max = MAX (*max, batch_elapsed);
		*elapsed += batch_elapsed;
		*elems += 1;
		g_timer_reset (timer);
	}

	/* We count things by resources, not batches */
	*min /= batch_size;
	*max /= batch_size;
	*elems *= batch_size;

	g_timer_destroy (timer);
}

static void
benchmark_update_insert_delete (TrackerSparqlConnection *conn,
                                DataCreateFunc           data_func,
                                double                  *elapsed,
                                int                     *elems,
                                double                  *min,
                                double                  *max)
{
	GTimer *timer;
	GError *error = NULL;

	timer = g_timer_new ();

	while (*elapsed < duration) {
		TrackerBatch *batch;
		double batch_elapsed;

		g_timer_reset (timer);
		batch = create_batch_insert_delete (conn);
		tracker_batch_execute (batch, NULL, &error);
		g_assert_no_error (error);
		g_object_unref (batch);

		batch_elapsed = g_timer_elapsed (timer, NULL);
		*min = MIN (*min, batch_elapsed);
		*max = MAX (*max, batch_elapsed);
		*elapsed += batch_elapsed;
		*elems += 1;
	}

	/* We count things by resources, not batches */
	*min /= batch_size;
	*max /= batch_size;
	*elems *= batch_size;

	g_timer_destroy (timer);
}

static void
benchmark_query_statement (TrackerSparqlConnection *conn,
                           DataCreateFunc           data_func,
                           double                  *elapsed,
                           int                     *elems,
                           double                  *min,
                           double                  *max)
{
	TrackerSparqlStatement *stmt;
	GTimer *timer;
	GError *error = NULL;
	gchar *query;

	timer = g_timer_new ();
	query = data_func ();
	stmt = tracker_sparql_connection_query_statement (conn, query,
	                                                  NULL, &error);
	g_assert_no_error (error);
	g_free (query);

	while (*elapsed < duration) {
		TrackerSparqlCursor *cursor;
		double query_elapsed;

		cursor = tracker_sparql_statement_execute (stmt, NULL, &error);
		g_assert_no_error (error);
		consume_cursor (cursor);
		g_object_unref (cursor);

		query_elapsed = g_timer_elapsed (timer, NULL);
		*min = MIN (*min, query_elapsed);
		*max = MAX (*max, query_elapsed);
		*elapsed += query_elapsed;
		*elems += 1;
		g_timer_reset (timer);
	}

	g_object_unref (stmt);
	g_timer_destroy (timer);
}

static void
benchmark_query_sparql (TrackerSparqlConnection *conn,
                        DataCreateFunc           data_func,
                        double                  *elapsed,
                        int                     *elems,
                        double                  *min,
                        double                  *max)
{
	GTimer *timer;
	GError *error = NULL;
	gchar *query;

	timer = g_timer_new ();
	query = data_func ();

	while (*elapsed < duration) {
		TrackerSparqlCursor *cursor;
		double query_elapsed;

		cursor = tracker_sparql_connection_query (conn, query,
		                                          NULL, &error);
		g_assert_no_error (error);
		consume_cursor (cursor);
		g_object_unref (cursor);

		query_elapsed = g_timer_elapsed (timer, NULL);
		*min = MIN (*min, query_elapsed);
		*max = MAX (*max, query_elapsed);
		*elapsed += query_elapsed;
		*elems += 1;
		g_timer_reset (timer);
	}

	g_timer_destroy (timer);
	g_free (query);
}

struct {
	const gchar *desc;
	BenchmarkFunc func;
	DataCreateFunc data_func;
} benchmarks[] = {
	{ "Resource batch update (sync)", benchmark_update_batch, create_resource },
	{ "SPARQL batch update (sync)", benchmark_update_sparql, create_resource },
	{ "Resource modification (sync)", benchmark_update_batch, create_changing_resource },
	{ "Resource insert+delete (sync)", benchmark_update_insert_delete, NULL },
	{ "Prepared statement query (sync)", benchmark_query_statement, create_query },
	{ "SPARQL query (sync)", benchmark_query_sparql, create_query },
};

static void
run_benchmarks (TrackerSparqlConnection *conn)
{
	guint i;
	guint max_len = 0;

	for (i = 0; i < G_N_ELEMENTS (benchmarks); i++)
		max_len = MAX (max_len, strlen (benchmarks[i].desc));

	g_print ("%*s\t\tElements\tElems/sec\tMin         \tMax         \tAvg\n",
	         max_len, "Test");

	for (i = 0; i < G_N_ELEMENTS (benchmarks); i++) {
		double elapsed = 0, min = G_MAXDOUBLE, max = -G_MAXDOUBLE, adjusted, avg;
		int elems = 0;

		g_print ("%*s\t\t", max_len, benchmarks[i].desc);

		benchmarks[i].func (conn, benchmarks[i].data_func,
		                    &elapsed, &elems, &min, &max);

		if (elapsed > duration) {
			/* To avoid explaining how long did the benchmark
			 * actually take to run. Adjust the output to the
			 * specified time limit.
			 */
			adjusted = elems * ((double) duration / elapsed);
		} else {
			adjusted = elems;
		}

		avg = elapsed / elems;
		g_print ("%.3f\t%.3f\t%.3f %s\t%.3f %s\t%3.3f %s\n",
		         adjusted,
		         elems / elapsed,
		         transform_unit (min), unit_string (min),
		         transform_unit (max), unit_string (max),
		         transform_unit (avg), unit_string (avg));
	}
}

int
main (int argc, char *argv[])
{
	TrackerSparqlConnection *conn;
	GOptionContext *context;
	GError *error = NULL;
	GFile *db = NULL;

        setlocale (LC_ALL, "");

	context = g_option_context_new (NULL);
	g_option_context_add_main_entries (context, entries, NULL);

	if (!g_option_context_parse (context, &argc, (char***) &argv, &error)) {
		g_printerr ("%s, %s\n", "Unrecognized options", error->message);
		g_error_free (error);
		g_option_context_free (context);
		return EXIT_FAILURE;
	}

	g_option_context_free (context);

	g_print ("Batch size: %d, Individual test duration: %d sec\n",
	         batch_size, duration);

	if (database_path) {
		if (g_file_test (database_path, G_FILE_TEST_EXISTS)) {
			g_printerr ("Database path '%s' already exists", database_path);
			return EXIT_FAILURE;
		}

		g_print ("Opening file database at '%s'…\n",
		         database_path);
		db = g_file_new_for_commandline_arg (database_path);
	} else {
		g_print ("Opening in-memory database…\n");
	}

	conn = tracker_sparql_connection_new (0, db,
	                                      tracker_sparql_get_ontology_nepomuk(),
	                                      NULL, &error);
	g_assert_no_error (error);

	run_benchmarks (conn);

	g_object_unref (conn);
	g_clear_object (&db);

	return EXIT_SUCCESS;
}