/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk
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
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "../tools/disp.h"
#include "../xmlgen/xmlgen.h"
#include "../xmlgen/opcgen.h"

#ifdef GENERATOR_XMARK
#include "../xmlgen/xmark.h"
#endif /* GENERATOR_XMARK */

#define MAX_LEVELS 127

int walk_tree = 0;
int fast_mode = 0;
int force_xpath = 0;

//const char *walk_xpath = "//*[(@* or text()) and (count(../child::*)>3)]";
//const char *walk_xpath = "//*[(number(@*) or number(text())) and (count(../child::*)>3)]";
//const char *walk_xpath = "//*[(number(@*) or number(text())) and (last()>3)]";
const char *walk_xpath = "//*[count(child::*)>3]/*[number(@*) or number(text())]";

int get_walk_mode() {
    return walk_tree;
}

struct TestData {
    unsigned long iterations;
    unsigned long size;
    
    char *fn;
    char *xml;
    unsigned long xmllen;
};

char *ReadFile(const char *fn) {
    FILE *f;
    struct stat st;
    char *buf;
    
    f=fopen(fn,"r");
    if (!f) {
	printf("Can't open XML file!\n");
	exit(1);
    }
    fstat(f->_fileno,&st);
    
    buf=(char*)malloc(st.st_size+1+sizeof(unsigned long));
    if (!buf) {
	printf("Can't allocate memory!\n");
	exit(1);
    }
    ((unsigned long*)buf)[0]=st.st_size;
    fread(buf+sizeof(unsigned long),st.st_size,1,f);
    fclose(f);
    buf[st.st_size+sizeof(unsigned long)]=0;
    return buf;
}

void ReadConfig(struct TestData *td) {
    FILE *f;
    f=fopen("config","r");
    if (!f) {
	printf("Error opening config file!\n");
	exit(1);
    }
    fscanf(f,"%lu",&(td->iterations));
    fclose(f);
}

void initXML(struct TestData *td);
void releaseXML(struct TestData *td);
void parseXML(struct TestData *td, unsigned long iter);
void preparseXML(struct TestData *td, unsigned long iter);
void postparseXML(struct TestData *td, unsigned long iter);


void Usage(char *myname) {

    printf("Usage:\n\t%s <iterations> [<xml file>|xmlgen|opcgen",myname);
#ifdef GENERATOR_XMARK
    printf("|xmark");
#ifdef DTD_VALIDATION
#ifdef DTD_VALIDATION_SUPPORTED
    printf("|xmarkdtd");
#endif
#endif
#endif /* GENERATOR_XMARK */
    printf("] <size>\n");
    exit(0);
}

double get_value(const char *str) {
    double res;
    char *tmp;

    res = strtod(str,&tmp);
    if (*tmp) return 0;
    return res;
}

double get_value_0(const char *str, size_t len) {
    char copy[32];
    double res;
    char *tmp;
    
	// Too long to be a number
    if (len > 32) return 0;
    strncpy(copy, str, len); copy[len]=0;
    
    res = strtod(copy,&tmp);
    if (*tmp) return 0;
    return res;
}

