From 339504cd07bef1767536d6817953ba084e00855d Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Mon, 22 Nov 2004 17:20:15 +0000 Subject: [PATCH] added parse test for - expat - libxml2 (xmlreader API) - libxml2 (SAX2 API) --- helm/papers/use_case/stats/parse/.cvsignore | 3 + helm/papers/use_case/stats/parse/Makefile | 13 +++ .../papers/use_case/stats/parse/parse_expat.c | 50 ++++++++++ .../use_case/stats/parse/parse_xmlreader.c | 31 ++++++ .../use_case/stats/parse/parse_xmlsax.c | 98 +++++++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 helm/papers/use_case/stats/parse/.cvsignore create mode 100644 helm/papers/use_case/stats/parse/Makefile create mode 100644 helm/papers/use_case/stats/parse/parse_expat.c create mode 100644 helm/papers/use_case/stats/parse/parse_xmlreader.c create mode 100644 helm/papers/use_case/stats/parse/parse_xmlsax.c diff --git a/helm/papers/use_case/stats/parse/.cvsignore b/helm/papers/use_case/stats/parse/.cvsignore new file mode 100644 index 000000000..866398111 --- /dev/null +++ b/helm/papers/use_case/stats/parse/.cvsignore @@ -0,0 +1,3 @@ +parse_expat +parse_xmlreader +parse_xmlsax diff --git a/helm/papers/use_case/stats/parse/Makefile b/helm/papers/use_case/stats/parse/Makefile new file mode 100644 index 000000000..9c889e011 --- /dev/null +++ b/helm/papers/use_case/stats/parse/Makefile @@ -0,0 +1,13 @@ +CC = gcc +LIBXML_FLAGS = `pkg-config --cflags --libs libxml-2.0` +EXPAT_FLAGS = -lexpat +BIN = parse_expat parse_xmlreader parse_xmlsax +all: $(BIN) +clean: + rm -f $(BIN) +parse_expat: parse_expat.c + $(CC) $(EXPAT_FLAGS) -o $@ $< +parse_xmlreader: parse_xmlreader.c + $(CC) $(LIBXML_FLAGS) -o $@ $< +parse_xmlsax: parse_xmlsax.c + $(CC) $(LIBXML_FLAGS) -o $@ $< diff --git a/helm/papers/use_case/stats/parse/parse_expat.c b/helm/papers/use_case/stats/parse/parse_expat.c new file mode 100644 index 000000000..900888728 --- /dev/null +++ b/helm/papers/use_case/stats/parse/parse_expat.c @@ -0,0 +1,50 @@ +#include +#include +#include + +static void XMLCALL +startElement(void *userData, const char *name, const char **atts) { return; } + +static void XMLCALL +endElement(void *userData, const char *name) { return; } + +static void XMLCALL +characterData(void *userData, const char *s, int len) { return; } + +static void XMLCALL +startCdata(void *userData) { return; } + +static void XMLCALL +endCdata(void *userData) { return; } + +int +main(int argc, char *argv[]) +{ + char buf[BUFSIZ]; + int done; + FILE *xmlfile = stdin; + struct timeval timing1, timing2; + XML_Parser parser = XML_ParserCreateNS(NULL,' '); + XML_SetElementHandler(parser, startElement, endElement); + XML_SetCharacterDataHandler(parser, characterData); + XML_SetCdataSectionHandler(parser, startCdata, endCdata); + gettimeofday(&timing1, NULL); + do { + size_t len = fread(buf, 1, sizeof(buf), xmlfile); + done = len < sizeof(buf); + if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) { + fprintf(stderr, + "%s at line %d\n", + XML_ErrorString(XML_GetErrorCode(parser)), + XML_GetCurrentLineNumber(parser)); + return 1; + } + } while (!done); + gettimeofday(&timing2, NULL) != 0; + XML_ParserFree(parser); + fprintf(stdout, "%d\n", + ((timing2.tv_sec * 1000000 + timing2.tv_usec) - + (timing1.tv_sec * 1000000 + timing1.tv_usec)) / 1000); + return 0; +} + diff --git a/helm/papers/use_case/stats/parse/parse_xmlreader.c b/helm/papers/use_case/stats/parse/parse_xmlreader.c new file mode 100644 index 000000000..e154b709c --- /dev/null +++ b/helm/papers/use_case/stats/parse/parse_xmlreader.c @@ -0,0 +1,31 @@ +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + struct timeval timing1, timing2; + xmlTextReaderPtr reader; + int ret; + reader = xmlReaderForFd(0, NULL, NULL, 0); + if (reader == NULL) { + fprintf(stderr, "Can't instantiate parser\n"); + return 1; + } + gettimeofday(&timing1, NULL); + ret = xmlTextReaderRead(reader); + while (ret == 1) { + ret = xmlTextReaderRead(reader); + } + gettimeofday(&timing2, NULL); + xmlFreeTextReader(reader); + if (ret != 0) { + fprintf(stderr, "failed to parse STDIN\n"); + } + fprintf(stdout, "%d\n", + ((timing2.tv_sec * 1000000 + timing2.tv_usec) - + (timing1.tv_sec * 1000000 + timing1.tv_usec)) / 1000); + return 0; +} + diff --git a/helm/papers/use_case/stats/parse/parse_xmlsax.c b/helm/papers/use_case/stats/parse/parse_xmlsax.c new file mode 100644 index 000000000..334a42e7d --- /dev/null +++ b/helm/papers/use_case/stats/parse/parse_xmlsax.c @@ -0,0 +1,98 @@ +#include +#include +#include + +void characters_cb +(void * ctx, const xmlChar * ch, int len) { return; } + +void whitespace_cb +(void *ctx, const xmlChar *ch, int len) { return; } + +void cdata_cb +(void *ctx, const xmlChar *value, int len) { return; } + +void start_element_cb +(void *ctx, const xmlChar *name, const xmlChar **atts) { return; } + +void start_element_ns_cb +(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI, + int nb_namespaces, const xmlChar **namespaces, int nb_attributes, + int nb_defaulted, const xmlChar **attributes) +{ return; } + +void end_element_ns_cb +(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI) +{ return; } + +void error_cb +(void *ctx, const char *msg, ...) { + fprintf(stderr, "Error: %s\n", msg); + return; +} + +int +main(int argc, char *argv[]) +{ + char buf[BUFSIZ]; + int res; + int done; + FILE *xmlfile = stdin; + struct timeval timing1, timing2; + xmlParserCtxtPtr ctxt; + xmlSAXHandler sax = { + NULL, /* internalSubsetSAXFunc */ + NULL, /* isStandaloneSAXFunc */ + NULL, /* hasInternalSubsetSAXFunc */ + NULL, /* hasExternalSubsetSAXFunc */ + NULL, /* resolveEntitySAXFunc */ + NULL, /* getEntitySAXFunc */ + NULL, /* entityDeclSAXFunc */ + NULL, /* notationDeclSAXFunc */ + NULL, /* attributeDeclSAXFunc */ + NULL, /* elementDeclSAXFunc */ + NULL, /* unparsedEntityDeclSAXFunc */ + NULL, /* setDocumentLocatorSAXFunc */ + NULL, /* startDocumentSAXFunc */ + NULL, /* endDocumentSAXFunc */ + start_element_cb, /* startElementSAXFunc */ + NULL, /* endElementSAXFunc */ + NULL, /* referenceSAXFunc */ + characters_cb, /* charactersSAXFunc */ + whitespace_cb, /* ignorableWhitespaceSAXFunc */ + NULL, /* processingInstructionSAXFunc */ + NULL, /* commentSAXFunc */ + NULL, /* warningSAXFunc */ + error_cb, /* errorSAXFunc */ + NULL, /* fatalErrorSAXFunc */ + NULL, /* getParameterEntitySAXFunc */ + cdata_cb, /* cdataBlockSAXFunc */ + NULL, /* externalSubsetSAXFunc */ + XML_SAX2_MAGIC, /* initialized */ + NULL, /* _private */ + start_element_ns_cb, /* startElementNsSAX2Func */ + end_element_ns_cb, /* startElementNsSAX2Func */ + NULL /* xmlStructuredErrorFunc */ + }; + ctxt = xmlCreatePushParserCtxt(&sax, NULL, NULL, 0, NULL); + if (ctxt == NULL) { + fprintf(stderr, "Can't instantiate parser\n"); + return 1; + } + gettimeofday(&timing1, NULL); + do { + size_t len = fread(buf, 1, sizeof(buf), xmlfile); + done = len < sizeof(buf); + res = xmlParseChunk(ctxt, buf, len, 0); + if (res != 0) { + fprintf(stderr, "Parse error\n"); + return 1; + } + } while (!done); + gettimeofday(&timing2, NULL); + xmlFreeParserCtxt(ctxt); + fprintf(stdout, "%d\n", + ((timing2.tv_sec * 1000000 + timing2.tv_usec) - + (timing1.tv_sec * 1000000 + timing1.tv_usec)) / 1000); + return 0; +} + -- 2.39.2