summaryrefslogtreecommitdiffstats
path: root/tools/iksperf.c
blob: dd240aab379251c1ae7bcb9d60d505c8f724c7ba (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
/* iksemel (XML parser for Jabber)
** Copyright (C) 2000-2003 Gurer Ozen
** This code is free software; you can redistribute it and/or
** modify it under the terms of GNU Lesser General Public License.
*/

#include "common.h"
#include "iksemel.h"
#include "perf.h"

#include <sys/stat.h>

#ifdef HAVE_GETOPT_LONG
#include <getopt.h>
#endif

#ifdef HAVE_GETOPT_LONG
static struct option longopts[] = {
	{ "all", 0, 0, 'a' },
	{ "sax", 0, 0, 's' },
	{ "dom", 0, 0, 'd' },
	{ "serialize", 0, 0, 'e' },
	{ "sha1", 0, 0, '1' },
	{ "block", required_argument, 0, 'b' },
	{ "memdbg", 0, 0, 'm' },
	{ "help", 0, 0, 'h' },
	{ "version", 0, 0, 'V' },
	{ 0, 0, 0, 0 }
};
#endif

static char *shortopts = "asde1b:mhV";

static void
print_usage (void)
{
	puts ("Usage: iksperf [OPTIONS] FILE\n"
		"This tool measures the performance of the iksemel library.\n"
		" -a, --all         Make all tests.\n"
		" -s, --sax         Sax test.\n"
		" -d, --dom         Tree generating test.\n"
		" -e, --serialize   Tree serializing test.\n"
		" -1, --sha1        SHA1 hashing test.\n"
		" -b, --block SIZE  Parse the file in SIZE byte blocks.\n"
		" -m, --memdbg      Trace malloc and free calls.\n"
		" -h, --help        Print this text and exit.\n"
		" -V, --version     Print version and exit.\n"
#ifndef HAVE_GETOPT_LONG
		"(long options are not supported on your system)\n"
#endif
		"Report bugs to <iksemel-dev@jabberstudio.org>.");
}

/* if not 0, file is parsed in block_size byte blocks */
size_t block_size;

char *load_file (const char *fname, size_t *sizeptr)
{
	FILE *f;
	char *buf;
	struct stat fs;
	size_t size, ret;

	if (stat (fname, &fs) != 0) {
		fprintf (stderr, "Cannot access file '%s'.\n", fname);
		exit (1);
	}
	size = fs.st_size;

	printf ("Test file '%s' (%d bytes):\n", fname, size);

	f = fopen (fname, "rb");
	if (!f) {
		fprintf (stderr, "Cannot open file.\n");
		exit (1);
	}

	buf = malloc (size);
	if (!buf) {
		fclose (f);
		fprintf (stderr, "Cannot allocate %d bytes for buffer.\n", size);
		exit (2);
	}

	ret = fread (buf, 1, size, f);
	if (ret < size) {
		fprintf (stderr, "Read error in file.\n");
		exit (1);
	}

	*sizeptr = size;
	fclose (f);
	return buf;
}

/* stats */
int sax_tag;
int sax_cdata;

int
tagHook (void *udata, char *name, char **atts, int type)
{
	++sax_tag;
	return IKS_OK;
}

int
cdataHook (void *udata, char *data, size_t len)
{
	++sax_cdata;
	return IKS_OK;
}

void
sax_test (char *buf, size_t len)
{
	unsigned long time;
	iksparser *prs;
	size_t bs, i;
	int err;

	bs = block_size;
	if (0 == bs) bs = len;
	sax_tag = 0;
	sax_cdata = 0;

	t_reset ();

	prs = iks_sax_new (NULL, tagHook, cdataHook);
	i = 0;
	while (i < len) {
		if (i + bs > len) bs = len - i;
		err = iks_parse (prs, buf + i, bs, 0);
		switch (err) {
			case IKS_OK:
				break;
			case IKS_NOMEM:
				exit (2);
			case IKS_BADXML:
				fprintf (stderr, "Invalid xml at byte %ld, line %ld\n",
					iks_nr_bytes (prs), iks_nr_lines (prs));
				exit (1);
			case IKS_HOOK:
				exit (1);
		}
		i += bs;
	}

	time = t_elapsed ();

	printf ("SAX: parsing took %ld milliseconds.\n", time);
	printf ("SAX: tag hook called %d, cdata hook called %d times.\n", sax_tag, sax_cdata);

	iks_parser_delete (prs);
}

void dom_test (char *buf, size_t len)
{
	size_t bs, i;
	int err;
	iksparser *prs;
	unsigned long time;
	iks *x;
	size_t allocated, used;

	bs = block_size;
	if (0 == bs) bs = len;

	t_reset ();

	prs = iks_dom_new (&x);
	iks_set_size_hint (prs, len);
	i = 0;
	while (i < len) {
		if (i + bs > len) bs = len - i;
		err = iks_parse (prs, buf + i, bs, 0);
		switch (err) {
			case IKS_OK:
				break;
			case IKS_NOMEM:
				exit (2);
			case IKS_BADXML:
				fprintf (stderr, "Invalid xml at byte %ld, line %ld\n",
					iks_nr_bytes (prs), iks_nr_lines (prs));
				exit (1);
			case IKS_HOOK:
				exit (1);
		}
		i += bs;
	}

	time = t_elapsed ();
	iks_stack_stat (iks_stack (x), &allocated, &used);

	printf ("DOM: parsing and building the tree took %ld milliseconds.\n", time);
	printf ("DOM: ikstack: %d bytes allocated, %d bytes used.\n", allocated, used);

	t_reset ();
	iks_delete (x);
	time = t_elapsed ();
	printf ("DOM: deleting the tree took %ld milliseconds.\n", time);

	iks_parser_delete (prs);
}

void
serialize_test (char *buf, size_t len)
{
	unsigned long time;
	iks *x;
	iksparser *prs;
	char *xml;
	int err;

	prs = iks_dom_new (&x);
	err = iks_parse (prs, buf, len, 1);
	switch (err) {
		case IKS_OK:
			break;
		case IKS_NOMEM:
			exit (2);
		case IKS_BADXML:
			fprintf (stderr, "Invalid xml at byte %ld, line %ld\n",
				iks_nr_bytes (prs), iks_nr_lines (prs));
			exit (1);
		case IKS_HOOK:
			exit (1);
	}
	iks_parser_delete (prs);

	t_reset ();

	xml = iks_string (iks_stack (x), x);

	time = t_elapsed ();

	printf ("Serialize: serializing the tree took %ld milliseconds.\n", time);

	iks_delete (x);
}

void
sha_test (char *buf, size_t len)
{
	unsigned long time;
	iksha *s;
	char out[41];

	t_reset ();

	s = iks_sha_new ();
	iks_sha_hash (s, (unsigned char *) buf, len, 1);
	iks_sha_print (s, out);
	out[40] = '\0';
	iks_sha_delete (s);

	time = t_elapsed ();

	printf ("SHA: hashing took %ld milliseconds.\n", time);
	printf ("SHA: hash [%s]\n", out);
}

int
main (int argc, char *argv[])
{
	int test_type = 0;
	int c;

#ifdef HAVE_GETOPT_LONG
	int i;
	while ((c = getopt_long (argc, argv, shortopts, longopts, &i)) != -1) {
#else
	while ((c = getopt (argc, argv, shortopts)) != -1) {
#endif
		switch (c) {
			case 'a':
				test_type = 0xffff;
				break;
			case 's':
				test_type |= 1;
				break;
			case 'd':
				test_type |= 2;
				break;
			case 'e':
				test_type |= 4;
				break;
			case '1':
				test_type |= 8;
				break;
			case 'b':
				block_size = atoi (optarg);
				break;
			case 'm':
				m_trace ();
				break;
			case 'h':
				print_usage ();
				exit (0);
			case 'V':
				puts ("iksperf (iksemel) "VERSION);
				exit (0);
		}
	}
	for (; optind < argc; optind++) {
		char *buf;
		size_t len;

		buf = load_file (argv[optind], &len);
		if (test_type & 1) sax_test (buf, len);
		if (test_type == 0 || test_type & 2) dom_test (buf, len);
		if (test_type & 4) serialize_test (buf, len);
		if (test_type & 8) sha_test (buf, len);
		free (buf);
	}

	return 0;
}