| 1 |
;;;; statprof.test --- test suite for Guile's profiler -*- scheme -*- |
| 2 |
;;;; Copyright (C) 2017 Free Software Foundation, Inc. |
| 3 |
;; Copyright (C) 2004, 2009, 2010, 2014 Andy Wingo <wingo at pobox dot com> |
| 4 |
;; Copyright (C) 2001 Rob Browning <rlb at defaultvalue dot org> |
| 5 |
|
| 6 |
;; This library is free software; you can redistribute it and/or |
| 7 |
;; modify it under the terms of the GNU Lesser General Public |
| 8 |
;; License as published by the Free Software Foundation; either |
| 9 |
;; version 2.1 of the License, or (at your option) any later version. |
| 10 |
;; |
| 11 |
;; This library is distributed in the hope that it will be useful, |
| 12 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
;; Lesser General Public License for more details. |
| 15 |
;; |
| 16 |
;; You should have received a copy of the GNU Lesser General Public |
| 17 |
;; License along with this program; if not, contact: |
| 18 |
;; |
| 19 |
;; Free Software Foundation Voice: +1-617-542-5942 |
| 20 |
;; 59 Temple Place - Suite 330 Fax: +1-617-542-2652 |
| 21 |
;; Boston, MA 02111-1307, USA [email protected] |
| 22 |
|
| 23 |
;;; Commentary: |
| 24 |
;; |
| 25 |
;; Unit tests for (debugging statprof). |
| 26 |
;; |
| 27 |
;;; Code: |
| 28 |
|
| 29 |
(define-module (test-suite test-statprof) |
| 30 |
#:use-module (test-suite lib) |
| 31 |
#:use-module (system base compile) |
| 32 |
#:use-module (srfi srfi-1) |
| 33 |
#:use-module (statprof)) |
| 34 |
|
| 35 |
;; Throw `unresolved' upon ENOSYS or EINVAL. This is used to skip tests |
| 36 |
;; on platforms such as GNU/Hurd or Cygwin where `ITIMER_PROF' is is |
| 37 |
;; currently unimplemented. |
| 38 |
(define-syntax-rule (when-implemented body ...) |
| 39 |
(catch 'system-error |
| 40 |
(lambda () |
| 41 |
body ...) |
| 42 |
(lambda args |
| 43 |
(let ((errno (system-error-errno args))) |
| 44 |
(false-if-exception (statprof-stop)) |
| 45 |
(if (or (= errno ENOSYS) (= errno EINVAL)) |
| 46 |
(throw 'unresolved) |
| 47 |
(apply throw args)))))) |
| 48 |
|
| 49 |
(pass-if-equal "return values" |
| 50 |
'(42 77) |
| 51 |
(when-implemented |
| 52 |
(call-with-values |
| 53 |
(lambda () |
| 54 |
(with-output-to-port (%make-void-port "w") |
| 55 |
(lambda () |
| 56 |
(statprof |
| 57 |
(lambda () |
| 58 |
(let loop ((i 10000)) |
| 59 |
(if (zero? i) |
| 60 |
(values 42 77) |
| 61 |
(loop (1- i))))))))) |
| 62 |
list))) |
| 63 |
|
| 64 |
(pass-if "statistical sample counts within expected range" |
| 65 |
(when-implemented |
| 66 |
;; test to see that if we call 3 identical functions equally, they |
| 67 |
;; show up equally in the call count, +/- 30%. it's a big range, and |
| 68 |
;; I tried to do something more statistically valid, but failed (for |
| 69 |
;; the moment). |
| 70 |
|
| 71 |
;; make sure these are compiled so we're not swamped in `eval' |
| 72 |
(define (make-func) |
| 73 |
;; Disable partial evaluation so that `(+ i i)' doesn't get |
| 74 |
;; stripped. |
| 75 |
(compile '(lambda (n) |
| 76 |
(do ((i 0 (+ i 1))) ((= 200 i)) (+ i i))) |
| 77 |
#:opts '(#:partial-eval? #f))) |
| 78 |
(define run-test |
| 79 |
(compile '(lambda (num-calls funcs) |
| 80 |
(let loop ((x num-calls) (funcs funcs)) |
| 81 |
(cond |
| 82 |
((positive? x) |
| 83 |
((car funcs) x) |
| 84 |
(loop (- x 1) (cdr funcs)))))))) |
| 85 |
|
| 86 |
(let ((num-calls 200000) |
| 87 |
(funcs (circular-list (make-func) (make-func) (make-func)))) |
| 88 |
|
| 89 |
;; Run test. 20000 us == 200 Hz. |
| 90 |
(statprof-reset 0 20000 #f #f) |
| 91 |
(statprof-start) |
| 92 |
(run-test num-calls funcs) |
| 93 |
(statprof-stop) |
| 94 |
|
| 95 |
(let ((a-data (statprof-proc-call-data (car funcs))) |
| 96 |
(b-data (statprof-proc-call-data (cadr funcs))) |
| 97 |
(c-data (statprof-proc-call-data (caddr funcs)))) |
| 98 |
(if (and a-data b-data c-data) |
| 99 |
(let* ((samples (map statprof-call-data-cum-samples |
| 100 |
(list a-data b-data c-data))) |
| 101 |
(expected (/ (apply + samples) 3.0)) |
| 102 |
(diffs (map (lambda (x) (abs (- x expected))) |
| 103 |
samples)) |
| 104 |
(max-diff (apply max diffs))) |
| 105 |
|
| 106 |
(or (< max-diff (sqrt expected)) |
| 107 |
;; don't stop the test suite for what statistically is |
| 108 |
;; bound to happen. |
| 109 |
(begin |
| 110 |
(format (current-warning-port) |
| 111 |
";;; warning: max diff ~a > (sqrt ~a)\n" |
| 112 |
max-diff expected) |
| 113 |
(throw 'unresolved)))) |
| 114 |
|
| 115 |
;; Samples were not collected for at least one of the |
| 116 |
;; functions, possibly because NUM-CALLS is too low compared |
| 117 |
;; to the CPU speed. |
| 118 |
(throw 'unresolved (pk (list a-data b-data c-data)))))))) |
| 119 |
|
| 120 |
(pass-if "accurate call counting" |
| 121 |
(when-implemented |
| 122 |
;; Test to see that if we call a function N times while the profiler |
| 123 |
;; is active, it shows up N times. |
| 124 |
(let ((num-calls 200)) |
| 125 |
|
| 126 |
(define do-nothing |
| 127 |
(compile '(lambda (n) |
| 128 |
(simple-format #f "FOO ~A\n" (+ n n))))) |
| 129 |
|
| 130 |
;; Run test. |
| 131 |
(statprof-reset 0 50000 #t #f) |
| 132 |
(statprof-start) |
| 133 |
(let loop ((x num-calls)) |
| 134 |
(cond |
| 135 |
((positive? x) |
| 136 |
(do-nothing x) |
| 137 |
(loop (- x 1)) |
| 138 |
#t))) |
| 139 |
(statprof-stop) |
| 140 |
|
| 141 |
;; Check result. |
| 142 |
(let ((proc-data (statprof-proc-call-data do-nothing))) |
| 143 |
(and proc-data |
| 144 |
(= (statprof-call-data-calls proc-data) |
| 145 |
num-calls)))))) |