[xslt] timing diffs for non-gettimeofday use



Hiya,

RISC OS doesn't contain a gettimeofday function, and thus large chunks
of the tools fail when attempting to do timing. I've previously just
stuck #ifndef HAVE_GETTIMEOFDAY around these, but decided that it was
probably better to submit some patches back for others who may have
the same problem.

In order to simplify the timing process, I've abstracted the timing into two
functions - startTimer() and endTimer(format,...). These should be called at
the beginning of the process to be timed, and at the end (or part way
through, if you wish - the timer implicitly keeps running). The format string
is displayed before the string ' took ? ms', and allows printf style
formatted arguments to be passed to the function. The use of two functions
instead of repeating the same code has also simplified some sections.

There is the minor issue that this requires stdarg.h to be included, but
this is also a standard C function, and so should not be a problem.

Three implementations are provided :

  * using gettimeofday if HAVE_GETTIMEOFDAY is defined
  * using clock if HAVE_TIME_H is defined
  * doing nothing if neither of the above are defined

This should cater for unix-like systems, standard C-like systems and
brain-damaged systems.

At present I have only applied these changes to the xsltproc, but intend
to implement them in the libxml2 tools. Before I do that, I felt it better
to check that these were suitable and that I am not making assumptions
about other systems.

The diffs are, I believe, the minimum required for the changes. I do not
believe I have left any RISC OS specific code in there.

-- 
Gerph {djf0-.3w6e2w2.226,6q6w2q2,2.3,2m4}
URL: http://www.movspclr.co.uk/
... Eyes to the heavens, screaming at the sky;
    Trying to send you messages, but choking on goodbye.
*** Original/xsltproc/c/xsltproc	Wed Sep 19 02:55:14 2001
--- RISCOS/xsltproc/c/xsltproc	Mon Nov  5 17:52:41 2001
***************
*** 23,28 ****
--- 26,35 ----
  #ifdef HAVE_STDLIB_H
  #include <stdlib.h>
  #endif
+ #ifdef HAVE_STDARG_H
+ /* HAVE_STDARG_H may need adding to config.h or auto-configuration scripts */
+ #include <stdarg.h>
+ #endif
  #include <libxml/xmlmemory.h>
  #include <libxml/debugXML.h>
  #include <libxml/HTMLtree.h>
***************
*** 53,59 ****
--- 60,70 ----
  #define gettimeofday(p1,p2)
  #endif /* _MS_VER */
  #else /* WIN32 */
+ #if defined(HAVE_SYS_TIME_H)
  #include <sys/time.h>
+ #elif defined(HAVE_TIME_H)
+ #include <time.h>
+ #endif
  #endif /* WIN32 */

  #ifndef HAVE_STAT
***************
*** 85,95 ****
--- 96,203 ----
  static int nonet;
  static xmlExternalEntityLoader defaultLoader = NULL;

+ /***** Internal timing routines to remove the necessity to have unix-specific
+        function calls *****/
+
+ #if defined(HAVE_GETTIMEOFDAY)
  static struct timeval begin, end;
+ /*
+  * startTimer: call where you want to start timing
+  */
+ static void startTimer(void)
+ {
+     gettimeofday(&begin,NULL);
+ }
+ /*
+  * endTimer: call where you want to stop timing and to print out a
+  *           message about the timing performed; format is a printf
+  *           type argument
+  */
+ static void endTimer(char *format, ...)
+ {
+     long msec;
+
+     gettimeofday(&end, NULL);
+     msec = end.tv_sec - begin.tv_sec;
+     msec *= 1000;
+     msec += (end.tv_usec - begin.tv_usec) / 1000;
+
+ #ifndef HAVE_STDARG_H
+ #error "endTimer required stdarg functions"
+ #endif
+     va_list ap;
+     va_start(ap, format);
+     vfprintf(stderr,format,ap);
+     va_end(ap);
+
+     fprintf(stderr, " took %ld ms\n", msec);
+ }
+
+ #elif defined(HAVE_TIME_H)
+
+ /*
+  * No gettimeofday function, so we have to make do with calling clock.
+  * This is obviously less accurate, but there's little we can do about
+  * that.
+  */
+
+ clock_t begin, end;
+ static void startTimer(void)
+ {
+     begin=clock();
+ }
+ static void endTimer(char *format, ...)
+ {
+     long msec;
+     va_list ap;
+
+     end=clock();
+     msec = ((end-begin) * 1000) / CLOCKS_PER_SEC;
+
+ #ifndef HAVE_STDARG_H
+ #error "endTimer required stdarg functions"
+ #endif
+     va_start(ap, format);
+     vfprintf(stderr,format,ap);
+     va_end(ap);
+     fprintf(stderr, " took %ld ms\n", msec);
+ }
+
+ #else
+
+ /*
+  * We don't have a gettimeofday or time.h, so we just don't do timing
+  */
+ static void startTimer(void)
+ {
+   /*
+    * Do nothing
+    */
+ }
+ static void endTimer(char *format, ...)
+ {
+   /*
+    * We cannot do anything because we don't have a timing function
+    */
+ #ifdef HAVE_STDARG_H
+     va_start(ap, format);
+     vfprintf(stderr,format,ap);
+     va_end(ap);
+     fprintf(stderr, " was not timed\n", msec);
+ #else
+   /* We don't have gettimeofday, time or stdarg.h, what crazy world is
+    * this ?!
+    */
+ #endif
+ }
+ #endif
+
  static const char *params[16 + 1];
  static int nbparams = 0;
  static const char *output = NULL;