int Test(int argc, char *argv[]) {
    int i,j;
    struct timeval pre_time,post_time;
    struct timezone tz;
    unsigned long init_time,init_time2,time;
    double init_dtime,init_d;
    unsigned char mode=0;	/* 0 - file, 1 - xmlgen, 2 - opcgen */
    struct TestData td;
    char *xmlbuf;
    FILE *f;

    int execute_xml = 1;
    
    if (getenv("skip_xml")) execute_xml = 0;
    if (getenv("walk_tree")) walk_tree = 1;
    else walk_tree = 0;
    if (getenv("fast_mode")) fast_mode = 1;
    else fast_mode = 0;
    if (getenv("force_xpath")) force_xpath = 1;
    else force_xpath = 0;

    
    if ((argc<3)||(argc>4)) Usage(argv[0]);
    
    td.iterations = atol(argv[1]);
    if (td.iterations<0) Usage(argv[0]);

#ifdef DTD_VALIDATION
#ifndef DTD_VALIDATION_SUPPORTED
    fprintf(stderr,"DTD validation not implemented yet or not supported by parser!\n");
    exit(1);
#endif
#endif

    if (!strncmp(argv[2],"xmlgen",6)) mode=1;
    else if (!strncmp(argv[2],"opcgen",6)) mode=2;
#ifdef GENERATOR_XMARK
    else if (!strncmp(argv[2],"xmark",8)) mode=3;
#ifdef DTD_VALIDATION
    else if (!strncmp(argv[2],"xmarkdtd",8)) mode=4;
#endif
#endif /* GENERATOR_XMARK */

    td.fn=argv[2];
    
    if (mode) {
	if (argc==4) td.size = atol(argv[3]);
	else Usage(argv[0]);
	if (td.size<1) Usage(argv[0]);

        rnd_init();	/* Initialising Random Generator */
    }
    
//    ReadConfig(&td);
    
    switch (mode) {
	case 0:
	    xmlbuf=ReadFile(argv[2]);
	    if (!xmlbuf) {
		fprintf(stderr,"Can't read XML file!\n");
		exit(0);
	    }
	    td.xml=xmlbuf+sizeof(unsigned long);
	    td.xmllen=((unsigned long*)xmlbuf)[0];
	break;
	case 1:
	    xmlbuf=xmlgen_init(td.size);
	    td.xml=xmlbuf;
	    td.xmllen=xmlgen();
	break;
	case 2:
	    xmlbuf=opcgen_init(td.size);
	    td.xml=xmlbuf;
	    td.xmllen=opcgen();
	break;
#ifdef GENERATOR_XMARK
	case 3:
	    xmlbuf=xmark_init(td.size);
	    td.xml=xmlbuf;
	    td.xmllen=xmark(0);
	break;
	case 4:
	    xmlbuf=xmark_init(td.size);
	    td.xml=xmlbuf;
	    td.xmllen=xmark(1);
	break;
#endif /* GENERATOR_XMARK */

    }
//    printf("here: %u %p %p\n", sizeof(struct TestData), &td, td.xml);
    
    gettimeofday(&pre_time,NULL);
    initXML(&td);
    gettimeofday(&post_time,NULL);
    init_time=(post_time.tv_sec-pre_time.tv_sec)*1000000+(post_time.tv_usec-pre_time.tv_usec);

#ifdef PARABIX_PARSER_FIX
	f = fopen("/tmp/parabix.test", "w");
	fwrite(td.xml, td.xmllen, 1, f);
	fclose(f);
#endif

#ifdef PREPARE_XML
    if (execute_xml) preparseXML(&td, 0);
#endif /* preparseXML */
    gettimeofday(&pre_time,NULL);
    if (execute_xml) parseXML(&td,0);
    gettimeofday(&post_time,NULL);
#ifdef PREPARE_XML
    if (execute_xml) postparseXML(&td, 0);
#endif /* postparseXML */
    init_time2=(post_time.tv_sec-pre_time.tv_sec)*1000000+(post_time.tv_usec-pre_time.tv_usec);
        
    disp_init();
    for (i=1;i<=td.iterations;i++) {
	switch (mode) {
#if defined(RAPIDXML_PARSER_FIX)
	    case 0:
		f=fopen(argv[2],"r");
		if (f) {
		    fread(td.xml,td.xmllen,1,f);
		    fclose(f);
		}
	    break;
#endif /* defined(RAPIDXML_PARSER_FIX) */
	    case 1:
		td.xmllen=xmlgen();
	    break;
	    case 2:
		td.xmllen=opcgen();
	    break;
#ifdef GENERATOR_XMARK
	    case 3:
		td.xmllen=xmark(0);
	    break;
	    case 4:
		td.xmllen=xmark(1);
	    break;
#endif /* GENERATOR_XMARK */
	}

#ifdef PARABIX_PARSER_FIX
	f = fopen("/tmp/parabix.test", "w");
	fwrite(td.xml, td.xmllen, 1, f);
	fclose(f);
#endif
	
#ifdef PREPARE_XML
    if (execute_xml) preparseXML(&td, i);
#endif /* preparseXML */
	gettimeofday(&pre_time,NULL);
	if (execute_xml) parseXML(&td,i);
	gettimeofday(&post_time,NULL);
#ifdef PREPARE_XML
    if (execute_xml) postparseXML(&td, i);
#endif /* postparseXML */
	time=(post_time.tv_sec-pre_time.tv_sec)*1000000+(post_time.tv_usec-pre_time.tv_usec);
	disp_event(time);


//	printf("%lu\n",time);
    }
    disp_post();
    releaseXML(&td);
    
    switch (mode) {
	case 0:
	    free(xmlbuf);
	break;
	case 1:
	    xmlgen_deinit();
	break;
	case 2:
	    opcgen_deinit();
	break;
#ifdef GENERATOR_XMARK
	    case 3:
	    case 4:
		xmark_deinit();
	    break;
#endif /* GENERATOR_XMARK */
    }

    if (mode==2) 
	printf("Parsing Time %lf ms for %lu messages\n", ((double)disp_s) / 1000,td.iterations);
    else {
	if ((init_time2 - disp_m)<10) {
	    init_d=0;
	    init_dtime=0;
	} else {
	    init_d=300 * disp_d / fabs(init_time2 - disp_m);
	    init_dtime=(init_time2 - disp_m)/1000;
	}
	printf("Initialisation time %.3lf + %.3lf(%.2lf%) ms, Parsing Time %.3lf(%.2lf%) ms\n",(1. * init_time) / 1000, init_dtime, init_d, disp_m / 1000, 300*disp_d/disp_m);
    }

    return 0;
}