1 ;; guile-lib -*- scheme -*-
2 ;; Copyright (C) 2004, 2009, 2010, 2014 Andy Wingo <wingo at pobox dot com>
3 ;; Copyright (C) 2001 Rob Browning <rlb at defaultvalue dot org>
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 2.1 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this program; if not, contact:
18 ;; Free Software Foundation Voice: +1-617-542-5942
19 ;; 59 Temple Place - Suite 330 Fax: +1-617-542-2652
20 ;; Boston, MA 02111-1307, USA gnu@gnu.org
24 ;; Unit tests for (debugging statprof).
28 (define-module (test-suite test-statprof)
29 #:use-module (test-suite lib)
30 #:use-module (system base compile)
31 #:use-module (srfi srfi-1)
32 #:use-module (statprof))
34 ;; Throw `unresolved' upon ENOSYS. This is used to skip tests on
35 ;; platforms such as GNU/Hurd where `ITIMER_PROF' is is currently
37 (define-syntax-rule (when-implemented body ...)
42 (let ((errno (system-error-errno args)))
43 (false-if-exception (statprof-stop))
46 (apply throw args))))))
48 (pass-if "statistical sample counts within expected range"
50 ;; test to see that if we call 3 identical functions equally, they
51 ;; show up equally in the call count, +/- 30%. it's a big range, and
52 ;; I tried to do something more statistically valid, but failed (for
55 ;; make sure these are compiled so we're not swamped in `eval'
57 ;; Disable partial evaluation so that `(+ i i)' doesn't get
60 (do ((i 0 (+ i 1))) ((= 200 i)) (+ i i)))
61 #:opts '(#:partial-eval? #f)))
63 (compile '(lambda (num-calls funcs)
64 (let loop ((x num-calls) (funcs funcs))
68 (loop (- x 1) (cdr funcs))))))))
70 (let ((num-calls 200000)
71 (funcs (circular-list (make-func) (make-func) (make-func))))
73 ;; Run test. 20000 us == 200 Hz.
74 (statprof-reset 0 20000 #f #f)
76 (run-test num-calls funcs)
79 (let ((a-data (statprof-proc-call-data (car funcs)))
80 (b-data (statprof-proc-call-data (cadr funcs)))
81 (c-data (statprof-proc-call-data (caddr funcs))))
82 (if (and a-data b-data c-data)
83 (let* ((samples (map statprof-call-data-cum-samples
84 (list a-data b-data c-data)))
85 (expected (/ (apply + samples) 3.0))
86 (diffs (map (lambda (x) (abs (- x expected)))
88 (max-diff (apply max diffs)))
90 (or (< max-diff (sqrt expected))
91 ;; don't stop the test suite for what statistically is
94 (format (current-warning-port)
95 ";;; warning: max diff ~a > (sqrt ~a)\n"
97 (throw 'unresolved))))
99 ;; Samples were not collected for at least one of the
100 ;; functions, possibly because NUM-CALLS is too low compared
102 (throw 'unresolved (pk (list a-data b-data c-data))))))))
104 (pass-if "accurate call counting"
106 ;; Test to see that if we call a function N times while the profiler
107 ;; is active, it shows up N times.
108 (let ((num-calls 200))
111 (compile '(lambda (n)
112 (simple-format #f "FOO ~A\n" (+ n n)))))
115 (statprof-reset 0 50000 #t #f)
117 (let loop ((x num-calls))
126 (let ((proc-data (statprof-proc-call-data do-nothing)))
128 (= (statprof-call-data-calls proc-data)