***************
*** 212,233 ****
  #ifdef LIBXML_XINCLUDE_ENABLED
      if (xinclude) {
  	if (timing)
! 	    gettimeofday(&begin, NULL);
  	xmlXIncludeProcess(doc);
  	if (timing) {
! 	    long msec;
!
! 	    gettimeofday(&end, NULL);
! 	    msec = end.tv_sec - begin.tv_sec;
! 	    msec *= 1000;
! 	    msec += (end.tv_usec - begin.tv_usec) / 1000;
! 	    fprintf(stderr, "XInclude processing %s took %ld ms\n",
! 		    filename, msec);
  	}
      }
  #endif
      if (timing)
! 	gettimeofday(&begin, NULL);
      if (output == NULL) {
  	if (repeat) {
  	    int j;
--- 345,359 ----
  #ifdef LIBXML_XINCLUDE_ENABLED
      if (xinclude) {
  	if (timing)
! 	    startTimer();
  	xmlXIncludeProcess(doc);
  	if (timing) {
! 	    endTimer("XInclude processing %s", filename);
  	}
      }
  #endif
      if (timing)
!         startTimer();
      if (output == NULL) {
  	if (repeat) {
  	    int j;
***************
*** 255,273 ****
  	    res = xsltApplyStylesheet(cur, doc, params);
  	}
  	if (timing) {
- 	    long msec;
-
- 	    gettimeofday(&end, NULL);
- 	    msec = end.tv_sec - begin.tv_sec;
- 	    msec *= 1000;
- 	    msec += (end.tv_usec - begin.tv_usec) / 1000;
  	    if (repeat)
! 		fprintf(stderr,
! 			"Applying stylesheet %d times took %ld ms\n",
! 			repeat, msec);
  	    else
! 		fprintf(stderr,
! 			"Applying stylesheet took %ld ms\n", msec);
  	}
  	xmlFreeDoc(doc);
  	if (res == NULL) {
--- 381,390 ----
  	    res = xsltApplyStylesheet(cur, doc, params);
  	}
  	if (timing) {
  	    if (repeat)
! 		endTimer("Applying stylesheet %d times");
  	    else
! 		endTimer("Applying stylesheet");
  	}
  	xmlFreeDoc(doc);
  	if (res == NULL) {
***************
*** 285,320 ****
  #endif
  	    if (cur->methodURI == NULL) {
  		if (timing)
! 		    gettimeofday(&begin, NULL);
  		xsltSaveResultToFile(stdout, res, cur);
  		if (timing) {
! 		    long msec;
!
! 		    gettimeofday(&end, NULL);
! 		    msec = end.tv_sec - begin.tv_sec;
! 		    msec *= 1000;
! 		    msec += (end.tv_usec - begin.tv_usec) / 1000;
! 		    fprintf(stderr, "Saving result took %ld ms\n",
! 			    msec);
  		}
  	    } else {
  		if (xmlStrEqual
  		    (cur->method, (const xmlChar *) "xhtml")) {
  		    fprintf(stderr, "non standard output xhtml\n");
  		    if (timing)
! 			gettimeofday(&begin, NULL);
  		    xsltSaveResultToFile(stdout, res, cur);
  		    if (timing) {
! 			long msec;
!
! 			gettimeofday(&end, NULL);
! 			msec = end.tv_sec - begin.tv_sec;
! 			msec *= 1000;
! 			msec +=
! 			    (end.tv_usec - begin.tv_usec) / 1000;
! 			fprintf(stderr,
! 				"Saving result took %ld ms\n",
! 				msec);
  		    }
  		} else {
  		    fprintf(stderr,
--- 402,421 ----
  #endif
  	    if (cur->methodURI == NULL) {
  		if (timing)
! 		    startTimer();
  		xsltSaveResultToFile(stdout, res, cur);
  		if (timing) {
! 		    endTimer("Saving result");
  		}
  	    } else {
  		if (xmlStrEqual
  		    (cur->method, (const xmlChar *) "xhtml")) {
  		    fprintf(stderr, "non standard output xhtml\n");
  		    if (timing)
! 			startTimer();
  		    xsltSaveResultToFile(stdout, res, cur);
  		    if (timing) {
! 			endTimer("Saving result");
  		    }
  		} else {
  		    fprintf(stderr,
***************
*** 330,349 ****
      } else {
  	xsltRunStylesheet(cur, doc, params, output, NULL, NULL);
  	if (timing) {
! 	    long msec;
!
! 	    gettimeofday(&end, NULL);
! 	    msec = end.tv_sec - begin.tv_sec;
! 	    msec *= 1000;
! 	    msec += (end.tv_usec - begin.tv_usec) / 1000;
! 	    fprintf(stderr,
! 		"Running stylesheet and saving result took %ld ms\n",
! 		    msec);
  	}
  	xmlFreeDoc(doc);
      }
  }

  static void usage(const char *name) {
      printf("Usage: %s [options] stylesheet file [file ...]\n", name);
      printf("   Options:\n");
--- 431,530 ----
      } else {
  	xsltRunStylesheet(cur, doc, params, output, NULL, NULL);
  	if (timing) {
! 	    endTimer("Running stylesheet and saving result");
  	}
  	xmlFreeDoc(doc);
      }
  }

  static void usage(const char *name) {
      printf("Usage: %s [options] stylesheet file [file ...]\n", name);
      printf("   Options:\n");
***************
*** 364,378 ****
  #endif
      printf("      --param name value : pass a (parameter,value) pair\n");
      printf("            string values must be quoted like \"'string'\"\n");
!     printf("      --nonet refuse to fetch DTDs or entities over network\n");
!     printf("      --warnnet warn against fetching over the network\n");
  #ifdef LIBXML_CATALOG_ENABLED
      printf("      --catalogs : use the catalogs from $SGML_CATALOG_FILES\n");
  #endif
  #ifdef LIBXML_XINCLUDE_ENABLED
      printf("      --xinclude : do XInclude processing on document intput\n");
  #endif
      printf("      --profile or --norman : dump profiling informations \n");
  }

  int
--- 545,566 ----
  #endif
      printf("      --param name value : pass a (parameter,value) pair\n");
      printf("            string values must be quoted like \"'string'\"\n");
!     printf("      --nonet: refuse to fetch DTDs or entities over network\n");
!     printf("      --warnnet: warn against fetching over the network\n");
  #ifdef LIBXML_CATALOG_ENABLED
      printf("      --catalogs : use the catalogs from $SGML_CATALOG_FILES\n");
  #endif
  #ifdef LIBXML_XINCLUDE_ENABLED
      printf("      --xinclude : do XInclude processing on document intput\n");
  #endif
      printf("      --profile or --norman : dump profiling informations \n");
  }

  int
***************
*** 540,556 ****
          }
          if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
              if (timing)
!                 gettimeofday(&begin, NULL);
  	    style = xmlParseFile((const char *) argv[i]);
              if (timing) {
!                 long msec;
!
!                 gettimeofday(&end, NULL);
!                 msec = end.tv_sec - begin.tv_sec;
!                 msec *= 1000;
!                 msec += (end.tv_usec - begin.tv_usec) / 1000;
!                 fprintf(stderr, "Parsing stylesheet %s took %ld ms\n",
!                         argv[i], msec);
              }
  	    if (style == NULL) {
  		fprintf(stderr,  "cannot parse %s\n", argv[i]);
--- 760,769 ----
          }
          if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
              if (timing)
!                 startTimer();
  	    style = xmlParseFile((const char *) argv[i]);
              if (timing) {
!                 endTimer("Parsing stylesheet %s", argv[i]);
              }
  	    if (style == NULL) {
  		fprintf(stderr,  "cannot parse %s\n", argv[i]);
***************
*** 590,596 ****
          for (; i < argc; i++) {
  	    doc = NULL;
              if (timing)
!                 gettimeofday(&begin, NULL);
  #ifdef LIBXML_HTML_ENABLED
              if (html)
                  doc = htmlParseFile(argv[i], NULL);
--- 803,809 ----
          for (; i < argc; i++) {
  	    doc = NULL;
              if (timing)
!                 startTimer();
  #ifdef LIBXML_HTML_ENABLED
              if (html)
                  doc = htmlParseFile(argv[i], NULL);
***************
*** 607,620 ****
                  continue;
              }
              if (timing) {
!                 long msec;
!
!                 gettimeofday(&end, NULL);
!                 msec = end.tv_sec - begin.tv_sec;
!                 msec *= 1000;
!                 msec += (end.tv_usec - begin.tv_usec) / 1000;
!                 fprintf(stderr, "Parsing document %s took %ld ms\n",
!                         argv[i], msec);
              }
  	    xsltProcess(doc, cur, argv[i]);
          }
--- 820,826 ----
                  continue;
              }
              if (timing) {
!                 endTimer("Parsing document %s", argv[i]);
              }
  	    xsltProcess(doc, cur, argv[i]);
          }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]