statprof.test 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ;; guile-lib -*- scheme -*-
  2. ;; Copyright (C) 2004, 2009, 2010 Andy Wingo <wingo at pobox dot com>
  3. ;; Copyright (C) 2001 Rob Browning <rlb at defaultvalue dot org>
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this program; if not, contact:
  16. ;;
  17. ;; Free Software Foundation Voice: +1-617-542-5942
  18. ;; 59 Temple Place - Suite 330 Fax: +1-617-542-2652
  19. ;; Boston, MA 02111-1307, USA gnu@gnu.org
  20. ;;; Commentary:
  21. ;;
  22. ;; Unit tests for (debugging statprof).
  23. ;;
  24. ;;; Code:
  25. (define-module (test-suite test-statprof)
  26. #:use-module (test-suite lib)
  27. #:use-module (system base compile)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (statprof))
  30. (pass-if "statistical sample counts within expected range"
  31. (let ()
  32. ;; test to see that if we call 3 identical functions equally, they
  33. ;; show up equally in the call count, +/- 30%. it's a big range, and
  34. ;; I tried to do something more statistically valid, but failed (for
  35. ;; the moment).
  36. ;; make sure these are compiled so we're not swamped in `eval'
  37. (define (make-func)
  38. (compile '(lambda (n)
  39. (do ((i 0 (+ i 1))) ((= 200 i)) (+ i i)))))
  40. (define run-test
  41. (compile '(lambda (num-calls funcs)
  42. (let loop ((x num-calls) (funcs funcs))
  43. (cond
  44. ((positive? x)
  45. ((car funcs) x)
  46. (loop (- x 1) (cdr funcs))))))))
  47. (let ((num-calls 40000)
  48. (funcs (circular-list (make-func) (make-func) (make-func))))
  49. ;; Run test. 10000 us == 100 Hz.
  50. (statprof-reset 0 10000 #f #f)
  51. (statprof-start)
  52. (run-test num-calls funcs)
  53. (statprof-stop)
  54. (let* ((a-data (statprof-proc-call-data (car funcs)))
  55. (b-data (statprof-proc-call-data (cadr funcs)))
  56. (c-data (statprof-proc-call-data (caddr funcs)))
  57. (samples (map statprof-call-data-cum-samples
  58. (list a-data b-data c-data)))
  59. (average (/ (apply + samples) 3))
  60. (max-allowed-drift 0.30) ; 30%
  61. (diffs (map (lambda (x) (abs (- x average)))
  62. samples))
  63. (max-diff (apply max diffs)))
  64. (let ((drift-fraction (/ max-diff average)))
  65. (or (< drift-fraction max-allowed-drift)
  66. ;; don't stop the the test suite for what statistically is
  67. ;; bound to happen.
  68. (throw 'unresolved (pk average drift-fraction))))))))
  69. (pass-if "accurate call counting"
  70. (let ()
  71. ;; Test to see that if we call a function N times while the profiler
  72. ;; is active, it shows up N times.
  73. (let ((num-calls 200))
  74. (define do-nothing
  75. (compile '(lambda (n)
  76. (simple-format #f "FOO ~A\n" (+ n n)))))
  77. ;; Run test.
  78. (statprof-reset 0 50000 #t #f)
  79. (statprof-start)
  80. (let loop ((x num-calls))
  81. (cond
  82. ((positive? x)
  83. (do-nothing x)
  84. (loop (- x 1))
  85. #t)))
  86. (statprof-stop)
  87. ;; Check result.
  88. (let ((proc-data (statprof-proc-call-data do-nothing)))
  89. (and proc-data
  90. (= (statprof-call-data-calls proc-data)
  91. num-calls))))))