guile-benchmark 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!../libguile/guile \
  2. -e main -s
  3. !#
  4. ;;;; guile-benchmark --- run the Guile benchmark suite
  5. ;;;; Adapted from code by Jim Blandy <jimb@red-bean.com> --- May 1999
  6. ;;;;
  7. ;;;; Copyright (C) 2002, 2006, 2010 Free Software Foundation, Inc.
  8. ;;;;
  9. ;;;; This program is free software; you can redistribute it and/or
  10. ;;;; modify it under the terms of the GNU Lesser General Public
  11. ;;;; License as published by the Free Software Foundation; either
  12. ;;;; version 3, or (at your option) any later version.
  13. ;;;;
  14. ;;;; This program is distributed in the hope that it will be useful,
  15. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;;; GNU Lesser General Public License for more details.
  18. ;;;;
  19. ;;;; You should have received a copy of the GNU Lesser General Public
  20. ;;;; License along with this software; see the file COPYING.LESSER.
  21. ;;;; If not, write to the Free Software Foundation, Inc., 51 Franklin
  22. ;;;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. ;;;; Usage: [guile -e main -s] guile-benchmark [OPTIONS] [BENCHMARK ...]
  24. ;;;;
  25. ;;;; Run benchmarks from the Guile benchmark suite. Report timing
  26. ;;;; results to the standard output, along with a summary of all
  27. ;;;; the results. Record each reported benchmark outcome in the log
  28. ;;;; file, `benchmarks.log'.
  29. ;;;;
  30. ;;;; Normally, guile-benchmark scans the benchmark directory, and
  31. ;;;; executes all files whose names end in `.bm'. (It assumes they contain
  32. ;;;; Scheme code.) However, you can have it execute specific benchmarks by
  33. ;;;; listing their filenames on the command line.
  34. ;;;;
  35. ;;;; The option `--benchmark-suite' can be given to specify the benchmark
  36. ;;;; directory. If no such option is given, the benchmark directory is
  37. ;;;; taken from the environment variable BENCHMARK_SUITE_DIR (if defined),
  38. ;;;; otherwise a default directory that is hardcoded in this file is
  39. ;;;; used (see "Installation" below).
  40. ;;;;
  41. ;;;; If present, the `--iteration-factor FACTOR' option tells
  42. ;;;; `guile-benchmark' to multiply the number of iterations given with
  43. ;;;; each single benchmark by the value of FACTOR. This allows to
  44. ;;;; reduce or increase the total time for benchmarking.
  45. ;;;;
  46. ;;;; If present, the `--log-file LOG' option tells `guile-benchmark' to put
  47. ;;;; the log output in a file named LOG.
  48. ;;;;
  49. ;;;; If present, the `--debug' option will enable a debugging mode.
  50. ;;;;
  51. ;;;;
  52. ;;;; Installation:
  53. ;;;;
  54. ;;;; If you change the #! line at the top of this script to point at
  55. ;;;; the Guile interpreter you want to run, you can call this script
  56. ;;;; as an executable instead of having to pass it as a parameter to
  57. ;;;; guile via "guile -e main -s guile-benchmark". Further, you can edit
  58. ;;;; the definition of default-benchmark-suite to point to the parent
  59. ;;;; directory of the `benchmarks' tree, which makes it unnecessary to set
  60. ;;;; the environment variable `BENCHMARK_SUITE_DIR'.
  61. ;;;;
  62. ;;;;
  63. ;;;; Shortcomings:
  64. ;;;;
  65. ;;;; At the moment, due to a simple-minded implementation, benchmark files
  66. ;;;; must live in the benchmark directory, and you must specify their names
  67. ;;;; relative to the top of the benchmark directory. If you want to send
  68. ;;;; me a patch that fixes this, but still leaves sane benchmark names in
  69. ;;;; the log file, that would be great. At the moment, all the benchmarks
  70. ;;;; I care about are in the benchmark directory, though.
  71. ;;;;
  72. ;;;; It would be nice if you could specify the Guile interpreter you
  73. ;;;; want to benchmark on the command line. As it stands, if you want to
  74. ;;;; change which Guile interpreter you're benchmarking, you need to edit
  75. ;;;; the #! line at the top of this file, which is stupid.
  76. ;;; User configurable settings:
  77. (define default-benchmark-suite
  78. (string-append (getenv "HOME") "/bogus-path/benchmark-suite"))
  79. (use-modules (benchmark-suite lib)
  80. (ice-9 getopt-long)
  81. (ice-9 and-let-star)
  82. (ice-9 rdelim))
  83. ;;; Variables that will receive their actual values later.
  84. (define benchmark-suite default-benchmark-suite)
  85. (define tmp-dir #f)
  86. ;;; General utilities, that probably should be in a library somewhere.
  87. ;;; Enable debugging
  88. (define (enable-debug-mode)
  89. (write-line %load-path)
  90. (set! %load-verbosely #t)
  91. (debug-enable 'backtrace 'debug))
  92. ;;; Traverse the directory tree at ROOT, applying F to the name of
  93. ;;; each file in the tree, including ROOT itself. For a subdirectory
  94. ;;; SUB, if (F SUB) is true, we recurse into SUB. Do not follow
  95. ;;; symlinks.
  96. (define (for-each-file f root)
  97. ;; A "hard directory" is a path that denotes a directory and is not a
  98. ;; symlink.
  99. (define (file-is-hard-directory? filename)
  100. (eq? (stat:type (lstat filename)) 'directory))
  101. (let visit ((root root))
  102. (let ((should-recur (f root)))
  103. (if (and should-recur (file-is-hard-directory? root))
  104. (let ((dir (opendir root)))
  105. (let loop ()
  106. (let ((entry (readdir dir)))
  107. (cond
  108. ((eof-object? entry) #f)
  109. ((or (string=? entry ".")
  110. (string=? entry "..")
  111. (string=? entry "CVS")
  112. (string=? entry "RCS"))
  113. (loop))
  114. (else
  115. (visit (string-append root "/" entry))
  116. (loop))))))))))
  117. ;;; The benchmark driver.
  118. ;;; Localizing benchmark files and temporary data files.
  119. (define (data-file-name filename)
  120. (in-vicinity tmp-dir filename))
  121. (define (benchmark-file-name benchmark)
  122. (in-vicinity benchmark-suite benchmark))
  123. ;;; Return a list of all the benchmark files in the benchmark tree.
  124. (define (enumerate-benchmarks benchmark-dir)
  125. (let ((root-len (+ 1 (string-length benchmark-dir)))
  126. (benchmarks '()))
  127. (for-each-file (lambda (file)
  128. (if (string-suffix? ".bm" file)
  129. (let ((short-name
  130. (substring file root-len)))
  131. (set! benchmarks (cons short-name benchmarks))))
  132. #t)
  133. benchmark-dir)
  134. ;; for-each-file presents the files in whatever order it finds
  135. ;; them in the directory. We sort them here, so they'll always
  136. ;; appear in the same order. This makes it easier to compare benchmark
  137. ;; log files mechanically.
  138. (sort benchmarks string<?)))
  139. (define (main args)
  140. (let ((options (getopt-long args
  141. `((benchmark-suite
  142. (single-char #\t)
  143. (value #t))
  144. (iteration-factor
  145. (single-char #\t)
  146. (value #t))
  147. (log-file
  148. (single-char #\l)
  149. (value #t))
  150. (debug
  151. (single-char #\d))))))
  152. (define (opt tag default)
  153. (let ((pair (assq tag options)))
  154. (if pair (cdr pair) default)))
  155. (if (opt 'debug #f)
  156. (enable-debug-mode))
  157. (set! benchmark-suite
  158. (or (opt 'benchmark-suite #f)
  159. (getenv "BENCHMARK_SUITE_DIR")
  160. default-benchmark-suite))
  161. (set! iteration-factor
  162. (string->number (opt 'iteration-factor "1")))
  163. ;; directory where temporary files are created.
  164. (set! tmp-dir (getcwd))
  165. (let* ((benchmarks
  166. (let ((foo (opt '() '())))
  167. (if (null? foo)
  168. (enumerate-benchmarks benchmark-suite)
  169. foo)))
  170. (log-file
  171. (opt 'log-file "benchmarks.log")))
  172. ;; Open the log file.
  173. (let ((log-port (open-output-file log-file)))
  174. ;; Register some reporters.
  175. (register-reporter (make-log-reporter log-port))
  176. (register-reporter user-reporter)
  177. ;; Run the benchmarks.
  178. (for-each (lambda (benchmark)
  179. (with-benchmark-prefix benchmark
  180. (load (benchmark-file-name benchmark))))
  181. benchmarks)
  182. (close-port log-port)))))
  183. ;;; Local Variables:
  184. ;;; mode: scheme
  185. ;;; End: